如何在窗口调整大小时调整HTML5 Canvas图像的大小

时间:2014-12-10 12:49:04

标签: javascript html5 canvas google-chrome-app

我正在设计一个谷歌Chrome应用程序,它有一个用于显示图像的html5画布。在通过ctx.drawImage(...)显示图像之前,我确保保留图像的宽高比等。当调整窗口大小时,我添加了一个事件监听器和一个函数调用来调整画布和图像的大小,这是相关的代码片段:

var ctx = document.getElementById('canvas').getContext('2d');

window.onload = function() {
  ctx.canvas.width  = window.innerWidth-10;
  ctx.canvas.height = window.innerHeight-30;
  window.addEventListener('resize', resizeCanvas);
};

function resizeCanvas(){
  var imgData = ctx.getImageData(0,0, ctx.canvas.width, ctx.canvas.height);
  ctx.canvas.width  = window.innerWidth-10;
  ctx.canvas.height = window.innerHeight-85;
  ctx.putImageData(imgData, 0, 0); // Put the image data back onto the canvas
}

然而这不起作用。看起来画布正确调整大小(因为我在其上添加了一个边框,我可以看到它调整),但是putImageData没有更新图像?有关为什么会发生这种情况的任何想法?

更新:根据以下建议: 我更改了调整大小功能,尝试重新缩放画布 - 但是现在一旦窗口调整大小,图像就不会显示了

function resizeCanvas(){
    //var imgData = ctx.getImageData(0,0, ctx.canvas.width, ctx.canvas.height);

  ctx.scale(ctx.canvas.width/window.innerWidth,ctx.canvas.height/window.innerHeight);
  ctx.canvas.width  = window.innerWidth-10;
  ctx.canvas.height = window.innerHeight-85;
  //ctx.putImageData(imgData, 0, 0); // DO I NEED THIS NOW?
}

谢谢!

2 个答案:

答案 0 :(得分:1)

我不确定它是否是最优雅的方式,但我现在所做的是完全重绘原始图像,即:

var ctx = document.getElementById('canvas').getContext('2d');
var img = new Image;

window.onload = function() {
  ctx.canvas.width  = window.innerWidth-10;
  ctx.canvas.height = window.innerHeight-30;
  window.addEventListener('resize', resizeCanvas);
};

function drawMainStage(img){

  var h             = img.height;
  var w             = img.width;
  var image_ratio   = h/w;
  var canvas_ratio  = Math.round(ctx.canvas.height/ctx.canvas.width);

  if (ctx.canvas.width >= ctx.canvas.height){
    // Landscape canvas, scale using height of canvas
    if (h > ctx.canvas.height){

      // Image larger in at least one dimension compared to the canvas
      var width_scaled  = Math.round((ctx.canvas.height/h)*w);
      var height_scaled = ctx.canvas.height;

      if ( w > h ){
        if ( width_scaled > ctx.canvas.width ){
          height_scaled = Math.round((ctx.canvas.width/w)*h);
          width_scaled  = ctx.canvas.width;
        }
      }
      var x1 = ctx.canvas.width/2 - width_scaled/2;
      var y1 = ctx.canvas.height/2 - height_scaled/2;
      ctx.drawImage(img, 0,0, w,h, x1,y1, width_scaled, height_scaled);
    }
    else
    {
        // Images smaller than the canvas
        var x1 = Math.round(ctx.canvas.width/2  - w/2);
        var y1 = Math.round(ctx.canvas.height/2 - h/2);
        ctx.drawImage(img, x1,y1);
    }
 }
 // else {
 //TBD}

 function resizeCanvas(){
     ctx.canvas.width  = window.innerWidth-10;
     ctx.canvas.height = window.innerHeight-85;
     drawMainStage(img);  
 }

答案 1 :(得分:0)

您可以使用另一个画布存储原始图像,并使用它在新尺寸的画布上重绘。您还可以使用图像元素(但这需要重新加载图像)。

var canvasImageName = "//www.somedomain.com/someimage.png";
var original_Image, canvas_Resize, ctx_Resize mycanvas, myctx;

$(document).ready(function()
{
     window.onresize = CanvasSizeChanged;

     original_Image = new Image;
     original_Image.src = canvasImageName;

     // canvas for image
     mycanvas = document.getElementById('mycanvas');
     myctx = canvas_Real.getContext("2d");

     // create canvas to be used for resizing
     canvas_Resize = document.createElement('canvas');
     ctx_Resize = canvas_Resize.getContext('2d');

     original_Image.onload=function()
     {
         // draw image on canvas
         myctx.drawImage(this, 0, 0);

         // also draw on background canvas
         ctx_Resize.drawImage(this, 0, 0);
     }
});

function CanvasSizeChanged()
{
    var w = mycanvas.parentNode.clientWidth;  // find parent dimensions

    var aspect = mycanvas.height/mycanvas.width;

    mycanvas.width = w;
    mycanvas.height = aspect*w;

    // redraw scaled image using your backed up canvas image
    myctx.drawImage(canvas_Resize, 0, 0, canvas_Resize.width, canvas_Resize.height, 
                                   0, 0, mycanvas.width, mycanvas.height); 
}