OLD | NEW |
| (Empty) |
1 // Copyright 2012 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 "config.h" | |
6 | |
7 #include "CCPageScaleAnimation.h" | |
8 | |
9 #include "FloatRect.h" | |
10 #include "FloatSize.h" | |
11 | |
12 #include <math.h> | |
13 | |
14 namespace cc { | |
15 | |
16 PassOwnPtr<CCPageScaleAnimation> CCPageScaleAnimation::create(const IntSize& scr
ollStart, float pageScaleStart, const IntSize& windowSize, const IntSize& conten
tSize, double startTime) | |
17 { | |
18 return adoptPtr(new CCPageScaleAnimation(scrollStart, pageScaleStart, window
Size, contentSize, startTime)); | |
19 } | |
20 | |
21 | |
22 CCPageScaleAnimation::CCPageScaleAnimation(const IntSize& scrollStart, float pag
eScaleStart, const IntSize& windowSize, const IntSize& contentSize, double start
Time) | |
23 : m_scrollStart(scrollStart) | |
24 , m_pageScaleStart(pageScaleStart) | |
25 , m_windowSize(windowSize) | |
26 , m_contentSize(contentSize) | |
27 , m_anchorMode(false) | |
28 , m_scrollEnd(scrollStart) | |
29 , m_pageScaleEnd(pageScaleStart) | |
30 , m_startTime(startTime) | |
31 , m_duration(0) | |
32 { | |
33 } | |
34 | |
35 void CCPageScaleAnimation::zoomTo(const IntSize& finalScroll, float finalPageSca
le, double duration) | |
36 { | |
37 if (m_pageScaleStart != finalPageScale) { | |
38 // For uniform-looking zooming, infer the anchor (point that remains in | |
39 // place throughout the zoom) from the start and end rects. | |
40 FloatRect startRect(IntPoint(m_scrollStart), m_windowSize); | |
41 FloatRect endRect(IntPoint(finalScroll), m_windowSize); | |
42 endRect.scale(m_pageScaleStart / finalPageScale); | |
43 | |
44 // The anchor is the point which is at the same ratio of the sides of | |
45 // both startRect and endRect. For example, a zoom-in double-tap to a | |
46 // perfectly centered rect will have anchor ratios (0.5, 0.5), while one | |
47 // to a rect touching the bottom-right of the screen will have anchor | |
48 // ratios (1.0, 1.0). In other words, it obeys the equations: | |
49 // anchorX = start_width * ratioX + start_x | |
50 // anchorX = end_width * ratioX + end_x | |
51 // anchorY = start_height * ratioY + start_y | |
52 // anchorY = end_height * ratioY + end_y | |
53 // where both anchor{x,y} and ratio{x,y} begin as unknowns. Solving | |
54 // for the ratios, we get the following formulas: | |
55 float ratioX = (startRect.x() - endRect.x()) / (endRect.width() - startR
ect.width()); | |
56 float ratioY = (startRect.y() - endRect.y()) / (endRect.height() - start
Rect.height()); | |
57 | |
58 IntSize anchor(m_windowSize.width() * ratioX, m_windowSize.height() * ra
tioY); | |
59 zoomWithAnchor(anchor, finalPageScale, duration); | |
60 } else { | |
61 // If this is a pure translation, then there exists no anchor. Linearly | |
62 // interpolate the scroll offset instead. | |
63 m_scrollEnd = finalScroll; | |
64 m_pageScaleEnd = finalPageScale; | |
65 m_duration = duration; | |
66 m_anchorMode = false; | |
67 } | |
68 } | |
69 | |
70 void CCPageScaleAnimation::zoomWithAnchor(const IntSize& anchor, float finalPage
Scale, double duration) | |
71 { | |
72 m_scrollEnd = m_scrollStart + anchor; | |
73 m_scrollEnd.scale(finalPageScale / m_pageScaleStart); | |
74 m_scrollEnd -= anchor; | |
75 | |
76 m_scrollEnd.clampNegativeToZero(); | |
77 FloatSize scaledContentSize(m_contentSize); | |
78 scaledContentSize.scale(finalPageScale / m_pageScaleStart); | |
79 IntSize maxScrollPosition = roundedIntSize(scaledContentSize - m_windowSize)
; | |
80 m_scrollEnd = m_scrollEnd.shrunkTo(maxScrollPosition); | |
81 | |
82 m_anchor = anchor; | |
83 m_pageScaleEnd = finalPageScale; | |
84 m_duration = duration; | |
85 m_anchorMode = true; | |
86 } | |
87 | |
88 IntSize CCPageScaleAnimation::scrollOffsetAtTime(double time) const | |
89 { | |
90 return scrollOffsetAtRatio(progressRatioForTime(time)); | |
91 } | |
92 | |
93 float CCPageScaleAnimation::pageScaleAtTime(double time) const | |
94 { | |
95 return pageScaleAtRatio(progressRatioForTime(time)); | |
96 } | |
97 | |
98 bool CCPageScaleAnimation::isAnimationCompleteAtTime(double time) const | |
99 { | |
100 return time >= endTime(); | |
101 } | |
102 | |
103 float CCPageScaleAnimation::progressRatioForTime(double time) const | |
104 { | |
105 if (isAnimationCompleteAtTime(time)) | |
106 return 1; | |
107 | |
108 return (time - m_startTime) / m_duration; | |
109 } | |
110 | |
111 IntSize CCPageScaleAnimation::scrollOffsetAtRatio(float ratio) const | |
112 { | |
113 if (ratio <= 0) | |
114 return m_scrollStart; | |
115 if (ratio >= 1) | |
116 return m_scrollEnd; | |
117 | |
118 float currentPageScale = pageScaleAtRatio(ratio); | |
119 IntSize currentScrollOffset; | |
120 if (m_anchorMode) { | |
121 // Keep the anchor stable on the screen at the current scale. | |
122 IntSize documentAnchor = m_scrollStart + m_anchor; | |
123 documentAnchor.scale(currentPageScale / m_pageScaleStart); | |
124 currentScrollOffset = documentAnchor - m_anchor; | |
125 } else { | |
126 // First move both scroll offsets to the current coordinate space. | |
127 FloatSize scaledStartScroll(m_scrollStart); | |
128 scaledStartScroll.scale(currentPageScale / m_pageScaleStart); | |
129 FloatSize scaledEndScroll(m_scrollEnd); | |
130 scaledEndScroll.scale(currentPageScale / m_pageScaleEnd); | |
131 | |
132 // Linearly interpolate between them. | |
133 FloatSize delta = scaledEndScroll - scaledStartScroll; | |
134 delta.scale(ratio); | |
135 currentScrollOffset = roundedIntSize(scaledStartScroll + delta); | |
136 } | |
137 | |
138 return currentScrollOffset; | |
139 } | |
140 | |
141 float CCPageScaleAnimation::pageScaleAtRatio(float ratio) const | |
142 { | |
143 if (ratio <= 0) | |
144 return m_pageScaleStart; | |
145 if (ratio >= 1) | |
146 return m_pageScaleEnd; | |
147 | |
148 // Linearly interpolate the magnitude in log scale. | |
149 // Log scale is needed to maintain the appearance of uniform zoom. For | |
150 // example, if we zoom from 0.5 to 4.0 in 3 seconds, then we should | |
151 // be zooming by 2x every second. | |
152 float diff = m_pageScaleEnd / m_pageScaleStart; | |
153 float logDiff = log(diff); | |
154 logDiff *= ratio; | |
155 diff = exp(logDiff); | |
156 return m_pageScaleStart * diff; | |
157 } | |
158 | |
159 } // namespace cc | |
OLD | NEW |