summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/web-animations/animation-model/keyframe-effects/spacing-keyframes.html
blob: 90e26d276d8ca80a909fe0ef6aa1a2bfaa5a70e2 (plain)
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
<!DOCTYPE html>
<meta charset=utf-8>
<title>Keyframe spacing tests</title>
<link rel="help" href="https://w3c.github.io/web-animations/#spacing-keyframes">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../testcommon.js"></script>
<body>
<div id="log"></div>
<script>
"use strict";

test(function(t) {
  var anim = createDiv(t).animate([ { marginLeft: '0px' },
                                    { marginLeft: '-20px' },
                                    { marginLeft: '100px' },
                                    { marginLeft: '50px' } ],
                                  { duration: 100 * MS_PER_SEC });

  var frames = anim.effect.getKeyframes();
  var slots = frames.length - 1;
  assert_equals(frames[0].computedOffset, 0.0, '1st frame offset');
  assert_equals(frames[1].computedOffset, 1.0 / slots, '2nd frame offset');
  assert_equals(frames[2].computedOffset, 2.0 / slots, '3rd frame offset');
  assert_equals(frames[3].computedOffset, 1.0, 'last frame offset');
}, 'Test distribute spacing');

test(function(t) {
  var anim = createDiv(t).animate([ { marginLeft: '0px' },
                                    { marginLeft: '-20px' },
                                    { marginLeft: '100px', offset: 0.5 },
                                    { marginLeft: '50px' } ],
                                  { duration: 100 * MS_PER_SEC,
                                    spacing: 'distribute' });

  var frames = anim.effect.getKeyframes();
  assert_equals(frames[0].computedOffset, 0.0, '1st frame offset');
  assert_equals(frames[1].computedOffset, 0.5 * 1 / 2, '2nd frame offset');
  assert_equals(frames[2].computedOffset, 0.5, '3rd frame offset');
  assert_equals(frames[3].computedOffset, 1.0, 'last frame offset');
}, 'Test distribute spacing with specific offsets');

test(function(t) {
  var anim = createDiv(t).animate(null,
                                  { duration: 100 * MS_PER_SEC,
                                    spacing: 'paced(margin-left)' });

  var frames = anim.effect.getKeyframes();
  assert_equals(frames.length, 0, "empty keyframe list");
}, 'Test paced spacing without any keyframe');


test(function(t) {
  var anim = createDiv(t).animate([ { marginLeft: '0px' },
                                    { marginLeft: '-20px' },
                                    { marginLeft: '100px' },
                                    { marginLeft: '50px' } ],
                                  { duration: 100 * MS_PER_SEC,
                                    spacing: 'paced(margin-left)' });

  var frames = anim.effect.getKeyframes();
  var cumDist = [0, 20, 140, 190];
  assert_equals(frames[0].computedOffset, 0.0,
                '1st frame offset');
  assert_equals(frames[1].computedOffset, cumDist[1] / cumDist[3],
                '2nd frame offset');
  assert_equals(frames[2].computedOffset, cumDist[2] / cumDist[3],
                '3rd frame offset');
  assert_equals(frames[3].computedOffset, 1.0,
                'last frame offset');
}, 'Test paced spacing');

test(function(t) {
  var anim = createDiv(t).animate([ { marginLeft: '0px' },
                                    { marginLeft: '-20px' },
                                    { marginLeft: '100px', offset: 0.5 },
                                    { marginLeft: '120px' },
                                    { marginLeft: '50px' } ],
                                  { duration: 100 * MS_PER_SEC,
                                    spacing: 'paced(margin-left)' });

  var frames = anim.effect.getKeyframes();
  var cumDist1 = [ 0, 20, 140 ];
  var cumDist2 = [ 0, 20, 90 ];
  assert_equals(frames[1].computedOffset, 0.5 * cumDist1[1] / cumDist1[2],
                '2nd frame offset');
  assert_equals(frames[2].computedOffset, 0.5,
                '3rd frame offset');
  assert_equals(frames[3].computedOffset, 0.5 + 0.5 * cumDist2[1] / cumDist2[2],
                '4th frame offset');
}, 'Test paced spacing with specific offsets');

test(function(t) {
  var anim = createDiv(t).animate([ { marginLeft: '0px' },
                                    { marginLeft: '0px' },
                                    { marginLeft: '100px' },
                                    { marginLeft: '50px' } ],
                                  { duration: 100 * MS_PER_SEC,
                                    spacing: 'paced(margin-left)' });

  var frames = anim.effect.getKeyframes();
  var cumDist = [0, 0, 100, 150];
  assert_equals(frames[1].computedOffset, cumDist[1] / cumDist[3],
                '2nd frame offset');
  assert_equals(frames[2].computedOffset, cumDist[2] / cumDist[3],
                '3rd frame offset');
}, 'Test paced spacing if some paced property values are equal');

