{"id":343,"date":"2009-07-12T07:49:55","date_gmt":"2009-07-12T14:49:55","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1750"},"modified":"2009-07-12T07:49:55","modified_gmt":"2009-07-12T14:49:55","slug":"bitmapdata-particles","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=343","title":{"rendered":"BitmapData &#038; Particles"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nstage.frameRate = 30;\r\nstage.quality = StageQuality.LOW;\r\n\r\nvar canvas:BitmapData = new BitmapData(400,400,false, 0x000000);\r\naddChild(new Bitmap(canvas));\r\nvar overlay:BitmapData = new BitmapData(400,400,true, 0x12000000);\r\nvar particles:Dictionary = new Dictionary(true);\r\n\r\nvar blur:Array = &#x5B;new BlurFilter(8,8,1)];\r\n\t\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\tfor (var i:int= 0; i&lt;3; i++) createParticle();\r\n\tcanvas.copyPixels(overlay, canvas.rect, new Point(0,0), null, null, true);\r\n}\r\n \r\nfunction createParticle():void{\r\n\tvar s:MovieClip = new MovieClip();\r\n\tvar diameter:Number = Math.random()*50 + 2;\r\n\tvar radius:Number = diameter \/ 2;\r\n\tvar mat:Matrix = new Matrix();\r\n    mat.createGradientBox(diameter, diameter, 0, -radius, -radius);\r\n\tmat.createGradientBox(diameter, diameter, 0, -radius, -radius);\r\n    s.graphics.beginGradientFill(GradientType.RADIAL, &#x5B; 0xFFCC32, 0xFF0000], &#x5B;0.5,0.8], &#x5B;0, 255], mat, SpreadMethod.PAD);\r\n\ts.graphics.drawCircle(0,0,radius);\r\n\ts.velX = 0;\r\n\ts.velY =  -Math.random()*6 + 3;\r\n\ts.posX = s.x = 150+ Math.random() * 100;\r\n\ts.posY = s.y = 200;\r\n\ts.theta= Math.random() * Math.PI * 2;\r\n\ts.inc = Math.random() * 0.4 + 0.01;\r\n\ts.rad = Math.random() * 4 + 1;\r\n\ts.filters = blur;\r\n\t\/\/ since the particles aren&#039;t on the display list, we need to keep a reference to them\r\n\tparticles&#x5B;s] = s;\r\n\ts.addEventListener(Event.ENTER_FRAME, onRunParticle);\r\n}\r\nfunction onRunParticle(evt:Event):void {\r\n\tvar s:MovieClip = MovieClip(evt.currentTarget);\r\n\ts.posX += s.velX;\r\n\ts.posY += s.velY;\r\n\ts.velX = s.rad * Math.cos(s.theta);\r\n\ts.theta += s.inc;\r\n\ts.scaleX = s.scaleY -=  .03;\r\n\tif (s.scaleX &lt; 0){\r\n\t\tparticles&#x5B;s] = null;\r\n\t\ts.removeEventListener(Event.ENTER_FRAME, onRunParticle);\r\n\t}\r\n\ts.x = s.posX;\r\n\ts.y = s.posY;\r\n\tcanvas.draw(s, s.transform.matrix, null, BlendMode.ADD);\r\n}\r\n<\/pre>\n<p>This is a slight variation on a simple particle system that I show in my intermediate class. It is almost the same as <a href=\"https:\/\/actionsnippet.com\/?p=99\" target=\"blank\">this example<\/a>. But it makes use of filters and BitmapData.<\/p>\n<p>Here is a still generated with this snippet. You&#8217;ll notice it vaguely resembles fire.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/firey.jpg\" \/><\/p>\n<p>If you need more particles, there are <a href=\"https:\/\/actionsnippet.com\/?p=224\">other approaches<\/a> that are more appropriate. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>stage.frameRate = 30; stage.quality = StageQuality.LOW; var canvas:BitmapData = new BitmapData(400,400,false, 0x000000); addChild(new Bitmap(canvas)); var overlay:BitmapData = new BitmapData(400,400,true, 0x12000000); var particles:Dictionary = new Dictionary(true); var blur:Array = &#x5B;new BlurFilter(8,8,1)]; addEventListener(Event.ENTER_FRAME, onLoop); function onLoop(evt:Event):void { for (var i:int= 0; i&lt;3; i++) createParticle(); canvas.copyPixels(overlay, canvas.rect, new Point(0,0), null, null, true); } function createParticle():void{ var s:MovieClip = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,33,13],"tags":[3,14,4],"class_list":["post-343","post","type-post","status-publish","format-standard","hentry","category-bitmapdata","category-graphics","category-motion","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=343"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/343\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}