如何在html5画布上将多个图像组合在一起,然后将它们显示为单个图像?
请帮忙。
答案 0 :(得分:4)
要在画布上绘制多个图像,请使用以下代码
var canvas = document.getElementById('canvas_id');
var context = canvas.getContext('2d');
var imageObj1 = new Image();
imageObj1.src = "your image source"
imageObj1.onload = function() {
context.drawImage(imageObj1, x, y);
};
var imageObj2 = new Image();
imageObj2.src = "your image source"
imageObj2.onload = function() {
context.drawImage(imageObj2, x, y);
};
现在从画布中获取图像数据URL
// get png data url
var pngUrl = canvas.toDataURL();
// get jpeg data url
var jpegUrl = canvas.toDataURL('image/jpeg');
如果您想要 ImageData ,请使用
context.getImageData(x,y,width,height);