test(function(t) {
  var anim = createDiv(t).animate([ { marginLeft: '0px' },
                                    { marginLeft: '0px' },
                                    { marginLeft: '0px' },
                                    { marginLeft: '0px' } ],
                                  { duration: 100 * MS_PER_SEC,
                                    spacing: 'paced(margin-left)' });

  var frames = anim.effect.getKeyframes();
  var slots = frames.length - 1;
  assert_equals(frames[1].computedOffset, 1.0 / slots, '2nd frame offset');
  assert_equals(frames[2].computedOffset, 2.0 / slots, '3rd frame offset');
}, 'Test falling back to distribute spacing if all paced property value ' +
   'are equal');

test(function(t) {
  var anim = createDiv(t).animate([ { margin: '0px' },
                                    { marginTop: '-20px' },
                                    { marginLeft: '100px' },
                                    { margin: '50px' } ],
                                  { duration: 100 * MS_PER_SEC,
                                    spacing: 'paced(margin-left)' });

  var frames = anim.effect.getKeyframes();
  assert_equals(frames[1].computedOffset, frames[2].computedOffset * 1 / 2,
                '2nd frame offset using distribute spacing');
  assert_equals(frames[2].computedOffset, 100 / 150,
                '3rd frame offset using paced spacing');
}, 'Test paced spacing if there a keyframe without the paced property');

test(function(t) {
  var anim = createDiv(t).animate([ { margin: '0px' },
                                    { marginTop: '40px' },
                                    { marginTop: '-20px' },
                                    { marginLeft: '40px' },
                                    { marginTop: '60px' },
                                    { margin: '10px' } ],
                                  { duration: 100 * MS_PER_SEC,
                                    spacing: 'paced(margin-left)' });

  var frames = anim.effect.getKeyframes();
  var cumDist = [0, 0, 0, 40, 40, 70];
  assert_equals(frames[1].computedOffset, frames[3].computedOffset * 1 / 3,
                '2nd frame offset using distribute spacing');
  assert_equals(frames[2].computedOffset, frames[3].computedOffset * 2 / 3,
                '3rd frame offset using distribute spacing');
  assert_equals(frames[3].computedOffset, cumDist[3] / cumDist[5],
                '4th frame offset using paced spacing');
  assert_equals(frames[4].computedOffset,
                frames[3].computedOffset +
                  (1 - frames[3].computedOffset) * 1 / 2,
                '5th frame offset using distribute spacing');
}, 'Test paced spacing if a paced property that appears on only some ' +
   'keyframes');

test(function(t) {
  var anim = createDiv(t).animate([ { margin: '0px' },
                                    { marginTop: '-20px', offset: 0.5 },
                                    { marginLeft: '40px' },
                                    { marginLeft: '100px' },
                                    { margin: '50px' } ],
                                  { duration: 100 * MS_PER_SEC,
                                    spacing: 'paced(margin-left)' });

  var frames = anim.effect.getKeyframes();
  assert_equals(frames[2].computedOffset, 0.5 + 0.5 * 1 / 3,
                '3rd frame offset using distribute spacing because it is the ' +
                'first paceable keyframe');
  assert_equals(frames[3].computedOffset,
                frames[2].computedOffset +
                (1.0 - frames[2].computedOffset) * 60 / 110,
                '4th frame offset using paced spacing');
}, 'Test paced spacing if a paced property that appears on only some ' +
   'keyframes and there is a specific offset');

