javascript - html2canvas Uncaught (in promise) undefined - Stack Overflow

I have a html tag canvas.<canvas id="myCanvas"><canvas>I can draw on it successf

I have a html tag canvas.

<canvas id="myCanvas"></canvas>

I can draw on it successfully, and it looks really good, as i wanted to be. The problem is on the conversion to png.I use html2canvas for that with Canvas2Image. The html2canvas consoles logs an error: Uncaught (in promise) undefined. Canvas2Image console logs Uncaught (in promise) DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The image argument is a canvas element with a width or height of 0. I can understand the error very well, the canvas has width and height != 0. Any ideas?


html2canvas code:

    html2canvas(document.getElementById('myCanvas')).then(canvas9 => {

    var theimage9 = Canvas2Image.convertToPNG(canvas9);
    var imageData9 = $(theimage9).attr('src');


    console.log(imageData9);
  });

I have a html tag canvas.

<canvas id="myCanvas"></canvas>

I can draw on it successfully, and it looks really good, as i wanted to be. The problem is on the conversion to png.I use html2canvas for that with Canvas2Image. The html2canvas consoles logs an error: Uncaught (in promise) undefined. Canvas2Image console logs Uncaught (in promise) DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The image argument is a canvas element with a width or height of 0. I can understand the error very well, the canvas has width and height != 0. Any ideas?


html2canvas code:

    html2canvas(document.getElementById('myCanvas')).then(canvas9 => {

    var theimage9 = Canvas2Image.convertToPNG(canvas9);
    var imageData9 = $(theimage9).attr('src');


    console.log(imageData9);
  });
Share Improve this question asked Aug 9, 2018 at 12:50 CacheCache 5012 gold badges5 silver badges14 bronze badges 1
  • Did you ever get this figured out? @Cache – FabricioG Commented Oct 5, 2018 at 19:46
Add a ment  | 

2 Answers 2

Reset to default 2

Tested Chrome, Firefox ok, IE 11 needs adding 2 extra js library to support promise.

function takeSnapShot() {
	html2canvas(document.querySelector("#capture")).then(function(canvas9) {
		var theimage9 = canvas9.toDataURL("image/png");
		document.querySelector("#theimage9").src = theimage9; 
	});
}
<script src="https://cdn.jsdelivr/npm/es6-promise@4/dist/es6-promise.min.js"></script>
<script src="https://cdn.jsdelivr/npm/es6-promise@4/dist/es6-promise.auto.min.js"></script> 
<script src="https://html2canvas.hertzen./dist/html2canvas.min.js"></script>
<div id="capture" style="padding: 10px; background: #f5da55">
    <h4 style="color: #000; ">Hello world!</h4>
</div>
<input type="button" value="Capture" onclick="takeSnapShot()"/>
<img id="theimage9" border="0" />

html2canvas is used for converting html code into a canvas view. If I am understanding correctly, you just want to get an image from the canvas so html2canvas is not needed. Simply just use Canvas2Image on it's own.

Example (blue is the image, red is the canvas):

// Example canvas with circle
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(95,50,40,0,2*Math.PI);
ctx.stroke();


var image = Canvas2Image.convertToPNG(c); // Returns HTMLImageElement 

$("#canvasImg").attr('src', image.src); // Set the src of our output image to the src of the HTMLImageElement
<!-- Output -->
<img id="canvasImg" src="" style="border:2px solid blue;">

<!-- Input -->
<canvas id="myCanvas" width="200" height="100" style="border:2px solid red;"></canvas>

<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr/npm/[email protected]/canvas2image.min.js"></script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745339492a4623259.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信