我正在尝试放大给定的点,老实说我无法理解我做错了什么。
首先,我得到了鼠标指针,并使用了画布变换矩阵来获取画布上下文中的指针(https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-gettransform)。
const domPoint = new window.DOMPoint(event.offsetX, event.offsetY);
const currMatrix = this.canvasManager.ctx.getTransform();
const canvasPoint = domPoint.matrixTransform(currMatrix);
然后,我使用getTransform(DOMMatrix对象)中返回的矩阵进行平移,缩放和平移。
const m = currMatrix
.translateSelf(canvasPoint.x, canvasPoint.y)
.scaleSelf(scale, scale)
.translateSelf(-canvasPoint.x, -canvasPoint.y);
最后,我使用最后一个矩阵设置转换。
this.ctx.setTransform(this.zoom.matrix)
我使用的方法基于一个stack overflow answer。
这是我的代码
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var currMatrix = ctx.getTransform();
var plusBtn = document.getElementById("plus");
var minusBtn = document.getElementById("minus");
var infoP = document.getElementById("info");
var zoom;
var scale = 1;
const scaleFactor = 0.1;
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.setTransform(currMatrix)
ctx.fillStyle = "blue";
ctx.fillRect(50, 50, 100, 100);
}
draw()
function getDomPoint(event) {
const offSetCanvasLeft = canvas.getBoundingClientRect().left;
const offSetCanvasTop = canvas.getBoundingClientRect().top;
return {
x: event.pageX - offSetCanvasLeft,
y: event.pageY - offSetCanvasTop,
}
}
function domToCanvasPoint(point) {
const domPoint = new window.DOMPoint(point.x, point.y);
return domPoint.matrixTransform(currMatrix);
}
function updateScale() {
scale = zoom === 'in' ? scale + scaleFactor : scale - scaleFactor ;
setDebugInfo(scale)
}
function zoomMatrixIntoPoint(point) {
currMatrix = currMatrix
.translateSelf(point.x, point.y)
.scaleSelf(scale, scale)
.translateSelf(-point.x, -point.y);
}
function setDebugInfo(msg) {
infoP.innerHTML = msg;
}
canvas.addEventListener('mousedown', function(event) {
const domPoint = getDomPoint(event);
const canvasPoint = domToCanvasPoint(domPoint);
updateScale();
zoomMatrixIntoPoint(canvasPoint);
draw()
}, false);
plusBtn.addEventListener('click', function(event) {
zoom = 'in'
setDebugInfo(`ZOOM IN with scale ${scale}`)
}, false);
minusBtn.addEventListener('click', function(event) {
zoom = 'out'
setDebugInfo(`ZOOM OUT with scale ${scale}`)
}, false);
<canvas id="myCanvas" width="300" height="300" style="border:1px solid black"></canvas>
<button id="plus">+</button>
<button id="minus">-</button>
<p id="info">info here!</p>
将不胜感激。谢谢。
最佳答案
您的问题是一个简单的逻辑错误。
您保留curMatrix
并每次都对其进行更新。
这意味着您确实传递给translateSelf
和scaleSelf
的所有值都相对于先前的值。
但是,当您执行scale -= scaleFactor
或scale += scaleFactor
时,scale
是绝对比例值。
因此,以后在scaleSelf
中使用它时,您会迅速设置一个巨大的绝对比例值,直到它变得小于1
时,它才会下降。
(1.1 * 1.2 * 1.3 * 1.4 * 1.5 * 1.6) => real scale is 5.8
和
(1.1 * 1.2 * 1.3 * 1.2 * 1.1 * 1) => real scale is 2.3
^-- click zoom-out
因此,您需要修复的只是设置此
scale
值的行:不必递增此值,而应始终基于1
进行设置。scale = zoom === 'in' ? 1 + scaleFactor : 1 - scaleFactor ;
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var currMatrix = ctx.getTransform();
var plusBtn = document.getElementById("plus");
var minusBtn = document.getElementById("minus");
var infoP = document.getElementById("info");
var zoom;
var scale = 1;
const scaleFactor = 0.1;
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.setTransform(currMatrix)
ctx.fillStyle = "blue";
ctx.fillRect(50, 50, 100, 100);
}
draw()
function getDomPoint(event) {
const offSetCanvasLeft = canvas.getBoundingClientRect().left;
const offSetCanvasTop = canvas.getBoundingClientRect().top;
return {
x: event.pageX - offSetCanvasLeft,
y: event.pageY - offSetCanvasTop,
}
}
function domToCanvasPoint(point) {
const domPoint = new window.DOMPoint(point.x, point.y);
return domPoint.matrixTransform(currMatrix);
}
function updateScale() {
scale = zoom === 'in' ? 1 + scaleFactor : 1 - scaleFactor;
setDebugInfo(scale)
}
function zoomMatrixIntoPoint(point) {
currMatrix
.translateSelf(point.x, point.y)
.scaleSelf(scale, scale)
.translateSelf(-point.x, -point.y);
}
function setDebugInfo(msg) {
infoP.innerHTML = msg;
}
canvas.addEventListener('mousedown', function(event) {
const domPoint = getDomPoint(event);
const canvasPoint = domToCanvasPoint(domPoint);
updateScale();
zoomMatrixIntoPoint(canvasPoint);
draw()
}, false);
plusBtn.addEventListener('click', function(event) {
zoom = 'in'
setDebugInfo(`ZOOM IN with scale ${scale}`)
}, false);
minusBtn.addEventListener('click', function(event) {
zoom = 'out'
setDebugInfo(`ZOOM OUT with scale ${scale}`)
}, false);
<canvas id="myCanvas" width="300" height="300" style="border:1px solid black"></canvas>
<button id="plus">+</button>
<button id="minus">-</button>
<p id="info">info here!</p>
另请注意,DOMMatrix#scale()接受可选的origin参数,这将使您避免两个转换调用:
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var currMatrix = ctx.getTransform();
var plusBtn = document.getElementById("plus");
var minusBtn = document.getElementById("minus");
var infoP = document.getElementById("info");
var zoom;
var scale = 1;
const scaleFactor = 0.1;
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.setTransform(currMatrix)
ctx.fillStyle = "blue";
ctx.fillRect(50, 50, 100, 100);
}
draw()
function getDomPoint(event) {
const offSetCanvasLeft = canvas.getBoundingClientRect().left;
const offSetCanvasTop = canvas.getBoundingClientRect().top;
return {
x: event.pageX - offSetCanvasLeft,
y: event.pageY - offSetCanvasTop,
}
}
function domToCanvasPoint(point) {
const domPoint = new window.DOMPoint(point.x, point.y);
return domPoint.matrixTransform(currMatrix);
}
function updateScale() {
scale = zoom === 'in' ? 1 + scaleFactor : 1 - scaleFactor;
setDebugInfo(scale)
}
function zoomMatrixIntoPoint(point) {
currMatrix
// scaleSelf(scaleX, scaleY, scaleZ, originX, originY, originZ)
.scaleSelf(scale, scale, 1, point.x, point.y, 0)
}
function setDebugInfo(msg) {
infoP.innerHTML = msg;
}
canvas.addEventListener('mousedown', function(event) {
const domPoint = getDomPoint(event);
const canvasPoint = domToCanvasPoint(domPoint);
updateScale();
zoomMatrixIntoPoint(canvasPoint);
draw()
}, false);
plusBtn.addEventListener('click', function(event) {
zoom = 'in'
setDebugInfo(`ZOOM IN with scale ${scale}`)
}, false);
minusBtn.addEventListener('click', function(event) {
zoom = 'out'
setDebugInfo(`ZOOM OUT with scale ${scale}`)
}, false);
<canvas id="myCanvas" width="300" height="300" style="border:1px solid black"></canvas>
<button id="plus">+</button>
<button id="minus">-</button>
<p id="info">info here!</p>
而且,如果您需要所有值都是绝对的(即也进行转换),那么每次都只需创建一个新的DOMMatrix,即可保持原始比例增量:
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var currMatrix = ctx.getTransform();
var plusBtn = document.getElementById("plus");
var minusBtn = document.getElementById("minus");
var infoP = document.getElementById("info");
var zoom;
var scale = 1;
const scaleFactor = 0.1;
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.setTransform(currMatrix)
ctx.fillStyle = "blue";
ctx.fillRect(50, 50, 100, 100);
}
draw()
function getDomPoint(event) {
const offSetCanvasLeft = canvas.getBoundingClientRect().left;
const offSetCanvasTop = canvas.getBoundingClientRect().top;
return {
x: event.pageX - offSetCanvasLeft,
y: event.pageY - offSetCanvasTop,
}
}
function domToCanvasPoint(point) {
const domPoint = new window.DOMPoint(point.x, point.y);
return domPoint.matrixTransform(currMatrix);
}
function updateScale() {
scale = zoom === 'in' ? scale + scaleFactor : scale - scaleFactor;
setDebugInfo(scale)
}
function zoomMatrixIntoPoint(point) {
// create a new DOMMatrix
currMatrix = new DOMMatrix()
.scaleSelf(scale, scale, 1, point.x, point.y, 0)
}
function setDebugInfo(msg) {
infoP.innerHTML = msg;
}
canvas.addEventListener('mousedown', function(event) {
const domPoint = getDomPoint(event);
const canvasPoint = domToCanvasPoint(domPoint);
updateScale();
zoomMatrixIntoPoint(canvasPoint);
draw()
}, false);
plusBtn.addEventListener('click', function(event) {
zoom = 'in'
setDebugInfo(`ZOOM IN with scale ${scale}`)
}, false);
minusBtn.addEventListener('click', function(event) {
zoom = 'out'
setDebugInfo(`ZOOM OUT with scale ${scale}`)
}, false);
<canvas id="myCanvas" width="300" height="300" style="border:1px solid black"></canvas>
<button id="plus">+</button>
<button id="minus">-</button>
<p id="info">info here!</p>