javascript对Canvas与img图片的互相转换

Life is short , play more!
本文来自lihao's Blog,转载请注明。

用javascript将img图片转换为Canvas

// Converts image to canvas; returns new canvas element
function convertImageToCanvas(image) {
	var canvas = document.createElement("canvas");
	canvas.width = image.width;
	canvas.height = image.height;
	canvas.getContext("2d").drawImage(image, 0, 0);

	return canvas;
}

用javascript将Canvas转化为Image 

// Converts canvas to an image
function convertCanvasToImage(canvas) {
	var image = new Image();
	image.src = canvas.toDataURL("image/png");
	return image;
}

上述代码是将canvas转化为了一个png格式的图片

 


发表评论

电子邮件地址不会被公开。 必填项已用*标注