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.
5 #include "cc/input/page_scale_animation.h"
9 #include "base/logging.h"
10 #include "cc/animation/timing_function.h"
11 #include "ui/gfx/point_f.h"
12 #include "ui/gfx/rect_f.h"
13 #include "ui/gfx/vector2d_conversions.h"
17 // This takes a viewport-relative vector and returns a vector whose values are
18 // between 0 and 1, representing the percentage position within the viewport.
19 gfx::Vector2dF NormalizeFromViewport(gfx::Vector2dF denormalized,
20 gfx::SizeF viewport_size) {
21 return gfx::ScaleVector2d(denormalized,
22 1.f / viewport_size.width(),
23 1.f / viewport_size.height());
26 gfx::Vector2dF DenormalizeToViewport(gfx::Vector2dF normalized,
27 gfx::SizeF viewport_size) {
28 return gfx::ScaleVector2d(normalized,
29 viewport_size.width(),
30 viewport_size.height());
33 gfx::Vector2dF InterpolateBetween(gfx::Vector2dF start,
36 return start + gfx::ScaleVector2d(end - start, interp);
43 scoped_ptr<PageScaleAnimation> PageScaleAnimation::Create(
44 gfx::Vector2dF start_scroll_offset,
45 float start_page_scale_factor,
46 gfx::SizeF viewport_size,
47 gfx::SizeF root_layer_size,
48 scoped_ptr<TimingFunction> timing_function) {
49 return make_scoped_ptr(new PageScaleAnimation(start_scroll_offset,
50 start_page_scale_factor,
53 timing_function.Pass()));
56 PageScaleAnimation::PageScaleAnimation(
57 gfx::Vector2dF start_scroll_offset,
58 float start_page_scale_factor,
59 gfx::SizeF viewport_size,
60 gfx::SizeF root_layer_size,
61 scoped_ptr<TimingFunction> timing_function)
62 : start_page_scale_factor_(start_page_scale_factor),
63 target_page_scale_factor_(0.f),
64 start_scroll_offset_(start_scroll_offset),
67 viewport_size_(viewport_size),
68 root_layer_size_(root_layer_size),
71 timing_function_(timing_function.Pass()) {}
73 PageScaleAnimation::~PageScaleAnimation() {}
75 void PageScaleAnimation::ZoomTo(gfx::Vector2dF target_scroll_offset,
76 float target_page_scale_factor,
78 target_page_scale_factor_ = target_page_scale_factor;
79 target_scroll_offset_ = target_scroll_offset;
80 ClampTargetScrollOffset();
83 if (start_page_scale_factor_ == target_page_scale_factor) {
84 start_anchor_ = start_scroll_offset_;
85 target_anchor_ = target_scroll_offset;
89 // For uniform-looking zooming, infer an anchor from the start and target
91 InferTargetAnchorFromScrollOffsets();
92 start_anchor_ = target_anchor_;
95 void PageScaleAnimation::ZoomWithAnchor(gfx::Vector2dF anchor,
96 float target_page_scale_factor,
98 start_anchor_ = anchor;
99 target_page_scale_factor_ = target_page_scale_factor;
100 duration_ = duration;
102 // We start zooming out from the anchor tapped by the user. But if
103 // the target scale is impossible to attain without hitting the root layer
104 // edges, then infer an anchor that doesn't collide with the edges.
105 // We will interpolate between the two anchors during the animation.
106 InferTargetScrollOffsetFromStartAnchor();
107 ClampTargetScrollOffset();
109 if (start_page_scale_factor_ == target_page_scale_factor_) {
110 target_anchor_ = start_anchor_;
113 InferTargetAnchorFromScrollOffsets();
116 void PageScaleAnimation::InferTargetScrollOffsetFromStartAnchor() {
117 gfx::Vector2dF normalized = NormalizeFromViewport(
118 start_anchor_ - start_scroll_offset_, StartViewportSize());
119 target_scroll_offset_ =
120 start_anchor_ - DenormalizeToViewport(normalized, TargetViewportSize());
123 void PageScaleAnimation::InferTargetAnchorFromScrollOffsets() {
124 // The anchor is the point which is at the same normalized relative position
125 // within both start viewport rect and target viewport rect. For example, a
126 // zoom-in double-tap to a perfectly centered rect will have normalized
127 // anchor (0.5, 0.5), while one to a rect touching the bottom-right of the
128 // screen will have normalized anchor (1.0, 1.0). In other words, it obeys
130 // anchor = start_size * normalized + start_offset
131 // anchor = target_size * normalized + target_offset
132 // where both anchor and normalized begin as unknowns. Solving
133 // for the normalized, we get the following:
135 1.f / (TargetViewportSize().width() - StartViewportSize().width());
137 1.f / (TargetViewportSize().height() - StartViewportSize().height());
138 gfx::Vector2dF normalized = gfx::ScaleVector2d(
139 start_scroll_offset_ - target_scroll_offset_, width_scale, height_scale);
141 target_scroll_offset_ + DenormalizeToViewport(normalized,
142 TargetViewportSize());
145 void PageScaleAnimation::ClampTargetScrollOffset() {
146 gfx::Vector2dF max_scroll_offset =
147 gfx::RectF(root_layer_size_).bottom_right() -
148 gfx::RectF(TargetViewportSize()).bottom_right();
149 target_scroll_offset_.SetToMax(gfx::Vector2dF());
150 target_scroll_offset_.SetToMin(max_scroll_offset);
153 gfx::SizeF PageScaleAnimation::StartViewportSize() const {
154 return gfx::ScaleSize(viewport_size_, 1.f / start_page_scale_factor_);
157 gfx::SizeF PageScaleAnimation::TargetViewportSize() const {
158 return gfx::ScaleSize(viewport_size_, 1.f / target_page_scale_factor_);
161 gfx::SizeF PageScaleAnimation::ViewportSizeAt(float interp) const {
162 return gfx::ScaleSize(viewport_size_, 1.f / PageScaleFactorAt(interp));
165 bool PageScaleAnimation::IsAnimationStarted() const {
166 return start_time_ >= 0;
169 void PageScaleAnimation::StartAnimation(double time) {
170 DCHECK_GT(0, start_time_);
174 gfx::Vector2dF PageScaleAnimation::ScrollOffsetAtTime(double time) const {
175 DCHECK_GE(start_time_, 0);
176 return ScrollOffsetAt(InterpAtTime(time));
179 float PageScaleAnimation::PageScaleFactorAtTime(double time) const {
180 DCHECK_GE(start_time_, 0);
181 return PageScaleFactorAt(InterpAtTime(time));
184 bool PageScaleAnimation::IsAnimationCompleteAtTime(double time) const {
185 DCHECK_GE(start_time_, 0);
186 return time >= end_time();
189 float PageScaleAnimation::InterpAtTime(double time) const {
190 DCHECK_GE(start_time_, 0);
191 DCHECK_GE(time, start_time_);
192 if (IsAnimationCompleteAtTime(time))
195 const double normalized_time = (time - start_time_) / duration_;
196 return timing_function_->GetValue(normalized_time);
199 gfx::Vector2dF PageScaleAnimation::ScrollOffsetAt(float interp) const {
201 return start_scroll_offset_;
203 return target_scroll_offset_;
205 return AnchorAt(interp) - ViewportRelativeAnchorAt(interp);
208 gfx::Vector2dF PageScaleAnimation::AnchorAt(float interp) const {
209 // Interpolate from start to target anchor in absolute space.
210 return InterpolateBetween(start_anchor_, target_anchor_, interp);
213 gfx::Vector2dF PageScaleAnimation::ViewportRelativeAnchorAt(
214 float interp) const {
215 // Interpolate from start to target anchor in normalized space.
216 gfx::Vector2dF start_normalized =
217 NormalizeFromViewport(start_anchor_ - start_scroll_offset_,
218 StartViewportSize());
219 gfx::Vector2dF target_normalized =
220 NormalizeFromViewport(target_anchor_ - target_scroll_offset_,
221 TargetViewportSize());
222 gfx::Vector2dF interp_normalized =
223 InterpolateBetween(start_normalized, target_normalized, interp);
225 return DenormalizeToViewport(interp_normalized, ViewportSizeAt(interp));
228 float PageScaleAnimation::PageScaleFactorAt(float interp) const {
230 return start_page_scale_factor_;
232 return target_page_scale_factor_;
234 // Linearly interpolate the magnitude in log scale.
235 float diff = target_page_scale_factor_ / start_page_scale_factor_;
236 float log_diff = log(diff);
238 diff = exp(log_diff);
239 return start_page_scale_factor_ * diff;