Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: remoting/client/fling_tracker.cc

Issue 2869723007: [CRD iOS] Viewport fling animation (Closed)
Patch Set: Merge branch 'master' into feat-fling-animation Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/client/fling_tracker.h ('k') | remoting/client/gesture_interpreter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "remoting/client/fling_tracker.h"
6
7 #include <cmath>
8
9 namespace remoting {
10
11 namespace {
12
13 // TODO(yuweih): May need to tweak these numbers to get better smoothness.
14
15 // Stop flinging if the speed drops below this.
16 // 1px per 16ms. i.e. 1px/frame.
17 // TODO(yuweih): The screen unit may not be in pixel. This needs to be
18 // normalized with the DPI.
19 const float kMinTrackSpeed = 0.0625f;
20
21 // The minimum fling duration (ms) needed to trigger the fling animation. This
22 // is to prevent unintentional fling with low velocity.
23 const float kMinFlingTime = 500.f;
24
25 float GetExpFactor(float time_constant, float time_elapsed) {
26 return std::exp(-time_elapsed / time_constant);
27 }
28
29 float GetDisplacement(float initial_speed_rate, float exp_factor) {
30 // x = v0 * (1 - e^(-t / T))
31 // This comes from a solution to the linear drag equation F=-kv
32 return initial_speed_rate * (1.f - exp_factor);
33 }
34
35 float GetSpeed(float initial_speed_rate,
36 float time_constant,
37 float exp_factor) {
38 // v = (1 / T) * v0 * e^(-t / T).
39 // Derivative of the displacement.
40 return (1.f / time_constant) * initial_speed_rate * exp_factor;
41 }
42
43 } // namespace
44
45 FlingTracker::FlingTracker(float time_constant)
46 : time_constant_(time_constant) {}
47
48 FlingTracker::~FlingTracker() {}
49
50 void FlingTracker::StartFling(float velocity_x, float velocity_y) {
51 start_time_ = base::TimeTicks::Now();
52
53 initial_speed_rate_ =
54 std::sqrt(velocity_x * velocity_x + velocity_y * velocity_y);
55
56 if (GetSpeed(initial_speed_rate_, time_constant_,
57 GetExpFactor(time_constant_, kMinFlingTime)) < kMinTrackSpeed) {
58 StopFling();
59 return;
60 }
61
62 velocity_ratio_x_ = velocity_x / initial_speed_rate_;
63 velocity_ratio_y_ = velocity_y / initial_speed_rate_;
64
65 previous_position_x_ = 0;
66 previous_position_y_ = 0;
67 }
68
69 void FlingTracker::StopFling() {
70 initial_speed_rate_ = 0.f;
71 }
72
73 bool FlingTracker::IsFlingInProgress() const {
74 return initial_speed_rate_ > 0;
75 }
76
77 bool FlingTracker::TrackMovement(base::TimeDelta time_elapsed,
78 float* dx,
79 float* dy) {
80 if (!IsFlingInProgress()) {
81 return false;
82 }
83
84 float time_elapsed_ms = time_elapsed.InMilliseconds();
85
86 float exp_factor = GetExpFactor(time_constant_, time_elapsed_ms);
87
88 float speed = GetSpeed(initial_speed_rate_, time_constant_, exp_factor);
89
90 if (speed < kMinTrackSpeed) {
91 StopFling();
92 return false;
93 }
94
95 float displacement = GetDisplacement(initial_speed_rate_, exp_factor);
96
97 float position_x = displacement * velocity_ratio_x_;
98 float position_y = displacement * velocity_ratio_y_;
99
100 *dx = position_x - previous_position_x_;
101 *dy = position_y - previous_position_y_;
102
103 previous_position_x_ = position_x;
104 previous_position_y_ = position_y;
105
106 return true;
107 }
108
109 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/client/fling_tracker.h ('k') | remoting/client/gesture_interpreter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698