test(function(t) {
  var anim = createDiv(t).animate([ { margin: '0px' },
                                    { marginTop: '20px', offset: 0.2 },
                                    { marginTop: '40px' },
                                    { marginTop: '-20px' },
                                    { marginLeft: '-20px' },
                                    { marginLeft: '40px' },
                                    { marginTop: '60px' },
                                    { marginLeft: '100px' },
                                    { marginTop: '50px' },
                                    { marginTop: '100px', offset: 0.8 },
                                    { margin: '0px' } ],
                                  { duration: 100 * MS_PER_SEC,
                                    spacing: 'paced(margin-left)' });
  var frames = anim.effect.getKeyframes();
  // Test distribute spacing in (A, Paced A] and [Paced B, frame B).
  var slots = frames.length - 3;
  var start = 0.2;
  var diff = 0.8 - start;
  assert_equals(frames[2].computedOffset, start + diff * 1.0 / slots,
                '3nd frame offset using distribute spacing');
  assert_equals(frames[3].computedOffset, start + diff * 2.0 / slots,
                '4rd frame offset using distribute spacing');
  assert_equals(frames[4].computedOffset, start + diff * 3.0 / slots,
                '5th frame offset using distribute spacing because it is ' +
                'the first paceable keyframe');
  assert_equals(frames[7].computedOffset, start + diff * 6.0 / slots,
                '8th frame offset using distribute spacing because it is ' +
                'the last paceable keyframe');
  assert_equals(frames[8].computedOffset, start + diff * 7.0 / slots,
                '9th frame offset using distribute spacing');
  // Test paced spacing and other null computed offsets in (Paced A, Paced B).
  var cumDist = [0, 60, 60, 120];
  assert_equals(frames[5].computedOffset,
                frames[4].computedOffset + cumDist[2] / cumDist[3] *
                  (frames[7].computedOffset - frames[4].computedOffset),
                '6th frame offset using paced spacing');
  assert_equals(frames[6].computedOffset,
                frames[5].computedOffset + 1.0 / 2.0 *
                  (frames[7].computedOffset - frames[5].computedOffset),
                '7th frame offset using distribute spacing');
}, 'Test paced spacing where there are some keyframes without offsets and ' +
   'without the paced property before the first paceable keyframe and ' +
   'after the last paceable keyframe');

test(function(t) {
  var anim = createDiv(t).animate([ { margin: '0px' },
                                    { margin: '-20px' },
                                    { margin: '100px' },
                                    { margin: '50px' } ],
                                  { duration: 100 * MS_PER_SEC,
                                    spacing: 'paced(margin)' });

  var frames = anim.effect.getKeyframes();
  var cumDist = [0, 20, 140, 190];
  assert_equals(frames[1].computedOffset, cumDist[1] / cumDist[3],
                '2nd frame offset');
  assert_equals(frames[2].computedOffset, cumDist[2] / cumDist[3],
                '3rd frame offset');
}, 'Test paced spacing for using shorthand property');

test(function(t) {
  var anim =
    createDiv(t).animate([ { marginLeft: '0px',   marginRight:  '0px',
                             marginTop:  '10px',  marginBottom: '10px' },
                           { marginLeft: '-20px', marginRight:  '-20px',
                             marginTop:  '0px',   marginBottom: '0px' },
                           { marginLeft: '100px', marginRight:  '100px',
                             marginTop:  '-50px', marginBottom: '-50px' },
                           { marginLeft: '50px',  marginRight:  '50px',
                             marginTop:  '80px',  marginBottom: '80px' } ],
                         { duration: 100 * MS_PER_SEC,
                           spacing: 'paced(margin)' });

  var frames = anim.effect.getKeyframes();
  var dist = [ 0,
               Math.sqrt(20 * 20 * 2 + 10 * 10 * 2),
               Math.sqrt(120 * 120 * 2 + 50 * 50 * 2),
               Math.sqrt(50 * 50 * 2 + 130 * 130 * 2) ];
  var cumDist = [];
  dist.reduce(function(prev, curr, i) { return cumDist[i] = prev + curr; }, 0);
  assert_approx_equals(frames[1].computedOffset, cumDist[1] / cumDist[3],
                       0.0001, '2nd frame offset');
  assert_approx_equals(frames[2].computedOffset, cumDist[2] / cumDist[3],
                       0.0001, '3rd frame offset');
}, 'Test paced spacing using shorthand property where only the longhand ' +
   'components are specified');

test(function(t) {
  var anim = createDiv(t).animate([ { marginLeft: '0px', marginTop: '0px' },
                                    { marginLeft: '-20px', marginTop: '-20px' },
                                    { marginLeft: '100px', marginTop: '100px' },
                                    { marginLeft: '50px', marginTop: '50px' } ],
                                  { duration: 100 * MS_PER_SEC,
                                    spacing: 'paced(margin)' });

  var frames = anim.effect.getKeyframes();
  var slots = frames.length - 1;
  assert_equals(frames[1].computedOffset, 1 / slots, '2nd frame offset');
  assert_equals(frames[2].computedOffset, 2 / slots, '3rd frame offset');
}, 'Test falling back to distribute spacing if all keyframe miss some ' +
   'components');

