summaryrefslogtreecommitdiffstats
path: root/dom/animation/test/mozilla/file_spacing_transform.html
blob: 0de7737864f51b60abaa6ba5b8917c7bf24a07dd (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
<!doctype html>
<meta charset=utf-8>
<script src="../testcommon.js"></script>
<body>
<script>
'use strict';

const pi  = Math.PI;
const cos = Math.cos;
const sin = Math.sin;
const tan = Math.tan;
const sqrt = Math.sqrt;

// Help function for testing the computed offsets by the distance array.
function assert_animation_offsets(anim, dist) {
  const epsilon = 0.00000001;
  const frames = anim.effect.getKeyframes();
  const cumDist = dist.reduce( (prev, curr) => {
    prev.push(prev.length == 0 ? curr : curr + prev[prev.length - 1]);
    return prev;
  }, []);

  const total = cumDist[cumDist.length - 1];
  for (var i = 0; i < frames.length; ++i) {
    assert_approx_equals(frames[i].computedOffset, cumDist[i] / total,
                         epsilon, 'computedOffset of frame ' + i);
  }
}

function getAngleDist(rotate1, rotate2) {
  function quaternion(axis, angle) {
    var x = axis[0] * sin(angle/2.0);
    var y = axis[1] * sin(angle/2.0);
    var z = axis[2] * sin(angle/2.0);
    var w = cos(angle/2.0);
    return { 'x': x, 'y': y, 'z': z, 'w': w };
  }
  var q1 = quaternion(rotate1.axis, rotate1.angle);
  var q2 = quaternion(rotate2.axis, rotate2.angle);
  var dotProduct = q1.x * q2.x + q1.y * q2.y + q1.z * q2.z + q1.w * q2.w;
  return 2.0 * Math.acos(dotProduct);
}

function createMatrix(elements, Is3D) {
  return (Is3D ? "matrix3d" : "matrix") + "(" + elements.join() + ")";
}

test(function(t) {
  var anim = addDiv(t).animate([ { transform: "none" },
                                 { transform: "translate(-20px)" },
                                 { transform: "translate(100px)" },
                                 { transform: "translate(50px)"} ],
                               { spacing: "paced(transform)" });
  assert_animation_offsets(anim, [ 0, 20, 120, 50 ]);
}, 'Test spacing on translate' );

test(function(t) {
  var anim =
    addDiv(t).animate([ { transform: "none" },
                        { transform: "translate3d(-20px, 10px, 100px)" },
                        { transform: "translate3d(100px, 200px, 50px)" },
                        { transform: "translate(50px, -10px)"} ],
                      { spacing: "paced(transform)" });
  var dist = [ 0,
               Math.sqrt(20 * 20 + 10 * 10 + 100 * 100),
               Math.sqrt(120 * 120 + 190 * 190 + 50 * 50),
               Math.sqrt(50 * 50 + 210 * 210 + 50 * 50) ];
  assert_animation_offsets(anim, dist);
}, 'Test spacing on translate3d' );

test(function(t) {
  var anim = addDiv(t).animate([ { transform: "scale(0.5)" },
                                 { transform: "scale(4.5)" },
                                 { transform: "scale(2.5)" },
                                 { transform: "none"} ],
                               { spacing: "paced(transform)" });
  assert_animation_offsets(anim, [ 0, 4.0, 2.0, 1.5 ]);
}, 'Test spacing on scale' );

test(function(t) {
  var anim = addDiv(t).animate([ { transform: "scale(0.5, 0.5)" },
                                 { transform: "scale3d(4.5, 5.0, 2.5)" },
                                 { transform: "scale3d(2.5, 1.0, 2.0)" },
                                 { transform: "scale3d(1, 0.5, 1.0)"} ],
                               { spacing:"paced(transform)" });
  var dist = [ 0,
               Math.sqrt(4.0 * 4.0 + 4.5 * 4.5 + 1.5 * 1.5),
               Math.sqrt(2.0 * 2.0 + 4.0 * 4.0 + 0.5 * 0.5),
               Math.sqrt(1.5 * 1.5 + 0.5 * 0.5 + 1.0 * 1.0) ];
  assert_animation_offsets(anim, dist);
}, 'Test spacing on scale3d' );

test(function(t) {
  var anim = addDiv(t).animate([ { transform: "rotate(60deg)" },
                                 { transform: "none" },
                                 { transform: "rotate(720deg)" },
                                 { transform: "rotate(-360deg)"} ],
                               { spacing: "paced(transform)" });
  assert_animation_offsets(anim, [ 0, 60, 720, 1080 ]);
}, 'Test spacing on rotate' );

test(function(t) {
  var anim = addDiv(t).animate([ { transform: "rotate3d(1,0,0,60deg)" },
                                 { transform: "rotate3d(1,0,0,70deg)" },
                                 { transform: "rotate3d(0,0,1,-110deg)" },
                                 { transform: "rotate3d(1,0,0,219deg)"} ],
                               { spacing: "paced(transform)" });
  var dist = [ 0,
               getAngleDist({ axis: [1,0,0], angle: 60 * pi / 180 },
                            { axis: [1,0,0], angle: 70 * pi / 180 }),
               getAngleDist({ axis: [0,1,0], angle: 70 * pi / 180 },
                            { axis: [0,0,1], angle: -110 * pi / 180 }),
               getAngleDist({ axis: [0,0,1], angle: -110 * pi / 180 },
                            { axis: [1,0,0], angle: 219 * pi / 180 }) ];
  assert_animation_offsets(anim, dist);
}, 'Test spacing on rotate3d' );

test(function(t) {
  var anim = addDiv(t).animate([ { transform: "skew(60deg)" },
                                 { transform: "none" },
                                 { transform: "skew(-90deg)" },
                                 { transform: "skew(90deg)"} ],
                               { spacing: "paced(transform)" });
  assert_animation_offsets(anim, [ 0, 60, 90, 180 ]);
}, 'Test spacing on skew' );

test(function(t) {
  var anim = addDiv(t).animate([ { transform: "skew(60deg, 30deg)" },
                                 { transform: "none" },
                                 { transform: "skew(-90deg, 60deg)" },
                                 { transform: "skew(90deg, 60deg)"} ],
                               { spacing: "paced(transform)" });
  var dist = [ 0,
               sqrt(60 * 60 + 30 * 30),
               sqrt(90 * 90 + 60 * 60),
               sqrt(180 * 180 + 0) ];
  assert_animation_offsets(anim, dist);
}, 'Test spacing on skew along both X and Y' );

test(function(t) {
  // We calculate the distance of two perspective functions by converting them
  // into two matrix3ds, and then do matrix decomposition to get two
  // perspective vectors, so the equivalent perspective vectors are:
  // perspective 1: (0, 0, -1/128, 1);
  // perspective 2: (0, 0, -1/infinity = 0, 1);
  // perspective 3: (0, 0, -1/1024, 1);
  // perspective 4: (0, 0, -1/32, 1);
  var anim = addDiv(t).animate([ { transform: "perspective(128px)" },
                                 { transform: "none" },
                                 { transform: "perspective(1024px)" },
                                 { transform: "perspective(32px)"} ],
                               { spacing: "paced(transform)" });
  assert_animation_offsets(anim,
                           [ 0, 1/128, 1/1024, 1/32 - 1/1024 ]);
}, 'Test spacing on perspective' );

test(function(t) {
  var anim =
    addDiv(t).animate([ { transform: "none" },
                        { transform: "rotate(180deg) translate(0px)" },
                        { transform: "rotate(180deg) translate(1000px)" },
                        { transform: "rotate(360deg) translate(1000px)"} ],
                      { spacing: "paced(transform)" });
  var dist = [ 0,
               sqrt(pi * pi + 0),
               sqrt(1000 * 1000),
               sqrt(pi * pi + 0) ];
  assert_animation_offsets(anim, dist);
}, 'Test spacing on matched transform lists' );

test(function(t) {
  // matrix1 => translate(100px, 50px), skewX(60deg).
  // matrix2 => translate(1000px), rotate(180deg).
  // matrix3 => translate(1000px), scale(1.5, 0.7).
  const matrix1 = createMatrix([ 1, 0, tan(pi/4.0), 1, 100, 50 ]);
  const matrix2 = createMatrix([ cos(pi), sin(pi),
                                -sin(pi), cos(pi),
                                 1000, 0 ]);
  const matrix3 = createMatrix([ 1.5, 0, 0, 0.7, 1000, 0 ]);
  var anim = addDiv(t).animate([ { transform: "none" },
                                 { transform: matrix1 },
                                 { transform: matrix2 },
                                 { transform: matrix3 } ],
                               { spacing: "paced(transform)" });
  var dist = [ 0,
               sqrt(100 * 100 + 50 * 50 + pi/4 * pi/4),
               sqrt(900 * 900 + 50 * 50 + pi * pi + pi/4 * pi/4),
               sqrt(pi * pi + 0.5 * 0.5 + 0.3 * 0.3) ];
  assert_animation_offsets(anim, dist);
}, 'Test spacing on matrix' );

test(function(t) {
  // matrix1 => translate3d(100px, 50px, -10px), skew(60deg).
  // matrix2 => translate3d(1000px, 0, 0), rotate3d(1, 0, 0, 180deg).
  // matrix3 => translate3d(1000px, 0, 0), scale3d(1.5, 0.7, 2.2).
  const matrix1 = createMatrix([ 1, 0, 0, 0,
                                 tan(pi/4.0), 1, 0, 0,
                                 0, 0, 1, 0,
                                 100, 50, -10, 1 ], true);
  const matrix2 = createMatrix([ 1, 0, 0, 0,
                                 0, cos(pi), sin(pi), 0,
                                 0, -sin(pi), cos(pi), 0,
                                 1000, 0, 0, 1 ], true);
  const matrix3 = createMatrix([ 1.5, 0, 0, 0,
                                 0, 0.7, 0, 0,
                                 0, 0, 2.2, 0,
                                 1000, 0, 0, 1 ], true);
  var anim = addDiv(t).animate([ { transform: "none" },
                                 { transform: matrix1 },
                                 { transform: matrix2 },
                                 { transform: matrix3 } ],
                               { spacing: "paced(transform)" });
  var dist = [ 0,
               sqrt(100 * 100 + 50 * 50 + 10 * 10 + pi/4 * pi/4),
               sqrt(900 * 900 + 50 * 50 + 10 * 10 + pi/4 * pi/4 + pi * pi),
               sqrt(0.5 * 0.5 + 0.3 * 0.3 + 1.2 * 1.2 + pi * pi) ];
  assert_animation_offsets(anim, dist);
}, 'Test spacing on matrix3d' );

test(function(t) {
  var anim =
    addDiv(t).animate([ { transform: "none" },
                        { transform: "translate(100px, 50px) skew(45deg)" },
                        { transform: "translate(1000px) " +
                                     "rotate3d(1, 0, 0, 180deg)" },
                        { transform: "translate(1000px) " +
                                     "scale3d(2.5, 0.5, 0.7)" } ],
                      { spacing: "paced(transform)" });

  var dist = [ 0,
               sqrt(100 * 100 + 50 * 50 + pi/4 * pi/4),
               sqrt(900 * 900 + 50 * 50 + pi/4 * pi/4 + pi * pi),
               sqrt(1.5 * 1.5 + 0.5 * 0.5 + 0.3 * 0.3 + pi * pi) ];
  assert_animation_offsets(anim, dist);
}, 'Test spacing on mismatched transform list' );

done();

</script>
</body>