菜鸡记录不常用到的东西,免得以后如果再遇到要重看一遍,有问题请帮忙指出一下,谢谢~~
目的:在有背景的图上作画,画错的地方要清除,背景要保留,不能也清除。
指定某张图为背景,在上面画图,一圆一框
代码如下:
<div>
<img id="mzsh_img" src="./mzsh.jpeg" />
<canvas id="canvas_background"></canvas>
</div>
<script>
var backgroundImg = document.getElementById("mzsh_img");
var canvasBackground = document.getElementById("canvas_background");
canvasBackground.width = backgroundImg.width;//设置画布大小和图片相同,否则画布有默认大小
canvasBackground.height = backgroundImg.height;
var ctx = canvasBackground.getContext("2d"); //ctx对画布做需要的操作
ctx.drawImage(backgroundImg, 0, 0);
ctx.stroke(); //确认绘制
ctx.lineWidth = 3;
ctx.arc(200,200,50,0,2*Math.PI);//圆