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"
32 * PerAxisData is a port of GtkKineticScrolling as of GTK+ 3.20,
33 * mimicking its API and its behavior.
35 * All our curves are second degree linear differential equations, and
36 * so they can always be written as linear combinations of 2 base
37 * solutions. coef1 and coef2 are the coefficients to these two base
38 * solutions, and are computed from the initial position and velocity.
40 * In the case of simple deceleration, the differential equation is
44 * With m the resistence factor. For this we use the following 2
50 * In the case of overshoot, the differential equation is
54 * With m the resistance, and k the spring stiffness constant. We let
55 * k = m^2 / 4, so that the system is critically damped (ie, returns to its
56 * equilibrium position as quickly as possible, without oscillating), and offset
57 * the whole thing, such that the equilibrium position is at 0. This gives the
60 * f1(x) = exp(-mx / 2)
61 * f2(x) = t exp(-mx / 2)
64 static const double decelFriction = 4;
65 static const double frameRate = 60;
66 static const Seconds tickTime = 1_s / frameRate;
67 static const Seconds minimumTimerInterval { 1_ms };
71 ScrollAnimationKinetic::PerAxisData::PerAxisData(double lower, double upper, double initialPosition, double initialVelocity)
74 , m_coef1(initialVelocity / decelFriction + initialPosition)
75 , m_coef2(-initialVelocity / decelFriction)
76 , m_position(clampTo(initialPosition, lower, upper))
77 , m_velocity(initialPosition < lower || initialPosition > upper ? 0 : initialVelocity)
81 bool ScrollAnimationKinetic::PerAxisData::animateScroll(Seconds timeDelta)
83 auto lastPosition = m_position;
84 auto lastTime = m_elapsedTime;
85 m_elapsedTime += timeDelta;
87 double exponentialPart = exp(-decelFriction * m_elapsedTime.value());
88 m_position = m_coef1 + m_coef2 * exponentialPart;
89 m_velocity = -decelFriction * m_coef2 * exponentialPart;
91 if (m_position < m_lower) {
94 } else if (m_position > m_upper) {
97 } else if (fabs(m_velocity) < 1 || (lastTime && fabs(m_position - lastPosition) < 1)) {
98 m_position = round(m_position);
105 ScrollAnimationKinetic::ScrollAnimationKinetic(ScrollableArea& scrollableArea, std::function<void(FloatPoint&&)>&& notifyPositionChangedFunction)
106 : ScrollAnimation(scrollableArea)
107 , m_notifyPositionChangedFunction(WTFMove(notifyPositionChangedFunction))
108 , m_animationTimer(*this, &ScrollAnimationKinetic::animationTimerFired)
112 ScrollAnimationKinetic::~ScrollAnimationKinetic() = default;
114 void ScrollAnimationKinetic::stop()
116 m_animationTimer.stop();
117 m_horizontalData = WTF::nullopt;
118 m_verticalData = WTF::nullopt;
121 void ScrollAnimationKinetic::start(const FloatPoint& initialPosition, const FloatPoint& velocity, bool mayHScroll, bool mayVScroll)
125 m_position = initialPosition;
127 if (!velocity.x() && !velocity.y())
131 m_horizontalData = PerAxisData(m_scrollableArea.minimumScrollPosition().x(),
132 m_scrollableArea.maximumScrollPosition().x(),
133 initialPosition.x(), velocity.x());
136 m_verticalData = PerAxisData(m_scrollableArea.minimumScrollPosition().y(),
137 m_scrollableArea.maximumScrollPosition().y(),
138 initialPosition.y(), velocity.y());
141 m_startTime = MonotonicTime::now() - tickTime / 2.;
142 animationTimerFired();
145 void ScrollAnimationKinetic::animationTimerFired()
147 MonotonicTime currentTime = MonotonicTime::now();
148 Seconds deltaToNextFrame = 1_s * ceil((currentTime - m_startTime).value() * frameRate) / frameRate - (currentTime - m_startTime);
150 if (m_horizontalData && !m_horizontalData.value().animateScroll(deltaToNextFrame))
151 m_horizontalData = WTF::nullopt;
153 if (m_verticalData && !m_verticalData.value().animateScroll(deltaToNextFrame))
154 m_verticalData = WTF::nullopt;
156 // If one of the axes didn't finish its animation we must continue it.
157 if (m_horizontalData || m_verticalData)
158 m_animationTimer.startOneShot(std::max(minimumTimerInterval, deltaToNextFrame));
160 double x = m_horizontalData ? m_horizontalData.value().position() : m_position.x();
161 double y = m_verticalData ? m_verticalData.value().position() : m_position.y();
162 m_position = FloatPoint(x, y);
163 m_notifyPositionChangedFunction(FloatPoint(m_position));
166 } // namespace WebCore