2 * Copyright (C) 2016 Igalia S.L.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "ScrollAnimationKinetic.h"
29 #include "ScrollableArea.h"
30 #include <wtf/CurrentTime.h>
33 * PerAxisData is a port of GtkKineticScrolling as of GTK+ 3.20,
34 * mimicking its API and its behavior.
36 * All our curves are second degree linear differential equations, and
37 * so they can always be written as linear combinations of 2 base
38 * solutions. coef1 and coef2 are the coefficients to these two base
39 * solutions, and are computed from the initial position and velocity.
41 * In the case of simple deceleration, the differential equation is
45 * With m the resistence factor. For this we use the following 2
51 * In the case of overshoot, the differential equation is
55 * With m the resistance, and k the spring stiffness constant. We let
56 * k = m^2 / 4, so that the system is critically damped (ie, returns to its
57 * equilibrium position as quickly as possible, without oscillating), and offset
58 * the whole thing, such that the equilibrium position is at 0. This gives the
61 * f1(x) = exp(-mx / 2)
62 * f2(x) = t exp(-mx / 2)
65 static const double decelFriction = 4;
66 static const double frameRate = 60;
67 static const Seconds tickTime = 1_s / frameRate;
68 static const Seconds minimumTimerInterval { 1_ms };
72 ScrollAnimationKinetic::PerAxisData::PerAxisData(double lower, double upper, double initialPosition, double initialVelocity)
75 , m_coef1(initialVelocity / decelFriction + initialPosition)
76 , m_coef2(-initialVelocity / decelFriction)
77 , m_position(clampTo(initialPosition, lower, upper))
78 , m_velocity(initialPosition < lower || initialPosition > upper ? 0 : initialVelocity)
82 bool ScrollAnimationKinetic::PerAxisData::animateScroll(Seconds timeDelta)
84 auto lastPosition = m_position;
85 auto lastTime = m_elapsedTime;
86 m_elapsedTime += timeDelta;
88 double exponentialPart = exp(-decelFriction * m_elapsedTime.value());
89 m_position = m_coef1 + m_coef2 * exponentialPart;
90 m_velocity = -decelFriction * m_coef2 * exponentialPart;
92 if (m_position < m_lower) {
95 } else if (m_position > m_upper) {
98 } else if (fabs(m_velocity) < 1 || (lastTime && fabs(m_position - lastPosition) < 1)) {
99 m_position = round(m_position);
106 ScrollAnimationKinetic::ScrollAnimationKinetic(ScrollableArea& scrollableArea, std::function<void(FloatPoint&&)>&& notifyPositionChangedFunction)
107 : ScrollAnimation(scrollableArea)
108 , m_notifyPositionChangedFunction(WTFMove(notifyPositionChangedFunction))
109 , m_animationTimer(*this, &ScrollAnimationKinetic::animationTimerFired)
113 ScrollAnimationKinetic::~ScrollAnimationKinetic() = default;
115 void ScrollAnimationKinetic::stop()
117 m_animationTimer.stop();
118 m_horizontalData = std::nullopt;
119 m_verticalData = std::nullopt;
122 void ScrollAnimationKinetic::start(const FloatPoint& initialPosition, const FloatPoint& velocity, bool mayHScroll, bool mayVScroll)
126 m_position = initialPosition;
128 if (!velocity.x() && !velocity.y())
132 m_horizontalData = PerAxisData(m_scrollableArea.minimumScrollPosition().x(),
133 m_scrollableArea.maximumScrollPosition().x(),
134 initialPosition.x(), velocity.x());
137 m_verticalData = PerAxisData(m_scrollableArea.minimumScrollPosition().y(),
138 m_scrollableArea.maximumScrollPosition().y(),
139 initialPosition.y(), velocity.y());
142 m_startTime = MonotonicTime::now() - tickTime / 2.;
143 animationTimerFired();
146 void ScrollAnimationKinetic::animationTimerFired()
148 MonotonicTime currentTime = MonotonicTime::now();
149 Seconds deltaToNextFrame = 1_s * ceil((currentTime - m_startTime).value() * frameRate) / frameRate - (currentTime - m_startTime);
151 if (m_horizontalData && !m_horizontalData.value().animateScroll(deltaToNextFrame))
152 m_horizontalData = std::nullopt;
154 if (m_verticalData && !m_verticalData.value().animateScroll(deltaToNextFrame))
155 m_verticalData = std::nullopt;
157 // If one of the axes didn't finish its animation we must continue it.
158 if (m_horizontalData || m_verticalData)
159 m_animationTimer.startOneShot(std::max(minimumTimerInterval, deltaToNextFrame));
161 double x = m_horizontalData ? m_horizontalData.value().position() : m_position.x();
162 double y = m_verticalData ? m_verticalData.value().position() : m_position.y();
163 m_position = FloatPoint(x, y);
164 m_notifyPositionChangedFunction(FloatPoint(m_position));
167 } // namespace WebCore