canvas之刮刮乐
效果图:

代码:
<!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之刮刮乐的更多相关文章
- 游戏的套路你知道吗? H5 Canvas刮刮乐
玩游戏的人 很多时候都会遇到翻牌子 开宝箱. 总有人傻傻的在哪里还纠结很久到底点哪一个! 纠结 指不定翻哪一个会多一点,你明明看到那个卡片的奖项多 . 那我就告诉你好了 其实很多时候在你点开那个 ...
- H5 Canvas刮刮乐
玩游戏的人 很多时候都会遇到翻牌子 开宝箱. 总有人傻傻的在哪里还纠结很久到底点哪一个! 纠结 指不定翻哪一个会多一点,你明明看到那个卡片的奖项多 . 那我就告诉你好了 其实很多时候在你点开那个 ...
- canvas刮刮乐
这周有点迷茫,不知道干嘛了,一天天就过去了!我在博客右侧公告栏加了qq交流,各位有好的主题,或者有趣的技术,欢迎交流!今天突发奇想,就写了2个h5 canvas的demo玩玩! demo一:刮刮乐 舍 ...
- 【Android界面实现】使用Canvas对象实现“刮刮乐”效果
在淘宝.京东等电商举办活动的时候,常常能够看到在移动client推出的各种刮奖活动,而这样的活动也受到了非常多人的喜爱.从client的体验来说,这样的效果应该是通过网页来实现的,那么,我们使用And ...
- HTML5 CSS3 诱人的实例 :canvas 模拟实现电子彩票刮刮乐
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/34089553 今天给大家带来一个刮刮乐的小例子~基于HTML5 canvas的, ...
- canvas刮刮乐游戏等
裁剪 ctx.clip():当前路径外的区域不再绘制 <canvas id="cans" width=500 height=500></canvas> &l ...
- canvas 写一个刮刮乐抽奖
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 用Canvas画一个刮刮乐
Canvas 通过 JavaScript 来绘制 2D图形.Canvas 是逐像素进行渲染的.开发者可以通过javascript脚本实现任意绘图.Canvas元素是HTML5的一部分,允许脚本语言动态 ...
- canvas刮刮乐效果(pc端&H5、zepto-touchmove)
一.html <div id="canvasArea" style="width:300px;height:200px;position:relative;&quo ...
随机推荐
- 用C++调用C的库函数(转载)
转自:http://linhs.blog.51cto.com/370259/140927 C++调用C的库函数时,如果头文件定义得不恰当,可能会出现明明某函数在obj文件中存在,但是却发生链接失败的情 ...
- python 元类 type metaclass
python中一切皆对象,类对象创建实例对象,元类创建类对象,元类创建元类. 元类创建类对象有2中方式: 一.type方法 type(类名, 由父类名称组成的元组(针对继承的情况,可以为空),包含属性 ...
- bzoj 2761: [JLOI2011]不重复数字【hash】
map会T,双hash会冲突--于是非酋写了个三hash #include<iostream> #include<cstdio> #include<cstring> ...
- 【POJ - 3190 】Stall Reservations(贪心+优先队列)
Stall Reservations 原文是English,这里直接上中文吧 Descriptions: 这里有N只 (1 <= N <= 50,000) 挑剔的奶牛! 他们如此挑剔以致于 ...
- YCOJ-DFS
DFS搜索是搜索中的一种,即深度优先搜索(Depth First Search),其过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次. 图示: 如图,这是邻接矩阵,我 ...
- shell Syntax error: Bad fd number 错误解决
最近在玩spark , 需要看一下python的spark lib 是怎么加入环境变量的. 执行: sh -x bin/pyspark 报错 + dirname bin/pyspark + cd bi ...
- 密码(Password)
#include<cstdio> #include<cstring> using namespace std; int k, cnt; ][][], ans[]; bool d ...
- 在vue组件的stylus样式总 取消search类型的input按钮中默认样式
在vue组件的stylus样式中 取消search类型的input按钮中默认样式 记录一个坑 环境 Vue组件 使用了Stylus的CSS风格. 问题 input输入框使用了 type="s ...
- RHEL 6.5----CDN(lumanger)
主机名 IP 服务 master 192.168.30.130 CDN(LuManager) slave 192.168.30.131 DNS 软件安装包下载地址及安装方法 http:// ...
- 【前端】模拟微信上传图片(带预览,支持预览gif)
一.Html <style type="text/css"> #previewDiv{width:50px;height:50px;overflow:hidden;po ...