Canvas装载图片

例子:
var context = document.getElementById("canvas").getContext("2d");
	var mashroomImg = new Image();
	mashroomImg.src = "images/mushroom.png";
	mashroomImg.addEventListener("load", function(){
		//清除canvas--(x,y,width,height)
		context.clearRect(0,0,this.canvasWidth,this.canvasHeight);
		//保存状态
		context.save();
		//装载图片
		context.drawImage(mashroomImg,x,y);
		context.restore();
});

PS:由于Imge装载图片需要时间,所以需要用回调函数load去触发

你可能感兴趣的:(canvas)