<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="./scratch.js"></script>
</head>
<body>
<canvas id="down" width=300 height=100 style="position:absolute;left:10px; top:10px; border:1px solid #333;" ></canvas>
<canvas id="up" width=300 height=100 style="position:absolute;left:10px; top:10px; border:1px solid #333"></canvas> <script>
var options = {
awards:['一等奖','二等奖','三等奖','特等奖','没中奖'],
maskColor:"gray",
textStyle:{
'size':'30',
'family':'Arial',
'align':'center',
'color':"orange"
},
radius:30
}
var scratch = new Scratch(options);
scratch.init();
</script>
</body>
</html>
(function (doc,win){
    var Scratch = function(options){
        this.options = options;
        this.up = doc.querySelector("#up");
        this.upCtx = this.up.getContext("2d");
        this.down = doc.querySelector("#down")
        this.downCtx = this.down.getContext("2d");

this.width = this.up.width;
        this.height = this.up.height;
    }

Scratch.prototype = {
            constructor: Scratch,
            init:function(){
                this.drawText();
                this.drawMask();
                this.addEvent();
            },
            drawText:function(){
                var ctx = this.downCtx;
                ctx.font = this.options.textStyle.size + "px " + this.options.textStyle.family;
                ctx.textAlign = this.options.textStyle.align;
                ctx.fillStyle = this.options.textStyle.color;
                ctx.textBaseline = "top";
                var random = parseInt(Math.random() * this.options.awards.length) || 0;
                this.award = this.options.awards[random];
                ctx.fillText(this.award,this.width/2,this.height/2 - this.options.textStyle.size/2);

},
            drawMask:function(){
                var ctx = this.upCtx;
                ctx.fillStyle = this.options.maskColor;
                ctx.fillRect(0,0,this.width,this.height);
                ctx.globalCompositeOperation = 'destination-out';
            },
            addEvent:function(){
                var _this = this;
                _this.up.addEventListener('mousedown',function(ev){
                    _this.up.addEventListener('mousemove',callback1 = function(ev){
                        var ctx = _this.upCtx;
                        var x = ev.clientX - _this.up.offsetLeft;
                        var y = ev.clientY - _this.up.offsetTop;
                        var radius = _this.options.radius;
                        ctx.beginPath();
                        var gradient = ctx.createRadialGradient(x, y, 0, x, y, options.radius);
                        gradient.addColorStop(0,"rgba(255, 255, 255, 0.3)");
                        gradient.addColorStop(1,"rgba(255, 255, 255, 0)");
                        ctx.fillStyle = gradient;
                        ctx.arc(x, y, radius, 0, Math.PI * 2, true);
                        ctx.fill();
                        ctx.closePath();

if(_this.result() > 0.8){
                            alert(_this.award);
                        }

},false)

doc.addEventListener('mouseup',callback2 = function(ev){
                        _this.up.removeEventListener('mousemove',callback1);
                        doc.removeEventListener('mouseup',callback2);
                    },false)
                },false)

},
            result:function(){
                //文字宽度
                var textWidth = this.options.textStyle.size * this.award.length;
                //文字高度
                var textHeight = this.options.textStyle.size;
                var transPixel = [];
                var imgData = this.upCtx.getImageData(this.width/2-textWidth/2,this.height/2-textHeight/2,textWidth,textHeight);
                var pixels = imgData.data;
                for(var i = 0; i < pixels.length; i += 4){
                    var a = pixels[i+3];
                    if(a < 128){
                        transPixel.push(a);
                    }
                }
                return transPixel.length / (pixels.length/4);
            }

};
    win.Scratch = Scratch;

})(document,window)

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

  1. canvas 绘制刮刮卡

    思路=> 用div来展示刮奖结果,用canvas绘制刮奖前展示的图片或者文字:将canvas叠在div上方,刮奖是只需要操作canvas配合touch事件即可简单完成. canvas刮奖可以用g ...

  2. canvas实现刮刮卡效果

    canvas实现刮刮卡效果 实现步骤: 设置页面背景图,即刮刮卡底部图片 绘制canvas 刮刮卡顶部图片drawImage 绑定事件 addEventListener  touchstart.tou ...

  3. qt qml 刮刮卡效果

    用canvas+mouseArea实现的刮刮卡效果. 表层是一层色彩,用手指划开,可看到下面的文字Lisence: MIT, 请保留本文档说明Author: surfsky.cnblogs.com 2 ...

  4. 网页闯关游戏(riddle webgame)--H5刮刮卡的原理和实践

    前言: 之前编写了一个网页闯关游戏(类似Riddle Game), 除了希望大家能够体验一下我的游戏外. 也愿意分享编写这个网页游戏过程中, 学到的一些知识. 对于刮刮卡, 想必大家都很熟悉, 也很喜 ...

  5. Html5实现移动端、PC端 刮刮卡效果

    刚从南方回来就分了一个刮刮卡效果的页面,特么的我在烦恼怎么用H5去实现这个效果呢,好不容易写出来了,产品居然说:“既然你可以写出来这个效果那当然好了,开始我只是打算让你实现点击就出现呢!”… … 尼玛 ...

  6. 用c#开发微信 (16) 微活动 2 刮刮卡

    微信营销是一种新型的营销模式,由于微信更重视用户之间的互动,故而这种营销推广不不能盲目地套用微博营销的单纯大量广告推送方式.这种方式在微信营销中的效果非常差,会令用户反感,继而取消去企业或商家的微信公 ...

  7. Atitit .html5刮刮卡的gui实现总结

    Atitit .html5刮刮卡的gui实现总结 #----两个案例canvas或者wScratchPad-1.4.4 1 #----1.添加panel  ,这个十mask div.....posti ...

  8. 使用HTML5实现刮刮卡效果

    你玩过刮刮卡么?一不小心可以中奖的那种.今天我给大家分享一个基于HTML5技术实现的刮刮卡效果,在PC上只需按住鼠标,在手机上你只需按住指头,轻轻刮去图层就可以模拟真实的刮奖效果. 我们利用HTML5 ...

  9. Android 自定义View修炼-【2014年最后的分享啦】Android实现自定义刮刮卡效果View

    一.简介: 今天是2014年最后一天啦,首先在这里,我祝福大家在新的2015年都一个个的新健康,新收入,新顺利,新如意!!! 上一偏,我介绍了用Xfermode实现自定义圆角和椭圆图片view的博文& ...

  10. Android刮刮卡自定义控件

    网上的都是自己绘制的或者图片,我的需求是可以随意的自定义底部和顶部的布局.所以自己重写一个,原理就是直接继承 View 来实现一个刮层,让这个刮层和图片以及文字不产生任何依赖,再结合 FrameLay ...

