【问题标题】:HTML5 canvas zoom where mouse coordinatesHTML5画布缩放鼠标坐标
【发布时间】:2014-08-27 14:01:42
【问题描述】:

如何用鼠标坐标缩放背景图像。

鼠标事件只在中心缩放mousewheel

me code: http://jsfiddle.net/gamealchemist/74MCQ/4/

带有 mousedownmousemove 事件的绘图

我需要缩放鼠标在图像上的位置,例如谷歌地图。

【问题讨论】:

    标签: html canvas


    【解决方案1】:

    注释代码和演示:http://jsfiddle.net/m1erickson/asT8x/

    // clear the canvas
    
    ctx.clearRect(0,0,canvas.width,canvas.height);
    
    // save the context state
    
    ctx.save();
    
    // translate to the coordinate point you wish to zoom in on
    
    ctx.translate(mouseX,mouseY);
    
    // scale the canvas to the desired zoom level
    
    ctx.scale(scale,scale);
    
    // draw the image with an offset of -mouseX,-mouseY
    // (offset to center image at the selected zoom point);
    
    ctx.drawImage(img,-mouseX,-mouseY);
    
    // restore the context to its untranslated/unrotated state
    
    ctx.restore();
    

    【讨论】:

    • Okey 非常漂亮,但如果我想缩放和改变位置,这个例子只能开始改变位置
    猜你喜欢
    • 2016-01-01
    • 2021-11-18
    • 2015-03-13
    • 2014-09-13
    • 2011-07-08
    • 2013-01-02
    • 1970-01-01
    • 2014-03-10
    相关资源
    最近更新 更多