Skip to content

Commit

Permalink
feat: capture pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
Quozul committed May 13, 2024
1 parent b13e1df commit ad85881
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/listenMouseMove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ export function listenMouseMove(
handleMove(event);
evCache.push(event);
previousDistance = calculateTouchDistance();
element.setPointerCapture(event.pointerId);
};

const handleEnd = (event: PointerEvent) => {
const index = evCache.findIndex((cachedEv) => cachedEv.pointerId === event.pointerId);
evCache.splice(index, 1);
element.releasePointerCapture(event.pointerId);
};

const handleWheel = (event: WheelEvent) => {
Expand Down
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,17 @@ class CanvasImage extends HTMLElement {
attributeChangedCallback(name: string, _oldValue: string, newValue: string) {
if (name === "src") {
this.loadImage(newValue);
this.calculateImageZoom(true);
}
}

private calculateImageZoom() {
private calculateImageZoom(forceCenter: boolean = false) {
if (!this.context || !this.source) return;

this.displayWidth = (this.source.height * this.context.canvas.width * this.zoomFactor) / this.context.canvas.height;
this.displayHeight = this.source.height * this.zoomFactor;

if (this.offsetX === 0) {
if (forceCenter || this.offsetX === 0) {
this.offsetX = -this.displayWidth / 2 + this.source.width / 2;
}
}
Expand Down

0 comments on commit ad85881

Please sign in to comment.