Last active
September 27, 2025 17:48
-
-
Save gstreetmedia/11ec85cc7c5368fdb5c05367956dc140 to your computer and use it in GitHub Desktop.
Touch and Mouse Controls for a paper.js View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (The MIT License) | |
| Copyright (c) 2025 Andrew Greenstreet <andrew@gstreet.net> | |
| Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| import paper from "paper"; | |
| export default class ViewZoom { | |
| /** | |
| * @type {paper.Project} | |
| */ | |
| project | |
| factor = 1.1; | |
| mouseNativeStart | |
| viewCenterStart | |
| last = {x: 0, y: 0}; | |
| initialDist = 0; | |
| initialZoom = 1; | |
| /** | |
| * @type paper.View | |
| */ | |
| view | |
| /** | |
| * @type {boolean} | |
| */ | |
| isTouching = false; | |
| /** | |
| * @type {AbortController} | |
| */ | |
| abortController | |
| /** | |
| * | |
| * @param {paper.Project} project | |
| * @param {AbortSignal} signal | |
| */ | |
| constructor(project, signal) { | |
| this.project = project; | |
| const view = this.project.view; | |
| this.view = view; | |
| if (signal){ | |
| this.signal = signal | |
| } else { | |
| this.abortController = new AbortController(); | |
| this.signal = this.abortController.signal; | |
| } | |
| this.setupEvents(view); | |
| } | |
| dispose() { | |
| if (this.abortController) { | |
| this.abortController.abort(); | |
| this.abortController = null; | |
| } | |
| } | |
| setupEvents(view) { | |
| view.element.addEventListener("wheel", (event) => { | |
| const mousePosition = new paper.Point(event.offsetX, event.offsetY); | |
| this.changeZoomCentered(event.deltaY, mousePosition); | |
| },{signal:this.signal}); | |
| view.on("mousedown", ev => { | |
| if (ev.event?.touches.length > 0) { | |
| return; | |
| } | |
| if (ev.event.button === 2) { | |
| this.viewCenterStart = view.center; | |
| // Have to use native mouse offset, because ev.delta | |
| // changes as the view is scrolled. | |
| this.mouseNativeStart = new paper.Point(ev.event.offsetX, ev.event.offsetY); | |
| } | |
| this.addMouseEvents(view) | |
| },{signal:this.signal}); | |
| const isTouchDevice = window.matchMedia("(pointer: coarse)").matches; | |
| if (isTouchDevice) { | |
| this.addTouchEvents(view); | |
| } | |
| } | |
| /** | |
| * Initialize touch events for zooming and panning. | |
| * @param {paper.View} view | |
| */ | |
| addTouchEvents(view) { | |
| let isDragging = false; | |
| let lastTouch = null; | |
| let pinchLastCenter = null; | |
| let pinchLastDistance = 0; | |
| function getDistance(touches) { | |
| let dx = touches[0].pageX - touches[1].pageX; | |
| let dy = touches[0].pageY - touches[1].pageY; | |
| return Math.sqrt(dx * dx + dy * dy); | |
| } | |
| function getCenter(touches) { | |
| return new paper.Point( | |
| (touches[0].pageX + touches[1].pageX) / 2, | |
| (touches[0].pageY + touches[1].pageY) / 2 | |
| ); | |
| } | |
| view.element.addEventListener("touchstart", (e) => { | |
| if (e.touches.length === 1) { | |
| // Single finger → pan | |
| isDragging = true; | |
| lastTouch = new paper.Point(e.touches[0].pageX, e.touches[0].pageY); | |
| } else if (e.touches.length === 2) { | |
| // Two fingers → pinch zoom | |
| e.preventDefault(); | |
| pinchLastCenter = getCenter(e.touches); | |
| pinchLastDistance = getDistance(e.touches); | |
| isDragging = false; // cancel single drag | |
| } | |
| }, {signal:this.signal}); | |
| view.element.addEventListener("touchmove", (e) => { | |
| if (e.touches.length === 1 && isDragging) { | |
| // Pan | |
| e.preventDefault(); | |
| let current = new paper.Point(e.touches[0].pageX, e.touches[0].pageY); | |
| let delta = lastTouch.subtract(current); | |
| view.center = view.center.add(delta.divide(view.zoom)); // adjust for zoom | |
| lastTouch = current; | |
| } else if (e.touches.length === 2) { | |
| // Pinch zoom | |
| e.preventDefault(); | |
| let newCenter = getCenter(e.touches); | |
| let newDistance = getDistance(e.touches); | |
| // Pan by pinch center movement | |
| let delta = pinchLastCenter.subtract(newCenter); | |
| view.center = view.center.add(delta.divide(view.zoom)); | |
| // Zoom by pinch distance | |
| let zoomFactor = newDistance / pinchLastDistance; | |
| view.zoom *= zoomFactor; | |
| // Update | |
| pinchLastCenter = newCenter; | |
| pinchLastDistance = newDistance; | |
| } | |
| }, {signal:this.signal}); | |
| view.element.addEventListener("touchend", (e) => { | |
| if (e.touches.length === 0) { | |
| isDragging = false; | |
| lastTouch = null; | |
| } else if (e.touches.length < 2) { | |
| pinchLastCenter = null; | |
| pinchLastDistance = 0; | |
| } | |
| }, {signal:this.signal}); | |
| } | |
| /** | |
| * Add events to the view. | |
| * @param {paper.View} view | |
| */ | |
| addMouseEvents(view) { | |
| view.on("mousedrag", ev => { | |
| if (!this.mouseNativeStart) { | |
| return; | |
| } | |
| if (this.viewCenterStart) { | |
| const nativeDelta = new paper.Point( | |
| ev.event.offsetX - this.mouseNativeStart.x, | |
| ev.event.offsetY - this.mouseNativeStart.y | |
| ); | |
| // Move into view coordinates to subract delta, | |
| // then back into project coords. | |
| view.center = view.viewToProject( | |
| view.projectToView(this.viewCenterStart) | |
| .subtract(nativeDelta)); | |
| } | |
| }); | |
| view.on("mouseup", ev => { | |
| if (this.mouseNativeStart) { | |
| this.mouseNativeStart = null; | |
| this.viewCenterStart = null; | |
| } | |
| this.removeEvents(view) | |
| }); | |
| } | |
| removeEvents(view) { | |
| view.off("mousedrag"); | |
| view.off("mouseup"); | |
| } | |
| get zoom() { | |
| return this.view.zoom; | |
| } | |
| /** | |
| * Set zoom level. | |
| * @returns zoom level that was set, or null if it was not changed | |
| */ | |
| setZoomConstrained(zoom) { | |
| if (this._minZoom) { | |
| zoom = Math.max(zoom, this._minZoom); | |
| } | |
| if (this._maxZoom) { | |
| zoom = Math.min(zoom, this._maxZoom); | |
| } | |
| const view = this.project.view; | |
| if (zoom != view.zoom) { | |
| view.zoom = zoom; | |
| return zoom; | |
| } | |
| return null; | |
| } | |
| setZoomRange(range) { | |
| const view = this.project.view; | |
| const aSize = range.shift(); | |
| const bSize = range.shift(); | |
| const a = aSize && Math.min( | |
| view.bounds.height / aSize.height, | |
| view.bounds.width / aSize.width); | |
| const b = bSize && Math.min( | |
| view.bounds.height / bSize.height, | |
| view.bounds.width / bSize.width); | |
| const min = Math.min(a, b); | |
| if (min) { | |
| this._minZoom = min; | |
| } | |
| const max = Math.max(a, b); | |
| if (max) { | |
| this._maxZoom = max; | |
| } | |
| return [this._minZoom, this._maxZoom]; | |
| } | |
| changeZoomCentered(delta, mousePos) { | |
| if (!delta) { | |
| return; | |
| } | |
| const view = this.project.view; | |
| const oldZoom = view.zoom; | |
| const oldCenter = view.center; | |
| const viewPos = view.viewToProject(mousePos); | |
| let newZoom = delta < 0 | |
| ? view.zoom * this.factor | |
| : view.zoom / this.factor; | |
| newZoom = this.setZoomConstrained(newZoom); | |
| if (!newZoom) { | |
| return; | |
| } | |
| const zoomScale = oldZoom / newZoom; | |
| const centerAdjust = viewPos.subtract(oldCenter); | |
| const offset = viewPos.subtract(centerAdjust.multiply(zoomScale)) | |
| .subtract(oldCenter); | |
| view.center = view.center.add(offset); | |
| }; | |
| zoomTo(rect) { | |
| const view = this.project.view; | |
| view.center = rect.center; | |
| view.zoom = Math.min( | |
| view.viewSize.height / rect.height, | |
| view.viewSize.width / rect.width); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//Basic Usage
const paperScope = new paper.PaperScope();
paperScope.setup(canvas);
const paperProject = paperScope.project;
const abortController = new AbortController(); //if you want event destroyed by shutdown function
const viewZoom = new ViewZoom(paperProject, abortController.signal);
`