EaselJS - broken panning on zoomed image

故事扮演 提交于 2020-05-15 05:58:22

问题


I'm having trouble fixing panning in this example - it works fine unless you move the zoomed image and then zoom again(offset is set to default value and the view jumps to initial position - http://jsfiddle.net/p2Qzg/). Any ideas on how to fix that? I've been trying to solve that for three days now, without any good results.

    var canvas= document.getElementById("myCanvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;   
var stage = new createjs.Stage("myCanvas");

function addCircle(r,x,y){
    var g=new createjs.Graphics().beginFill("#ff0000").drawCircle(0,0,r);
    var s=new createjs.Shape(g)
    s.x=x;
    s.y=y;
    stage.addChild(s);
    stage.update();
}

addCircle(10,200,100);
addCircle(5,canvas.width/2,canvas.height/2);
addCircle(3,400,400);

canvas.addEventListener("mousewheel", MouseWheelHandler, false);
canvas.addEventListener("DOMMouseScroll", MouseWheelHandler, false);

var zoom;

function MouseWheelHandler(e) {
    if(Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)))>0)
        zoom=1.1;
    else
        zoom=1/1.1;
    stage.regX += stage.mouseX - stage.regX;
    stage.regY += stage.mouseY - stage.regY;
    stage.x=stage.mouseX;
    stage.y=stage.mouseY;   
    stage.scaleX=stage.scaleY*=zoom;

    stage.update();

}


stage.addEventListener("stagemousedown", function(e) {
    var offset={x:stage.x-e.stageX,y:stage.y-e.stageY};
    stage.addEventListener("stagemousemove",function(ev) {
        stage.x = ev.stageX+offset.x;
        stage.y = ev.stageY+offset.y;
        stage.update();
    });
    stage.addEventListener("stagemouseup", function(){
        stage.removeAllEventListeners("stagemousemove");
    });
}); 

回答1:


Kind of old but since it still need an anwser:

Great json fiddle though. I used it in my work an fixed the bug.

Just replace:

  stage.regX += stage.mouseX - stage.regX;
  stage.regY += stage.mouseY - stage.regY;

With these code lines :

var local = stage.globalToLocal(stage.mouseX, stage.mouseY);
stage.regX=local.x;
stage.regY=local.y;

Works like a charm. I forked of your jsfiddle and changed it accordingly. http://jsfiddle.net/kz0dL78k/



来源:https://stackoverflow.com/questions/25227975/easeljs-broken-panning-on-zoomed-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!