HTML:
<body>
<canvas id="mycanvas" width="640" height="450"></canvas>
<img src="" alt=""/>
</body>
JS代码:
var canvas = document.getElementById("mycanvas"); var context = canvas.getContext('2d'); var cw = 640; var ch = 450; context.fillStyle = "#ff0000"; context.fillRect(0, 0, cw, ch); var img=new Image(); img.src="images/12.jpeg"; context.drawImage(img,0,0, cw,ch); var imgData =canvas.toDataURL("image/png"); $("body img").attr("src",imgData);