1616 var canvas ;
1717 var stage ;
1818
19- var imgSeq = new Image ( ) ; // The image for the sparkle animation
20- var sprite ; // The animated sparkle template to clone
19+ var imgSeq = new Image ( ) ; // The image for the sparkle animation
20+ var sprite ; // The animated sparkle template to clone
2121 var fpsLabel ;
22+ var fader ; // Semi fades the Background to give a tracing effect
23+ var spkls ; // Container for all the sparkles
2224
2325 function init ( ) {
2426 // create a new stage and point it at our canvas:
2527 canvas = document . getElementById ( "testCanvas" ) ;
2628 stage = new createjs . Stage ( canvas ) ;
29+ stage . autoClear = false ;
30+
2731 // attach mouse handlers directly to the source canvas.
2832 // better than calling from canvas tag for cross browser compatibility:
2933 stage . addEventListener ( "stagemousemove" , moveCanvas ) ;
3438 // it will auto-calculate the number of frames from the image dimensions and loop them
3539 var data = {
3640 images : [ "../_assets/art/spritesheet_sparkle.png" ] ,
37- frames : { width : 21 , height : 23 , regX : 10 , regY : 11 }
41+ frames : {
42+ width : 21 ,
43+ height : 23 ,
44+ regX : 10 ,
45+ regY : 11
46+ }
3847 } ;
3948
4049 // set up an animation instance, which we will clone
4150 sprite = new createjs . Sprite ( new createjs . SpriteSheet ( data ) ) ;
4251
52+ // setup slow fadeout
53+ fader = new createjs . Shape ( ) ;
54+ stage . addChild ( fader ) ;
55+ var gfx = fader . graphics ;
56+ gfx . beginFill ( "rgba(0,0,0, 0.3)" ) . drawRect ( 0 , 0 , 1024 , 704 ) . endFill ( ) ;
57+ fader . cache ( 0 , 0 , 1024 , 1024 , 1 / 32 ) ; // Power of two textures smooth better, but we don't need full resolution either
58+
59+ spkls = new createjs . Container ( ) ;
60+ stage . addChild ( spkls ) ;
61+
4362 // add a text object to output the current FPS:
44- fpsLabel = new createjs . Text ( "-- fps" , "bold 14px Arial" , "#FFF" ) ;
63+ fpsLabel = new createjs . Text ( "------ @ -- fps" , "bold 14px Arial" , "#FFF" ) ;
4564 stage . addChild ( fpsLabel ) ;
4665 fpsLabel . x = 10 ;
4766 fpsLabel . y = 20 ;
5372
5473 function tick ( event ) {
5574 // loop through all of the active sparkles on stage:
56- var l = stage . numChildren ;
75+ var l = spkls . numChildren ;
5776 var m = event . delta / 16 ;
58- for ( var i = l - 1 ; i > 0 ; i -- ) {
59- var sparkle = stage . getChildAt ( i ) ;
77+ for ( var i = 0 ; i < l ; i ++ ) {
78+ var sparkle = spkls . getChildAt ( i ) ;
79+
80+ if ( -- sparkle . life <= 0 ) {
81+ spkls . removeChild ( sparkle ) ;
82+ i -- ; l -- ;
83+ continue ;
84+ }
6085
6186 // apply gravity and friction
62- sparkle . vY += 0.8 * m ;
63- sparkle . vX *= 0.99 ;
87+ sparkle . vY += 0.1 * m ;
6488
6589 // update position, scale, and alpha:
6690 sparkle . x += sparkle . vX * m ;
6791 sparkle . y += sparkle . vY * m ;
68- sparkle . scale = sparkle . scaleX + sparkle . vS * m ;
69- sparkle . alpha += sparkle . vA * m ;
7092
71- //remove sparkles that are off screen or not invisble
72- if ( sparkle . alpha <= 0 || sparkle . y > canvas . height ) {
73- stage . removeChildAt ( i ) ;
93+ sparkle . alpha = sparkle . alphaStart * ( sparkle . life / sparkle . lifeMax ) ;
94+
95+ // remove sparkles that are off screen or not invisible
96+ if ( sparkle . y > canvas . height ) {
97+ sparkle . vY *= - ( Math . random ( ) * 0.1 + 0.8 ) ;
98+ sparkle . vX += Math . cos ( Math . random ( ) * Math . PI * 2 ) * 3 ;
99+ } else if ( sparkle . y < 0 ) {
100+ sparkle . vY *= 0.9 ;
101+ }
102+
103+ if ( sparkle . x > canvas . width || sparkle . x < 0 ) {
104+ sparkle . vX *= - 1 ;
74105 }
75106 }
76107
77- fpsLabel . text = Math . round ( createjs . Ticker . getMeasuredFPS ( ) ) + " fps" ;
108+ fpsLabel . text = l + " @ " + Math . round ( createjs . Ticker . getMeasuredFPS ( ) ) + " fps" ;
78109
79110 // draw the updates to stage
80111 stage . update ( event ) ;
81112 }
82113
83- //sparkle explosion
114+ // sparkle explosion
84115 function clickCanvas ( evt ) {
85116 addSparkles ( Math . random ( ) * 200 + 100 | 0 , stage . mouseX , stage . mouseY , 1 ) ;
86117 }
87118
88- //sparkle trail
119+ // sparkle trail
89120 function moveCanvas ( evt ) {
90- addSparkles ( Math . random ( ) * 6 + 3 | 0 , stage . mouseX , stage . mouseY , 0.5 ) ;
121+ addSparkles ( ( Math . random ( ) * 4 + 2 ) | 0 , stage . mouseX , stage . mouseY , 0.1 ) ;
91122 }
92123
93124 function addSparkles ( count , x , y , speed ) {
94- //create the specified number of sparkles
125+ // create the specified number of sparkles
95126 for ( var i = 0 ; i < count ; i ++ ) {
96127 // clone the original sparkle, so we don't need to set shared properties:
97128 var sparkle = sprite . clone ( ) ;
98129
99130 // set display properties:
100131 sparkle . x = x ;
101132 sparkle . y = y ;
102- // sparkle.rotation = Math.random()*360;
103- sparkle . alpha = Math . random ( ) * 0.5 + 0.5 ;
133+ sparkle . rotation = Math . random ( ) * 360 ;
134+ sparkle . alpha = sparkle . alphaStart = Math . random ( ) * 0.7 + 0.6 ;
104135 sparkle . scale = Math . random ( ) + 0.3 ;
105136
137+ sparkle . life = sparkle . lifeMax = Math . random ( ) * 100 + 50 ;
138+
106139 // set up velocities:
107140 var a = Math . PI * 2 * Math . random ( ) ;
108141 var v = ( Math . random ( ) - 0.5 ) * 30 * speed ;
109142 sparkle . vX = Math . cos ( a ) * v ;
110143 sparkle . vY = Math . sin ( a ) * v ;
111- sparkle . vS = ( Math . random ( ) - 0.5 ) * 0.2 ; // scale
112- sparkle . vA = - Math . random ( ) * 0.05 - 0.01 ; // alpha
113144
114145 // start the animation on a random frame:
115- sparkle . gotoAndPlay ( Math . random ( ) * sparkle . spriteSheet . getNumFrames ( ) ) ;
146+ sparkle . gotoAndPlay ( Math . random ( ) * sparkle . spriteSheet . getNumFrames ( ) | 0 ) ;
116147
117148 // add to the display list:
118- stage . addChild ( sparkle ) ;
149+ spkls . addChild ( sparkle ) ;
119150 }
120151 }
121152
@@ -138,8 +169,7 @@ <h1>Sparkles</h1>
138169</ header >
139170
140171< div >
141- < canvas id ="testCanvas " width ="960 " height ="400 "
142- style ="background:black "> </ canvas >
172+ < canvas id ="testCanvas " width ="960 " height ="400 " style ="background:black "> </ canvas >
143173</ div >
144174</ body >
145175</ html >
0 commit comments