Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions src/components/canvas/canvas-gestures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface ZoomLimits {
max: number;
}

type Pair = [Point, Point];
export type Pair = [Point, Point];

const clamp = (value: number, { min, max }: ZoomLimits): number => Math.min(Math.max(value, min), max);

Expand All @@ -38,17 +38,32 @@ export function pinch(view: View, from: Pair, to: Pair, limits: ZoomLimits): Vie
return { ...zoomed, x: zoomed.x + after.x - before.x, y: zoomed.y + after.y - before.y };
}

/** Takes a two-finger gesture over from the canvas: `move` gets where the fingers landed and where they are now, in client pixels. */
export interface PinchTarget {
move: (landed: Pair, now: Pair) => void;
end: () => void;
/** The platform took a finger, or the sequence lost its end: whatever the gesture changed goes back. */
cancel: () => void;
}

/** A two-finger gesture starting on the canvas. A listener that claims it takes it over from the canvas. */
export interface CanvasGesture {
claim: (pinch: PinchTarget) => void;
}

export interface CanvasGesturesOptions {
getView: () => View;
setView: (view: View) => void;
limits: ZoomLimits;
/** The second pointer has landed: whatever one pointer was doing has to stop. */
onStart: () => void;
/** The second pointer has landed: whatever one pointer was doing has to stop. A target returned takes the gesture over. */
onStart: () => PinchTarget | undefined;
}

/** Two touches on the canvas pinch and pan it. One touch, a mouse, or anything that starts off the canvas is left alone. */
/** Two touches on the canvas pinch and pan it, unless the start hands them to a target. One touch, a mouse, or anything that starts off the canvas is left alone. */
export class CanvasGestures {
private readonly pointers = new Map<number, Point>();
private target: PinchTarget | undefined;
private landed = new Map<number, Point>();

constructor(
private readonly root: HTMLElement,
Expand Down Expand Up @@ -77,14 +92,20 @@ export class CanvasGestures {

private onDown = (e: PointerEvent): void => {
// A new touch sequence has begun, so whatever is still tracked lost its pointerup somewhere.
if (e.pointerType === "touch" && e.isPrimary) this.pointers.clear();
if (e.pointerType === "touch" && e.isPrimary) {
this.pointers.clear();
this.target?.cancel();
this.release();
}
if (e.pointerType !== "touch" || e.target !== this.canvas) return;

this.pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
if (this.pointers.size < 2) return;

e.stopPropagation();
if (this.pointers.size === 2) this.options.onStart();
if (this.pointers.size !== 2) return;
this.target = this.options.onStart();
if (this.target) this.landed = new Map(this.pointers);
};

private onMove = (e: PointerEvent): void => {
Expand All @@ -93,6 +114,11 @@ export class CanvasGestures {

this.pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
if (this.pointers.size !== 2) return;
// A third finger lifting leaves the target's own two, in the order they landed.
if (this.target) {
this.target.move([...this.landed.values()] as Pair, [...this.pointers.values()] as Pair);
return;
}

const { left, top } = this.canvas.getBoundingClientRect();
const local = (point: Point): Point => ({ x: point.x - left, y: point.y - top });
Expand All @@ -104,8 +130,18 @@ export class CanvasGestures {

private onEnd = (e: PointerEvent): void => {
this.pointers.delete(e.pointerId);
if (!this.landed.has(e.pointerId)) return;

if (e.type === "pointercancel") this.target?.cancel();
else this.target?.end();
this.release();
};

private release(): void {
this.target = undefined;
this.landed.clear();
}

// Only a pinch of ours: a trackpad pinch on desktop Safari is the user's page zoom.
private blockPageZoom = (e: Event): void => {
if (e.target === this.canvas && this.pointers.size > 0) e.preventDefault();
Expand Down
12 changes: 10 additions & 2 deletions src/components/canvas/shotstack-canvas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BufferingNotice, visibleBounds } from "@canvas/buffering-notice";
import { CanvasGestures, zoomAbout, type View } from "@canvas/canvas-gestures";
import { CanvasGestures, zoomAbout, type PinchTarget, type View } from "@canvas/canvas-gestures";
import type { Player } from "@canvas/players/player";
import { AlignmentGuides } from "@canvas/system/alignment-guides";
import { createWebGLErrorOverlay } from "@canvas/webgl-error-overlay";
Expand Down Expand Up @@ -161,7 +161,15 @@ export class Canvas {
getView: () => this.getView(),
setView: view => this.setView(view),
limits: this.zoomLimits,
onStart: () => this.edit.getInternalEvents().emit(InternalEvent.CanvasGestureStarted)
onStart: () => {
let pinch: PinchTarget | undefined;
this.edit.getInternalEvents().emit(InternalEvent.CanvasGestureStarted, {
claim: target => {
pinch = target;
}
});
return pinch;
}
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/core/events/edit-events.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { CanvasGesture } from "@canvas/canvas-gestures";
import type { Player } from "@canvas/players/player";
import type { MergeField } from "@core/merge/types";
import type { Clip, Destination, Edit as EditConfig, Output, ResolvedEdit } from "@schemas";
Expand Down Expand Up @@ -215,7 +216,7 @@ export type InternalEventMap = {
[InternalEvent.CanvasClipClicked]: { player: Player };
[InternalEvent.CanvasClipDoubleClicked]: { player: Player };
[InternalEvent.CanvasBackgroundClicked]: void;
[InternalEvent.CanvasGestureStarted]: void;
[InternalEvent.CanvasGestureStarted]: CanvasGesture;

// Font
[InternalEvent.FontCapabilitiesChanged]: { supportsBold: boolean };
Expand Down
Loading
Loading