-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathCache.html
executable file
·126 lines (104 loc) · 3.84 KB
/
Cache.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>EaselJS Example: Using cache and snapToPixel</title>
<link href="../_assets/css/shared.css" rel="stylesheet" type="text/css"/>
<link href="../_assets/css/examples.css" rel="stylesheet" type="text/css"/>
<script src="../_assets/js/examples.js"></script>
<script src="../lib/easeljs-NEXT.js"></script>
<!-- We also provide hosted minified versions of all CreateJS libraries.
http://code.createjs.com -->
<script id="editable">
var canvas;
var stage;
var shape;
var radius = 50;
var rings = 40;
function init() {
// create a new stage and point it at our canvas:
canvas = document.getElementById("testCanvas");
stage = new createjs.Stage(canvas);
// create a large number of slightly complex vector shapes, and give them random positions and velocities:
var colors = ["#828b20", "#b0ac31", "#cbc53d", "#fad779", "#f9e4ad", "#faf2db", "#563512", "#9b4a0b", "#d36600", "#fe8a00", "#f9a71f"];
for (var i = 0; i < 200; i++) {
shape = new createjs.Shape();
for (var j = rings; j > 0; j--) {
shape.graphics.beginFill(colors[Math.random() * colors.length | 0]).drawCircle(0, 0, radius * j / rings);
}
shape.x = Math.random() * canvas.width;
shape.y = Math.random() * canvas.height;
shape.velX = Math.random() * 10 - 5;
shape.velY = Math.random() * 10 - 5;
// turn snapToPixel on for all shapes - it's set to false by default on Shape.
// it won't do anything until stage.snapToPixelEnabled is set to true.
shape.snapToPixel = true;
stage.addChild(shape);
}
// add a text object to output the current FPS:
fpsLabel = new createjs.Text("-- fps", "bold 18px Arial", "#FFF");
stage.addChild(fpsLabel);
fpsLabel.x = 10;
fpsLabel.y = 20;
// start the tick and point it at the window so we can do some work before updating the stage:
createjs.Ticker.timingMode = createjs.Ticker.RAF;
createjs.Ticker.addEventListener("tick", tick);
}
function tick(event) {
var w = canvas.width + radius * 2;
var h = canvas.height + radius * 2;
var l = stage.numChildren - 1;
// iterate through all the children and move them according to their velocity:
for (var i = 1; i < l; i++) {
var shape = stage.getChildAt(i);
shape.x = (shape.x + radius + shape.velX + w) % w - radius;
shape.y = (shape.y + radius + shape.velY + h) % h - radius;
}
fpsLabel.text = Math.round(createjs.Ticker.getMeasuredFPS()) + " fps";
// draw the updates to stage:
stage.update(event);
}
function toggleCache(value) {
// iterate all the children except the fpsLabel, and set up the cache:
var l = stage.numChildren - 1;
for (var i = 0; i < l; i++) {
var shape = stage.getChildAt(i);
if (value) {
shape.cache(-radius, -radius, radius * 2, radius * 2);
} else {
shape.uncache();
}
}
}
</script>
<style>
.controls {
padding: 10px;
}
</style>
</head>
<body onload="init();">
<header class="EaselJS">
<h1>Cache as Bitmap Example</h1>
<p>This example demonstrates the effect of
<code>DisplayObject.cache()</code> and <code>snapToPixel</code>
on performance. Caching can provide performance benefits for some types
of content because the complex vector
shapes do not have to be re-rendered each tick. Different browsers have
different thresholds for when
caching makes sense. The results for snapToPixel vary between browsers,
but in general it will have no
effect on the un-cached shapes, but a significant effect when cached.
</p>
</header>
<div class="controls">
<input type="checkbox" onClick="toggleCache(this.checked);"
id="toggleCache"/><label for="toggleCache">cache
enabled</label>
</div>
<canvas id="testCanvas" width="960" height="350"></canvas>
<strong>Note:</strong> The performance benefit varies between browsers, based on
the relative performance of
vector rendering vs drawing small textures.
</body>
</html>