Skip to content

Commit b056f33

Browse files
authored
Merge pull request #916 from DavidHGillen/master
Updates to demos and StageGL. Fixes demo disparities and adds better texture cleanup handling to StageGL
2 parents c43176a + 2fede38 commit b056f33

3 files changed

Lines changed: 196 additions & 126 deletions

File tree

examples/Sparkles.html

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
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);
@@ -34,14 +38,29 @@
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;
@@ -53,69 +72,81 @@
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>

examples/WebGL/Sparkles.html

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
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
22+
var fader; // Semi fades the Background to give a tracing effect
23+
var spkls; // Container for all the sparkles
2424

2525
function init() {
2626
// create a new stage and point it at our canvas:
2727
canvas = document.getElementById("testCanvas");
2828
stage = new createjs.StageGL(canvas, {preserveBuffer:true, antialias:true});
2929
stage.autoClear = false;
30+
stage.setClearColor(0x000000);
3031

3132
// attach mouse handlers directly to the source canvas.
3233
// better than calling from canvas tag for cross browser compatibility:
@@ -39,17 +40,17 @@
3940
var data = {
4041
images: ["../../_assets/art/spritesheet_sparkle.png"],
4142
frames: {
42-
regX: 10,
43-
regY: 11,
43+
width: 21,
4444
height: 23,
45-
width: 21
45+
regX: 10,
46+
regY: 11
4647
}
4748
};
4849

4950
// set up an animation instance, which we will clone
5051
sprite = new createjs.Sprite(new createjs.SpriteSheet(data));
5152

52-
//setup slow fadeout
53+
// setup slow fadeout
5354
fader = new createjs.Shape();
5455
stage.addChild(fader);
5556
var gfx = fader.graphics;
@@ -93,7 +94,7 @@
9394

9495
sparkle.alpha = sparkle.alphaStart * (sparkle.life / sparkle.lifeMax);
9596

96-
//remove sparkles that are off screen or not invisible
97+
// remove sparkles that are off screen or not invisible
9798
if (sparkle.y > canvas.height) {
9899
sparkle.vY *= -(Math.random() * 0.1 + 0.8);
99100
sparkle.vX += Math.cos( Math.random() * Math.PI * 2) * 3;
@@ -120,7 +121,7 @@
120121

121122
// sparkle trail
122123
function moveCanvas(evt) {
123-
addSparkles(Math.random() * 4 | 0, stage.mouseX, stage.mouseY, 0.1);
124+
addSparkles((Math.random() * 6 + 3) | 0, stage.mouseX, stage.mouseY, 0.1);
124125
}
125126

126127
function addSparkles(count, x, y, speed) {
@@ -160,18 +161,16 @@
160161
<header class="EaselJS">
161162
<h1>Sparkles</h1>
162163

163-
<p>Example showing how to use simple animated <code>Sprite</code> instances
164-
with <code>stagemousemove</code> and <code>stagemousedown</code> events.
165-
Move your mouse and click
166-
on the canvas. It also demonstrates displaying the current measured
167-
framerate with <code>Ticker.getMeasuredFramerate()</code>
168-
and <code>Text</code>. Click repeatedly to generate lots of sparkles and
169-
slow down the framerate.<br/>
164+
<p>Example showing the differences in a <code>StageGL</code> project, compare
165+
the two files to see how little changed.
166+
It also demonstrates persistent drawing with <code>Stage.autoclear = false</code>
167+
and <code>preservesBuffer: true</code>. Click repeatedly to generate lots of sparkles and
168+
try to slow down the framerate. We're using more sparkles this time!<br/>
170169
</p>
171170
</header>
172171

173172
<div>
174-
<canvas id="testCanvas" width="960" height="400" style="background:black"></canvas>
173+
<canvas id="testCanvas" width="960" height="400"></canvas>
175174
</div>
176175
</body>
177176
</html>

0 commit comments

Comments
 (0)