-
Notifications
You must be signed in to change notification settings - Fork 4
/
move.js
694 lines (622 loc) · 20 KB
/
move.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
// Move.js
// Simple particle simulator.
//
// By Clark DuVall
(function() {
var root = this,
Move,
requestAnimationFrame = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame;
root.Move = Move = {};
/// class Vector
/// A simple 3D vector.
///
/// var v = new Move.Vector({x: 10, y: 0, z: 5});
/// v.multiplyScalar(2);
/// console.log(v.x);
/// > 20
///
/// Params:
/// options {Object}: Options to initialize this |@Vector|.
/// Properties:
/// [x] (0) {Number}: x coordinate
/// [y] (0) {Number}: y coordinate
/// [z] (0) {Number}: z coordinate
function Vector(options) {
options || (options = {});
_.defaults(options, {x: 0, y: 0, z: 0});
this.x = options.x;
this.y = options.y;
this.z = options.z;
}
/// function Vector.add
/// Add two |@Vectors| together.
///
/// Params:
/// other {@Vector}: The |@Vector| to add.
///
/// Returns:
/// {@Vector}: |this|
Vector.prototype.add = function(other) {
this.x += other.x;
this.y += other.y;
this.z += other.z;
return this;
};
/// function Vector.sub
/// Subtracts one |@Vector| from another.
///
/// Params:
/// other {@Vector}: The |@Vector| to subtract.
///
/// Returns:
/// {@Vector}: |this|
Vector.prototype.sub = function(other) {
this.x -= other.x;
this.y -= other.y;
this.z -= other.z;
return this;
};
/// function Vector.mulitplyScalar
/// Multiplies this |@Vector| by a scalar.
///
/// Params:
/// scalar {Number}: The scalar to multiply by.
///
/// Returns:
/// {@Vector}: |this|
Vector.prototype.multiplyScalar = function(scalar) {
this.x *= scalar;
this.y *= scalar;
this.z *= scalar;
return this;
};
/// function Vector.length
/// Gets the length of this |@Vector|.
///
/// Returns:
/// {@Vector}: |this|
Vector.prototype.length = function() {
return Math.sqrt(this.lengthSq());
};
/// function Vector.lengthSq
/// Gets the squared length of this |@Vector|.
///
/// Returns:
/// {@Vector}: |this|
Vector.prototype.lengthSq = function() {
return this.x * this.x + this.y * this.y + this.z * this.z;
};
Move.Vector = Vector;
/// class Controller
/// Handles everything relating to a single canvas.
///
/// var controller = new Move.Controller({
/// context: document.getElementById('canvas').getContext('2d')
/// });
///
/// controller.addSystem(new Move.System({...}));
/// controller.start();
///
/// Params:
/// options {Object}: Options for the controller.
/// Properties:
/// context {Context2D}: The context this controller will draw on.
///
/// [trace] (false) {Boolean}: Whether the canvas will be cleared every
/// frame.
///
/// [speed] (1) {Number}: The speed the simulation moves at.
///
/// [draw] (@Controller.draw) {Function}: The function used to draw the
/// particle systems this controller handles to the canvas.
/// Params:
/// ctx {Context2D}: The context to draw on.
///
/// [preDraw] {Function}: Called right before draw().
/// Params:
/// ctx {Context2D}: The context to draw on.
///
/// [postDraw] {Function}: Called right after draw().
/// Params:
/// ctx {Context2D}: The context to draw on.
///
/// [setContext] {Function}: Sets up the context before any
/// draw calls are made.
/// Params:
/// ctx {Context2D}: The context to draw on.
function Controller(options) {
var that = this;
options || (options = {});
_.defaults(options, {
trace: false,
speed: 1,
draw: Controller.prototype.draw,
preDraw: function(ctx) {},
postDraw: function(ctx) {},
setContext: function(ctx) {}
});
/// property Controller.systems {@System[]}
/// The |@Systems| this controller is in charge of.
this.systems = [];
if (!options.context && !options.draw) {
throw "Move.Controller requires a 2D context or custom draw function."
}
_.each(['trace', 'context', 'preDraw', 'postDraw', 'draw', 'setContext',
'speed'], function(name) {
that[name] = options[name];
});
_.bindAll(this, 'step');
}
Controller.prototype.update = function(delta) {
_.each(this.systems, function(system) {
system.update(delta);
});
};
/// function Controller.reset
/// Resets the |@Systems| this controller is in charge of.
Controller.prototype.reset = function() {
_.each(this.systems, function(system) {
system.reset();
});
};
/// function Controller.addSystem
/// Adds a system to this controller.
///
/// Params:
/// system {@System}: The system to add.
Controller.prototype.addSystem = function(system) {
this.systems.push(system);
return this;
};
/// function Controller.start
/// Begin or resume the simulation.
Controller.prototype.start = function() {
this.paused = false;
this.step(0);
};
/// function Controller.pause
/// Pause the simulation.
Controller.prototype.pause = function() {
this.paused = true;
};
Controller.prototype.step = function(ms) {
var delta = 0;
if (ms) {
delta = (ms - this.lastStep) * 0.001;
this.lastStep = ms;
}
// Must have switched tabs.
if (delta < .1) {
this.update(delta * this.speed);
this.draw(this.context);
}
if (!this.paused) {
requestAnimationFrame(this.step);
}
};
Controller.prototype.clear = function() {
this.context.clearRect(0, 0, this.context.canvas.width,
this.context.canvas.height);
};
/// function Controller.draw
/// Draws the systems this controller is in charge of to the canvas.
///
/// Params:
/// ctx {Context2D}: The context to draw on.
Controller.prototype.draw = function(ctx) {
ctx.save();
if (!this.trace) {
this.clear();
}
this.setContext(ctx);
this.preDraw(ctx);
_.each(this.systems, function(system) {
system.draw(ctx);
});
this.postDraw(ctx);
ctx.restore();
};
Move.Controller = Controller;
/// class System
/// A particle system.
///
/// var system = new Move.System({
/// numParticles: 100,
/// rules: [...],
/// newParticle: function(numParticles) {
/// return new Move.Particle({...});
/// }
/// });
///
/// controller.addSystem(system);
///
/// Params:
/// options {Object}: Options for the system.
/// Properties:
/// newParticle {Function}: Creates a new |@Particle| for the system.
/// Params:
/// numParticles {Number}: The current number of particles in the
/// system.
///
/// [numParticles] (0) {Number}: The number of particles to start this
/// system with.
///
/// [rules] ([]) {Function[]}: The starting set of |@Rules| for this
/// system.
///
/// [preDraw] {Function}: Called right before the |@Particles| in the
/// system are drawn.
/// Params:
/// ctx {Context2D}: The context to draw on.
///
/// [postDraw] {Function}: Called right after the |@Particles| in the
/// system are drawn.
/// Params:
/// ctx {Context2D}: The context to draw on.
///
/// [preUpdate] {Function}: Called right before the update step where
/// the new positions for the |@Particles| in the system are
/// computed.
/// Params:
/// delta {Number}: The change in time since the last update in
/// seconds.
///
/// [postUpdate] {Function}: Called right after the update step.
/// Params:
/// delta {Number}: The change in time since the last update in
/// seconds.
///
/// [setContext] {Function}: Sets up the context before any draw calls
/// are made for this system.
/// Params:
/// ctx {Context2D}: The context to draw on.
///
/// [onDeath] {Function}: Called when a particle dies.
///
/// [init] {Function}: Do custom initialization such as adding
/// properties to this.
function System(options) {
var that = this;
options || (options = {});
_.defaults(options, {
newParticle: function() {
return new Particle();
},
numParticles: 0,
rules: [],
preDraw: function(ctx) {},
postDraw: function(ctx) {},
preUpdate: function(delta) {},
postUpdate: function(delta) {},
setContext: function(ctx) {},
onDeath: function() {},
init: function() {}
});
/// property System.particles {@Particle[]}
/// The |@Particles| in this system.
this.particles = [];
this.lastStep = 0;
_.each(['newParticle', 'rules', 'trace', 'preDraw', 'postDraw',
'setContext', 'onDeath', 'numParticles', 'preUpdate', 'postUpdate'],
function(name) {
that[name] = options[name];
});
options.init.call(this);
this.reset();
}
/// function System.reset
/// Resets this system, and regenerates starting particles.
System.prototype.reset = function() {
this.particles = [];
for (var i = 0; i < this.numParticles; i++) {
this.addParticle();
}
};
/// function System.addRule
/// Adds a rule to this system. Rules are explained more in the |@Rules|
/// object.
///
/// Params:
/// rule {Function}: The rule to add.
/// Params:
/// particle {@Particle}: The particle to operate on.
/// i {Number}: The index of the particle in the system.
/// delta {Number}: The time in seconds since the last update.
System.prototype.addRule = function(rule) {
this.rules.push(rule);
};
System.prototype.draw = function(ctx) {
this.setContext(ctx);
this.preDraw(ctx);
_.each(this.particles, function(particle, i) {
particle.drawAll(ctx);
});
this.postDraw(ctx);
};
System.prototype.update = function(delta) {
var that = this;
this.preUpdate(delta);
_.each(this.particles, function(particle, i) {
_.each(that.rules, function(rule) {
rule(particle, i, delta);
});
if (particle.update(delta)) {
that.particles[i] = null;
that.particles = _.compact(that.particles);
that.onDeath();
}
});
this.postUpdate(delta);
};
/// function System.addParticle
/// Adds a new |@Particle| to the system.
///
/// Params:
/// [particle] {@Particle}: The particle to add. Defaults to the result of
/// calling the |newParticle()| function passed in to the |@System|.
System.prototype.addParticle = function(particle) {
this.particles.push(particle || this.newParticle(this.particles.length));
};
Move.System = System;
/// class Particle
/// A particle in a particle system.
///
/// var particle = new Move.Particle({
/// pos: new Move.Vector({x: 10, y: 5}),
/// vel: new Move.Vector({x: 1, y: 1}),
/// r: 10, g: 100, b: 255
/// });
///
/// system.addParticle(particle);
///
/// Params:
/// options {Object}: Options for the particle.
/// Properties:
/// [pos] (new Vector(\)) {@Vector}: The starting position of the
/// particle.
///
/// [vel] (new Vector(\)) {@Vector}: The starting velocity of the
/// particle.
///
/// [r] (255) {Number}: Red value.
/// [g] (0) {Number}: Green value.
/// [b] (0) {Number}: Blue value.
/// [a] (1) {Number}: Alpha value.
///
/// [size] (2) {Number}: Size of the particle.
///
/// [trail] (0) {Number}: Whether this particle will have a trail.
/// The trail created by drawing increasingly transparent versions
/// of the particle at previous positions. A trail of 10 will use
/// the 10 last positions.
///
/// [draw] (@Particle.draw) {Function}: Determines how this particle
/// should be drawn on the canvas. The default draw function draws
/// the particle as a circle.
///
/// Params:
/// ctx {Context2D}: The context to draw on.
///
/// opacity {Number}: If this particle has a trail, the opacity will
/// be a number from 0 - 1 that represents how transparent the
/// particle should be. 0 means completely transparent and 1
/// means completely opaque.
///
/// pos {@Vector}: The position to draw at. This may be different
/// than the particle's position if one of the tail sections is
/// being drawn.
///
/// [preUpdate] {Function}: Function called before particle update.
///
/// Params:
/// delta {Number}: The change in time since the last update in
/// seconds.
///
/// [postUpdate] {Function}: Function called after particle update.
///
/// Params:
/// delta {Number}: The change in time since the last update in
/// seconds.
///
/// [isDead] {Function}: Function called after every update. If it
/// returns true, the particle is removed from the |@System|, and
/// |@System.onDeath| is called.
///
/// [init] {Function}: Do custom initialization on this particle.
function Particle(options) {
var that = this;
options || (options = {});
this.defaults = {
pos: new Vector(),
vel: new Vector(),
r: 255, g: 0, b: 0, a: 1,
size: 2,
trail: 0,
draw: Particle.prototype.draw.bind(this),
preUpdate: function(delta) {},
postUpdate: function(delta) {},
isDead: function() { return false; },
init: function() {}
};
_.defaults(options, this.defaults);
if (!options.origPos) {
options.origPos = new Vector(options.pos);
}
this.prevPos = [];
/// property Particle.pos {@Vector}
/// The position of the particle.
//
/// property Particle.vel {@Vector}
/// The velocity of the particle.
//
/// property Particle.origPos {@Vector}
/// The original position of the particle.
//
/// property Particle.r {Number}
/// The red value of the color of the particle.
//
/// property Particle.g {Number}
/// The green value of the color of the particle.
//
/// property Particle.b {Number}
/// The blue value of the color of the particle.
//
/// property Particle.a {Number}
/// The alpha value of the color of the particle.
//
/// property Particle.size {Number}
/// The size of the particle.
_.each(['draw', 'isDead', 'trail', 'r', 'g', 'b', 'a', 'size', 'preUpdate',
'postUpdate', 'pos', 'vel', 'origPos'], function(name) {
that[name] = options[name];
});
options.init.call(this);
}
/// function Particle.draw
/// The default draw function. Draws the particle as a circle.
///
/// Params:
/// ctx {Context2D}: The context to draw on.
///
/// opacity {Number}: If this particle has a trail, the opacity will
/// be a number from 0 - 1 that represents how transparent the
/// particle should be. 0 means completely transparent and 1
/// means completely opaque.
///
/// pos {@Vector}: The position to draw at. This may be different
/// than the particle's position if one of the tail sections is
/// being drawn.
Particle.prototype.draw = function(ctx, opacity, pos) {
ctx.beginPath();
ctx.arc(pos.x, pos.y, this.size, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(' + this.r + ', ' + this.g + ', ' + this.b +
', ' + (this.a * opacity) + ')';
ctx.fill();
};
Particle.prototype.drawAll = function(ctx) {
var that = this;
_.each(this.prevPos, function(pos, i) {
that.draw(ctx, i / that.trail, pos);
});
this.draw(ctx, 1, this.pos);
};
Particle.prototype.update = function(delta) {
var temp;
this.preUpdate(delta);
temp = new Vector(this.vel);
if (this.trail) {
if (this.prevPos && this.prevPos.length === this.trail) {
this.prevPos = _.rest(this.prevPos);
}
this.prevPos.push(new Vector(this.pos));
}
this.pos.add(temp.multiplyScalar(delta));
this.postUpdate(delta);
return this.isDead();
};
Move.Particle = Particle;
/// object Rules
/// Rules can be added to a system to specify its behavior. The |@Rules|
/// object has some starter rules, but a complex system will probably have
/// some custom rules.
///
/// Rules are functions of the form:
///
/// function(particle, i, delta) {
/// ...
/// }
///
/// Where |particle| is a |@Particle|, |i| is the index of the particle in the
/// system, and |delta| is the time in seconds since the last update.
Move.Rules = {
/// function Rules.gravity
/// Pulls the particle downward.
///
/// Params:
/// strength {Number}: The strength of the gravity.
///
/// Returns:
/// {Function}: A gravity rule that can be passed to |@System.addRule|.
gravity: function(strength) {
return function(particle, i, delta) {
particle.vel.y += strength * delta;
}
},
/// function Rules.resistance
/// Slows a particle over time.
///
/// Params:
/// strength {Number}: The strength of the resistance.
///
/// Returns:
/// {Function}: A resistance rule that can be passed to |@System.addRule|.
resistance: function(strength) {
return function(particle, i, delta) {
particle.vel.multiplyScalar(1 - strength * delta);
}
},
/// function Rules.attract
/// Attracts a particle to a point with the formula |1/distance|.
///
/// Params:
/// strength {Number}: The strength of the attraction.
/// [pos] (@Particle.origPos) {@Vector}: The point to attract to.
///
/// Returns:
/// {Function}: An attraction rule that can be passed to
/// |@System.addRule|.
attract: function(strength, pos) {
return function(particle, i, delta) {
var pos2 = pos || particle.origPos,
temp = new Vector(particle.pos);
temp.sub(pos2);
particle.vel.sub(temp.multiplyScalar(delta * strength));
}
},
/// function Rules.magnet
/// Attracts a particle to a point with the formula |1/distance^2|.
///
/// Params:
/// strength {Number}: The strength of the attraction.
/// [pos] (@Particle.origPos) {@Vector}: The point to attract to.
///
/// Returns:
/// {Function}: A magnetism rule that can be passed to |@System.addRule|.
magnet: function(strength, pos) {
return function(particle, i, delta) {
var pos2 = pos || particle.origPos,
temp = new Vector(particle.pos),
lenSq;
temp.sub(pos2);
lenSq = temp.lengthSq();
if (lenSq < .01) return;
particle.vel.sub(temp.multiplyScalar(
delta * strength / lenSq));
}
},
/// function Rules.wall
/// Creates a wall that particles will bounce off of.
///
/// Params:
/// pos {@Vector}: The position of the wall.
/// coord {String}: The coordinate of the wall. Can be |'x'|, |'y'|, or
/// |'z'|.
///
/// Returns:
/// {Function}: A wall rule that can be passed to |@System.addRule|.
wall: function(pos, coord) {
return function(particle, i, delta) {
var vel = particle.vel[coord] * delta,
ppos = particle.pos[coord],
nextPos = pos + particle.size * (vel > 0 ? -1 : 1);
if ((ppos > nextPos) != (ppos + vel > nextPos)) {
particle.pos[coord] = 2 * nextPos - vel - ppos;
particle.vel[coord] = -particle.vel[coord];
}
}
}
};
}).call(this);