canvas.toDataURL() returns blank image. Because it executes before window.onload
window.onload = function() {
var canvas = document.getElementById('myCanvas');
var video = document.getElementById('video');
var context = canvas.getContext('2d');
context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
}
var canvas = document.getElementById('myCanvas');
var photo = canvas.toDataURL('image/png');
console.log(photo);
$.ajax({
method: 'POST',
url: 'script.php',
data: {
photo: photo
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<canvas id="myCanvas" width="578" height="200"></canvas>
<video id="video" src="http://www.w3schools.com/html/mov_bbb.mp4"></video>
draw the image perfectly in windows onload only the problem is dataurl returns blank because it executes first. How to solve this