简要教程
scratchcard是一款支持移动手机的html5 canvas刮刮卡插件。该刮刮卡插件支持移动触摸事件,提供刮卡的回调函数,简单易用,效果非常不错。
使用方法
在页面中引入scratch.js文件。
<script type="text/javascript" src="js/scratch.js"></script>
html结构
使用下面的html结构来制作一个刮刮卡:
<div class="scratch_container">
<div class="scratch_viewport">
<!-- result picture -->
<canvas id="js-scratch-canvas"></canvas>
</div>
</div>
css样式
为刮刮卡添加下面的css样式。
.scratch_container {
position: relative;
margin: 0 auto;
max-width: 1024px;
}
.scratch_viewport {
position: relative;
width: 250px;
height: 250px;
margin: 0 auto;
z-index: 0;
}
.scratch_picture-under {
position: absolute;
top: 0;
left: 0;
display: block;
z-index: -1;
}
.scratch_container canvas {
position: relative;
width: 100%;
height: auto;
z-index: 1;
}
初始化插件
在页面页面底部<body>标记结束之前,使用下面的代码来实例化一个刮刮卡对象。
var scratch = new scratch({
canvasid: 'js-scratch-canvas',
imagebackground: 'loose.jpg',
pictureover: 'foreground.jpg',
cursor: {
png: 'piece.png',
cur: 'piece.cur',
x: '20',
y: '17'
},
radius: 20,
npoints: 100,
percent: 50,
callback: function () {
alert('i am callback.');
},
pointsize: { x: 3, y: 3}
});
配置参数
该canvas刮刮卡插件的可用配置参数有:
canvasid:canvas的id。
imagebackground:背景图片(刮开后呈现的图片)。
pictureover:前景图片。
scenewidth:canvas的宽度。
sceneheight:canvas的高度。
radius:清除区域的半径。
npoints:清除区域的杂点数量。
percent:在清除多少区域之后清空canvas。
cursor:光标。
png:png格式的光标。
x:move position x。
y:move position y。
cur:cur格式的光标(ie使用)。
scratchcard canvas刮刮卡插件的github地址为:https://github.com/masth0/scratchcard
以上就是支持移动手机的canvas刮刮卡插件的内容。