【问题标题】:Resize a HTML 5 canvas and its content in pure Javascript在纯 Javascript 中调整 HTML 5 画布及其内容的大小
【发布时间】:2015-05-07 01:49:53
【问题描述】:

我想在我的 Web 应用中实现“缩放”功能,以按比例调整画布及其内容的大小(我不关心脏质量)。

这是我的代码。每次我们想要缩放时它都会重置画布以获取 Image 对象(用于使用 drawImage 方法),调整画布大小,然后使用 drawImage 方法按比例调整内容大小......

var Editor = {
    // The following variables contains informations
    // about the original canvas
    data: ..., // imageData
    width: 200,
    height: 50,

    // Zoom canvas
    zoomCanvas: function(zoom){ // zoom in percents
        var canvas = $('#canvas').get(0);
        var context = canvas.getContext();
        var data = this.data;
        var scale = zoom / 100;
        var width = this.width * scale;
        var height = this.height * scale;

        // Reset canvas to original size and imageData
        this.resizeCanvas(this.width, this.height); 
        context.scale(1, 1);
        context.putImageData(data, 0, 0);

        // Get Image from original canvas data
        var url = canvas.toDataURL();
        var img = $('<img src="' + url + '">').get(0);

        // Resize canvas, apply scale and draw Image with new proportions
        this.resizeCanvas(width, height);
        context.scale(scale, scale);
        context.drawImage(img, 0, 0);
    },

    // Resize canvas
    resizeCanvas: function(width, height){
        // NOTE : I don't know exactly how to resize it so...
        var canvas = $('#canvas').get(0);
        canvas.width = width;
        canvas.height = height;
        $(canvas).width(width).attr('width', width);
        $(canvas).height(height).attr('height', height);

        var context = canvas.getContext('2d');
        context.width = width;
        context.height = height;
    }
}

它适用于 Zoom + 但不适用于 Zoom -。

无论如何,我认为这不是我期望的最好方法,最好找到一种直接从Editor.data操作的方法,而不必每次都重置画布,或者保存Image对象. 另外,我不确定是否使用 scale 方法...

任何帮助将不胜感激

(对不起我的英语)

【问题讨论】:

    标签: javascript html canvas zooming


    【解决方案1】:

    尝试使用&lt;input type="range"&gt;transform: scale(sx[, sy])

    var canvas = document.querySelectorAll("canvas")[0]
    , scale = document.getElementById("scale");
    
    scale.addEventListener("change", function(e) {
      canvas.style.transform = "scale(" 
      + e.target.value 
      + ","      
      + e.target.value 
      + ")"
    })
    body {
      height:800px;
    }
    
    canvas {
      background:blue;
      position:relative;
      display:block;
    }
    
    #scale {
      position:relative;
      display:inline-block;
      padding:8px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <input id="scale" type="range" min="0" max="2" step="0.1" value=""/><br /><br /><br /><br /><br /><br /><br />
    <canvas width="200" height="200"></canvas><br />

    【讨论】:

    • 你应该避免使用 css 来转换画布元素,如果你尝试听 mouseEvents 等的话很快就会变得一团糟......
    猜你喜欢
    • 2011-04-02
    • 2022-01-15
    • 2014-07-23
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    相关资源
    最近更新 更多