【问题标题】:Canvas Zoom-in, Zoom-Out using mousewheel使用鼠标滚轮放大、缩小画布
【发布时间】:2018-11-14 00:49:32
【问题描述】:

我正在尝试使我的 <canvas> 可缩放(放大、缩小)。

我正在尝试使用以下代码来处理缩放功能,但是,它没有按预期工作,因为一旦我尝试放大/缩小,画布就会变空/空白。

这让我相信 mousewheel 函数正在工作,但由于它“删除”了画布绘图,因此出现了问题。

function drawZoom() {
var startScale = 1;
var scale = startScale;
var floor = $("#floorplan")[0].getContext("2d")
var width = floor.canvas.width;
var height = floor.canvas.height;
var intervalId;

var imageData = floor.getImageData(0, 0, width, height);
var canvas = $("<canvas>").attr("width", width).attr("height", height)[0];
canvas.getContext("2d").putImageData(imageData, 0, 0);

function drawContents(){
    var newWidth = width * scale;
    var newHeight = height * scale;

    floor.save();
    floor.translate(-((newWidth-width)/2), -((newHeight-height)/2));
    floor.scale(scale, scale);
    floor.clearRect(0, 0, width, height);
    floor.drawImage(canvas, 0, 0);
    floor.restore();
}

 $("#test").on('DOMMouseScroll mousewheel',function(e) {

        var e =  e || window.event; // old IE support
        var theEvent = e.originalEvent.wheelDelta || e.originalEvent.detail*-1;
        if(theEvent / 120 > 0) {
            zoomin();
        } else {
            zoomout();
        }
        if (e.preventDefault)
            e.preventDefault();
    });

    function zoomin()
    {
         scale = scale + 0.01;
         drawContents();
    }
    function zoomout()
    {
         scale = scale - 0.01;
         drawContents();
    }
}

CodePen Live Example

我在这里做错了什么?我该如何解决这个问题?

【问题讨论】:

    标签: javascript jquery html css html5-canvas


    【解决方案1】:

    我可以像这样在你的 drawSettings 中添加车轮把手:

    //Drag settings start
            function drawSettings() {
               //snip....
    
                if (canvas.addEventListener) {
                    // IE9, Chrome, Safari, Opera
                    canvas.addEventListener("mousewheel", MouseWheelHandler, false);
                    // Firefox
                    canvas.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
                }
                // IE 6/7/8
                else canvas.attachEvent("onmousewheel", MouseWheelHandler);
    

    //剩下的代码...

    需要使用 delta 的值为您的示例量身定制放大和缩小功能。添加我从这里找到的这个函数:https://www.sitepoint.com/html5-javascript-mouse-wheel/ 并根据需要更改画布大小。

    function MouseWheelHandler(e) {
    
                // cross-browser wheel delta
                var e = window.event || e; // old IE support
                var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail))); 
    
                canvas.style.width = Math.max(50, Math.min(800, canvas.width + (30 * delta))) + "px";
                canvas.style.height = Math.max(50, Math.min(800, canvas.width + (30 * delta))) + "px"; 
                return false;
            }
    

    【讨论】:

    • 它具有调整大小的功能,但放大/缩小时绘图仍然消失
    • 您必须使用从车轮接收到的增量来调整图像大小。
    • 我用我的第一个代码解决了这个问题...原来我没有更改 var 名称,它把一切都搞砸了
    猜你喜欢
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    相关资源
    最近更新 更多