test(function(t) {
  var anim = createDiv(t).animate([ { margin: '0px' },
                                    { marginLeft: '-20px' },
                                    { marginTop: '40px' },
                                    { margin: '100px' },
                                    { margin: '50px' } ],
                                  { duration: 100 * MS_PER_SEC,
                                    spacing: 'paced(margin)' });

  var frames = anim.effect.getKeyframes();
  assert_equals(frames[1].computedOffset, frames[3].computedOffset * 1 / 3,
                '2nd frame offset using distribute spacing');
  assert_equals(frames[2].computedOffset, frames[3].computedOffset * 2 / 3,
                '3rd frame offset using distribute spacing');
  assert_equals(frames[3].computedOffset, 100 / 150,
                '4th frame offset using paced spacing');
}, 'Test paced spacing only for keyframes specifying all longhand ' +
   'components, and falling back to distribute spacing for the reset');

test(function(t) {
  var anim = createDiv(t).animate([ { margin: '0px' },
                                    { marginLeft: '-20px' },
                                    { marginTop: '40px', offset: 0.5 },
                                    { margin: '100px' },
                                    { margin: '50px' } ],
                                  { duration: 100 * MS_PER_SEC,
                                    spacing: 'paced(margin)' });

  var frames = anim.effect.getKeyframes();
  assert_equals(frames[1].computedOffset, 0.5 * 1 / 2,
                '2nd frame offset using distribute spacing');
  assert_equals(frames[3].computedOffset, 0.5 + 0.5 * 1 / 2,
                '4th frame offset using distribute spacing because it is the ' +
                'first paceable keyframe from a non-null offset keyframe');
}, 'Test paced spacing only for keyframes specifying all some components, ' +
   'and falling back to distribute spacing for the reset with some specific ' +
   'offsets');

// Tests for setting spacing by KeyframeEffect.spacing.

test(function(t) {
  var anim = createDiv(t).animate([ { marginLeft: '0px' },
                                    { marginLeft: '-20px' },
                                    { marginLeft: '100px' },
                                    { marginLeft: '50px' } ],
                                  { duration: 100 * MS_PER_SEC });

  anim.effect.spacing = 'paced(margin-left)';

  var frames = anim.effect.getKeyframes();
  var cumDist = [0, 20, 140, 190];
  assert_equals(frames[0].computedOffset, 0.0,
                '1st frame offset');
  assert_equals(frames[1].computedOffset, cumDist[1] / cumDist[3],
                '2nd frame offset');
  assert_equals(frames[2].computedOffset, cumDist[2] / cumDist[3],
                '3rd frame offset');
  assert_equals(frames[3].computedOffset, 1.0,
                'last frame offset');
}, 'Test paced spacing by setter');

test(function(t) {
  var anim = createDiv(t).animate([ { marginLeft: '0px' },
                                    { marginLeft: '-20px' },
                                    { marginLeft: '100px' },
                                    { marginLeft: '50px' } ],
                                  { duration: 100 * MS_PER_SEC,
                                    spacing: 'paced(margin-left)' });

  anim.effect.spacing = 'distribute';

  var frames = anim.effect.getKeyframes();
  var slots = frames.length - 1;
  assert_equals(frames[0].computedOffset, 0.0, '1st frame offset');
  assert_equals(frames[1].computedOffset, 1.0 / slots, '2nd frame offset');
  assert_equals(frames[2].computedOffset, 2.0 / slots, '3rd frame offset');
  assert_equals(frames[3].computedOffset, 1.0, 'last frame offset');
}, 'Test distribute spacing by setter');

test(function(t) {
  var anim =
    createDiv(t).animate([ { marginLeft: '0px', borderRadius: '0%' },
                           { marginLeft: '-20px', borderRadius: '50%' },
                           { marginLeft: '100px', borderRadius: '25%' },
                           { marginLeft: '50px', borderRadius: '100%' } ],
                         { duration: 100 * MS_PER_SEC,
                           spacing: 'paced(margin-left)' });

  anim.effect.spacing = 'paced(border-radius)';

  var frames = anim.effect.getKeyframes();
  var cumDist = [0, 50, 50 + 25, 50 + 25 + 75];

  assert_equals(frames[0].computedOffset, 0.0,
                '1st frame offset');
  assert_equals(frames[1].computedOffset, cumDist[1] / cumDist[3],
                '2nd frame offset');
  assert_equals(frames[2].computedOffset, cumDist[2] / cumDist[3],
                '3rd frame offset');
  assert_equals(frames[3].computedOffset, 1.0,
                'last frame offset');
}, 'Test paced spacing by changing the paced property');

</script>
</body>