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