3 Copyright (c) 2013 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
7 <link rel="import" href="/tvcm/utils.html">
8 <link rel="import" href="/tvcm/ui/animation.html">
13 tvcm.exportTo('tracing', function() {
14 var kDefaultPanAnimatoinDurationMs = 100.0;
17 * Pans a TimelineDisplayTransform by a given amount.
19 * @extends {tvcm.ui.Animation}
20 * @param {Number} deltaX The total amount of change to the transform's panX.
21 * @param {Number} deltaY The total amount of change to the transform's panY.
22 * @param {Number=} opt_durationMs How long the pan animation should run.
23 * Defaults to kDefaultPanAnimatoinDurationMs.
25 function TimelineDisplayTransformPanAnimation(
26 deltaX, deltaY, opt_durationMs) {
29 if (opt_durationMs === undefined)
30 this.durationMs = kDefaultPanAnimatoinDurationMs;
32 this.durationMs = opt_durationMs;
34 this.startPanX = undefined;
35 this.startPanY = undefined;
36 this.startTimeMs = undefined;
39 TimelineDisplayTransformPanAnimation.prototype = {
40 __proto__: tvcm.ui.Animation.prototype,
43 return this.deltaY !== 0;
46 canTakeOverFor: function(existingAnimation) {
47 return existingAnimation instanceof TimelineDisplayTransformPanAnimation;
50 takeOverFor: function(existing, timestamp, target) {
51 var remainingDeltaXOnExisting = existing.goalPanX - target.panX;
52 var remainingDeltaYOnExisting = existing.goalPanY - target.panY;
53 var remainingTimeOnExisting = timestamp - (
54 existing.startTimeMs + existing.durationMs);
55 remainingTimeOnExisting = Math.max(remainingTimeOnExisting, 0);
57 this.deltaX += remainingDeltaXOnExisting;
58 this.deltaY += remainingDeltaYOnExisting;
59 this.durationMs += remainingTimeOnExisting;
62 start: function(timestamp, target) {
63 this.startTimeMs = timestamp;
64 this.startPanX = target.panX;
65 this.startPanY = target.panY;
68 tick: function(timestamp, target) {
69 var percentDone = (timestamp - this.startTimeMs) / this.durationMs;
70 percentDone = tvcm.clamp(percentDone, 0, 1);
72 target.panX = tvcm.lerp(percentDone, this.startPanX, this.goalPanX);
74 target.panY = tvcm.lerp(percentDone, this.startPanY, this.goalPanY);
75 return timestamp >= this.startTimeMs + this.durationMs;
79 return this.startPanX + this.deltaX;
83 return this.startPanY + this.deltaY;
88 * Zooms in/out on a specified location in the world.
90 * Zooming in and out is all about keeping the area under the mouse cursor,
91 * here called the "focal point" in the same place under the zoom. If one
92 * simply changes the scale, the area under the mouse cursor will change. To
93 * keep the focal point from moving during the zoom, the pan needs to change
94 * in order to compensate. Thus, a ZoomTo animation is given both a focal
95 * point in addition to the amount by which to zoom.
98 * @extends {tvcm.ui.Animation}
99 * @param {Number} goalFocalPointXWorld The X coordinate in the world which is
101 * @param {Number} goalFocalPointXView Where on the screen the
102 * goalFocalPointXWorld should stay centered during the zoom.
103 * @param {Number} goalFocalPointY Where the panY should be when the zoom
105 * @param {Number} zoomInRatioX The ratio of the current scaleX to the goal
108 function TimelineDisplayTransformZoomToAnimation(
109 goalFocalPointXWorld,
114 this.goalFocalPointXWorld = goalFocalPointXWorld;
115 this.goalFocalPointXView = goalFocalPointXView;
116 this.goalFocalPointY = goalFocalPointY;
117 this.zoomInRatioX = zoomInRatioX;
118 if (opt_durationMs === undefined)
119 this.durationMs = kDefaultPanAnimatoinDurationMs;
121 this.durationMs = opt_durationMs;
123 this.startTimeMs = undefined;
124 this.startScaleX = undefined;
125 this.goalScaleX = undefined;
126 this.startPanY = undefined;
127 this.goalPanY = undefined;
130 TimelineDisplayTransformZoomToAnimation.prototype = {
131 __proto__: tvcm.ui.Animation.prototype,
134 return this.startPanY != this.goalPanY;
137 canTakeOverFor: function(existingAnimation) {
141 takeOverFor: function(existingAnimation, timestamp, target) {
142 this.goalScaleX = target.scaleX * this.zoomInRatioX;
145 start: function(timestamp, target) {
146 this.startTimeMs = timestamp;
147 this.startScaleX = target.scaleX;
148 this.goalScaleX = this.zoomInRatioX * target.scaleX;
149 this.startPanY = target.panY;
152 tick: function(timestamp, target) {
153 var percentDone = (timestamp - this.startTimeMs) / this.durationMs;
154 percentDone = tvcm.clamp(percentDone, 0, 1);
156 target.scaleX = tvcm.lerp(percentDone, this.startScaleX, this.goalScaleX);
157 if (this.affectsPanY) {
158 target.panY = tvcm.lerp(
159 percentDone, this.startPanY, this.goalFocalPointY);
162 target.xPanWorldPosToViewPos(
163 this.goalFocalPointXWorld, this.goalFocalPointXView);
164 return timestamp >= this.startTimeMs + this.durationMs;
169 TimelineDisplayTransformPanAnimation:
170 TimelineDisplayTransformPanAnimation,
171 TimelineDisplayTransformZoomToAnimation:
172 TimelineDisplayTransformZoomToAnimation