optimize: ♻️ 新增画布缩放注释

This commit is contained in:
Bunny 2024-07-24 23:49:26 +08:00
parent 4e3b595694
commit d7434d70e7
1 changed files with 7 additions and 3 deletions

View File

@ -18,6 +18,8 @@ const zoomed = () => {
// //
stage.value!.on('wheel', e => { stage.value!.on('wheel', e => {
if (!stage.value) return; if (!stage.value) return;
if (!e.evt.ctrlKey) return;
e.evt.preventDefault();
// XY // XY
const oldScaleX = stage.value!.scaleX(); const oldScaleX = stage.value!.scaleX();
@ -25,6 +27,7 @@ const zoomed = () => {
// //
const pointer = stage.value!.getPointerPosition(); const pointer = stage.value!.getPointerPosition();
//
const mousePointTo = { const mousePointTo = {
x: (pointer!.x - stage.value!.x()) / oldScaleX, x: (pointer!.x - stage.value!.x()) / oldScaleX,
y: (pointer!.y - stage.value!.y()) / oldScaleY, y: (pointer!.y - stage.value!.y()) / oldScaleY,
@ -32,14 +35,15 @@ const zoomed = () => {
// //
let direction = e.evt.deltaY > 0 ? -1 : 1; let direction = e.evt.deltaY > 0 ? -1 : 1;
if (e.evt.ctrlKey) direction = -direction; // direction = -direction;
// X // X
const newScaleX = direction > 0 ? oldScaleX * SCALE_BY : oldScaleX / SCALE_BY; const newScaleX = direction > 0 ? oldScaleX * SCALE_BY : oldScaleX / SCALE_BY;
// Y // Y
const newScaleY = direction > 0 ? oldScaleY * SCALE_BY : oldScaleY / SCALE_BY; const newScaleY = direction > 0 ? oldScaleY * SCALE_BY : oldScaleY / SCALE_BY;
stage.value!.scale({ x: newScaleX, y: newScaleY }); stage.value!.scale({ x: newScaleX, y: newScaleY });
//
const newPos = { const newPos = {
x: pointer!.x - mousePointTo.x * newScaleX, x: pointer!.x - mousePointTo.x * newScaleX,
y: pointer!.y - mousePointTo.y * newScaleY, y: pointer!.y - mousePointTo.y * newScaleY,