-
Notifications
You must be signed in to change notification settings - Fork 1
/
scripts.js
346 lines (270 loc) · 8.5 KB
/
scripts.js
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Use the anchor to enable gif output
var doGif = window.location.hash.substring(1) === 'gif';
// Use the query string as the seed value
var inputSeed = parseInt(window.location.search.substring(1))
// Quick and dirty pseudo-random numbers
Math.seed = function(s) {
console.log(s);
var m_w = s;
var m_z = s + 987654321;
var mask = 0xffffffff;
var random = function() {
m_z = (36969 * (m_z & 65535) + (m_z >> 16)) & mask;
m_w = (18000 * (m_w & 65535) + (m_w >> 16)) & mask;
var result = ((m_z << 16) + m_w) & mask;
result /= 4294967296;
return result + 0.5;
}
return random;
}
function randomRange(random, min, max) {
return random()*(max-min) + min;
}
function vectorLength(x, y) {
return Math.sqrt(x*x + y*y);
}
function Point(x, y) {
this.x = x || 0;
this.y = y || 0;
}
// 2-dimensional axis-aligned bounding box
function Bounds() {
this.min = new Point();
this.max = new Point();
this.reset = function() {
this.min.x = 4503599627370495;
this.min.y = 4503599627370495;
this.max.x = -4503599627370495;
this.max.y = -4503599627370495;
}
this.reset();
this.addPoint = function(p) {
if (p.x < this.min.x)
this.min.x = p.x;
if (p.x > this.max.x)
this.max.x = p.x;
if (p.y < this.min.y)
this.min.y = p.y;
if (p.y > this.max.y)
this.max.y = p.y;
}
this.center = function() {
return new Point(this.min.x + (this.max.x - this.min.x)/2.0,
this.min.y + (this.max.y - this.min.y)/2.0);
}
this.size = function() {
return new Point(Math.abs(this.max.x - this.min.x),
Math.abs(this.max.y - this.min.y));
}
}
// This is a glyph renderer which doesn't draw anything
// Instead it calculates the bounding box and line length of the glyph
function Measure() {
this.length = 0;
this.transform = new Matrix();
this.bounds = new Bounds();
this._addPoint = function(p) {
q = this.transform.applyToPoint(p.x, p.y);
this.bounds.addPoint(q);
}
this._addCircle = function(p, r) {
q = this.transform.applyToPoint(p.x, p.y);
this.bounds.addPoint(new Point(q.x+r, q.y+r));
this.bounds.addPoint(new Point(q.x-r, q.y-r));
}
this.begin = function() {
this.length = 0;
this.transform.reset();
this.bounds.reset();
}
this.end = function() {}
this.translate = function(x, y) {
this.transform.translate(x, y);
}
this.rotate = function(angle) {
this.transform.rotate(angle);
}
this.line = function(low, high, end) {
this.length += Math.abs(high - low);
this._addPoint(new Point(0, low));
this._addPoint(new Point(0, high));
this.transform.translate(0, end);
}
this.arc = function(centerX, centerY, pointX, pointY, arcAngle, anticlockwise) {
var diffX = centerX - pointX;
var diffY = centerY - pointY;
var radius = vectorLength(diffX, diffY);
var startAngle = Math.atan2(diffY, diffX) - Math.PI;
var endAngle = startAngle + arcAngle;
var x = centerX + radius*Math.cos(endAngle);
var y = centerY + radius*Math.sin(endAngle);
this.length += radius * arcAngle;
this._addCircle(new Point(centerX, centerY), radius);
this.transform.translate(x, y);
this.transform.rotate(endAngle - Math.PI/2);
}
}
// This renderer actually draws the glyph, clipped to a percentage of the
// total length, after applying a preliminary transform
function Renderer(context) {
this.context = context;
this.maxLength = 4503599627370495;
this.length = 0;
this.preTransform = new Matrix();
this.begin = function() {
this.length = 0;
this.context.save();
this.context.fillStyle = 'rgb(0,0,0)'
this.context.fillRect(0, 0, 512, 512);
this.context.strokeStyle = 'rgb(255,255,255)';
this.preTransform.applyToContext(this.context);
this.context.beginPath();
}
this.end = function() {
this.context.stroke();
this.context.restore();
}
this.translate = function(x, y) {
this.context.translate(x, y);
}
this.rotate = function(angle) {
this.context.rotate(angle);
}
this.line = function(low, high, end) {
if (this.length >= this.maxLength)
return;
this.length += Math.abs(high - low);
if (this.length > this.maxLength)
high -= (this.length - this.maxLength) * Math.sign(high - low);
this.context.moveTo(0, low);
this.context.lineTo(0, high);
this.context.translate(0, end);
}
this.arc = function(centerX, centerY, pointX, pointY, arcAngle, anticlockwise) {
if (this.length >= this.maxLength)
return;
var diffX = centerX - pointX;
var diffY = centerY - pointY;
var radius = vectorLength(diffX, diffY);
this.length += radius * arcAngle;
if (this.length > this.maxLength)
arcAngle -= (this.length - this.maxLength)*1.0/radius;
var startAngle = Math.atan2(diffY, diffX) - Math.PI;
var endAngle = startAngle + arcAngle;
var x = centerX + radius*Math.cos(endAngle);
var y = centerY + radius*Math.sin(endAngle);
this.context.arc(centerX, centerY, radius, startAngle, endAngle, anticlockwise);
this.context.translate(x, y);
this.context.rotate(endAngle - Math.PI/2);
}
}
// This is the state machine which actually generates the glyph
function Shape(seed) {
console.log(seed)
this.doLine = function(renderer, random) {
renderer.line(randomRange(random, -25, 0),
randomRange(random, 75, 100),
randomRange(random, 25, 75));
this.last = 'line';
}
this.doArc = function(renderer, random) {
if (random() < 0.5)
var angle = Math.round(randomRange(random, 1, 8)) * Math.PI/4.0;
else
var angle = Math.PI*2.0;
var radius = randomRange(random, 15, 60);
var direction = Math.round(randomRange(random, 1, 8)) * Math.PI/4.0;
renderer.arc(radius * Math.sin(direction),
radius * Math.cos(direction),
0,
0,
angle,
false);
this.last = 'arc';
}
this.doRotate = function(renderer, random) {
renderer.rotate(Math.round(randomRange(random, 1, 7)) * Math.PI/4.0);
this.last = 'rotate';
}
this.draw = function(renderer) {
renderer.begin();
var random = Math.seed(seed);
this.last = 'rotate';
var segments = randomRange(random, 4, 6);
for (var i = 0; i < segments; ++i) {
if (this.last === 'rotate' || this.last === 'arc') {
this.doLine(renderer, random);
} else if (this.last === 'line') {
if (random() < 0.75)
this.doArc(renderer, random);
else {
this.doRotate(renderer, random);
}
}
}
renderer.end();
}
}
// This renders a Shape in 2 passes, once to measure, and again to draw,
// after applying a suitable transformation
function Glyph(context, seed) {
this.context = context;
this.measure = new Measure();
this.renderer = new Renderer(context);
this.shape = new Shape(seed);
this.loop = function(t) {
this.shape.draw(this.measure);
this.renderer.maxLength = this.measure.length * t;
var min = this.measure.bounds.min;
var size = this.measure.bounds.size();
var sx = size.x + 20;
var sy = size.y + 20;
var factor = Math.min(512.0/sx, 512.0/sy);
this.renderer.preTransform.reset();
this.renderer.preTransform.translate((512 - size.x*factor)/2, (512 - size.y*factor)/2);
this.renderer.preTransform.scale(factor, factor);
this.renderer.preTransform.translate(-min.x, -min.y);
this.shape.draw(this.renderer);
}
}
// Main entry point, optionally sets up the gif generator
function start() {
var canvas = document.getElementById('bitmap');
var context = canvas.getContext('2d');
var encoder = doGif ? new GIF({
workers: 4,
quality: 10,
width: 512,
height: 512,
dither: true
}) : null;
var randomSeed = inputSeed || Math.floor(Math.random()*1024);
document.getElementById('randomSeed').textContent = randomSeed;
glyph = new Glyph(context, randomSeed);
draw(glyph, encoder, 0.0);
}
// If we are making a gif, encode it here
function finish(encoder) {
if (!doGif)
return;
encoder.on('finished', function(blob) {
window.open(URL.createObjectURL(blob));
});
encoder.render();
}
// This is the main loop
function draw(glyph, encoder, t) {
glyph.loop(t);
t += 1.0/240.0;
if (doGif) {
delay = 16;
if (t >= 1.0)
delay = 3000;
encoder.addFrame(glyph.context, {delay: delay, copy: true});
}
if (t < 1.0) {
requestAnimationFrame(function() { draw(glyph, encoder, t) });
} else {
finish(encoder);
}
}