小编Max*_* R.的帖子

在纯 Javascript 中调整 HTML 5 画布及其内容的大小

我想在我的 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); …
Run Code Online (Sandbox Code Playgroud)

html javascript canvas zooming

5
推荐指数
1
解决办法
4656
查看次数

标签 统计

canvas ×1

html ×1

javascript ×1

zooming ×1