-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathGraphicsReuse.html
executable file
·96 lines (73 loc) · 2.14 KB
/
GraphicsReuse.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>EaselJS: Reusing Graphics</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 headRadius;
var g;
function init() {
canvas = document.getElementById('myCanvas');
width = canvas.width;
height = canvas.height;
stage = new createjs.Stage(canvas);
layoutSmiley();
stage.addEventListener("stagemousedown", handlePress);
stage.update();
}
function layoutSmiley() {
// get a single Graphics instance.
g = drawSmiley(createjs.Graphics.getHSL(Math.random() * 360, 100, Math.random() * 50 + 30));
for (var i = 0; i < 141; i++) {
// share the Graphics instance between all of the shapes:
var shape = new createjs.Shape(g);
shape.scaleX = shape.scaleY = i / 400 + 0.4;
shape.x = Math.cos(i / 4) * i * 3.75 + canvas.width / 2;
shape.y = Math.sin(i / 4) * i * 3.75 + canvas.height / 2;
shape.rotation = Math.random() * 360;
stage.addChild(shape);
}
stage.update();
}
function handlePress() {
stage.removeAllChildren();
layoutSmiley();
}
function drawSmiley(fillColor) {
var g = new createjs.Graphics();
//Head
g.setStrokeStyle(10, 'round', 'round');
g.beginStroke("#000");
g.beginFill(fillColor);
g.drawCircle(0, 0, 100); //55,53
//Mouth
g.beginFill(); // no fill
g.arc(0, 0, 60, 0, Math.PI);
//Right eye
g.beginStroke(); // no stroke
g.beginFill("#000");
g.drawCircle(-30, -30, 15);
//Left eye
g.drawCircle(30, -30, 15);
return g;
}
</script>
</head>
<body onload="init()">
<header class="EaselJS">
<h1>Graphics Re-use</h1>
<p>This example shows how a single <code>Graphics</code> object can be
reused between multiple <code>Shape</code> instances.
Click to redraw.</p>
</header>
<canvas id="myCanvas" width="960" height="400"></canvas>
</body>
</html>