question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Zooming into a specific canvas point

See original GitHub issue

Hi,

I’m trying to setup zooming into a specific point on the paper. On mouse wheel event, when I scale using event provided coordinates:

paper.scale(scale, scale, x, y);

graph scales off target point.

Is this achievable using paper.prototype.scale() method, or should I be using a different approach?

JointJS@2.2.1

Thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
TaiAivarascommented, Jan 16, 2019

Doesn’t seem like an ideal solution, since this translates view-port to coordinate origin.

To anyone else wondering, I got my desired behavior like this:

	handleCellMouseWheel = (cellView, e, x, y, delta) =>
		this.handleCanvasMouseWheel(e, x, y, delta);

	handleCanvasMouseWheel = (e, x, y, delta) => {
		e.preventDefault();

		const oldScale = this.paper.scale().sx;
		const newScale = oldScale + delta * .1;

		this.scaleToPoint(newScale, x, y);
	};

	scaleToPoint = (nextScale, x, y) => {
		if (nextScale >= MIN_SCALE && nextScale <= MAX_SCALE) {
			const currentScale = this.paper.scale().sx;

			const beta = currentScale / nextScale;

			const ax = x - (x * beta);
			const ay = y - (y * beta);

			const translate = this.paper.translate();

			const nextTx = translate.tx - ax * nextScale;
			const nextTy = translate.ty - ay * nextScale;

			this.paper.translate(nextTx, nextTy);

			const ctm = this.paper.matrix();

			ctm.a = nextScale;
			ctm.d = nextScale;

			this.paper.matrix(ctm);
		}
	};

I’ll be closing the issue.

2reactions
vtalascommented, Jan 16, 2019

Note that joint.ui.PaperScroller has the ready-made solution for this:

paper.on('blank:mousewheel', function(evt, x, y, delta) {
    paperScroller.zoom(delta * 0.2, { min: 0.2, max: 5, grid: 0.2, ox: x, oy: y });
});

https://resources.jointjs.com/docs/rappid/v2.4/ui.html#ui.PaperScroller

Read more comments on GitHub >

github_iconTop Results From Across the Web

Zoom in on a point (using scale and translate) - Stack Overflow
The zoom point is simply the point in the old zoom and the new zoom that you want to remain the same. Which...
Read more >
How to zoom in on a point using scale and translate
You are given a zoom in to particular image by using scale and translate ... If you scale a canvas, all future drawings...
Read more >
Zoom in and out on the canvas using the Zoom tool
To zoom out, hold Alt and click the area or press Ctrl + 0 (Windows), hold Opt and click the area or press...
Read more >
Zooming via HTML5 Canvas Context - Phrogz.net
Showing how to use transform methods on the HTML5 Canvas Context to selectively zoom in and out. Drag to pan. Click to zoom...
Read more >
Creating a Zoom UI - Steve Ruiz
A zoom UI has two coordinate systems: screen coordinates and canvas coordinates. A certain point on the screen will always refer to a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found