效果图:

代码:

 <!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>刮刮乐</title>
<style>
.canvasBox {
width: 400px;
height: 200px;
margin: 100px auto;
} #canvas1 {
background-repeat: no-repeat;
background-position: center;
background-size: 300px 180px;
}
</style>
</head> <body>
<div class="canvasBox">
<canvas width="" height= id="canvas1"></canvas>
</div>
<script>
var oCanvas = document.getElementById('canvas1');
var ctx = oCanvas.getContext('2d');
var w = oCanvas.width;
var h = oCanvas.height;
var lastPoint = {};
var nowPoint = {};
var lastPointX, lastPointY;
var nowPointX, nowPointY; function init() {
ctx.fillStyle = '#ccc';
ctx.fillRect(, , w, h); var r = Math.random(),
oImg = new Image(); if (r < 0.5) {
oImg.src = './1.png';
} else {
oImg.src = './2.jpg';
}
oImg.onload = function () {
oCanvas.style.backgroundImage = 'url(' + oImg.src + ')';
ctx.globalCompositeOperation = 'destination-out';
document.addEventListener('mousedown', downFun, false);
}
}
init(); function downFun(e) {
lastPointX = e.clientX - oCanvas.offsetLeft;
lastPointY = e.clientY - oCanvas.offsetTop;
oCanvas.addEventListener('mousemove', moveFun, false);
document.addEventListener('mouseup', upFun, false);
} function moveFun(e) {
nowPointX = e.clientX - oCanvas.offsetLeft;
nowPointY = e.clientY - oCanvas.offsetTop; ctx.beginPath();
ctx.fillStyle = 'transpatent'; ctx.lineWidth = ;
ctx.moveTo(lastPointX, lastPointY);
ctx.lineTo(nowPointX, nowPointY);
ctx.stroke(); ctx.arc(nowPointX, nowPointY, , , Math.PI * , );
ctx.closePath();
ctx.fill(); lastPointX = nowPointX;
lastPointY = nowPointY;
} function upFun() {
oCanvas.removeEventListener('mousemove', moveFun, false);
document.removeEventListener('mouseup', upFun, false);
clearAll();
} function clearAll() {
var d = ctx.getImageData(, , w, h),
c = ,
len = d.data.length;
for (var i = ; i < len; i += ) {
if (d.data[i] === ) {
c++;
}
}
if (c > w * h * 0.7) {
ctx.clearRect(, , w, h);
}
}
</script>
</body> </html>

一开始鼠标滑动太快会导致两点之间产生空白,后面通过记录鼠标移动前后两点的位置,使用stroke画线即可

 ctx.lineWidth = 20;
ctx.moveTo(lastPointX, lastPointY);
ctx.lineTo(nowPointX, nowPointY);
ctx.stroke();
当刮开一定的面积时就自动全部展示出来

canvas之刮刮乐的更多相关文章

  1. 游戏的套路你知道吗? H5 Canvas刮刮乐

    玩游戏的人 很多时候都会遇到翻牌子  开宝箱. 总有人傻傻的在哪里还纠结很久到底点哪一个! 纠结  指不定翻哪一个会多一点,你明明看到那个卡片的奖项多 . 那我就告诉你好了  其实很多时候在你点开那个 ...

  2. H5 Canvas刮刮乐

    玩游戏的人 很多时候都会遇到翻牌子  开宝箱. 总有人傻傻的在哪里还纠结很久到底点哪一个! 纠结  指不定翻哪一个会多一点,你明明看到那个卡片的奖项多 . 那我就告诉你好了  其实很多时候在你点开那个 ...

  3. canvas刮刮乐

    这周有点迷茫,不知道干嘛了,一天天就过去了!我在博客右侧公告栏加了qq交流,各位有好的主题,或者有趣的技术,欢迎交流!今天突发奇想,就写了2个h5 canvas的demo玩玩! demo一:刮刮乐 舍 ...

  4. 【Android界面实现】使用Canvas对象实现“刮刮乐”效果

    在淘宝.京东等电商举办活动的时候,常常能够看到在移动client推出的各种刮奖活动,而这样的活动也受到了非常多人的喜爱.从client的体验来说,这样的效果应该是通过网页来实现的,那么,我们使用And ...

  5. HTML5 CSS3 诱人的实例 :canvas 模拟实现电子彩票刮刮乐

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/34089553 今天给大家带来一个刮刮乐的小例子~基于HTML5 canvas的, ...

  6. canvas刮刮乐游戏等

    裁剪 ctx.clip():当前路径外的区域不再绘制 <canvas id="cans" width=500 height=500></canvas> &l ...

  7. canvas 写一个刮刮乐抽奖

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. 用Canvas画一个刮刮乐

    Canvas 通过 JavaScript 来绘制 2D图形.Canvas 是逐像素进行渲染的.开发者可以通过javascript脚本实现任意绘图.Canvas元素是HTML5的一部分,允许脚本语言动态 ...

  9. canvas刮刮乐效果(pc端&H5、zepto-touchmove)

    一.html <div id="canvasArea" style="width:300px;height:200px;position:relative;&quo ...

随机推荐

  1. 51nod 1344 【前缀和】

    思路:求一下最小负数的前缀和 #include<cstdio> #include <map> #include<iostream> #include<stri ...

  2. android的logcat的message有字符长度的限制,超过将直接截断

    http://blog.csdn.net/qidizi/article/details/47291803 今天想在logcat中输出足够多的调试信息时,发现从logcat中返回的信息中明显少了后面一节 ...

  3. bzoj 4903: [Ctsc2017]吉夫特【lucas+状压dp】

    首先根据lucas, \[ C_n^m\%2=C_{n\%2}^{m\%2}*C_{n/2}^{m/2} \] 让这个式子的结果为计数的情况只有n&m==m,因为m的每一个为1的二进制位都需要 ...

  4. UltraEdit配置C++开发环境

    1.下载UltraEdit软件安装 2.下载MinGW库安装 3.在系统设置环境变量,在path里加上你的MinGW\bin目录即可. 4.进入UltraEdit软件进行如下配置: 菜单栏---高级- ...

  5. 使用c++的一些建议

    1: 不要使用宏,用const或enum定义常量 用inline避免函数的额外调用(使用inline的函数,块里面尽量不要使用循环和递归) 用template去荷花一些函数或者类型 用namespac ...

  6. python实现计数排序

    计数排序有局限性,最小值和最大值决定着数组的长度,如果分配均匀的话可以用 # @File: count_sort import random def count_sort(li, max_num=10 ...

  7. 总结 - 常见的JavaScript兼容性问题

    添加事件的方法 (元素, 绑定的事件类型, 事件触发的方法) addHandler: function (element, type, handler) { if (element.addEventL ...

  8. vs2010 坑爹的BUG

    以前用VS2005的时候,就遇到过一些很奇怪的BUG,比如始终报错,然后把项目文件删除,重新创建一个项目文件,就好了. 今天用VS2010测试程序时,又发现一个坑爹的BUG,这绝对不是我的错! sta ...

  9. 序列化json模块

    1.用json模块来进行序列化和反序列化 注意:用json序列化的数据类型得到的文件后缀名必须是json.因为如果不是json后缀,别人也不知道这是用json序列化的文件. 序列化:json.dump ...

  10. vs 2015 编译cocos2dx 报错

    VS 2015 compiling cocos2d-x 3.3 error “fatal error C1189: #error: Macro definition of snprintf confl ...