-
Notifications
You must be signed in to change notification settings - Fork 74
/
impulse.js
2223 lines (1869 loc) · 56.5 KB
/
impulse.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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Impulse=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var Body = require('./body')
var simulation = require('./simulation')
var Boundary = require('./boundary')
var Animation = require('./animation')
var Vector = require('./vector')
var height = require('./util').height
var Accelerate = module.exports = Animation({
defaultOptions: {
acceleration: 1000,
bounce: false,
minBounceDistance: 5,
damping: 0.2,
restitution: 0.2
},
onStart: function(velocity, from, to, opts, update, done) {
var direction = to.sub(from).normalize()
if(typeof opts.acceleration === 'number') {
var acceleration = direction.mult(opts.acceleration)
} else {
var acceleration = Vector(opts.acceleration)
}
var bounceAcceleration = direction.mult(opts.bounceAcceleration || acceleration)
, bouncing = false
, boundary = Boundary({
left: (to.x > from.x) ? -Infinity : to.x,
right: (to.x > from.x) ? to.x : Infinity,
top: (to.y > from.y) ? -Infinity : to.y,
bottom: (to.y > from.y) ? to.y : Infinity
})
if(to.sub(from).norm() < .001 && velocity.norm() < .001) {
return update.done(to, velocity)
}
var restitution = opts.restitution || opts.damping // TODO remove damping
var body = this._body = Body(velocity, from, {
accelerate: function(s, t) {
if(bouncing)
return bounceAcceleration
else
return acceleration
},
update: function(position, velocity) {
if(boundary.contains(position)) {
update.state(position, velocity)
} else {
if(opts.bounce &&
Math.abs(height(bounceAcceleration.norm(), velocity.norm() * restitution, 0)) > opts.minBounceDistance) {
bouncing = true
body.position = Vector(to)
body.velocity.selfMult(-opts.damping)
update.state(to, body.velocity)
} else {
update.done(to, velocity)
}
}
}
})
simulation.addBody(this._body)
},
onEnd: function() {
simulation.removeBody(this._body)
}
})
},{"./animation":2,"./body":4,"./boundary":5,"./simulation":11,"./util":13,"./vector":14}],2:[function(require,module,exports){
var defaults = require('lodash.defaults')
, Promise = window.Promise || require('promise')
, Boundary = require('./boundary')
, Vector = require('./vector')
, Emitter = require('component-emitter')
var proto = {
to: function(x, y) {
if(x instanceof Boundary)
this._to = x
else
this._to = Vector(x, y)
return this
},
velocity: function(x, y) {
this._velocity = Vector(x, y)
return this
},
from: function(x, y) {
this._from = Vector(x, y)
return this
},
_updateState: function(position, velocity) {
this._phys.position(position)
this._phys.velocity(velocity)
},
cancel: function() {
this._onEnd()
this._running = false
this._reject()
},
running: function() {
return this._running || false
},
start: function() {
var that = this
, from = (this._from) ? this._from : this._phys.position()
, to = (this._to) ? this._to : this._phys.position()
, velocity = (this._velocity) ? this._velocity : this._phys.velocity()
, opts = defaults({}, this._opts || {}, this._defaultOpts)
var update = {
state: function(position, velocity) {
that._updateState(position, velocity)
},
done: function(position, velocity) {
that._updateState(position, velocity)
that._onEnd()
that._running = false
that._resolve({ position: position, velocity: velocity })
},
cancel: function(position, velocity) {
that._updateState(position, velocity)
that._onEnd()
that._running = false
that._reject()
}
}
this._phys._startAnimation(this)
this._running = true
if(to instanceof Boundary)
to = to.nearestIntersect(from, velocity)
this._onStart(velocity, from, to, opts, update)
return that._ended
}
}
function Animation(callbacks) {
var animation = function(phys, opts) {
var that = this
this._opts = opts
this._phys = phys
this._onStart = callbacks.onStart
this._onEnd = callbacks.onEnd
this._defaultOpts = callbacks.defaultOptions
this._ended = new Promise(function(resolve, reject) {
that._resolve = resolve
that._reject = reject
})
this.start = this.start.bind(this)
}
Emitter(animation.prototype)
animation.prototype = proto
return animation
}
module.exports = Animation
},{"./boundary":5,"./vector":14,"component-emitter":16,"lodash.defaults":17,"promise":24}],3:[function(require,module,exports){
var defaults = require('lodash.defaults')
, Vector = require('./vector')
, simulation = require('./simulation')
, Body = require('./body')
var defaultOptions = {
tension: 100,
damping: 10,
seperation: 0,
offset: { x: 0, y: 0 }
}
module.exports = AttachSpring
function AttachSpring(phys, attachment, opts) {
this._phys = phys
this._opts = defaults({}, opts || {}, defaultOptions)
this._position = phys.position()
this._velocity = phys.velocity()
if(typeof attachment.position === 'function')
this._attachment = attachment.position.bind(attachment)
else
this._attachment = attachment
}
AttachSpring.prototype.position = function(x, y) {
if(arguments.length === 0) {
return this._position
}
if(this._body)
this._body.position = this._position = Vector(x, y)
else
this._position = Vector(x, y)
}
AttachSpring.prototype.velocity = function(x, y) {
if(this._body)
this._body.velocity = this._velocity = Vector(x, y)
else
this._velocity = Vector(x, y)
}
AttachSpring.prototype.cancel = function(x, y) {
this._running = false
simulation.removeBody(this._body)
}
AttachSpring.prototype.stop = function(x, y) {
this._running = false
simulation.removeBody(this._body)
}
AttachSpring.prototype.running = function(x, y) {
return this._running
}
window.unit = 0
AttachSpring.prototype.start = function() {
var attachment = this._attachment
, opts = this._opts
, phys = this._phys
, velocity = this._velocity
, position = this._position
, that = this
phys._startAnimation(this)
this._running = true
var body = this._body = Body(velocity, position, {
accelerate: function(state, t) {
var distVec = state.position.selfSub(attachment())
, dist = distVec.norm()
, distNorm = distVec.normalize()
if(distNorm.x === 0 && distNorm.y === 0) {
distNorm.x = distNorm.y = 1
distNorm.normalize()
}
var accel = distNorm
.selfMult(-opts.tension)
.selfMult(dist - opts.seperation)
.selfSub(state.velocity.selfMult(opts.damping))
return accel
},
update: function(position, velocity) {
that._position = body.position
that._velocity = body.velocity
if(opts.offset) {
var pos = position.add(opts.offset)
phys.position(pos)
} else {
phys.position(position)
}
phys.velocity(velocity)
}
})
simulation.addBody(body)
return this
}
},{"./body":4,"./simulation":11,"./vector":14,"lodash.defaults":17}],4:[function(require,module,exports){
var Vector = require('./vector')
module.exports = Body
function Body(vel, from, fns) {
if(!(this instanceof Body)) return new Body(vel, from, fns)
this.previousPosition = this.position = Vector(from)
this.velocity = Vector(vel)
this._fns = fns
}
Body.prototype.update = function(alpha) {
var pos = this.previousPosition.clone().lerp(this.position, alpha)
this._fns.update(pos, this.velocity)
}
Body.prototype.accelerate = function(state, t) {
return this._fns.accelerate(state, t)
}
Body.prototype.atRest = function() {
return this.velocity.norm() < .01
}
Body.prototype.atPosition = function(pos) {
//return whether the distance between this.position and pos is less than .1
return this.position.sub(Vector(pos)).norm() < .01
}
},{"./vector":14}],5:[function(require,module,exports){
var Vector = require('./vector')
module.exports = Boundary
function pointBetween(p, p1, p2) {
return p >= p1 && p <= p2
}
function yIntersect(y, point, direction) {
var factor = (y - point.y) / direction.y
return point.add(direction.clone().mult(factor))
}
function xIntersect(x, point, direction) {
var factor = (x - point.x) / direction.x
return point.add(direction.clone().mult(factor))
}
Boundary.prototype.applyDamping = function(position, damping) {
var x = position.x
, y = position.y
if(x < this.left)
x = this.left - (this.left - x) * damping
if(y < this.top)
y = this.top - (this.top - y) * damping
if(x > this.right)
x = this.right - (this.right - x) * damping
if(y > this.bottom)
y = this.bottom - (this.bottom - y) * damping
return Vector(x, y)
}
function Boundary(boundary) {
if(!(this instanceof Boundary))
return new Boundary(boundary)
this.left = (typeof boundary.left !== 'undefined') ? boundary.left : -Infinity
this.right = (typeof boundary.right !== 'undefined') ? boundary.right : Infinity
this.top = (typeof boundary.top !== 'undefined') ? boundary.top : -Infinity
this.bottom = (typeof boundary.bottom !== 'undefined') ? boundary.bottom : Infinity
}
Boundary.prototype.contains = function(pt) {
return pt.x >= this.left &&
pt.x <= this.right &&
pt.y >= this.top &&
pt.y <= this.bottom
}
Boundary.prototype.nearestIntersect = function(point, velocity) {
var direction = Vector(velocity).normalize()
, point = Vector(point)
, isect
, distX
, distY
if(velocity.y < 0)
isect = yIntersect(this.top, point, direction)
if(velocity.y > 0)
isect = yIntersect(this.bottom, point, direction)
if(isect && pointBetween(isect.x, this.left, this.right))
return isect
if(velocity.x < 0)
isect = xIntersect(this.left, point, direction)
if(velocity.x > 0)
isect = xIntersect(this.right, point, direction)
if(isect && pointBetween(isect.y, this.top, this.bottom))
return isect
//if the velocity is zero, or it didn't intersect any lines (outside the box)
//just send it it the nearest boundary
distX = (Math.abs(point.x - this.left) < Math.abs(point.x - this.right)) ? this.left : this.right
distY = (Math.abs(point.y - this.top) < Math.abs(point.y - this.bottom)) ? this.top : this.bottom
return (distX < distY) ? Vector(distX, point.y) : Vector(point.x, distY)
}
},{"./vector":14}],6:[function(require,module,exports){
var Body = require('./body')
var simulation = require('./simulation')
var Boundary = require('./boundary')
var Animation = require('./animation')
var Decelerate = module.exports = Animation({
defaultOptions: {
deceleration: 400
},
onStart: function(velocity, from, to, opts, update, done) {
var direction = to.sub(from).normalize()
, deceleration = direction.mult(opts.deceleration).negate()
, boundary = Boundary({
left: Math.min(to.x, from.x),
right: Math.max(to.x, from.x),
top: Math.min(to.y, from.y),
bottom: Math.max(to.y, from.y)
})
velocity = direction.mult(velocity.norm())
this._body = Body(velocity, from, {
accelerate: function(s, t) {
return deceleration
},
update: function(position, velocity) {
if(!direction.directionEqual(velocity)) {
update.cancel(position, { x: 0, y: 0 })
} else if(boundary.contains(position)) {
update.state(position, velocity)
} else {
update.done(to, velocity)
}
}
})
simulation.addBody(this._body)
},
onEnd: function() {
simulation.removeBody(this._body)
}
})
},{"./animation":2,"./body":4,"./boundary":5,"./simulation":11}],7:[function(require,module,exports){
var Emitter = require('component-emitter')
, defaults = require('lodash.defaults')
var defaultOpts = {}
module.exports = Drag
function Drag(phys, opts, start) {
var handles
this._phys = phys
if(typeof opts === 'function') {
this._startFn = opts
opts = {}
} else {
this._startFn = start
}
this._opts = defaults({}, defaultOpts, opts)
//Warn of deprecated option
if(this._opts.boundry){
console.warn("Warning: Misspelled option 'boundry' is being deprecated. Please use 'boundary' instead.");
this._opts.boundary = this._opts.boundry;
delete this._opts.boundry;
}
handles = this._opts.handle
if(handles && !handles.length) {
handles = [handles]
} else if(handles && handles.length) {
handles = [].slice.call(handles)
} else {
handles = phys.els
}
handles.forEach(this._setupHandle, this)
}
Emitter(Drag.prototype)
Drag.prototype.moved = function() {
return (this._interaction.distance() > 10)
}
Drag.prototype._setupHandle = function(el) {
//start events
el.addEventListener('touchstart', this._start.bind(this))
el.addEventListener('mousedown', this._start.bind(this))
//move events
el.addEventListener('touchmove', this._move.bind(this))
//apply the move event to the window, so it keeps moving,
//event if the handle doesn't
window.addEventListener('mousemove', this._move.bind(this))
//end events
el.addEventListener('touchend', this._end.bind(this))
window.addEventListener('mouseup', this._end.bind(this))
}
Drag.prototype._start = function(evt) {
this._startTime = Date.now()
evt.preventDefault()
this._mousedown = true
this._interaction = this._phys.interact({
boundary: this._opts.boundary,
damping: this._opts.damping,
direction: this._opts.direction
})
var promise = this._interaction.start(evt)
this._startFn && this._startFn(promise)
this.emit('start', evt)
}
Drag.prototype._move = function(evt) {
if(!this._mousedown) return
evt.preventDefault()
this._interaction.update(evt)
this.emit('move', evt)
}
Drag.prototype._end = function(evt) {
if(!this._mousedown) return
evt.preventDefault()
this._mousedown = false
this._interaction.end()
this.emit('end', evt)
}
},{"component-emitter":16,"lodash.defaults":17}],8:[function(require,module,exports){
var simulation = require('./simulation')
var Vector = require('./vector')
var Renderer = require('./renderer')
var defaults = require('lodash.defaults')
var Spring = require('./spring')
var AttachSpring = require('./attach-spring')
var Decelerate = require('./decelerate')
var Accelerate = require('./accelerate')
var Drag = require('./drag')
var Interact = require('./interact')
var Boundary = require('./boundary')
var Promise = window.Promise || require('promise')
module.exports = Physics
function Physics(rendererOrEls) {
if(!(this instanceof Physics)) {
return new Physics(rendererOrEls)
}
if(typeof rendererOrEls === 'function') {
this._render = rendererOrEls
this.els = []
} else {
if(rendererOrEls.length)
this.els = [].slice.call(rendererOrEls)
else
this.els = [rendererOrEls]
this._renderer = new Renderer(this.els)
this._render = this._renderer.update.bind(this._renderer)
}
this._position = Vector(0, 0)
this._velocity = Vector(0, 0)
}
Physics.Boundary = Boundary
Physics.Boundry = Boundary
Physics.Vector = Vector
Physics.Promise = Promise
Physics.prototype.style = function() {
this._renderer.style.apply(this._renderer, arguments)
return this
}
Physics.prototype.visible = function() {
this._renderer.visible.apply(this._renderer, arguments)
return this
}
Physics.prototype.direction = function(d) {
var velocity = this.velocity()
, h, v, c
if(velocity.x < 0) h = 'left'
else if(velocity.x > 0) h = 'right'
if(velocity.y < 0) v = 'up'
else if(velocity.y > 0) v = 'down'
var c = h + (v || '').toUpperCase()
return d === h || d === v || d === c
}
Physics.prototype.forceRender = function() {
if(this._renderer) {
this._renderer.changed = true
}
}
Physics.prototype.atRest = function() {
var velocity = this.velocity()
return velocity.x === 0 && velocity.y === 0
}
Physics.prototype._startAnimation = function(animation) {
if(this._currentAnimation && this._currentAnimation.running()) {
this._currentAnimation.cancel()
}
this._currentAnimation = animation
}
Physics.prototype.velocity = function(x, y) {
if(!arguments.length) return this._velocity
this._velocity = Vector(x, y)
return this
}
Physics.prototype.position = function(x, y) {
if(!arguments.length) return this._position.clone()
this._position = Vector(x, y)
this._render(this._position.x, this._position.y)
return this
}
Physics.prototype.interact = function(opts) {
return new Interact(this, opts)
}
Physics.prototype.drag = function(opts, start) {
return new Drag(this, opts, start)
}
Physics.prototype.spring = function(opts) {
return new Spring(this, opts)
}
Physics.prototype.decelerate = function(opts) {
return new Decelerate(this, opts)
}
Physics.prototype.accelerate = function(opts) {
return new Accelerate(this, opts)
}
Physics.prototype.attachSpring = function(attachment, opts) {
return new AttachSpring(this, attachment, opts)
}
},{"./accelerate":1,"./attach-spring":3,"./boundary":5,"./decelerate":6,"./drag":7,"./interact":9,"./renderer":10,"./simulation":11,"./spring":12,"./vector":14,"lodash.defaults":17,"promise":24}],9:[function(require,module,exports){
var defaults = require('lodash.defaults')
var Velocity = require('touch-velocity')
var Vector = require('./vector')
var Promise = require('promise')
var util = require('./util')
var Boundary = require('./boundary')
module.exports = Interact
var defaultOpts = {
boundary: Boundary({}),
damping: 0,
direction: 'both'
}
function Interact(phys, opts) {
this._phys = phys
this._running = false
this._opts = defaults({}, opts, defaultOpts)
//Warn of deprecated option
if(this._opts.boundry){
console.warn("Warning: Misspelled option 'boundry' is being deprecated. Please use 'boundary' instead.");
this._opts.boundary = this._opts.boundry;
delete this._opts.boundry;
}
}
Interact.prototype.position = function(x, y) {
var direction = this._opts.direction
, boundary = this._opts.boundary
, pos = Vector(x, y)
if(direction !== 'both' && direction !== 'horizontal') pos.x = 0
if(direction !== 'both' && direction !== 'vertical') pos.y = 0
this._veloX.updatePosition(pos.x)
this._veloY.updatePosition(pos.y)
this._phys.velocity(this._veloX.getVelocity(), this._veloY.getVelocity())
pos = boundary.applyDamping(pos, this._opts.damping)
this._phys.position(pos)
return this
}
Interact.prototype.update = function(evt) {
//for jquery and hammer.js
evt = evt.originalEvent || evt
var position = util.eventVector(evt).sub(this._startPosition)
this.position(position)
return this
}
Interact.prototype.start = function(evt) {
var that = this
, evtPosition = evt && util.eventVector(evt)
, position = this._phys.position()
this._running = true
this._phys._startAnimation(this)
this._startPosition = evt && evtPosition.sub(position)
this._initialPosition = this._phys.position()
this._veloX = new Velocity()
this._veloY = new Velocity()
this.position(position)
return this._ended = new Promise(function(res, rej) {
that._resolve = res
that._reject = rej
})
}
Interact.prototype.distance = function() {
return this._initialPosition.sub(this._phys.position()).norm()
}
Interact.prototype.cancel = function() {
this._running = false
this._reject(new Error('Canceled the interaction'))
}
Interact.prototype.running = function() {
return this._running
}
Interact.prototype.end = function() {
this._phys.velocity(this._veloX.getVelocity(), this._veloY.getVelocity())
this._resolve({ velocity: this._phys.velocity(), position: this._phys.position() })
return this._ended
}
},{"./boundary":5,"./util":13,"./vector":14,"lodash.defaults":17,"promise":24,"touch-velocity":28}],10:[function(require,module,exports){
var prefixes = ['Webkit', 'Moz', 'Ms', 'ms']
var calls = []
var transformProp
var raf = require('raf')
var floatEqual = require('./util').floatEqual
function loop() {
raf(function() {
loop()
for(var i = calls.length - 1; i >= 0; i--) {
calls[i]()
}
})
}
loop()
function prefixed(prop) {
var prefixed
for (var i = 0; i < prefixes.length; i++) {
prefixed = prefixes[i] + prop[0].toUpperCase() + prop.slice(1)
if(typeof document.body.style[prefixed] !== 'undefined')
return prefixed
}
return prop
}
var transformsProperties = ['translate', 'translateX', 'translateY', 'translateZ',
'rotate', 'rotateX', 'rotateY', 'rotate3d', 'rotateZ',
'scale', 'scaleX', 'scaleY', 'scaleZ',
'skew', 'skewX', 'skewY', 'skewZ']
module.exports = Renderer
function Renderer(els) {
if(typeof els.length === 'undefined')
els = [els]
this.els = els
this.styles = {}
this.invisibleEls = []
this.changed = false
calls.push(this.render.bind(this))
}
Renderer.prototype.render = function() {
if(!this.changed) return
if(!transformProp)
transformProp = prefixed('transform')
var transformsToApply
, els = this.els
, position = this.currentPosition
, styles = this.styles
, value
, props = Object.keys(styles)
, elsLength = els.length
, propsLength = props.length
, i, j
, transforms
this.changed = false
for(i = 0 ; i < elsLength ; i++) {
transformsToApply = []
if(this.visibleFn && !this.visibleFn(position, i)) {
if(!this.invisibleEls[i]) {
els[i].style.webkitTransform = 'translate3d(0, -99999px, 0)'
}
this.invisibleEls[i] = true
} else {
this.invisibleEls[i] = false
for (j = 0; j < propsLength; j++) {
prop = props[j]
value = (typeof styles[prop] === 'function') ? styles[prop](position.x, position.y, i) : styles[prop]
if(transformsProperties.indexOf(prop) !== -1) {
transformsToApply.push(prop + '(' + value + ')')
} else {
els[i].style[prop] = value
}
}
transforms = transformsToApply.join(' ')
transforms += ' translateZ(0)'
els[i].style[transformProp] = transforms
}
}
}
Renderer.prototype.style = function(property, value) {
if(typeof property === 'object') {
for(prop in property) {
if(property.hasOwnProperty(prop)) {
this.style(prop, property[prop])
}
}
}
this.styles[property] = value
return this
}
Renderer.prototype.visible = function(isVisible) {
this.visibleFn = isVisible
return this
}
Renderer.prototype.update = function(x, y) {
if(this.currentPosition) {
var equal = floatEqual(x, this.currentPosition.x) && floatEqual(y, this.currentPosition.y)
this.changed = this.changed || !equal
} else {
this.changed = true
}
this.currentPosition = { x: x, y: y }
}
},{"./util":13,"raf":26}],11:[function(require,module,exports){
var Vector = require('./vector')
, bodies = []
, raf = require('raf')
function increment(a, b, c, d) {
var vec = Vector(0, 0)
vec.selfAdd(a)
vec.selfAdd(b.add(c).selfMult(2))
vec.selfAdd(d)
vec.selfMult(1/6)
return vec
}
var positionVec = Vector(0, 0)
var velocityVec = Vector(0, 0)
function evaluate(initial, t, dt, d) {
var state = {}
state.position = positionVec.setv(d.dx).selfMult(dt).selfAdd(initial.position)
state.velocity = velocityVec.setv(d.dv).selfMult(dt).selfAdd(initial.velocity)
var next = {
dx: state.velocity.clone(),
dv: initial.accelerate(state, t).clone()
}
return next
}
var der = { dx: Vector(0, 0), dv: Vector(0, 0) }
function integrate(state, t, dt) {
var a = evaluate( state, t, 0, der )
var b = evaluate( state, t, dt*0.5, a )
var c = evaluate( state, t, dt*0.5, b )
var d = evaluate( state, t, dt, c )
var dxdt = increment(a.dx,b.dx,c.dx,d.dx)
, dvdt = increment(a.dv,b.dv,c.dv,d.dv)
state.position.selfAdd(dxdt.selfMult(dt));
state.velocity.selfAdd(dvdt.selfMult(dt));
}
var currentTime = Date.now() / 1000
, accumulator = 0
, t = 0
, dt = 0.015
function simulate() {
raf(function() {
simulate()
var newTime = Date.now() / 1000
var frameTime = newTime - currentTime
currentTime = newTime
if(frameTime > 0.05)
frameTime = 0.05
accumulator += frameTime
var j = 0
while(accumulator >= dt) {
for(var i = 0 ; i < bodies.length ; i++) {
bodies[i].previousPosition = bodies[i].position.clone()
integrate(bodies[i], t, dt)
}
t += dt
accumulator -= dt
}
for(var i = 0 ; i < bodies.length ; i++) {
bodies[i].update(accumulator / dt)
}
}, 16)
}
simulate()
module.exports.addBody = function(body) {
bodies.push(body)
return body
}
module.exports.removeBody = function(body) {
var index = bodies.indexOf(body)
if(index >= 0)
bodies.splice(index, 1)
}
},{"./vector":14,"raf":26}],12:[function(require,module,exports){
var Body = require('./body')
var simulation = require('./simulation')
var Boundary = require('./boundary')
var Animation = require('./animation')
var Spring = module.exports = Animation({
defaultOptions: {
tension: 100,
damping: 10
},
onStart: function(velocity, from, to, opts, update) {
var body = this._body = new Body(velocity, from, {
accelerate: function(state, t) {
return state.position.selfSub(to)
.selfMult(-opts.tension)
.selfSub(state.velocity.mult(opts.damping))
},
update: function(position, velocity) {
if(body.atRest() && body.atPosition(to)) {
update.done(to, { x: 0, y: 0 })
} else {
update.state(position, velocity)
}
}
})
simulation.addBody(this._body)
},
onEnd: function() {
simulation.removeBody(this._body)
}
})
},{"./animation":2,"./body":4,"./boundary":5,"./simulation":11}],13:[function(require,module,exports){
var Vector = require('./vector')
function vertex(a, b) {
return -b / (2 * a)
}
function height(a, b, c) {
return parabola(a, b, c, vertex(a, b))
}