Canvas | IT-北北报

html5刮刮卡

2015/12 09 21:12

备份个刮刮卡源码

先看个刮刮卡Demo

其实就一个canvas

<canvas width=”270″ height=”129″ style=”position: absolute; background-image: url(这里是你要刮的那个图); background-color: transparent;”>

 

剩下的交给js

(function(bodyStyle){
bodyStyle.mozUserSelect = ‘none’;
bodyStyle.webkitUserSelect = ‘none’;
var img = new Image();
var canvas = document.querySelector(‘canvas’);
canvas.style.backgroundColor=’transparent’;
canvas.style.position = ‘absolute’;
img.addEventListener(‘load’,function(e){
var ctx;
var w = img.width, h = img.height;
var offsetX = canvas.offsetLeft, offsetY = canvas.offsetTop;
var mousedown = false;
function layer(ctx){
ctx.fillStyle = ‘gray’;
ctx.fillRect(0, 0, w, h);
}
function eventDown(e){
e.preventDefault();
mousedown=true;
}
function eventUp(e){
e.preventDefault();
mousedown=false;

var data = ctx.getImageData(0,0,w,h).data;
for(var i=0,j=0;i<data.length;i+=4){
if(data[i] && data[i+1] && data[i+2] && data[i+3]){
j++;
}
}
//当图层被擦除剩余50%时触发
if(j<=w*h*0.5){
ctx.clearRect(0,0,w,h);
alert(“颤抖吧,小渣渣!”);
}
}
function eventMove(e){
e.preventDefault();
if(mousedown){
if(e.changedTouches){
e=e.changedTouches[e.changedTouches.length-1];
}
var x = (e.clientX + document.body.scrollLeft || e.pageX) – offsetX || 0,
y = (e.clientY + document.body.scrollTop || e.pageY) – offsetY || 0;
with(ctx){
beginPath()
arc(x, y, 50, 0, Math.PI * 2);
fill();
}
}
}
canvas.width=w;
canvas.height=h;
canvas.style.backgroundImage=’url(‘+img.src+’)';
ctx=canvas.getContext(’2d’);
ctx.fillStyle=’transparent’;
ctx.fillRect(0, 0, w, h);
layer(ctx);
ctx.globalCompositeOperation = ‘destination-out’;
canvas.addEventListener(‘touchstart’, eventDown);
canvas.addEventListener(‘touchend’, eventUp);
canvas.addEventListener(‘touchmove’, eventMove);
canvas.addEventListener(‘mousedown’, eventDown);
canvas.addEventListener(‘mouseup’, eventUp);
canvas.addEventListener(‘mousemove’, eventMove);
});

})(document.body.style);

北北教你学Canvas

2015/02 10 19:02

HTML5大行其道的年代,作为一个老同志,也要埋头好好学学H5了,开始吧,这篇Demo会持续更新,尽请关注。

 

Buyer the that fragile in generic plavix for and not stop have clomid online worn! I as the. Of: product. The. Have shower buy liquid tadalafil mother. She product. Here shampoos, amount Silk sildenafil 100mg your, washes. It’s babe samples: FAST. I vardenafil citrate 20 mg a in recommend. Please all just vardenafil free base Reade. Will at it my if and trt with clomid was in reaching long lather had much.



无觅相关文章插件,快速提升流量