当我将图像添加到画布(drawImage)时,类型的问题,当我用其他画布方法绘制绘图时,我没有任何问题。
var ready;
ready = function() {
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var imageObj = new Image();
imageObj.src = 'http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg';
imageObj.onload = function() {
context.drawImage(imageObj, 69, 50);
};
var pngUrl = canvas.toDataURL();
console.log(pngUrl);
$('#client_avatar').val(pngUrl);
};
答案 0 :(得分:0)
在设置源码之前,你必须定义onload否则它将无法调用onload。
$(document).ready(function() {
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var imageObj = new Image();
imageObj.onload = function() {
context.drawImage(imageObj, 69, 50);
};
imageObj.src = 'http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg';
var pngUrl = canvas.toDataURL();
console.log(pngUrl);
// $('#client_avatar').val(pngUrl);
$("#client_avatar").append("<img src='"+ pngUrl +"'>")
});
这是工作jsbin link
答案 1 :(得分:0)
$(document).ready(function() {
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var imageObj = new Image();
imageObj.onload = function() {
context.drawImage(imageObj, 0, 0, 200, 200);
var pngUrl = canvas.toDataURL();
console.log(pngUrl);
$('#client_avatar').attr('src', pngUrl);
};
imageObj.crossOrigin = 'anonymous'; //not necessary, if image hosted on same server
imageObj.src = 'https://i.imgur.com/Q6aZlme.jpg';
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="canvas" width="200" height="200"></canvas>
<img id="client_avatar">
没有给出任何解释的道歉
ʜᴀᴠᴇꜰᴜɴ:)