简体   繁体   English

使用toDataURL在画布上绘制图像

[英]Drawing image on a canvas with toDataURL

I have toDataURL which I want drawn on a new canvas, which is 300 by 300 in size. 我有想要在新画布上绘制的数据,这个尺寸是300乘300。 The image doesn't stretch over the full space provided. 图像不会延伸到提供的整个空间。 I want it to be stretched over the canvas fully. 我希望它完全在画布上拉伸。 Even appending to a div is okay which I tried but didn't work out, it also didn't fully occupy the space.The orginal image is anyway less than 300 and 300.Also when I remove the alert code doesn't function at all. 即使附加到div也没关系,我试过但没有工作,它也没有完全占据空间。原始图像无论如何不到300和300.当我删除警报代码不起作用所有。

   var c4 =  document.getElementById("area_c4");
   var ctx4 = c4.getContext("2d");

   var dataURL = c2.toDataURL();

   var myImg = new Image;

   myImg.src = dataURL;

   myImg.width = c4.width; // c4.width is 300px
   myImg.height = c4.height; //c4.height is 300px
   alert(c4.width);  // when I remove this alert code doesn't work
   ctx4.drawImage(myImg,0, 0 ,c4.width,c4.height); // the image doesnt strtch over 300px 300px region. It is displayed in its original size

To resize the image you will have to use the following version of CanvasRenderingContext2D.drawImage() 要调整图像大小,您必须使用以下版本的CanvasRenderingContext2D.drawImage()

void ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)

  • image : An element to draw into the context. image :绘制到上下文中的元素。
  • sx : The x-axis coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context. sx :要image到目标上下文的源image的子矩形左上角的x轴坐标。
  • sy : The y-axis coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context. sy :要image到目标上下文的源image的子矩形左上角的y轴坐标。
  • sWidth : The width of the sub-rectangle of the source image to draw into the destination context. sWidth :要绘制到目标上下文中的源image的子矩形的宽度。 If not specified, the entire rectangle from the coordinates specified by sx and sy to the bottom-right corner of the image is used. 如果未指定,则使用从sxsy指定的坐标到图像右下角的整个矩形。
  • sHeight : The height of the sub-rectangle of the source image to draw into the destination context. sHeight :要绘制到目标上下文中的源image的子矩形的高度。
  • dx : The x-axis coordinate in the destination canvas at which to place the top-left corner of the source image . dx :目标画布中放置源image左上角的x轴坐标。
  • dy : The y-axis coordinate in the destination canvas at which to place the top-left corner of the source image . dy :目标画布中的y轴坐标,用于放置源image左上角。
  • dWidth : The width to draw the image in the destination canvas. dWidth :在目标画布中绘制image的宽度。 This allows scaling of the drawn image. 这允许缩放绘制的图像。 If not specified, the image is not scaled in width when drawn. 如果未指定,则在绘制时图像的宽度不会缩放。
  • dHeight : The height to draw the image in the destination canvas. dHeight :在目标画布中绘制image的高度。 This allows scaling of the drawn image. 这允许缩放绘制的图像。 If not specified, the image is not scaled in height when drawn. 如果未指定,则在绘制时图像的高度不会缩放。

Also you will have to wait for the image to process the data uri before you can draw it onto the canvas. 此外,您必须等待图像处理数据uri,然后才能将其绘制到画布上。 For this you can use the load event: 为此,您可以使用load事件:

myImg.onload = function() {
    // here you can draw the image on the canvas
}

The input image in the example is 10x10 pixels wide and will be stretched to 300x300 pixels. 示例中的输入图像宽度为10x10像素,将拉伸至300x300像素。

 const c4 = document.getElementById("area_c4"); var ctx4 = c4.getContext("2d"); var dataURL = "data:image/bmp;base64,Qk26AQAAAAAAAHoAAABsAAAACgAAAAoAAAABABgAAAAAAEABAAATCwAAEwsAAAAAAAAAAAAAQkdScwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAABUVPxUVPxUVPxUVPxUVPz/AAD/AAD/AAD/AAD/AAAAAFRU/FRU/FRU/FRU/FRU/P8AAP8AAP8AAP8AAP8AAAAAVFT8VFT8VFT8VFT8VFT8/wAA/wAA/wAA/wAA/wAAAABUVPxUVPxUVPxUVPxUVPz/AAD/AAD/AAD/AAD/AAAAAFRU/FRU/FRU/FRU/FRU/P8AAP8AAP8AAP8AAP8AAAAAVFT8VFT8VFT8VFT8VFT8/wAA/wAA/wAA/wAA/wAAAABUVPxUVPxUVPxUVPxUVPz/AAD/AAD/AAD/AAD/AAAAAFRU/FRU/FRU/FRU/FRU/P8AAP8AAP8AAP8AAP8AAAAAVFT8VFT8VFT8VFT8VFT8/wAA/wAA/wAA/wAA/wAAAABUVPxUVPxUVPxUVPxUVPz/AAD/AAD/AAD/AAD/AAAAAA=="; var myImg = new Image(); // wait until the data uri has been processed myImg.onload = function() { // draw the image and scale it to the size of the canvas ctx4.drawImage(this, 0, 0, this.width, this.height, /* source */ 0, 0, c4.width, c4.height); /* destination */ } myImg.src = dataURL; /* not necessary for the solution, just to show the size of the input image */ document.getElementById("showcase").src = dataURL; 
 canvas { width: 300px; height: 300px; } 
 <p> <!-- not necessary for the solution, just to show the size of the input image --> Input:<br /><img id="showcase" /> </p> <p> Output:<br /><canvas id="area_c4" width="300px" height="300px"></canvas> </p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM