-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathspirograph.html
227 lines (209 loc) · 7.38 KB
/
spirograph.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<!--
* Copyright 2013, Gregg Tavares.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Gregg Tavares. nor the names of his
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0, maximum-scale=1, minimal-ui">
<meta name="format-detection" content="telephone=no" />
<link type="text/css" href="js/css/doodle.css" rel="stylesheet" />
<link type="text/css" href="js/css/ui-lightness/jquery-ui-1.10.3.custom.css" rel="stylesheet" />
<script src="js/lzma.js"></script>
<script src="js/jquery-1.9.1.js"></script>
<script src="js/jquery-ui-1.10.3.custom.min.js"></script>
<script src="js/jquery.ui.touch-punch.min.js"></script>
<script src="js/canvas-wrapper.js"></script>
<script src="js/webgl-utils.js"></script>
<script src="js/doodles.js"></script>
<script id="code" type="something-not-javascript">
var params = runCtx._.params;
var place;
function getControlPoints(x0, y0, x1, y1, x2, y2, t){
var d01 = Math.sqrt(Math.pow(x1 - x0, 2) + Math.pow(y1 - y0, 2));
var d12 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
var fa = t * d01 / (d01 + d12); // scaling factor for triangle Ta
var fb = t * d12 / (d01 + d12); // ditto for Tb, simplifies to fb = t - fa
var p1x = x1 - fa * (x2 - x0); // x2 - x0 is the width of triangle T
var p1y = y1 - fa * (y2 - y0); // y2 - y0 is the height of T
var p2x = x1 + fb * (x2 - x0);
var p2y = y1 + fb * (y2 - y0);
return [p1x, p1y, p2x, p2y];
}
var drawInnerCircle = function(ctx, time, outerRadius, innerRadius, anim, depth) {
ctx.save();
var radius = outerRadius - innerRadius;
ctx.translate(radius, 0);
if (anim) {
ctx.beginPath();
ctx.arc(0, 0, Math.abs(innerRadius), 0, Math.PI * 2, 0);
ctx.stroke();
}
var innerTime = -time * params.speedRatio * (params.numLoops + (params.numSubLoops ? 1 / params.numSubLoops : 0)) / depth;
ctx.rotate(innerTime);
if (depth <= 1) {
if (anim) {
ctx.beginPath();
ctx.moveTo(innerRadius, 0);
}
ctx.translate(innerRadius * params.innerRadius, 0);
if (anim) {
ctx.lineTo(0, 0);
ctx.stroke();
ctx.beginPath();
ctx.arc(0, 0, 5, 0, Math.PI * 2, 0);
ctx.fill();
} else {
var t = ctx.getTransform();
runCtx._.newX = t[4];
runCtx._.newY = t[5];
}
} else {
drawInnerCircle(ctx, innerTime, innerRadius, innerRadius * params.radius, anim, depth - 1);
}
ctx.restore();
};
var drawOuterCircle = function(ctx, time, elapsedTime, anim) {
ctx.save();
if (anim) {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2);
var outerRadius = Math.min(ctx.canvas.width / 2, ctx.canvas.height / 2);
var scale = params.radius >= 0 ? 1 : (1 + params.radius);
outerRadius *= scale;
if (anim) {
ctx.strokeStyle = "rgba(255,0,0," + params.circleAlpha + ")";
ctx.fillStyle = "rgba(0,0,255," + params.circleAlpha + ")";
ctx.beginPath();
ctx.arc(0, 0, outerRadius, 0, Math.PI * 2, 1);
ctx.stroke();
}
ctx.rotate(time);
drawInnerCircle(ctx, time, outerRadius, outerRadius * params.radius, anim, params.depth);
ctx.restore();
};
var draw = function(ctx) {
var _ = ctx._;
if (ctx.time > _.oldTime) {
var elapsedTime = ctx.elapsedTime * params.speed;
_.clock += elapsedTime;
_.count = _.count ? _.count + 1 : 1;
// old alpha is needed to erase last alpha > 0 drawing.
if (params.circleAlpha || _.oldAlpha) {
drawOuterCircle(ctx.animCtx, _.clock, elapsedTime, true);
}
_.oldAlpha = params.circleAlpha;
drawOuterCircle(ctx.drawCtx, _.clock, elapsedTime, false);
if (!_.points) {
_.points = [];
_.cp = 0;
}
var cp = _.cp;
var points = _.points;
if (!points[cp]) {
points[cp] = {};
}
var p2 = points[cp];
p2.x = _.newX;
p2.y = _.newY;
if (_.count > 3) {
var p0 = points[(cp + 1) % 3];
var p1 = points[(cp + 2) % 3];
p1.cp = getControlPoints(p0.x, p0.y, p1.x, p1.y, p2.x, p2.y, params.smoothness);
if (_.count > 6) {
ctx.drawCtx.beginPath();
ctx.drawCtx.moveTo(p0.x, p0.y);
ctx.drawCtx.bezierCurveTo(p0.cp[2], p0.cp[3], p1.cp[0], p1.cp[1], p1.x, p1.y);
ctx.drawCtx.strokeStyle = "black";
ctx.drawCtx.lineWidth = params.thickness;
ctx.drawCtx.stroke();
}
}
_.cp = (cp + 1) % 3;
} else {
_.count = 0;
_.clock = 0;
}
_.oldTime = ctx.time;
};
draw(runCtx);
</script>
<script>
$(function(){
var ctx;
var fn = function() {
if (ctx) {
Doodles.resetCanvases(ctx);
}
};
var nop = function() {};
var ui = [
{name: 'radius', value: 0.66, min: -1, max: 1, },
{name: 'innerRadius', value: 0.33, min: 0, max: 1, },
{name: 'speedRatio', value: 1, min: 0, max: 4, },
{name: 'numLoops', value: 3, min: 0, max: 100, type: 'int', },
{name: 'numSubLoops', value: 20, min: 0, max: 100, type: 'int', },
{name: 'depth', value: 1, min: 1, max: 4, type: 'int', },
{name: 'smoothness', value: 0.5, min: 0, max: 2, },
{name: 'thickness', value: 0.5, min: 0, max: 5, },
{name: 'speed', value: 1, min: 0, max: 5, fn: nop, },
{name: 'circleAlpha', value: 1, min: 0, max: 1, fn: nop, },
];
Doodles.init(ui, fn, function(newCtx) {
ctx = newCtx;
});
});
</script>
</head>
<body>
<div id="content">
<table>
<tr>
<td>
<textarea id="editor"></textarea>
</td>
<td>
<div id="canvasContainer">
<canvas id="drawCanvas"></canvas>
<canvas id="animCanvas"></canvas>
</div>
</td>
</table>
</div>
<div id="uiContainer">
<div id="ui"></div>
</div>
</body>
</html>