Skip to content

Commit 7039091

Browse files
committed
Add Pulse Zoom button.
1 parent f8c950d commit 7039091

File tree

5 files changed

+50
-4
lines changed

5 files changed

+50
-4
lines changed

demo/client/html/svg-demo.html.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
class="btn btn-outline-secondary pf-toolbar-button">
2323
{{octicon "plus"}}
2424
</button>
25+
<button id="pf-zoom-pulse-button" type="button" title="Pulse Zoom"
26+
class="btn btn-outline-secondary pf-toolbar-button">
27+
{{octicon "watch"}}
28+
</button>
2529
</div>
2630
<button id="pf-screenshot-button" type="button"
2731
class="btn btn-outline-secondary pf-toolbar-button">

demo/client/html/text-demo.html.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
class="btn btn-outline-secondary pf-toolbar-button">
2222
{{octicon "plus"}}
2323
</button>
24+
<button id="pf-zoom-pulse-button" type="button" title="Pulse Zoom"
25+
class="btn btn-outline-secondary pf-toolbar-button">
26+
{{octicon "watch"}}
27+
</button>
2428
</div>
2529
<button id="pf-screenshot-button" type="button"
2630
class="btn btn-outline-secondary pf-toolbar-button">

demo/client/src/app-controller.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ export abstract class DemoAppController<View extends DemoView> extends AppContro
121121
}, false);
122122
}
123123

124+
const zoomPulseButton = document.getElementById('pf-zoom-pulse-button') as
125+
HTMLButtonElement | null;
126+
if (zoomPulseButton != null) {
127+
zoomPulseButton.addEventListener('click', () => {
128+
this.view.then(view => view.zoomPulse());
129+
}, false);
130+
}
131+
124132
this.filePickerView = FilePickerView.create();
125133
if (this.filePickerView != null) {
126134
this.filePickerView.onFileLoaded = fileData => this.fileLoaded(fileData, null);

demo/client/src/camera.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export abstract class Camera {
8383
this.canvas = canvas;
8484
}
8585

86+
abstract zoom(scale: number): void;
8687
abstract zoomIn(): void;
8788
abstract zoomOut(): void;
8889
}
@@ -148,7 +149,7 @@ export class OrthographicCamera extends Camera {
148149
glmatrix.vec2.scale(mouseLocation, mouseLocation, window.devicePixelRatio);
149150

150151
const scale = 1.0 - event.deltaY * window.devicePixelRatio * ORTHOGRAPHIC_ZOOM_SPEED;
151-
this.zoom(scale, mouseLocation);
152+
this._zoom(scale, mouseLocation);
152153
}
153154

154155
zoomToFit(): void {
@@ -168,12 +169,16 @@ export class OrthographicCamera extends Camera {
168169
this.translation[1] += this.canvas.height * 0.5;
169170
}
170171

172+
zoom(scale: number): void {
173+
this._zoom(scale, this.centerPoint);
174+
}
175+
171176
zoomIn(): void {
172-
this.zoom(ORTHOGRAPHIC_ZOOM_IN_FACTOR, this.centerPoint);
177+
this._zoom(ORTHOGRAPHIC_ZOOM_IN_FACTOR, this.centerPoint);
173178
}
174179

175180
zoomOut(): void {
176-
this.zoom(ORTHOGRAPHIC_ZOOM_OUT_FACTOR, this.centerPoint);
181+
this._zoom(ORTHOGRAPHIC_ZOOM_OUT_FACTOR, this.centerPoint);
177182
}
178183

179184
private onMouseDown(event: MouseEvent): void {
@@ -228,7 +233,7 @@ export class OrthographicCamera extends Camera {
228233
}
229234
}
230235

231-
private zoom(scale: number, point: glmatrix.vec2): void {
236+
private _zoom(scale: number, point: glmatrix.vec2): void {
232237
const absoluteTranslation = glmatrix.vec2.create();
233238
glmatrix.vec2.sub(absoluteTranslation, this.translation, point);
234239
glmatrix.vec2.scale(absoluteTranslation, absoluteTranslation, 1.0 / this.scale);
@@ -306,6 +311,10 @@ export class PerspectiveCamera extends Camera {
306311
]);
307312
}
308313

314+
zoom(scale: number): void {
315+
// TODO(pcwalton)
316+
}
317+
309318
zoomIn(): void {
310319
// TODO(pcwalton)
311320
}

demo/client/src/view.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export abstract class PathfinderView {
5454

5555
private dirty: boolean;
5656

57+
private pulseHandle: number;
58+
5759
constructor() {
5860
this.dirty = false;
5961
this.canvas = unwrapNull(document.getElementById('pf-canvas')) as HTMLCanvasElement;
@@ -75,6 +77,25 @@ export abstract class PathfinderView {
7577
this.camera.zoomOut();
7678
}
7779

80+
zoomPulse(): void {
81+
if (this.pulseHandle) {
82+
window.cancelAnimationFrame(this.pulseHandle);
83+
this.pulseHandle = 0;
84+
return;
85+
}
86+
let c = 0;
87+
let d = 0.005;
88+
const self = this;
89+
function tick() {
90+
self.camera.zoom(1 + d);
91+
if (c++ % 200 === 0) {
92+
d *= -1;
93+
}
94+
self.pulseHandle = window.requestAnimationFrame(tick);
95+
}
96+
this.pulseHandle = window.requestAnimationFrame(tick);
97+
}
98+
7899
protected resized(): void {
79100
this.setDirty();
80101
}

0 commit comments

Comments
 (0)