Skip to content

Commit

Permalink
fix: fix setZoomByViewportPoint (#1727)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarebecca authored Jul 9, 2024
1 parent eef62b9 commit 6363252
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions __tests__/demos/camera/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { setZoomByViewportPoint } from './set-zoom-by-viewport-point';
32 changes: 32 additions & 0 deletions __tests__/demos/camera/set-zoom-by-viewport-point.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Circle } from '../../../packages/g';
import type { Camera, FederatedWheelEvent } from '../../../packages/g';

export async function setZoomByViewportPoint(context) {
const { canvas } = context;
await canvas.ready;

const circle = new Circle({
style: {
cx: 100,
cy: 100,
r: 50,
fill: 'red',
},
});

canvas.appendChild(circle);

const camera: Camera = canvas.getCamera();

canvas.addEventListener('wheel', (event: FederatedWheelEvent) => {
const {
viewport: { x, y },
deltaY,
} = event;

const zoom = camera.getZoom();
const ratio = deltaY > 0 ? 0.9 : 1.1;

camera.setZoomByViewportPoint(zoom * ratio, [x, y]);
});
}
2 changes: 2 additions & 0 deletions __tests__/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as lottie from './demos/lottie';
import * as perf from './demos/perf';
import * as bugfix from './demos/bugfix';
import * as event from './demos/event';
import * as camera from './demos/camera';

const tests = {
...createSpecRender(namespace(basic2d, '2d')),
Expand All @@ -29,6 +30,7 @@ const tests = {
...createSpecRender(namespace(bugfix, 'bugfix')),
...createSpecRender(namespace(perf, 'perf')),
...createSpecRender(namespace(event, 'event')),
...createSpecRender(namespace(camera, 'camera')),
};

const renderers = {
Expand Down
6 changes: 5 additions & 1 deletion packages/g-lite/src/camera/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,11 @@ export class Camera implements ICamera {
const dx = vec3.dot(dvec, this.right) / vec3.length(this.right);
const dy = vec3.dot(dvec, this.up) / vec3.length(this.up);

this.pan(-dx, -dy);
const [px, py] = this.getPosition();
const [fx, fy] = this.getFocalPoint();

this.setPosition(px - dx, py - dy);
this.setFocalPoint(fx - dx, fy - dy);

return this;
}
Expand Down

0 comments on commit 6363252

Please sign in to comment.