{"id":325,"date":"2019-03-30T17:03:31","date_gmt":"2019-03-30T17:03:31","guid":{"rendered":"https:\/\/www.maxcanvas.org\/?page_id=325"},"modified":"2026-10-25T22:13:27","modified_gmt":"2026-10-25T22:13:27","slug":"fireworks","status":"publish","type":"page","link":"https:\/\/www.maxcanvas.org\/?page_id=325","title":{"rendered":"Fireworks"},"content":{"rendered":"\n<p>Fireworks stress tests the MC Engine by throwing 1,000&#8217;s of Sprite objects about on the screen.  <\/p>\n\n\n\n<p>LEFT CLICK TO FIRE.<\/p>\n\n\n\n<figure><iframe loading=\"lazy\" width=\"905px\" height=\"705px\" src=\"https:\/\/www.maxcanvas.org\/MaxCanvas\/Examples\/MC%20-%2006%20-%20FireWorks.html\"><\/iframe><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Code<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ MaxCanvas Demo - Fireworks\n\/\/ by Max Bryans (March 2019)\n\/\/ \n\/\/\n\/\/ Required to drag MC.js into Intellisense\n\/\/ thanks to https:\/\/visualstudiomagazine.com\/blogs\/tool-tracker\/2018\/07\/use-javascript-code.aspx (Dec 2018)\n\/\/\/ &lt;reference path=\"MC.js\" \/>\n\nwindow.onload = (function() {\n\n\/\/ AQUIRE HTML DOM CANVAS REFERENCE\n    \/\/     HTML has to have a HTML5 canvas element tagged as \"canvas\"\n    var c=document.getElementById(\"canvas\");\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/ INITIALISE MC Engine \/\/\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    \/\/ CHOOSE 1 \/\/ \n    \n    \/\/ CHOICE 1: Canvas Size fixed by HTML (in this case 900 x 700)\n    MC.init(c);\n    MC.game.setBounds(0, 1, 0, 1);\n    \n    \n    \/\/ CHOICE 2: Full Window Option (Canvas will be resized to fit window)\n    \/\/MC.init(c,\"fullWindow\");\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/ END OF Initialisation \/\/\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/-----------------------------------------------------------------------------\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/ VARIABLE DECLARATION \/\/\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n    \/\/ This simulation uses gravity\n    MC.game.gravity.set(0,15);\n    \n    \/\/ Container for Game Objects\n    var FW = {\n        spd: 50,\n        Particles: new MC.SpriteBin(),\n        Trails: new MC.SpriteBin(),\n        \/\/ 2 x ConBoxes as text holders.  1 for Reporting Sprite Count and fps, 2nd for Instructions\n        Report: new MC.ConBox(),\n        Instructions: new MC.ConBox()\n    };\n    \/\/ Configure Text boxes\n    FW.Report.pos.set(FW.Report.width\/2,MC.canvas.bottom - FW.Report.height\/2);\n    FW.Instructions.pos.set(MC.canvas.right - FW.Report.width\/2,MC.canvas.bottom - FW.Report.height\/2);\n    FW.Instructions.text = \"Click for Fireworks !!\";\n    \n    \/\/ Player Input Event Handler ... Left Mouse click = make a firework\n    MC.mouse.onClickL = function () { MakeBurst(); }\n\n    \/\/ Control Function, sets parameters for a series of \"Sparks\" which make up an explosion\n    function MakeBurst() {\n        var angle = 0;\n        var InitialVel = new MC.Point(FW.spd * MC.maths.randBetween(0.9,1.1),0);\n        var pos = MC.mouse.pos.clone();\n        var color = MC.utils.randomColor();\n        var rocketVel = new MC.Point(0,-FW.spd * 1.2);        \n        rocketVel.rotate(MC.maths.randBetween(-30,30));  \/\/ a small random direction to add a bit of variety\n        while (angle &lt; 360) {\n            \/\/ Set off a series of Sparks, with velocities radiating around (and away from) the burst centre\n            MakeSpark(pos,InitialVel.clone().rotate(angle).add(rocketVel), color);\n            angle += MC.maths.randBetween(5,25);        \/\/ between 15-72 Sparks per Burst\n        };\n    }\n    \n    \/\/ Sprite Factory\n    function MakeSpark(position, velocity, color) {\n        \/\/ Make standard Sprite object\n        var s = new MC.Sprite({\n            type: \"circ\",\n            size1: 3,\n            pos: position.clone(),\n            vel: velocity.clone(),\n            wrapAround: false,\n            fillColor: color.clone(),\n            edgeColor: null,\n            gravity: true,\n            killAge: 4\n        });\n        \n        \/\/ Customise behaviour via the Sprite.dev object\n        s.dev.oldPos = s.pos.clone();\n        s.dev.update = function () {\n            \/\/ Only need to add a \"Trail\" if the distance from the last one is > 1 pixel\n            if (MC.maths.rangeSqr(s.pos,s.dev.oldPos) > 1) {\n                MakeTrail(s.pos,s.fillColor);\n                s.dev.oldPos = s.pos.clone();\n            }\n            s.vel.times(0.995);     \/\/ retard velocity slightly each frame to emulate drag\n        };\n        \n        \/\/ Store in SpriteBin\n        FW.Particles.push(s);       \/\/ Spritebin will handle all hereafter\n    }\n    \n    \/\/ Sprite Factory.  Trails do not move, but shrink over a 1 second lifespan, creating the illusion of movement\n    function MakeTrail(position, color) {\n        var t = new MC.Sprite({\n            type: \"circ\",\n            size1: 3,\n            pos: new MC.Point(position),\n            vel: new MC.Point(0,0),\n            wrapAround: false,\n            fillColor: color,\n            edgeColor: null,\n            gravity: false\n        });\n        \n        t.dev.TrailTimer = 1;\n        t.dev.update = function () {\n            t.dev.TrailTimer -= MC.game.deltaTime;\n            if (t.dev.TrailTimer &lt; 0) {\n                    t.kill();\n                }\n            else {\n                    t.scale = t.dev.TrailTimer;\n                }\n            };\n        FW.Trails.push(t);\n    }\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/ END OF declarations  \/\/\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/-----------------------------------------------------------------------------\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/      GAME LOOP       \/\/\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n\n    \/\/ first call, request subsequently made within itself\n    gameLoop();\n    \n    function gameLoop() {\n        requestAnimationFrame(gameLoop);\n        \/\/ Essential to update MC.game to ensure MC.game.deltaTime is available\n        MC.game.update();\n        update();\n        render();\n    }\n    \n    function update() {\n        FW.Particles.update();\n        FW.Trails.update();\n        \/\/ Populate Report Text\n        FW.Report.text = (~~((FW.Particles.bin.length + FW.Trails.bin.length)\/100)*100) + \"+ Sprites @ \" + (~~(1\/MC.game.deltaTime\/10)*10) + \" fps\";\n    }\n    \n    function render() {\n        MC.draw.clearInBounds(\"Black\");\n        FW.Trails.render();\n        FW.Particles.render();\n        FW.Report.render();\n        FW.Instructions.render();\n    }\n    \n}); \/\/ EoF\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Fireworks stress tests the MC Engine by throwing 1,000&#8217;s of Sprite objects about on the screen. LEFT CLICK TO FIRE. Code<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_oct_exclude_from_cache":false,"footnotes":""},"class_list":["post-325","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/pages\/325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=325"}],"version-history":[{"count":5,"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/pages\/325\/revisions"}],"predecessor-version":[{"id":610,"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/pages\/325\/revisions\/610"}],"wp:attachment":[{"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}