1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | };
|
9 | return function (d, b) {
|
10 | extendStatics(d, b);
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | })();
|
15 | exports.__esModule = true;
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var events_1 = require("events");
|
23 | var easings = require("./easings");
|
24 | var utils_1 = require("./utils");
|
25 |
|
26 |
|
27 |
|
28 | var ANIMATE_DEFAULTS = {
|
29 | easing: "quadraticInOut",
|
30 | duration: 150
|
31 | };
|
32 | var DEFAULT_ZOOMING_RATIO = 1.5;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | var Camera = (function (_super) {
|
39 | __extends(Camera, _super);
|
40 | function Camera() {
|
41 | var _this = _super.call(this) || this;
|
42 | _this.x = 0.5;
|
43 | _this.y = 0.5;
|
44 | _this.angle = 0;
|
45 | _this.ratio = 1;
|
46 | _this.nextFrame = null;
|
47 | _this.enabled = true;
|
48 |
|
49 | _this.previousState = _this.getState();
|
50 | return _this;
|
51 | }
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 | Camera.prototype.enable = function () {
|
58 | this.enabled = true;
|
59 | return this;
|
60 | };
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 | Camera.prototype.disable = function () {
|
67 | this.enabled = false;
|
68 | return this;
|
69 | };
|
70 | |
71 |
|
72 |
|
73 |
|
74 |
|
75 | Camera.prototype.getState = function () {
|
76 | return {
|
77 | x: this.x,
|
78 | y: this.y,
|
79 | angle: this.angle,
|
80 | ratio: this.ratio
|
81 | };
|
82 | };
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 | Camera.prototype.getPreviousState = function () {
|
89 | var state = this.previousState;
|
90 | return {
|
91 | x: state.x,
|
92 | y: state.y,
|
93 | angle: state.angle,
|
94 | ratio: state.ratio
|
95 | };
|
96 | };
|
97 | |
98 |
|
99 |
|
100 |
|
101 |
|
102 | Camera.prototype.isAnimated = function () {
|
103 | return !!this.nextFrame;
|
104 | };
|
105 | |
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | Camera.prototype.graphToViewport = function (dimensions, x, y) {
|
117 | var smallestDimension = Math.min(dimensions.width, dimensions.height);
|
118 | var dx = smallestDimension / dimensions.width, dy = smallestDimension / dimensions.height;
|
119 |
|
120 |
|
121 | return {
|
122 | x: (x - this.x + this.ratio / 2 / dx) * (smallestDimension / this.ratio),
|
123 | y: (this.y - y + this.ratio / 2 / dy) * (smallestDimension / this.ratio)
|
124 | };
|
125 | };
|
126 | |
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | Camera.prototype.viewportToGraph = function (dimensions, x, y) {
|
137 | var smallestDimension = Math.min(dimensions.width, dimensions.height);
|
138 | var dx = smallestDimension / dimensions.width, dy = smallestDimension / dimensions.height;
|
139 | return {
|
140 | x: (this.ratio / smallestDimension) * x + this.x - this.ratio / 2 / dx,
|
141 | y: -((this.ratio / smallestDimension) * y - this.y - this.ratio / 2 / dy)
|
142 | };
|
143 | };
|
144 | |
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | Camera.prototype.viewRectangle = function (dimensions) {
|
152 |
|
153 | var marginX = (0 * dimensions.width) / 8, marginY = (0 * dimensions.height) / 8;
|
154 | var p1 = this.viewportToGraph(dimensions, 0 - marginX, 0 - marginY), p2 = this.viewportToGraph(dimensions, dimensions.width + marginX, 0 - marginY), h = this.viewportToGraph(dimensions, 0, dimensions.height + marginY);
|
155 | return {
|
156 | x1: p1.x,
|
157 | y1: p1.y,
|
158 | x2: p2.x,
|
159 | y2: p2.y,
|
160 | height: p2.y - h.y
|
161 | };
|
162 | };
|
163 | |
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | Camera.prototype.setState = function (state) {
|
170 | if (!this.enabled)
|
171 | return this;
|
172 |
|
173 |
|
174 |
|
175 | this.previousState = this.getState();
|
176 | if ("x" in state)
|
177 | this.x = state.x;
|
178 | if ("y" in state)
|
179 | this.y = state.y;
|
180 | if ("angle" in state)
|
181 | this.angle = state.angle;
|
182 | if ("ratio" in state)
|
183 | this.ratio = state.ratio;
|
184 |
|
185 |
|
186 | this.emit("updated", this.getState());
|
187 | return this;
|
188 | };
|
189 | |
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | Camera.prototype.animate = function (state, options, callback) {
|
199 | var _this = this;
|
200 | if (!this.enabled)
|
201 | return this;
|
202 |
|
203 | options = utils_1.assign({}, ANIMATE_DEFAULTS, options);
|
204 | var easing = typeof options.easing === "function" ? options.easing : easings[options.easing];
|
205 |
|
206 | if (this.nextFrame)
|
207 | cancelAnimationFrame(this.nextFrame);
|
208 |
|
209 | var start = Date.now(), initialState = this.getState();
|
210 |
|
211 | var fn = function () {
|
212 | var t = (Date.now() - start) / options.duration;
|
213 |
|
214 | if (t >= 1) {
|
215 | _this.nextFrame = null;
|
216 | _this.setState(state);
|
217 | if (typeof callback === "function")
|
218 | callback();
|
219 | return;
|
220 | }
|
221 | var coefficient = easing(t);
|
222 | var newState = {};
|
223 | if ("x" in state)
|
224 | newState.x = initialState.x + (state.x - initialState.x) * coefficient;
|
225 | if ("y" in state)
|
226 | newState.y = initialState.y + (state.y - initialState.y) * coefficient;
|
227 | if ("angle" in state)
|
228 | newState.angle = initialState.angle + (state.angle - initialState.angle) * coefficient;
|
229 | if ("ratio" in state)
|
230 | newState.ratio = initialState.ratio + (state.ratio - initialState.ratio) * coefficient;
|
231 | _this.setState(newState);
|
232 | _this.nextFrame = requestAnimationFrame(fn);
|
233 | };
|
234 | if (this.nextFrame) {
|
235 | cancelAnimationFrame(this.nextFrame);
|
236 | this.nextFrame = requestAnimationFrame(fn);
|
237 | }
|
238 | else {
|
239 | fn();
|
240 | }
|
241 | };
|
242 | |
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | Camera.prototype.animatedZoom = function (factorOrOptions) {
|
249 | if (!factorOrOptions) {
|
250 | return this.animate({ ratio: this.ratio / DEFAULT_ZOOMING_RATIO });
|
251 | }
|
252 | else {
|
253 | if (typeof factorOrOptions === "number")
|
254 | return this.animate({ ratio: this.ratio / factorOrOptions });
|
255 | else
|
256 | return this.animate({
|
257 | ratio: this.ratio / (factorOrOptions.factor || DEFAULT_ZOOMING_RATIO)
|
258 | }, factorOrOptions);
|
259 | }
|
260 | };
|
261 | |
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 | Camera.prototype.animatedUnzoom = function (factorOrOptions) {
|
268 | if (!factorOrOptions) {
|
269 | return this.animate({ ratio: this.ratio * DEFAULT_ZOOMING_RATIO });
|
270 | }
|
271 | else {
|
272 | if (typeof factorOrOptions === "number")
|
273 | return this.animate({ ratio: this.ratio * factorOrOptions });
|
274 | else
|
275 | return this.animate({
|
276 | ratio: this.ratio * (factorOrOptions.factor || DEFAULT_ZOOMING_RATIO)
|
277 | }, factorOrOptions);
|
278 | }
|
279 | };
|
280 | |
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | Camera.prototype.animatedReset = function (options) {
|
287 | return this.animate({
|
288 | x: 0.5,
|
289 | y: 0.5,
|
290 | ratio: 1,
|
291 | angle: 0
|
292 | }, options);
|
293 | };
|
294 | return Camera;
|
295 | }(events_1.EventEmitter));
|
296 | exports["default"] = Camera;
|