随机推荐

  1. AlloyRenderingEngine

    AlloyRenderingEngine燃烧的进度条 写在前面 Github: https://github.com/AlloyTeam/AlloyGameEngine HTML 5新增了progre ...

  2. jQuery本身方法($.each,$.map,$.contains,$ajax)

    常用工具方法 (1)$.trim $.trim方法用于移除字符串头部和尾部多余的空格. $.trim(' Hello ') // Hello (2)$.contains $.contains方法返回一 ...

  3. Meteor入门

    转载Meteor入门介绍   Meteor是什么 基于nodejs的实时web APP开发框架. Meteor能带来什么 简单的说,你可以用js搞定客户端.服务端的开发.另外,客户端.服务端的界限被极 ...

  4. 【机器学习算法-python实现】KNN-k近邻算法的实现(附源代码)

    ,400],[200,5],[100,77],[40,300]]) shape:显示(行,列)例:shape(group)=(4,2) zeros:列出一个同样格式的空矩阵,例:zeros(group ...

  5. Net开源网络爬虫

    转载.Net开源网络爬虫Abot介绍 .Net中也有很多很多开源的爬虫工具,abot就是其中之一.Abot是一个开源的.net爬虫,速度快,易于使用和扩展.项目的地址是https://code.goo ...

  6. HDU1325 Is It A Tree? 【并查集】

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  7. poj 3273 Monthly Expense (二分)

    //最大值最小 //天数的a[i]值是固定的 不能改变顺序 # include <algorithm> # include <string.h> # include <s ...

  8. 分享一个SQLSERVER脚本

    原文:分享一个SQLSERVER脚本 分享一个SQLSERVER脚本 很多时候我们都需要计算数据库中各个表的数据量很每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tablespac ...

  9. Thrift实践

    Thrift实践:(一)安装 -- 未完待续   1. 新建一个目录,C:\test\thrift-test,里面建2个子文件夹,client-node和sever-csharp,然后把Thrift官 ...

  10. Linux C语言操作MySQL

    原文:Linux C语言操作MySQL 1.MySQL数据库简介 MySQL是一个开源码的小型关系数据库管理系统,体积小,速度快,总体成本低,开源.MySQL有以下特性: (1) 使用C和C++编写, ...