JS Canvas放大和缩小平移未居中

时间:2019-01-27 17:54:41

标签: javascript canvas zoom

我正在自己的画布抽屉项目中工作,只是停留在缩放功能中。在我的项目中,我要使用缩放和平移来进行缩放,因为我想将所有画布及其元素保持在中心。 绘制了一点点(不是数学天才)后,我成功绘制了以下公式供翻译过程中使用,因此,缩放后画布将保留在其视口的中间:旧的宽度和高度/ 2-新的宽度和高度(旧的宽度和高度乘以比例步长,在我的情况下为1.1)/ 2。 从逻辑上讲,这应该使其起作用。但是在尝试了几次放大和缩小之后,我可以清楚地看到画布有一点偏移,并且没有居中于视口的中间(视口是指代表画布的描边正方形)。

我将代码从项目中取出,放在了小提琴中,就在这里:

https://jsfiddle.net/s82qambx/3/

index.html

<div id="letse-canvas-container">
<canvas id="letse-canvas" width="300px" height="300px"></canvas>
<canvas id="letse-upper-canvas" width="300px" height="300px"></canvas>
</div>
<div id="buttons">
<button id="zoomin">
Zoom-in
</button>
<button id="zoomout">
Zoom-out
</button>
</div>

main.js

const canvas = {
canvas: document.getElementById('letse-canvas'),
upperCanvas: document.getElementById('letse-upper-canvas')
};

canvas.canvas.ctx = canvas.canvas.getContext('2d');
canvas.upperCanvas.ctx = canvas.upperCanvas.getContext('2d');

const CANVAS_STATE = {
    canvas: {
    zoom: 1,
width: 300,
height: 300
}
}

const Elements = [
    {
    x: 20,
    y: 20,
    width: 30,
    height: 40
  },
  {
    x:170,
    y:30,
    width: 100,
    height: 100
  }
];

const button = {
zoomin: document.getElementById('zoomin'),
zoomout: document.getElementById('zoomout')
}

button.zoomin.addEventListener('click', (e) => {
canvasZoomIn(e, canvas);
});
button.zoomout.addEventListener('click', (e) => {
canvasZoomOut(e, canvas);
});

function canvasZoomIn(e, canvas) {
const zoomData = getZoomData('in');

canvas.upperCanvas.ctx.scale(zoomData.zoomStep, zoomData.zoomStep);
canvas.upperCanvas.ctx.translate(zoomData.translateX, zoomData.translateY);
canvas.upperCanvas.ctx.clearRect(0, 0, 300, 300);

canvas.canvas.ctx.scale(zoomData.zoomStep, zoomData.zoomStep);
canvas.canvas.ctx.translate(zoomData.translateX, zoomData.translateY);
canvas.canvas.ctx.clearRect(0, 0, 300, 300);
Elements.forEach((element) => {
canvas.canvas.ctx.strokeRect(element.x, element.y, element.width, element.height);
});

CANVAS_STATE.canvas.zoom = zoomData.scale;
CANVAS_STATE.canvas.width = zoomData.docWidth;
CANVAS_STATE.canvas.height = zoomData.docHeight;

console.log(CANVAS_STATE.canvas.zoom, 'zoom');
console.log(CANVAS_STATE.canvas.width, 'width');
console.log(CANVAS_STATE.canvas.height, 'height');

canvas.canvas.ctx.strokeRect(0, 0, 300, 300);
canvas.canvas.ctx.beginPath();
canvas.canvas.ctx.moveTo(0, 150);
canvas.canvas.ctx.lineTo(300, 150);
canvas.canvas.ctx.stroke();
CANVAS_STATE.canvas.draggable = canvas.canvas.width < CANVAS_STATE.canvas.width || canvas.canvas.height < CANVAS_STATE.canvas.height;
}

function canvasZoomOut(e, canvas) {
const zoomData = getZoomData('out');

canvas.upperCanvas.ctx.scale(zoomData.zoomStep, zoomData.zoomStep);
canvas.upperCanvas.ctx.translate(zoomData.translateX, zoomData.translateY);
canvas.upperCanvas.ctx.clearRect(0, 0, canvas.canvas.width, canvas.canvas.height);

canvas.canvas.ctx.scale(zoomData.zoomStep, zoomData.zoomStep);
canvas.canvas.ctx.translate(zoomData.translateX, zoomData.translateY);
canvas.canvas.ctx.clearRect(0, 0, canvas.canvas.width, canvas.canvas.height);
Elements.forEach((element) => {
canvas.canvas.ctx.strokeRect(element.x, element.y, element.width, element.height);
});

CANVAS_STATE.canvas.zoom = zoomData.scale;
CANVAS_STATE.canvas.width = zoomData.docWidth;
CANVAS_STATE.canvas.height = zoomData.docHeight;

console.log(CANVAS_STATE.canvas.zoom, 'zoom');
console.log(CANVAS_STATE.canvas.width, 'width');
console.log(CANVAS_STATE.canvas.height, 'height');

canvas.canvas.ctx.strokeRect(0, 0, 300, 300);
canvas.canvas.ctx.beginPath();
canvas.canvas.ctx.moveTo(0, 150);
canvas.canvas.ctx.lineTo(300, 150);
canvas.canvas.ctx.stroke();
CANVAS_STATE.canvas.draggable = canvas.canvas.width < CANVAS_STATE.canvas.width || canvas.canvas.height < CANVAS_STATE.canvas.height;
}

function getZoomData(zoom) {
const zoomStep = zoom === 'in' ? 1.1 : 1 / 1.1;
const scale = CANVAS_STATE.canvas.zoom * zoomStep;
const docWidth = CANVAS_STATE.canvas.width * zoomStep;
const docHeight = CANVAS_STATE.canvas.height * zoomStep;
const translateX = CANVAS_STATE.canvas.width / 2 - docWidth / 2;
const translateY = CANVAS_STATE.canvas.height / 2 - docHeight / 2;

console.log(zoomStep);
console.log(scale, 'check');
console.log(docWidth);
console.log(docHeight);
console.log(translateX, 'check');
console.log(translateY, 'check');

return {
zoomStep,
scale,
docWidth,
docHeight,
translateX,
translateY
};
}

main.css

#letse-canvas-container {
position: relative;
float: left;
}

#letse-canvas {
border: 1px solid rgb(0, 0, 0);
/* visibility: hidden; */
}

#letse-upper-canvas {
/* position: absolute; */
/* top: 0px; */
left: 0px;
border: 1px solid;
/* visibility: hidden; */
}

有人可以提出原因吗?我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

好!因此,在网上搜索并测试了很少的选择之后,我设法得出了正确的公式。我用过:

 function getZoomData(zoom) {
    const zoomStep = zoom === 'in' ? 1.1 : 1 / 1.1;
    const oldZoom = CANVAS_STATE.canvas.zoom;
    const newZoom = oldZoom * zoomStep;
    const zoomDifference = newZoom - oldZoom;
    const docWidth = CANVAS_STATE.canvas.width * newZoom;
    const docHeight = CANVAS_STATE.canvas.height * newZoom;
    const translateX = (-(canvas.canvas.width / 2 * zoomDifference / newZoom));
    const translateY = (-(canvas.canvas.height / 2 * zoomDifference / newZoom));