抽奖的时候后见过,类似于刮刮乐。
相关知识点:
- context.arc(x,y,r,sAngle,eAngle,counterclockwise);
- 参数:圆的中心的 x 坐标, 圆的中心的 y 坐标, 圆的半径,起始角,以弧度计(弧的圆形的三点钟位置是 0 度),结束角,以弧度计,可选。规定应该逆时针还是顺时针绘图。False = 顺时针,true = 逆时针。
- context.clip(); 方法从原始画布中剪切任意形状和尺寸。
- 提示:一旦剪切了某个区域,则所有之后的绘图都会被限制在被剪切的区域内(不能访问画布上的其他区域)。您也可以在使用 clip() 方法前通过使用 save() 方法对当前画布区域进行保存,并在以后的任意时间对其进行恢复(通过 restore() 方法)。
- 入栈save() 与 出栈restore() 必须成对出现。
<canvas width="600" height="400"></canvas>
<script>
// 先把画布搞出来
var canvas=document.querySelector("canvas");
var ctx=canvas.getContext("2d");
ctx.fillStyle="#000";
ctx.fillRect(0,0,canvas.width,canvas.height);
// 再把下面的图片加载进来
var img=new Image();
img.src="./img/sound.jpg";
img.addEventListener("load",loadHandler);
function loadHandler(e){
canvas.addEventListener("mousemove",mouseHandler);
// 不能放在这儿,会首先加载图片的
// ctx.drawImage(img,0,0,canvas.width,canvas.height);
}
function mouseHandler(e){
ctx.save();//入栈
ctx.beginPath();
//设置圆形,用来作为刮刮乐的下笔形状
ctx.arc(e.offsetX,e.offsetY,10,0,Math.PI/180*360);
ctx.clip();//剪切
ctx.drawImage(img,0,0,canvas.width,canvas.height);
ctx.restore();//出栈
}
</script>