原理:ctx.globalCompositeOperation = "destination-out"; 在源图像外显示目标图像。只有源图像外的目标图像部分会被显示,源图像是透明的。 //在小画板上画画的函数function tapClip(){ var hastouch =
//在小画板上画画的函数 function tapClip(){ var hastouch = "ontouchstart" in window ? true:false, tapstart = hastouch ? "touchstart":"mousedown", tapmove = hastouch ? "touchmove":"mousemove", tapend = hastouch ? "touchend":"mouseup"; ctx.lineCap = "round"; ctx.lineJoin = "round"; ctx.lineWidth = a; //在源图像外显示目标图像。只有源图像外的目标图像部分会被显示,源图像是透明的。 ctx.globalCompositeOperation = "destination-out"; //touchstart或mousedown canvas.addEventListener(tapstart , function(e){ e.preventDefault(); ctx.beginPath(); //touchmove或mousemove canvas.addEventListener(tapmove , tapmoveHandler); canvas.addEventListener(tapend , function(){ canvas.removeEventListener(tapmove , tapmoveHandler); //获取像素 var data = ctx.getImageData(0,0,w,h).data; for (var i=0,j=0;i