简体   繁体   English

html2canvas 更大尺寸的下载图片

[英]html2canvas bigger size of download image

I've set up html2canvas so I can edit text and then download that as image and that works great, but the size is limited to the width of its container.我已经设置了html2canvas ,因此我可以编辑文本,然后将其下载为图像,效果很好,但大小仅限于其容器的宽度。

If I set up canvas width and height render becomes that size but it's black.如果我设置 canvas 宽度和高度渲染变为那个大小,但它是黑色的。 What am I doing wrong?我究竟做错了什么?

Here is the JSfiddle with everything working but if you add width and height (currently commented), rendered pictured will get black.这是JSfiddle ,一切正常,但如果您添加宽度和高度(当前已评论),渲染的图片将变黑。

JavaScript JavaScript

html2canvas(document.getElementById("imagewrap"), {
  onrendered: function(canvas) {
    canvas.className = "html2canvas";
    // canvas.width = "1080";
    // canvas.height = "1920";
    document.getElementById("canvasWrapper").appendChild(canvas);
    var image = canvas.toDataURL("image/png");
    document.getElementById("downloadLink").href = image;
  },
  useCORS: true
});

After creating a new <canvas> element and setting the width and height attributes, the image is drawn on the canvas using the drawImage() method [1] .创建新的<canvas>元素并设置widthheight属性后,使用drawImage()方法[1]在 canvas 上绘制图像。

html2canvas(document.getElementById("imagewrap"), {
  onrendered: function(canvas) {
    var  _canvas = document.createElement("canvas");
    _canvas.setAttribute('width', 1080);
    _canvas.setAttribute('height', 1920);
    var ctx = _canvas.getContext('2d');
    ctx.drawImage(canvas, 0, 0, canvas.width, canvas.height, 0, 0, 1080, 1920);
    var dataURL = _canvas.toDataURL();  
    document.getElementById("canvasWrapper").appendChild(_canvas);
    var image = _canvas.toDataURL("image/png");
    document.getElementById("downloadLink").href = image;
  },
  useCORS: true
});

Copy the JavaScript code below without disturbing your current work on JSFiddle and click the Download button to download the image.复制下面的 JavaScript 代码,不要干扰您当前在 JSFiddle 上的工作,然后单击下载按钮下载图像。 Then review the properties and content of the downloaded image:然后查看下载图像的属性和内容:

在此处输入图像描述


1 - Html2Canvas Resize 1 - Html2Canvas 调整大小

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

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