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 ...
随机推荐
- JDK8 Lamdba表达式转换成Map,value为null问题
// 将list转换成Map类型 Map<String, String> map = list.stream().collect(Collectors.toMap(Person::getI ...
- Thrift 使用TNonblockingServer模型时调用PosixThreadFactory出错。
Thrift 使用TNonblockingServer模型时调用PosixThreadFactory出错. 我定位到shared_ptr<PosixThreadFactory> thr ...
- 51nod1256【exgcd求逆元】
思路: 把k*M%N=1可以写成一个不定方程,(k*M)%N=(N*x+1)%N,那么就是求k*M-N*x=1,k最小,不定方程我们可以直接利用exgcd,中间还搞错了: //小小地讲一下exgcd球 ...
- AspectCore的AOP操作
AOP实现缓存的一个例子 using AspectCore.DynamicProxy; using Microsoft.Extensions.Caching.Memory; [AttributeUsa ...
- LuoguP2657 [SCOI2009]windy数 【数位dp】By cellur925
题目传送门 题目大意:在A和B之间,包括A和B,总共有多少个不含前导零且相邻两个数字之差至少为2的正整数? 显然是数位dp啦=w=. 显然与上一位有关,我们$dfs$的时候就要记录$pre$.因为这是 ...
- 远程报:这可能是由于credssp加密oracle修正
此错误解决办法 1.Win+R 输入regedit打开注册表 找到对应的以下目录HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion ...
- 洛谷 P4585 [FJOI2015]火星商店问题
(勿看,仅作笔记) bzoj权限题... https://www.luogu.org/problemnew/show/P4585 对于特殊商品,直接可持久化trie处理一下即可 剩下的,想了一段时间c ...
- hbuilder 中文乱码
这是因为HBuilder默认文件编码是UTF-8,你可以在工具-选项-常规-工作空间选项中设置默认字符编码
- Could not open logfile" occurred when run "datapatch -verbose"
CAUSE Due to Bug 25459405 - DATAPATCH FAILS WITH SP2-0768 IF NLS_LANGUAGE IS NOT SET TO AMERICANwhic ...
- Python Selenium设计模式 - PO设计模式
整理一下python selenium自动化测试实践中使用较多的po设计模式. 为什么要用PO 基于python selenium2开始开始ui自动化测试脚本的编写不是多么艰巨的任务.只需要定位到元素 ...