@@ -162,6 +162,21 @@ static RefPtr<CSSStyleValue const> interpolate_translate(DOM::Element& element,
162
162
move (new_values));
163
163
}
164
164
165
+ // https://drafts.csswg.org/css-transforms-2/#interpolation-of-decomposed-3d-matrix-values
166
+ static FloatVector4 slerp (FloatVector4 const & from, FloatVector4 const & to, float delta)
167
+ {
168
+ auto product = from.dot (to);
169
+
170
+ product = clamp (product, -1 .0f , 1 .0f );
171
+ if (fabsf (product) >= 1 .0f )
172
+ return from;
173
+
174
+ auto theta = acosf (product);
175
+ auto w = sinf (delta * theta) / sqrtf (1 - (product * product));
176
+
177
+ return from * (cosf (delta * theta) - (product * w)) + to * w;
178
+ }
179
+
165
180
ValueComparingRefPtr<CSSStyleValue const > interpolate_property (DOM::Element& element, PropertyID property_id, CSSStyleValue const & a_from, CSSStyleValue const & a_to, float delta, AllowDiscrete allow_discrete)
166
181
{
167
182
auto from = with_keyword_values_resolved (element, property_id, a_from);
@@ -555,21 +570,7 @@ RefPtr<CSSStyleValue const> interpolate_transform(DOM::Element& element, CSSStyl
555
570
556
571
// https://drafts.csswg.org/css-transforms-2/#interpolation-of-decomposed-3d-matrix-values
557
572
static constexpr auto interpolate = [](DecomposedValues& from, DecomposedValues& to, float delta) -> DecomposedValues {
558
- auto product = clamp (from.rotation .dot (to.rotation ), -1 .0f , 1 .0f );
559
- FloatVector4 interpolated_rotation;
560
- if (fabsf (product) == 1 .0f ) {
561
- interpolated_rotation = from.rotation ;
562
- } else {
563
- auto theta = acos (product);
564
- auto w = sin (delta * theta) / sqrtf (1 .0f - product * product);
565
-
566
- for (int i = 0 ; i < 4 ; i++) {
567
- from.rotation [i] *= cos (delta * theta) - product * w;
568
- to.rotation [i] *= w;
569
- interpolated_rotation[i] = from.rotation [i] + to.rotation [i];
570
- }
571
- }
572
-
573
+ auto interpolated_rotation = slerp (from.rotation , to.rotation , delta);
573
574
return {
574
575
interpolate_raw (from.translation , to.translation , delta),
575
576
interpolate_raw (from.scale , to.scale , delta),
0 commit comments