[英]Canvas zoom into mouse point with a rotation
I'm using EaselJS to work on an HTML5 canvas for some pretty easy stuff. 我正在使用EaselJS在HTML5画布上处理一些非常简单的内容。 All I want to do is pan, zoom and rotate an image... I want to zoom the image around my mouse pointer and I know there's lots of answers for that question but I've got that sorted.
我要做的就是平移,缩放和旋转图像...我想围绕鼠标指针缩放图像,我知道该问题有很多答案,但是我已经将其排序了。 It's when I rotate the container it goes horribly wrong.
当我旋转容器时,它出现了严重错误。 Here's my zoom function:
这是我的缩放功能:
function zoom(delta){
var p = container.globalToLocal(stage.mouseX, stage.mouseY);
var scale = Math.max(0.1, Math.min(container.scaleX + delta, 3));
container.scaleX = container.scaleY = scale;
var p2 = container.globalToLocal(stage.mouseX, stage.mouseY);
container.x += (p2.x - p.x) * scale;
container.y += (p2.y - p.y) * scale;
}
Here's my JSFiddle to see it working well but not when the container has been rotated around its centre. 这是我的JSFiddle ,它可以正常工作,但是当容器绕其中心旋转时效果不佳。 I just can't seem to work out how I could use my transforms on the container to account for any rotation, any help and explanation would be greatly appreciated.
我似乎似乎无法弄清楚如何在容器上使用我的转换来解决任何旋转问题,我们将不胜感激任何帮助和解释。
Solution, you can see it in JS Fiddle : 解决方案,您可以在JS Fiddle中看到它:
function zoom(delta){
var p = container.globalToLocal(stage.mouseX, stage.mouseY);
var scale = Math.max(0.5, Math.min(container.scaleX + delta, 3));
container.scaleX = container.scaleY = scale;
var p2 = container.globalToLocal(stage.mouseX, stage.mouseY);
if(container.rotation == 0){
container.x += (p2.x - p.x) * scale;
container.y += (p2.y - p.y) * scale;
}else if (container.rotation == 90){
container.x -= (p2.y - p.y) * (scale);
container.y += (p2.x - p.x) * (scale);
}else if(container.rotation == 180){
container.x -= (p2.x - p.x) * scale;
container.y -= (p2.y - p.y) * scale;
}else{
container.x += (p2.y - p.y) * (scale);
container.y -= (p2.x - p.x) * (scale);
}
}
function rotate(r){
if((container.rotation+r) > 270){
container.rotation = 0;
}else{
container.rotation += r;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.