@tweenjs/tween.js 17.3.0 → 17.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.tmp/tests.js ADDED
@@ -0,0 +1,1320 @@
1
+ import * as TWEEN from './Index';
2
+ export var tests = {
3
+ hello: function (test) {
4
+ test.ok(TWEEN !== null);
5
+ test.done();
6
+ },
7
+ // TWEEN tests
8
+ 'TWEEN.getAll': function (test) {
9
+ test.ok(TWEEN.getAll() instanceof Array);
10
+ test.done();
11
+ },
12
+ 'TWEEN object stores tweens automatically on start': function (test) {
13
+ var numTweensBefore = TWEEN.getAll().length, t = new TWEEN.Tween({});
14
+ t.start();
15
+ var numTweensAfter = TWEEN.getAll().length;
16
+ test.equal(numTweensBefore + 1, numTweensAfter);
17
+ test.done();
18
+ },
19
+ 'TWEEN.removeAll()': function (test) {
20
+ var t = new TWEEN.Tween({});
21
+ TWEEN.removeAll();
22
+ test.equal(TWEEN.getAll().length, 0, 'No tweens left');
23
+ t.start();
24
+ test.equal(TWEEN.getAll().length, 1, 'A tween has been added');
25
+ TWEEN.removeAll();
26
+ test.equal(TWEEN.getAll().length, 0, 'No tweens left');
27
+ test.done();
28
+ },
29
+ 'TWEEN.add()': function (test) {
30
+ var all = TWEEN.getAll(), numTweens = all.length, t = new TWEEN.Tween({});
31
+ TWEEN.add(t);
32
+ test.equal(numTweens + 1, TWEEN.getAll().length);
33
+ test.done();
34
+ },
35
+ 'TWEEN.remove()': function (test) {
36
+ var all = TWEEN.getAll(), numTweens = all.length, t = new TWEEN.Tween({});
37
+ TWEEN.add(t);
38
+ test.ok(TWEEN.getAll().indexOf(t) != -1);
39
+ TWEEN.remove(t);
40
+ test.equal(numTweens, TWEEN.getAll().length);
41
+ test.equal(TWEEN.getAll().indexOf(t), -1);
42
+ test.done();
43
+ },
44
+ 'TWEEN.update() returns false when done (no tweens to animate)': function (test) {
45
+ TWEEN.removeAll();
46
+ test.deepEqual(TWEEN.update(), false);
47
+ test.done();
48
+ },
49
+ 'TWEEN.update() returns true when there are active tweens': function (test) {
50
+ TWEEN.removeAll();
51
+ var t = new TWEEN.Tween({});
52
+ t.start();
53
+ test.deepEqual(TWEEN.update(), true);
54
+ test.done();
55
+ },
56
+ 'TWEEN.update() removes tweens when they are finished': function (test) {
57
+ TWEEN.removeAll();
58
+ var t1 = new TWEEN.Tween({}).to({}, 1000), t2 = new TWEEN.Tween({}).to({}, 2000);
59
+ test.equal(TWEEN.getAll().length, 0);
60
+ t1.start(0);
61
+ t2.start(0);
62
+ test.equal(TWEEN.getAll().length, 2);
63
+ TWEEN.update(0);
64
+ test.equal(TWEEN.getAll().length, 2);
65
+ TWEEN.update(999);
66
+ test.equal(TWEEN.getAll().length, 2);
67
+ TWEEN.update(1000);
68
+ test.equal(TWEEN.getAll().length, 1);
69
+ test.equal(TWEEN.getAll().indexOf(t1), -1);
70
+ test.ok(TWEEN.getAll().indexOf(t2) != -1);
71
+ test.done();
72
+ },
73
+ 'TWEEN.update() does not remove tweens when they are finished with preserve flag': function (test) {
74
+ TWEEN.removeAll();
75
+ var t1 = new TWEEN.Tween({}).to({}, 1000), t2 = new TWEEN.Tween({}).to({}, 2000);
76
+ test.equal(TWEEN.getAll().length, 0);
77
+ t1.start(0);
78
+ t2.start(0);
79
+ test.equal(TWEEN.getAll().length, 2);
80
+ TWEEN.update(0, true);
81
+ test.equal(TWEEN.getAll().length, 2);
82
+ TWEEN.update(999, true);
83
+ test.equal(TWEEN.getAll().length, 2);
84
+ TWEEN.update(1000, true);
85
+ test.equal(TWEEN.getAll().length, 2);
86
+ TWEEN.update(1001, true);
87
+ test.equal(TWEEN.getAll().length, 2);
88
+ test.ok(TWEEN.getAll().indexOf(t1) != -1);
89
+ test.ok(TWEEN.getAll().indexOf(t2) != -1);
90
+ test.done();
91
+ },
92
+ 'Unremoved tweens which have been updated past their finish time may go backward in time': function (test) {
93
+ TWEEN.removeAll();
94
+ var target1 = { a: 0 };
95
+ var target2 = { b: 0 };
96
+ var t1 = new TWEEN.Tween(target1).to({ a: 1 }, 1000), t2 = new TWEEN.Tween(target2).to({ b: 1 }, 2000);
97
+ t1.start(0);
98
+ t2.start(0);
99
+ // To be able to make a tween go backward in time, it must be
100
+ // updated with preserve set to true. Otherwise, the
101
+ // backward-in-time feature does not apply.
102
+ TWEEN.update(200, true);
103
+ TWEEN.update(2500, true);
104
+ TWEEN.update(500, true);
105
+ test.equal(TWEEN.getAll().length, 2);
106
+ test.equal(target1.a, 0.5);
107
+ test.equal(target2.b, 0.25);
108
+ test.done();
109
+ },
110
+ // TWEEN.Tween tests
111
+ constructor: function (test) {
112
+ var t = new TWEEN.Tween({});
113
+ test.ok(t instanceof TWEEN.Tween, 'Pass');
114
+ test.done();
115
+ },
116
+ 'Return the same tween instance for method chaining': function (test) {
117
+ var t = new TWEEN.Tween({});
118
+ test.ok(t.to({}, 0) instanceof TWEEN.Tween);
119
+ test.equal(t.to({}, 0), t);
120
+ test.ok(t.start() instanceof TWEEN.Tween);
121
+ test.equal(t.start(), t);
122
+ test.ok(t.stop() instanceof TWEEN.Tween);
123
+ test.equal(t.stop(), t);
124
+ test.ok(t.delay() instanceof TWEEN.Tween);
125
+ test.equal(t.delay(), t);
126
+ test.ok(t.easing() instanceof TWEEN.Tween);
127
+ test.equal(t.easing(), t);
128
+ test.ok(t.interpolation() instanceof TWEEN.Tween);
129
+ test.equal(t.interpolation(), t);
130
+ test.ok(t.chain() instanceof TWEEN.Tween);
131
+ test.equal(t.chain(), t);
132
+ test.ok(t.onStart() instanceof TWEEN.Tween);
133
+ test.equal(t.onStart(), t);
134
+ test.ok(t.onStop() instanceof TWEEN.Tween);
135
+ test.equal(t.onStop(), t);
136
+ test.ok(t.onUpdate() instanceof TWEEN.Tween);
137
+ test.equal(t.onUpdate(), t);
138
+ test.ok(t.onComplete() instanceof TWEEN.Tween);
139
+ test.equal(t.onComplete(), t);
140
+ test.ok(t.duration() instanceof TWEEN.Tween);
141
+ test.equal(t.duration(), t);
142
+ test.ok(t.group() instanceof TWEEN.Tween);
143
+ test.equal(t.group(), t);
144
+ test.done();
145
+ },
146
+ 'Tween existing property': function (test) {
147
+ var obj = { x: 1 }, t = new TWEEN.Tween(obj);
148
+ t.to({ x: 2 }, 1000);
149
+ t.start(0);
150
+ t.update(1000);
151
+ test.deepEqual(obj.x, 2);
152
+ test.done();
153
+ },
154
+ 'Tween non-existing property': function (test) {
155
+ var obj = { x: 1 }, t = new TWEEN.Tween(obj);
156
+ t.to({ y: 0 }, 1000);
157
+ t.start(0);
158
+ t.update(1000);
159
+ test.deepEqual(obj.x, 1);
160
+ // eslint-disable-next-line
161
+ // @ts-ignore
162
+ test.equal(obj.y, undefined);
163
+ test.done();
164
+ },
165
+ 'Tween non-null property': function (test) {
166
+ var obj = { x: 1 }, t = new TWEEN.Tween(obj);
167
+ t.to({ x: 2 }, 1000);
168
+ t.start(0);
169
+ t.update(1000);
170
+ test.deepEqual(obj.x, 2);
171
+ test.ok(obj.x !== null);
172
+ test.done();
173
+ },
174
+ 'Tween function property': function (test) {
175
+ var my_function = new Function();
176
+ var obj = { x: my_function }, t = new TWEEN.Tween(obj);
177
+ t.to({ x: my_function });
178
+ t.start(0);
179
+ t.update(1000);
180
+ test.ok(obj.x === my_function);
181
+ test.done();
182
+ },
183
+ 'Tween boolean property': function (test) {
184
+ var obj = { x: true }, t = new TWEEN.Tween(obj);
185
+ t.to({ x: new Function() });
186
+ t.start(0);
187
+ t.update(1000);
188
+ test.ok(typeof obj.x === 'boolean');
189
+ test.ok(obj.x === true);
190
+ test.done();
191
+ },
192
+ 'Tween null property': function (test) {
193
+ var obj = { x: null }, t = new TWEEN.Tween(obj);
194
+ t.to({ x: 2 }, 1000);
195
+ t.start(0);
196
+ t.update(1000);
197
+ test.deepEqual(obj.x, 2);
198
+ test.done();
199
+ },
200
+ 'Tween undefined property': function (test) {
201
+ var obj = {}, t = new TWEEN.Tween(obj);
202
+ t.to({ x: 2 }, 1000);
203
+ t.start(0);
204
+ t.update(1000);
205
+ // eslint-disable-next-line
206
+ // @ts-ignore
207
+ test.equal(obj.x, undefined);
208
+ test.done();
209
+ },
210
+ 'Tween relative positive value': function (test) {
211
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj);
212
+ t.to({ x: '+100' }, 1000);
213
+ t.start(0);
214
+ t.update(1000);
215
+ test.equal(obj.x, 100);
216
+ test.done();
217
+ },
218
+ 'Tween relative negative value': function (test) {
219
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj);
220
+ t.to({ x: '-100' }, 1000);
221
+ t.start(0);
222
+ t.update(1000);
223
+ test.equal(obj.x, -100);
224
+ test.done();
225
+ },
226
+ 'String values without a + or - sign should not be interpreted as relative': function (test) {
227
+ var obj = { x: 100 }, t = new TWEEN.Tween(obj);
228
+ t.to({ x: '100' }, 1000);
229
+ t.start(0);
230
+ t.update(1000);
231
+ test.equal(obj.x, 100);
232
+ test.done();
233
+ },
234
+ 'Tween relative positive value, with yoyo': function (test) {
235
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj);
236
+ t.to({ x: '+100' }, 1000);
237
+ t.repeat(1);
238
+ t.yoyo(true);
239
+ t.start(0);
240
+ t.update(500);
241
+ test.equal(obj.x, 50);
242
+ t.update(1000);
243
+ test.equal(obj.x, 100);
244
+ t.update(1500);
245
+ test.equal(obj.x, 50);
246
+ t.update(2000);
247
+ test.equal(obj.x, 0);
248
+ test.done();
249
+ },
250
+ 'Tween relative negative value, with yoyo': function (test) {
251
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj);
252
+ t.to({ x: '-100' }, 1000);
253
+ t.repeat(1);
254
+ t.yoyo(true);
255
+ t.start(0);
256
+ t.update(500);
257
+ test.equal(obj.x, -50);
258
+ t.update(1000);
259
+ test.equal(obj.x, -100);
260
+ t.update(1500);
261
+ test.equal(obj.x, -50);
262
+ t.update(2000);
263
+ test.equal(obj.x, -0);
264
+ test.done();
265
+ },
266
+ 'Tween relative positive array interpolation values': function (test) {
267
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj);
268
+ t.to({ x: ['+100', '+0', '-100', '+0'] }, 2000);
269
+ t.start(0);
270
+ t.update(250);
271
+ test.equal(obj.x, 50);
272
+ t.update(500);
273
+ test.equal(obj.x, 100);
274
+ t.update(750);
275
+ test.equal(obj.x, 50);
276
+ t.update(1000);
277
+ test.equal(obj.x, 0);
278
+ t.update(1250);
279
+ test.equal(obj.x, -50);
280
+ t.update(1500);
281
+ test.equal(obj.x, -100);
282
+ t.update(1750);
283
+ test.equal(obj.x, -50);
284
+ t.update(2000);
285
+ test.equal(obj.x, 0);
286
+ test.done();
287
+ },
288
+ 'String values without a + or - sign should not be interpreted as relative with array interpolation values': function (test) {
289
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj);
290
+ t.to({ x: ['100', '0', '100', '0'] }, 2000);
291
+ t.start(0);
292
+ t.update(250);
293
+ test.equal(obj.x, 50);
294
+ t.update(500);
295
+ test.equal(obj.x, 100);
296
+ t.update(750);
297
+ test.equal(obj.x, 50);
298
+ t.update(1000);
299
+ test.equal(obj.x, 0);
300
+ t.update(1250);
301
+ test.equal(obj.x, 50);
302
+ t.update(1500);
303
+ test.equal(obj.x, 100);
304
+ t.update(1750);
305
+ test.equal(obj.x, 50);
306
+ t.update(2000);
307
+ test.equal(obj.x, 0);
308
+ test.done();
309
+ },
310
+ 'animate values in an array': function (test) {
311
+ var obj = [0, 0, 0], t = new TWEEN.Tween(obj);
312
+ t.to([1000, '-2000', '+2000'], 1000);
313
+ t.start(0);
314
+ t.update(250);
315
+ test.equal(obj[0], 250);
316
+ test.equal(obj[1], -500);
317
+ test.equal(obj[2], 500);
318
+ t.update(500);
319
+ test.equal(obj[0], 500);
320
+ test.equal(obj[1], -1000);
321
+ test.equal(obj[2], 1000);
322
+ t.update(750);
323
+ test.equal(obj[0], 750);
324
+ test.equal(obj[1], -1500);
325
+ test.equal(obj[2], 1500);
326
+ t.update(1000);
327
+ test.equal(obj[0], 1000);
328
+ test.equal(obj[1], -2000);
329
+ test.equal(obj[2], 2000);
330
+ test.done();
331
+ },
332
+ 'animate values in a nested array': function (test) {
333
+ var obj = { a: [0, 0, 0] }, t = new TWEEN.Tween(obj);
334
+ t.to({ a: [1000, '-2000', '+2000'] }, 1000);
335
+ t.start(0);
336
+ t.update(250);
337
+ test.equal(obj.a[0], 250);
338
+ test.equal(obj.a[1], -500);
339
+ test.equal(obj.a[2], 500);
340
+ t.update(500);
341
+ test.equal(obj.a[0], 500);
342
+ test.equal(obj.a[1], -1000);
343
+ test.equal(obj.a[2], 1000);
344
+ t.update(750);
345
+ test.equal(obj.a[0], 750);
346
+ test.equal(obj.a[1], -1500);
347
+ test.equal(obj.a[2], 1500);
348
+ t.update(1000);
349
+ test.equal(obj.a[0], 1000);
350
+ test.equal(obj.a[1], -2000);
351
+ test.equal(obj.a[2], 2000);
352
+ test.done();
353
+ },
354
+ 'Test TWEEN.Tween.start()': function (test) {
355
+ var obj = {}, t = new TWEEN.Tween(obj);
356
+ t.to({}, 1000);
357
+ TWEEN.removeAll();
358
+ test.equal(TWEEN.getAll().length, 0); // TODO move to TWEEN test
359
+ t.start(0);
360
+ test.equal(TWEEN.getAll().length, 1); // TODO ditto
361
+ test.equal(TWEEN.getAll()[0], t);
362
+ test.done();
363
+ },
364
+ 'Ensure tweens start without calling start() method.': function (test) {
365
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj);
366
+ t.to({ x: 1000 }, 1000);
367
+ var started = false;
368
+ t.onStart(function () { return (started = true); });
369
+ t.onComplete(function () { return (started = false); });
370
+ t.update(0);
371
+ test.deepEqual(started, true);
372
+ test.deepEqual(obj.x, 0);
373
+ t.update(500);
374
+ test.deepEqual(started, true);
375
+ test.deepEqual(obj.x, 500);
376
+ t.update(1000);
377
+ test.deepEqual(obj.x, 1000);
378
+ test.deepEqual(started, false);
379
+ test.done();
380
+ },
381
+ 'Test Tween.to() tweening towards a dynamic object': function (test) {
382
+ var rabbit = { x: 1000, y: 0 };
383
+ var tr = new TWEEN.Tween(rabbit);
384
+ tr.to({ y: 1000 }, 1000);
385
+ tr.start(0);
386
+ var fox = { x: 0, y: 0 };
387
+ var tf = new TWEEN.Tween(fox);
388
+ tf.to(rabbit, 1000); // fox chase rabbit!
389
+ tf.start(0);
390
+ tr.update(200);
391
+ tf.update(200);
392
+ test.equal(rabbit.x, 1000);
393
+ test.equal(rabbit.y, 200);
394
+ test.equal(fox.x, 200);
395
+ test.equal(fox.y, 40);
396
+ tr.update(500);
397
+ tf.update(500);
398
+ test.equal(rabbit.x, 1000);
399
+ test.equal(rabbit.y, 500);
400
+ test.equal(fox.x, 500);
401
+ test.equal(fox.y, 250);
402
+ tr.update(800);
403
+ tf.update(800);
404
+ test.equal(rabbit.x, 1000);
405
+ test.equal(rabbit.y, 800);
406
+ test.equal(fox.x, 800);
407
+ test.equal(fox.y, 640);
408
+ tr.update(1000);
409
+ tf.update(1000);
410
+ test.equal(rabbit.x, 1000);
411
+ test.equal(rabbit.y, 1000);
412
+ test.equal(fox.x, 1000);
413
+ test.equal(fox.y, 1000);
414
+ test.done();
415
+ },
416
+ 'Test TWEEN.Tween.stop()': function (test) {
417
+ var obj = {}, t = new TWEEN.Tween(obj);
418
+ t.to({ x: 2 }, 1000);
419
+ TWEEN.removeAll();
420
+ t.start();
421
+ t.stop();
422
+ test.equal(TWEEN.getAll().length, 0);
423
+ test.done();
424
+ },
425
+ 'Test TWEEN.Tween.delay()': function (test) {
426
+ var obj = { x: 1 }, t = new TWEEN.Tween(obj);
427
+ t.to({ x: 2 }, 1000);
428
+ t.delay(500);
429
+ t.start(0);
430
+ t.update(100);
431
+ test.deepEqual(obj.x, 1, "Tween hasn't started yet");
432
+ t.update(1000);
433
+ test.ok(obj.x !== 1 && obj.x !== 2, "Tween has started but hasn't finished yet");
434
+ t.update(1500);
435
+ test.equal(obj.x, 2, 'Tween finishes when expected');
436
+ test.done();
437
+ },
438
+ // TODO: not really sure how to test this. Advice appreciated!
439
+ 'Test TWEEN.Tween.easing()': function (test) {
440
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj);
441
+ t.to({ x: 1 }, 1000);
442
+ t.easing(TWEEN.Easing.Quadratic.In);
443
+ t.start(0);
444
+ t.update(500);
445
+ test.equal(obj.x, TWEEN.Easing.Quadratic.In(0.5));
446
+ test.done();
447
+ },
448
+ // TODO test interpolation()
449
+ 'Test TWEEN.Tween.chain --with one tween': function (test) {
450
+ var t = new TWEEN.Tween({}), t2 = new TWEEN.Tween({});
451
+ var tStarted = false, tCompleted = false, t2Started = false;
452
+ TWEEN.removeAll();
453
+ t.to({}, 1000);
454
+ t2.to({}, 1000);
455
+ t.chain(t2);
456
+ t.onStart(function () {
457
+ tStarted = true;
458
+ });
459
+ t.onComplete(function () {
460
+ tCompleted = true;
461
+ });
462
+ t2.onStart(function () {
463
+ test.equal(tStarted, true);
464
+ test.equal(tCompleted, true);
465
+ test.equal(t2Started, false);
466
+ t2Started = true;
467
+ });
468
+ test.equal(tStarted, false);
469
+ test.equal(t2Started, false);
470
+ t.start(0);
471
+ TWEEN.update(0);
472
+ test.equal(tStarted, true);
473
+ test.equal(t2Started, false);
474
+ TWEEN.update(1000);
475
+ test.equal(tCompleted, true);
476
+ TWEEN.update(1001);
477
+ test.equal(t2Started, true, 't2 is automatically started by t');
478
+ test.done();
479
+ },
480
+ 'Test TWEEN.Tween.chain --with several tweens in an array': function (test) {
481
+ var t = new TWEEN.Tween({}), chainedTweens = [], numChained = 3;
482
+ var numChainedStarted = 0;
483
+ TWEEN.removeAll();
484
+ t.to({}, 1000);
485
+ function onChainedStart() {
486
+ numChainedStarted++;
487
+ }
488
+ for (var i = 0; i < numChained; i++) {
489
+ var chained = new TWEEN.Tween({});
490
+ chained.to({}, 1000);
491
+ chainedTweens.push(chained);
492
+ chained.onStart(onChainedStart);
493
+ }
494
+ t.chain.apply(t, chainedTweens);
495
+ test.equal(numChainedStarted, 0);
496
+ t.start(0);
497
+ TWEEN.update(0);
498
+ TWEEN.update(1000);
499
+ TWEEN.update(1001);
500
+ test.equal(numChainedStarted, numChained, 'All chained tweens have been started');
501
+ test.done();
502
+ },
503
+ 'Test TWEEN.Tween.chain allows endless loops': function (test) {
504
+ var obj = { x: 0 }, t1 = new TWEEN.Tween(obj).to({ x: 100 }, 1000), t2 = new TWEEN.Tween(obj).to({ x: 0 }, 1000);
505
+ TWEEN.removeAll();
506
+ t1.chain(t2);
507
+ t2.chain(t1);
508
+ test.equal(obj.x, 0);
509
+ // x == 0
510
+ t1.start(0);
511
+ TWEEN.update(0);
512
+ test.equal(obj.x, 0);
513
+ TWEEN.update(500);
514
+ test.equal(obj.x, 50);
515
+ // there... (x == 100)
516
+ TWEEN.update(1000);
517
+ test.equal(obj.x, 100);
518
+ TWEEN.update(1500);
519
+ test.equal(obj.x, 50);
520
+ // ... and back again (x == 0)
521
+ TWEEN.update(2000);
522
+ test.equal(obj.x, 0);
523
+ TWEEN.update(2500);
524
+ test.equal(obj.x, 50);
525
+ TWEEN.update(3000);
526
+ test.equal(obj.x, 100); // and x == 100 again
527
+ // Repeat the same test but with the tweens added in the
528
+ // opposite order.
529
+ var obj2 = { x: 0 };
530
+ var t3 = new TWEEN.Tween(obj2).to({ x: 200 }, 1000);
531
+ var t4 = new TWEEN.Tween(obj2).to({ x: 100 }, 1000);
532
+ t4.chain(t3);
533
+ t3.chain(t4);
534
+ test.equal(obj2.x, 0);
535
+ t4.start(0);
536
+ TWEEN.update(0);
537
+ test.equal(obj2.x, 0);
538
+ TWEEN.update(500);
539
+ test.equal(obj2.x, 50);
540
+ TWEEN.update(1000);
541
+ test.equal(obj2.x, 100);
542
+ TWEEN.update(1500);
543
+ test.equal(obj2.x, 150);
544
+ TWEEN.update(2000);
545
+ test.equal(obj2.x, 0);
546
+ TWEEN.update(2500);
547
+ test.equal(obj2.x, 50);
548
+ TWEEN.update(3000);
549
+ test.equal(obj2.x, 100);
550
+ TWEEN.update(3500);
551
+ test.equal(obj2.x, 150);
552
+ TWEEN.update(4000);
553
+ test.equal(obj2.x, 0);
554
+ TWEEN.update(4500);
555
+ test.equal(obj2.x, 50);
556
+ test.done();
557
+ },
558
+ 'Test TWEEN.Tween.onStart': function (test) {
559
+ var obj = {}, t = new TWEEN.Tween(obj);
560
+ var counter = 0;
561
+ t.to({ x: 2 }, 1000);
562
+ t.onStart(function () {
563
+ test.ok(true, 'onStart callback is called');
564
+ counter++;
565
+ });
566
+ test.deepEqual(counter, 0);
567
+ t.start(0);
568
+ TWEEN.update(0);
569
+ test.deepEqual(counter, 1);
570
+ TWEEN.update(500);
571
+ test.deepEqual(counter, 1, 'onStart callback is not called again');
572
+ test.done();
573
+ },
574
+ 'Test TWEEN.Tween.onStop': function (test) {
575
+ var obj = {}, t = new TWEEN.Tween(obj);
576
+ var counter = 0;
577
+ t.to({ x: 2 }, 1000);
578
+ t.onStop(function () {
579
+ test.ok(true, 'onStop callback is called');
580
+ counter++;
581
+ });
582
+ test.deepEqual(counter, 0);
583
+ t.stop();
584
+ TWEEN.update(0);
585
+ test.deepEqual(counter, 0, "onStop callback not called when the tween hasn't started yet");
586
+ t.start(0);
587
+ TWEEN.update(0);
588
+ t.stop();
589
+ test.deepEqual(counter, 1, 'onStop callback is called if the tween has been started already and stop is invoked');
590
+ TWEEN.update(500);
591
+ t.stop();
592
+ test.deepEqual(counter, 1, 'onStop callback is not called again once the tween is stopped');
593
+ test.done();
594
+ },
595
+ 'Test TWEEN.Tween.onUpdate': function (test) {
596
+ var obj = {}, t = new TWEEN.Tween(obj);
597
+ var counter = 0;
598
+ t.to({ x: 2 }, 1000);
599
+ t.onUpdate(function () {
600
+ counter++;
601
+ });
602
+ test.deepEqual(counter, 0);
603
+ t.start(0);
604
+ TWEEN.update(0);
605
+ test.deepEqual(counter, 1);
606
+ TWEEN.update(500);
607
+ test.deepEqual(counter, 2);
608
+ TWEEN.update(600);
609
+ test.deepEqual(counter, 3);
610
+ TWEEN.update(1000);
611
+ test.deepEqual(counter, 4);
612
+ TWEEN.update(1500);
613
+ test.deepEqual(counter, 4, 'onUpdate callback should not be called after the tween has finished');
614
+ test.done();
615
+ },
616
+ 'Test TWEEN.Tween.onComplete': function (test) {
617
+ var obj = {}, t = new TWEEN.Tween(obj);
618
+ var counter = 0;
619
+ t.to({ x: 2 }, 1000);
620
+ t.onComplete(function () {
621
+ counter++;
622
+ });
623
+ test.deepEqual(counter, 0);
624
+ t.start(0);
625
+ TWEEN.update(0);
626
+ test.deepEqual(counter, 0);
627
+ TWEEN.update(500);
628
+ test.deepEqual(counter, 0);
629
+ TWEEN.update(600);
630
+ test.deepEqual(counter, 0);
631
+ TWEEN.update(1000);
632
+ test.deepEqual(counter, 1);
633
+ TWEEN.update(1500);
634
+ test.deepEqual(counter, 1, 'onComplete callback must be called only once');
635
+ test.done();
636
+ },
637
+ 'TWEEN.Tween does not repeat by default': function (test) {
638
+ TWEEN.removeAll();
639
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj).to({ x: 100 }, 100);
640
+ t.start(0);
641
+ TWEEN.update(0);
642
+ test.equal(obj.x, 0);
643
+ TWEEN.update(50);
644
+ test.equal(obj.x, 50);
645
+ TWEEN.update(100);
646
+ test.equal(obj.x, 100);
647
+ TWEEN.update(150);
648
+ test.equal(obj.x, 100);
649
+ test.done();
650
+ },
651
+ 'Test single repeat happens only once': function (test) {
652
+ TWEEN.removeAll();
653
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj).to({ x: 100 }, 100).repeat(1);
654
+ t.start(0);
655
+ TWEEN.update(0);
656
+ test.equal(obj.x, 0);
657
+ TWEEN.update(50);
658
+ test.equal(obj.x, 50);
659
+ TWEEN.update(100);
660
+ test.equal(obj.x, 100);
661
+ TWEEN.update(150);
662
+ test.equal(obj.x, 50);
663
+ TWEEN.update(200);
664
+ test.equal(obj.x, 100);
665
+ test.done();
666
+ },
667
+ 'Test Infinity repeat happens forever': function (test) {
668
+ TWEEN.removeAll();
669
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj).to({ x: 100 }, 100).repeat(Infinity);
670
+ t.start(0);
671
+ TWEEN.update(0);
672
+ test.equal(obj.x, 0);
673
+ TWEEN.update(50);
674
+ test.equal(obj.x, 50);
675
+ TWEEN.update(100);
676
+ test.equal(obj.x, 100);
677
+ TWEEN.update(150);
678
+ test.equal(obj.x, 50);
679
+ TWEEN.update(200);
680
+ test.equal(obj.x, 100);
681
+ TWEEN.update(250);
682
+ test.equal(obj.x, 50);
683
+ test.done();
684
+ },
685
+ 'Test tweening relatively with repeat': function (test) {
686
+ TWEEN.removeAll();
687
+ var obj = { x: 0, y: 0 }, t = new TWEEN.Tween(obj).to({ x: '+100', y: '-100' }, 100).repeat(1);
688
+ t.start(0);
689
+ TWEEN.update(0);
690
+ test.equal(obj.x, 0);
691
+ test.equal(obj.y, 0);
692
+ TWEEN.update(50);
693
+ test.equal(obj.x, 50);
694
+ test.equal(obj.y, -50);
695
+ TWEEN.update(100);
696
+ test.equal(obj.x, 100);
697
+ test.equal(obj.y, -100);
698
+ TWEEN.update(150);
699
+ test.equal(obj.x, 150);
700
+ test.equal(obj.y, -150);
701
+ TWEEN.update(200);
702
+ test.equal(obj.x, 200);
703
+ test.equal(obj.y, -200);
704
+ test.done();
705
+ },
706
+ 'Test yoyo with repeat Infinity happens forever': function (test) {
707
+ TWEEN.removeAll();
708
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj).to({ x: 100 }, 100).repeat(Infinity).yoyo(true);
709
+ t.start(0);
710
+ TWEEN.update(0);
711
+ test.equal(obj.x, 0);
712
+ TWEEN.update(25);
713
+ test.equal(obj.x, 25);
714
+ TWEEN.update(100);
715
+ test.equal(obj.x, 100);
716
+ TWEEN.update(125);
717
+ test.equal(obj.x, 75);
718
+ TWEEN.update(200);
719
+ test.equal(obj.x, 0);
720
+ TWEEN.update(225);
721
+ test.equal(obj.x, 25);
722
+ test.done();
723
+ },
724
+ 'Test yoyo with repeat 1 happens once': function (test) {
725
+ TWEEN.removeAll();
726
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj).to({ x: 100 }, 100).repeat(1).yoyo(true);
727
+ t.start(0);
728
+ TWEEN.update(0);
729
+ test.equal(obj.x, 0);
730
+ TWEEN.update(25);
731
+ test.equal(obj.x, 25);
732
+ TWEEN.update(100);
733
+ test.equal(obj.x, 100);
734
+ TWEEN.update(125);
735
+ test.equal(obj.x, 75);
736
+ TWEEN.update(200);
737
+ test.equal(obj.x, 0);
738
+ TWEEN.update(225);
739
+ test.equal(obj.x, 0);
740
+ test.done();
741
+ },
742
+ 'Test yoyo works with arrays': function (test) {
743
+ TWEEN.removeAll();
744
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj)
745
+ .to({ x: [100, 200] }, 100)
746
+ .repeat(1)
747
+ .yoyo(true);
748
+ t.start(0);
749
+ TWEEN.update(50);
750
+ test.equal(obj.x, 100);
751
+ TWEEN.update(100);
752
+ test.equal(obj.x, 200);
753
+ TWEEN.update(150);
754
+ test.equal(obj.x, 100);
755
+ TWEEN.update(200);
756
+ test.equal(obj.x, 0);
757
+ test.done();
758
+ },
759
+ 'Test yoyo can be stopped and restarted properly': function (test) {
760
+ TWEEN.removeAll();
761
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj).to({ x: 100 }, 100).repeat(1).yoyo(true);
762
+ t.start(0);
763
+ TWEEN.update(0);
764
+ test.equal(obj.x, 0);
765
+ TWEEN.update(25);
766
+ test.equal(obj.x, 25);
767
+ TWEEN.update(100);
768
+ test.equal(obj.x, 100);
769
+ TWEEN.update(125);
770
+ test.equal(obj.x, 75);
771
+ t.stop();
772
+ t.start(0);
773
+ TWEEN.update(0);
774
+ test.equal(obj.x, 0);
775
+ TWEEN.update(25);
776
+ test.equal(obj.x, 25);
777
+ TWEEN.update(100);
778
+ test.equal(obj.x, 100);
779
+ TWEEN.update(125);
780
+ test.equal(obj.x, 75);
781
+ TWEEN.update(200);
782
+ test.equal(obj.x, 0);
783
+ TWEEN.update(225);
784
+ test.equal(obj.x, 0);
785
+ test.done();
786
+ },
787
+ 'Test TWEEN.Tween.stopChainedTweens()': function (test) {
788
+ var t = new TWEEN.Tween({}), t2 = new TWEEN.Tween({});
789
+ var tStarted = false, tCompleted = false, t2Started = false;
790
+ TWEEN.removeAll();
791
+ t.to({}, 1000);
792
+ t2.delay(500).to({}, 1000);
793
+ t.chain(t2);
794
+ t2.chain(t);
795
+ t.onStart(function () {
796
+ tStarted = true;
797
+ });
798
+ t.onComplete(function () {
799
+ tCompleted = true;
800
+ });
801
+ t2.onStart(function () {
802
+ test.equal(tStarted, true);
803
+ test.equal(tCompleted, true);
804
+ test.equal(t2Started, false);
805
+ t2Started = true;
806
+ });
807
+ test.equal(tStarted, false);
808
+ test.equal(t2Started, false);
809
+ t.start(0);
810
+ TWEEN.update(1001);
811
+ t.stop();
812
+ test.equal(tStarted, true);
813
+ test.equal(t2Started, false);
814
+ test.equal(TWEEN.getAll().length, 0);
815
+ test.done();
816
+ },
817
+ 'Test TWEEN.Tween.chain progressess into chained tweens': function (test) {
818
+ var obj = { t: 1000 };
819
+ // 1000 of nothing
820
+ var blank = new TWEEN.Tween({}).to({}, 1000);
821
+ // tween obj.t from 1000 -> 2000 (in time with update time)
822
+ var next = new TWEEN.Tween(obj).to({ t: 2000 }, 1000);
823
+ blank.chain(next).start(0);
824
+ TWEEN.update(1500);
825
+ test.equal(obj.t, 1500);
826
+ TWEEN.update(2000);
827
+ test.equal(obj.t, 2000);
828
+ test.done();
829
+ },
830
+ 'Test that TWEEN.Tween.end sets the final values.': function (test) {
831
+ var object1 = { x: 0, y: -50, z: 1000 };
832
+ var target1 = { x: 50, y: 123, z: '+234' };
833
+ var tween1 = new TWEEN.Tween(object1).to(target1, 1000);
834
+ tween1.start();
835
+ tween1.end();
836
+ test.equal(object1.x, 50);
837
+ test.equal(object1.y, 123);
838
+ test.equal(object1.z, 1234);
839
+ var object2 = { x: 0, y: -50, z: 1000 };
840
+ var target2 = { x: 50, y: 123, z: '+234' };
841
+ var tween2 = new TWEEN.Tween(object2).to(target2, 1000);
842
+ tween2.start(300);
843
+ tween2.update(500);
844
+ tween2.end();
845
+ test.equal(object2.x, 50);
846
+ test.equal(object2.y, 123);
847
+ test.equal(object2.z, 1234);
848
+ test.done();
849
+ },
850
+ 'Test that TWEEN.Tween.end calls the onComplete callback of the tween.': function (test) {
851
+ test.expect(1);
852
+ var tween1 = new TWEEN.Tween({}).to({}, 1000).onComplete(function () {
853
+ test.ok(true);
854
+ });
855
+ tween1.start();
856
+ tween1.end();
857
+ test.done();
858
+ },
859
+ 'Ensure Tween.end() works after stopping a tween.': function (test) {
860
+ var object = { x: 0, y: -50, z: 1000 };
861
+ var target = { x: 50, y: 123, z: '+234' };
862
+ var tween = new TWEEN.Tween(object).to(target, 1000);
863
+ tween.start(300);
864
+ tween.update(500);
865
+ tween.stop();
866
+ tween.end();
867
+ test.equal(object.x, 50);
868
+ test.equal(object.y, 123);
869
+ test.equal(object.z, 1234);
870
+ test.done();
871
+ },
872
+ 'Test delay adds delay before each repeat': function (test) {
873
+ // If repeatDelay isn't specified then delay is used since
874
+ // that's the way it worked before repeatDelay was added.
875
+ TWEEN.removeAll();
876
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj).to({ x: 100 }, 100).repeat(1).delay(100);
877
+ t.start(0);
878
+ TWEEN.update(100);
879
+ test.equal(obj.x, 0);
880
+ TWEEN.update(150);
881
+ test.equal(obj.x, 50);
882
+ TWEEN.update(200);
883
+ test.equal(obj.x, 100);
884
+ TWEEN.update(250);
885
+ test.equal(obj.x, 100);
886
+ TWEEN.update(300);
887
+ test.equal(obj.x, 0);
888
+ TWEEN.update(350);
889
+ test.equal(obj.x, 50);
890
+ TWEEN.update(400);
891
+ test.equal(obj.x, 100);
892
+ test.done();
893
+ },
894
+ 'Test repeatDelay adds delay before each repeat': function (test) {
895
+ TWEEN.removeAll();
896
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj).to({ x: 100 }, 100).repeat(1).repeatDelay(200);
897
+ t.start(0);
898
+ TWEEN.update(0);
899
+ test.equal(obj.x, 0);
900
+ TWEEN.update(50);
901
+ test.equal(obj.x, 50);
902
+ TWEEN.update(100);
903
+ test.equal(obj.x, 100);
904
+ TWEEN.update(200);
905
+ test.equal(obj.x, 100);
906
+ TWEEN.update(300);
907
+ test.equal(obj.x, 0);
908
+ TWEEN.update(350);
909
+ test.equal(obj.x, 50);
910
+ TWEEN.update(400);
911
+ test.equal(obj.x, 100);
912
+ test.done();
913
+ },
914
+ 'Test repeatDelay and delay can be used together': function (test) {
915
+ TWEEN.removeAll();
916
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj).to({ x: 100 }, 100).delay(100).repeat(1).repeatDelay(200);
917
+ t.start(0);
918
+ TWEEN.update(100);
919
+ test.equal(obj.x, 0);
920
+ TWEEN.update(150);
921
+ test.equal(obj.x, 50);
922
+ TWEEN.update(200);
923
+ test.equal(obj.x, 100);
924
+ TWEEN.update(300);
925
+ test.equal(obj.x, 100);
926
+ TWEEN.update(400);
927
+ test.equal(obj.x, 0);
928
+ TWEEN.update(450);
929
+ test.equal(obj.x, 50);
930
+ TWEEN.update(500);
931
+ test.equal(obj.x, 100);
932
+ test.done();
933
+ },
934
+ 'Tween.js compatible with Object.defineProperty getter / setters': function (test) {
935
+ var obj = { _x: 0, x: 0 };
936
+ Object.defineProperty(obj, 'x', {
937
+ get: function () {
938
+ return this._x;
939
+ },
940
+ set: function (x) {
941
+ this._x = x;
942
+ },
943
+ });
944
+ test.equal(obj.x, 0);
945
+ var t = new TWEEN.Tween(obj).to({ x: 100 }, 100);
946
+ t.start(0);
947
+ test.equal(obj.x, 0);
948
+ TWEEN.update(37);
949
+ test.equal(obj.x, 37);
950
+ TWEEN.update(100);
951
+ test.equal(obj.x, 100);
952
+ TWEEN.update(115);
953
+ test.equal(obj.x, 100);
954
+ test.done();
955
+ },
956
+ 'tween.isPlaying() is false before the tween starts': function (test) {
957
+ TWEEN.removeAll();
958
+ var t = new TWEEN.Tween({ x: 0 }).to({ x: 1 }, 100);
959
+ test.equal(t.isPlaying(), false);
960
+ test.done();
961
+ },
962
+ 'tween.isPlaying() is true when a tween is started and before it ends': function (test) {
963
+ TWEEN.removeAll();
964
+ var t = new TWEEN.Tween({ x: 0 }).to({ x: 1 }, 100);
965
+ t.start(0);
966
+ test.equal(t.isPlaying(), true);
967
+ test.done();
968
+ },
969
+ 'tween.isPlaying() is false after a tween ends': function (test) {
970
+ TWEEN.removeAll();
971
+ var t = new TWEEN.Tween({ x: 0 }).to({ x: 1 }, 100);
972
+ t.start(0);
973
+ TWEEN.update(150);
974
+ test.equal(t.isPlaying(), false);
975
+ test.done();
976
+ },
977
+ 'A zero-duration tween finishes at its starting time without an error.': function (test) {
978
+ TWEEN.removeAll();
979
+ var object = { x: 0 };
980
+ var t = new TWEEN.Tween(object).to({ x: 1 }, 0);
981
+ t.start(0);
982
+ TWEEN.update(0);
983
+ test.equal(t.isPlaying(), false);
984
+ test.equal(object.x, 1);
985
+ test.done();
986
+ },
987
+ // Custom TWEEN.Group tests
988
+ 'Custom group.getAll()': function (test) {
989
+ var group = new TWEEN.Group();
990
+ test.ok(group.getAll() instanceof Array);
991
+ test.done();
992
+ },
993
+ 'Custom group stores tweens instead of global TWEEN group': function (test) {
994
+ var group = new TWEEN.Group();
995
+ var numGlobalTweensBefore = TWEEN.getAll().length;
996
+ var numGroupTweensBefore = group.getAll().length;
997
+ var globalTween = new TWEEN.Tween({});
998
+ var groupTweenA = new TWEEN.Tween({}, group);
999
+ var groupTweenB = new TWEEN.Tween({}, group);
1000
+ globalTween.start();
1001
+ groupTweenA.start();
1002
+ groupTweenB.start();
1003
+ test.equal(TWEEN.getAll().length, numGlobalTweensBefore + 1);
1004
+ test.equal(group.getAll().length, numGroupTweensBefore + 2);
1005
+ test.done();
1006
+ },
1007
+ "Custom group.removeAll() doesn't conflict with global TWEEN group": function (test) {
1008
+ var group = new TWEEN.Group();
1009
+ TWEEN.removeAll();
1010
+ group.removeAll();
1011
+ test.equal(TWEEN.getAll().length, 0, 'No global tweens left');
1012
+ test.equal(group.getAll().length, 0, 'No group tweens left');
1013
+ var globalTween = new TWEEN.Tween({});
1014
+ var groupTweenA = new TWEEN.Tween({}, group);
1015
+ var groupTweenB = new TWEEN.Tween({}, group);
1016
+ globalTween.start();
1017
+ groupTweenA.start();
1018
+ groupTweenB.start();
1019
+ test.equal(TWEEN.getAll().length, 1, 'One global tween has been added');
1020
+ test.equal(group.getAll().length, 2, 'Two group tweens have been added');
1021
+ group.removeAll();
1022
+ test.equal(TWEEN.getAll().length, 1, 'One global tween left');
1023
+ test.equal(group.getAll().length, 0, 'No group tweens left');
1024
+ TWEEN.removeAll();
1025
+ test.equal(TWEEN.getAll().length, 0, 'No global tweens left');
1026
+ test.done();
1027
+ },
1028
+ "Global TWEEN.removeAll() doesn't conflict with custom group": function (test) {
1029
+ var group = new TWEEN.Group();
1030
+ TWEEN.removeAll();
1031
+ group.removeAll();
1032
+ test.equal(TWEEN.getAll().length, 0, 'No global tweens left');
1033
+ test.equal(group.getAll().length, 0, 'No group tweens left');
1034
+ var globalTween = new TWEEN.Tween({});
1035
+ var groupTweenA = new TWEEN.Tween({}, group);
1036
+ var groupTweenB = new TWEEN.Tween({}, group);
1037
+ globalTween.start();
1038
+ groupTweenA.start();
1039
+ groupTweenB.start();
1040
+ test.equal(TWEEN.getAll().length, 1, 'One global tween has been added');
1041
+ test.equal(group.getAll().length, 2, 'Two group tweens have been added');
1042
+ TWEEN.removeAll();
1043
+ test.equal(TWEEN.getAll().length, 0, 'No global tweens left');
1044
+ test.equal(group.getAll().length, 2, 'Two group tweens left');
1045
+ group.removeAll();
1046
+ test.equal(group.getAll().length, 0, 'No group tweens left');
1047
+ test.done();
1048
+ },
1049
+ "Custom group.add() doesn't conflict with global TWEEN group, or vice versa": function (test) {
1050
+ var group = new TWEEN.Group();
1051
+ var globalTween = new TWEEN.Tween({});
1052
+ var groupTweenA = new TWEEN.Tween({}, group);
1053
+ var groupTweenB = new TWEEN.Tween({}, group);
1054
+ var numGlobalTweens = TWEEN.getAll().length;
1055
+ var numGroupTweens = group.getAll().length;
1056
+ TWEEN.add(globalTween);
1057
+ group.add(groupTweenA);
1058
+ group.add(groupTweenB);
1059
+ test.equal(numGlobalTweens + 1, TWEEN.getAll().length);
1060
+ test.equal(numGroupTweens + 2, group.getAll().length);
1061
+ test.done();
1062
+ },
1063
+ "Custom group.update() doesn't conflict with global TWEEN group": function (test) {
1064
+ var group = new TWEEN.Group();
1065
+ var startObj = { x: 1 };
1066
+ var endObj = { x: 2 };
1067
+ var duration = 1000;
1068
+ var globalObj = { x: 1 };
1069
+ new TWEEN.Tween(globalObj).to(endObj, duration).start(0);
1070
+ var groupObj = { x: 1 };
1071
+ new TWEEN.Tween(groupObj, group).to(endObj, duration).start(0);
1072
+ group.update(duration);
1073
+ test.deepEqual(globalObj, startObj);
1074
+ test.deepEqual(groupObj, endObj);
1075
+ test.done();
1076
+ },
1077
+ "Global TWEEN.update() doesn't conflict with custom group": function (test) {
1078
+ var group = new TWEEN.Group();
1079
+ var startObj = { x: 1 };
1080
+ var endObj = { x: 2 };
1081
+ var duration = 1000;
1082
+ var globalObj = { x: 1 };
1083
+ new TWEEN.Tween(globalObj).to(endObj, duration).start(0);
1084
+ var groupObj = { x: 1 };
1085
+ new TWEEN.Tween(groupObj, group).to(endObj, duration).start(0);
1086
+ TWEEN.update(duration);
1087
+ test.deepEqual(globalObj, endObj);
1088
+ test.deepEqual(groupObj, startObj);
1089
+ test.done();
1090
+ },
1091
+ 'Ensure tweens work without any group': function (test) {
1092
+ var obj = { x: 0 }, t = new TWEEN.Tween(obj, false);
1093
+ t.to({ x: 1000 }, 1000);
1094
+ t.start(0);
1095
+ test.equal(obj.x, 0);
1096
+ t.update(500);
1097
+ test.equal(obj.x, 500);
1098
+ t.pause(600);
1099
+ test.equal(obj.x, 500);
1100
+ t.update(750);
1101
+ test.equal(obj.x, 500);
1102
+ t.resume(800);
1103
+ test.equal(obj.x, 500);
1104
+ t.update(1000);
1105
+ test.equal(obj.x, 800);
1106
+ t.update(1001);
1107
+ test.equal(obj.x, 801);
1108
+ t.stop().end();
1109
+ test.equal(obj.x, 1000);
1110
+ test.done();
1111
+ },
1112
+ 'Stopping a tween within an update callback will not cause an error.': function (test) {
1113
+ TWEEN.removeAll();
1114
+ var tweenA = new TWEEN.Tween({ x: 1, y: 2 })
1115
+ .to({ x: 3, y: 4 }, 1000)
1116
+ .onUpdate(function () {
1117
+ tweenB.stop();
1118
+ })
1119
+ .start(0);
1120
+ var tweenB = new TWEEN.Tween({ x: 5, y: 6 })
1121
+ .to({ x: 7, y: 8 })
1122
+ .onUpdate(function () {
1123
+ tweenA.stop();
1124
+ })
1125
+ .start(0);
1126
+ var success = true;
1127
+ try {
1128
+ TWEEN.update(500);
1129
+ }
1130
+ catch (exception) {
1131
+ success = false;
1132
+ }
1133
+ finally {
1134
+ test.ok(success);
1135
+ test.done();
1136
+ }
1137
+ },
1138
+ 'Set the duration with .duration': function (test) {
1139
+ var obj = { x: 1 };
1140
+ var t = new TWEEN.Tween(obj).to({ x: 2 }).duration(1000).start(0);
1141
+ t.update(1000);
1142
+ test.deepEqual(obj.x, 2);
1143
+ test.done();
1144
+ },
1145
+ "Tween.group sets the tween's group.": function (test) {
1146
+ var group = new TWEEN.Group();
1147
+ var groupTweenA = new TWEEN.Tween({}).group(group);
1148
+ groupTweenA.start();
1149
+ test.equal(group.getAll().length, 1);
1150
+ test.done();
1151
+ },
1152
+ 'Test TWEEN.Tween.pause() and TWEEN.Tween.resume()': function (test) {
1153
+ var obj = { x: 0.0 }, t = new TWEEN.Tween(obj);
1154
+ t.to({ x: 1.0 }, 1000);
1155
+ TWEEN.removeAll();
1156
+ test.equal(TWEEN.getAll().length, 0);
1157
+ t.start(0);
1158
+ test.equal(TWEEN.getAll().length, 1);
1159
+ test.equal(t.isPaused(), false);
1160
+ TWEEN.update(400);
1161
+ test.equal(obj.x, 0.4);
1162
+ t.pause(450);
1163
+ test.equal(t.isPaused(), true);
1164
+ test.equal(TWEEN.getAll().length, 0);
1165
+ test.equal(obj.x, 0.4);
1166
+ TWEEN.update(900);
1167
+ test.equal(obj.x, 0.4);
1168
+ TWEEN.update(3000);
1169
+ test.equal(obj.x, 0.4);
1170
+ t.resume(3200);
1171
+ // values do not change until an update
1172
+ test.equal(obj.x, 0.4);
1173
+ test.equal(TWEEN.getAll().length, 1);
1174
+ test.equal(t.isPaused(), false);
1175
+ TWEEN.update(3500);
1176
+ test.equal(obj.x, 0.75);
1177
+ TWEEN.update(5000);
1178
+ test.equal(obj.x, 1.0);
1179
+ test.done();
1180
+ },
1181
+ 'Test TWEEN.Tween.pause() and TWEEN.Tween.resume(), without groups': function (test) {
1182
+ var obj = { x: 0.0 }, t = new TWEEN.Tween(obj, false);
1183
+ t.to({ x: 1.0 }, 1000);
1184
+ t.start(0);
1185
+ test.equal(t.isPaused(), false);
1186
+ t.update(400);
1187
+ test.equal(obj.x, 0.4);
1188
+ t.pause(450);
1189
+ test.equal(t.isPaused(), true);
1190
+ test.equal(obj.x, 0.4);
1191
+ t.update(900);
1192
+ test.equal(obj.x, 0.4);
1193
+ t.update(3000);
1194
+ test.equal(obj.x, 0.4);
1195
+ t.resume(3200);
1196
+ // values do not change until an update
1197
+ test.equal(obj.x, 0.4);
1198
+ test.equal(t.isPaused(), false);
1199
+ t.update(3500);
1200
+ test.equal(obj.x, 0.75);
1201
+ t.update(5000);
1202
+ test.equal(obj.x, 1.0);
1203
+ test.done();
1204
+ },
1205
+ 'Arrays in the object passed to to() are not modified by start().': function (test) {
1206
+ var start = { x: 10, y: 20 };
1207
+ var end = { x: 100, y: 200, values: ['a', 'b'] };
1208
+ var valuesArray = end.values;
1209
+ new TWEEN.Tween(start).to(end).start();
1210
+ test.equal(valuesArray, end.values);
1211
+ test.equal(end.values.length, 2);
1212
+ test.equal(end.values[0], 'a');
1213
+ test.equal(end.values[1], 'b');
1214
+ test.done();
1215
+ },
1216
+ 'Tween.js animate nested object': function (test) {
1217
+ var obj = { scale: { x: 0 }, alpha: 0 };
1218
+ var t = new TWEEN.Tween(obj).to({ scale: { x: 100 }, alpha: 100 }, 100);
1219
+ t.start(0);
1220
+ test.equal(obj.scale.x, 0);
1221
+ TWEEN.update(37);
1222
+ test.equal(obj.scale.x, 37);
1223
+ test.equal(obj.alpha, 37);
1224
+ TWEEN.update(100);
1225
+ test.equal(obj.scale.x, 100);
1226
+ test.equal(obj.alpha, 100);
1227
+ TWEEN.update(115);
1228
+ test.equal(obj.scale.x, 100);
1229
+ test.equal(obj.alpha, 100);
1230
+ test.done();
1231
+ },
1232
+ 'Tween.js animate nested object including relative value': function (test) {
1233
+ var obj = { world: { hero: { scale: { x: 0 }, x: 100 } }, time: 0 };
1234
+ var t = new TWEEN.Tween(obj).to({ world: { hero: { scale: { x: 100 }, x: '+100' } }, time: 100 }, 100);
1235
+ t.start(0);
1236
+ test.equal(obj.world.hero.scale.x, 0);
1237
+ TWEEN.update(37);
1238
+ test.equal(obj.world.hero.scale.x, 37);
1239
+ test.equal(obj.world.hero.x, 137);
1240
+ test.equal(obj.time, 37);
1241
+ TWEEN.update(100);
1242
+ test.equal(obj.world.hero.scale.x, 100);
1243
+ test.equal(obj.world.hero.x, 200);
1244
+ test.equal(obj.time, 100);
1245
+ TWEEN.update(115);
1246
+ test.equal(obj.world.hero.scale.x, 100);
1247
+ test.equal(obj.world.hero.x, 200);
1248
+ test.equal(obj.time, 100);
1249
+ test.done();
1250
+ },
1251
+ 'Test TWEEN.Tween with nested objects': function (test) {
1252
+ var obj = { x: 0.0, y: 100, some: { value: 0.0, style: { opacity: 1.0 } } }, t = new TWEEN.Tween(obj);
1253
+ t.to({ x: 1.0, y: 200, some: { value: 1.0, style: { opacity: 0.5 } } }, 1000);
1254
+ TWEEN.removeAll();
1255
+ test.equal(TWEEN.getAll().length, 0);
1256
+ t.start(0);
1257
+ test.equal(TWEEN.getAll().length, 1);
1258
+ test.equal(t.isPaused(), false);
1259
+ TWEEN.update(400);
1260
+ test.equal(obj.x, 0.4);
1261
+ test.equal(obj.y, 140);
1262
+ test.equal(obj.some.style.opacity, 0.8);
1263
+ test.equal(obj.some.value, 0.4);
1264
+ TWEEN.update(750);
1265
+ test.equal(obj.x, 0.75);
1266
+ test.equal(obj.y, 175);
1267
+ test.equal(obj.some.style.opacity, 0.625);
1268
+ test.equal(obj.some.value, 0.75);
1269
+ TWEEN.update(1000);
1270
+ test.equal(obj.x, 1.0);
1271
+ test.equal(obj.y, 200);
1272
+ test.equal(obj.some.style.opacity, 0.5);
1273
+ test.equal(obj.some.value, 1.0);
1274
+ test.done();
1275
+ },
1276
+ 'Test TWEEN.Tween.pause() and .resume() with nested objects': function (test) {
1277
+ var obj = { x: 0.0, y: 100, some: { value: 0.0 } }, t = new TWEEN.Tween(obj);
1278
+ t.to({ x: 1.0, y: 200, some: { value: 1.0 } }, 1000);
1279
+ TWEEN.removeAll();
1280
+ test.equal(TWEEN.getAll().length, 0);
1281
+ t.start(0);
1282
+ test.equal(TWEEN.getAll().length, 1);
1283
+ test.equal(t.isPaused(), false);
1284
+ TWEEN.update(400);
1285
+ test.equal(obj.x, 0.4);
1286
+ test.equal(obj.y, 140);
1287
+ test.equal(obj.some.value, 0.4);
1288
+ t.pause(450);
1289
+ test.equal(t.isPaused(), true);
1290
+ test.equal(TWEEN.getAll().length, 0);
1291
+ test.equal(obj.x, 0.4);
1292
+ test.equal(obj.y, 140);
1293
+ test.equal(obj.some.value, 0.4);
1294
+ TWEEN.update(900);
1295
+ test.equal(obj.x, 0.4);
1296
+ test.equal(obj.y, 140);
1297
+ test.equal(obj.some.value, 0.4);
1298
+ TWEEN.update(3000);
1299
+ test.equal(obj.x, 0.4);
1300
+ test.equal(obj.y, 140);
1301
+ test.equal(obj.some.value, 0.4);
1302
+ t.resume(3200);
1303
+ // values do not change until an update
1304
+ test.equal(obj.x, 0.4);
1305
+ test.equal(obj.y, 140);
1306
+ test.equal(obj.some.value, 0.4);
1307
+ test.equal(TWEEN.getAll().length, 1);
1308
+ test.equal(t.isPaused(), false);
1309
+ TWEEN.update(3500);
1310
+ test.equal(obj.x, 0.75);
1311
+ test.equal(obj.y, 175);
1312
+ test.equal(obj.some.value, 0.75);
1313
+ TWEEN.update(5000);
1314
+ test.equal(obj.x, 1.0);
1315
+ test.equal(obj.y, 200);
1316
+ test.equal(obj.some.value, 1.0);
1317
+ test.done();
1318
+ },
1319
+ };
1320
+ //# sourceMappingURL=tests.js.map