canvas 绘制刮刮卡

思路=》

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

 canvas刮奖可以用globalCompositeOperation属性制作。

 globalCompositeOperation:

属性值描述
source-over (default)新图形会覆盖在原有内容之上
destination-over会在原有内容之下绘制新图形
source-in新图形会仅仅出现与原有内容重叠的部分。其它区域都变成透明的
destination-in原有内容中与新图形重叠的部分会被保留,其它区域都变成透明的
source-out结果是只有新图形中与原有内容不重叠的部分会被绘制出来
destination-out原有内容中与新图形不重叠的部分会被保留
source-atop新图形中与原有内容重叠的部分会被绘制,并覆盖于原有内容之上
destination-atop原有内容中与新内容重叠的部分会被保留,并会在原有内容之下绘制新图形
lighter两图形中重叠部分作加色处理
darker两图形中重叠的部分作减色处理
xor重叠的部分会变成透明
copy只有新图形会被保留,其它都被清除掉

实现代码
class Scratch{
    constructor(options){
        this.obj = document.querySelector(options.obj);    //div容器
        this.bgPic = options.bgPic;    //刮刮卡前景图
        this.radius = options.radius;    //圆半径
        this.area = options.area || 50;    //擦拭部分面积 超过部分隐藏或者清除画布(当前清除画布)
        this.succuss = options.succuss;    //擦拭成功后执行方法
        this.startfn = options.startfn; //开始擦拭时调用刮刮乐结果(可以给div换图或者换样式)
        this.isPrize = false;    //是否擦拭完毕
    }
    //初始化
    init(){
        this.getSize();
        this.createCanvas();
        this.drawBg();
        this.event();
    }
    //获得容器的宽高(用于设置canvas宽高)
    getSize(){
        this.width = this.obj.offsetWidth;
        this.height = this.obj.offsetHeight;
        this.left = this.obj.offsetLeft;
        this.top = this.obj.offsetTop;
    }
    //创建canvas并设置宽高插入容器中
    createCanvas(){
        let canvas = document.createElement("canvas");
        canvas.width = this.width;
        canvas.height = this.height;
        this.ctx = canvas.getContext("2d");
        this.obj.append(canvas)
    }//绘制前景图 图片必须预加载
    drawBg(){
        let oImg = new Image(),
            that = this;
        oImg.src = that.bgPic;
        oImg.onload=()=>{
            this.touch = true;
            this.ctx.drawImage(oImg,0,0,oImg.width,oImg.height,0,0,this.width,this.height);
            this.ctx.globalCompositeOperation = 'destination-out';    //设置原有内容中与新图形不重叠的部分会被保留
        }
    }
    //添加touch事件
    event(){
        let obj = this.obj,
            that = this;
        obj.addEventListener("touchstart",event=>{that.touchCanvas(event).bind(this)})
        obj.addEventListener("touchmove",event=>{that.touchCanvas(event).bind(this)})
        obj.addEventListener("touchend",event=>{})
    }
    //擦拭canvas
    touchCanvas(event){
        if(!this.touch){
            return false;
        }
        if(!this.isPrize){
            this.isPrize = true;
            this.startfn();
        }

        var e=window.event||event;
        e.preventDefault();    //禁止ios和安卓默认事件页面下拉动
        this.clearCanvas(e.targetTouches[0].pageX-this.left,e.targetTouches[0].pageY-this.top);
    }
    //绘制圆形 橡皮擦
    clearCanvas(x,y){
        this.ctx.save();
        this.ctx.beginPath();
        this.ctx.arc(x,y,this.radius,0,2*Math.PI);
        this.ctx.fill();
        this.ctx.closePath();
        this.ctx.stroke();
        this.ctx.restore();
        this.compute();
    }
    //计算透明区域
    compute(){
        var pixels = this.ctx.getImageData(0,0,this.width,this.height).data;
        let transPixels = [];
        for(let i = 0; i < pixels.length; i += 4){
            // 严格上来说,判断像素点是否透明需要判断该像素点的a值是否等于0,
            // 为了提高计算效率,这儿设置当a值小于128,也就是半透明状态时就可以了
            if(pixels[i+3] < 128){
                transPixels.push(pixels[i+3]);
            }
        }
        let area= (transPixels.length / (pixels.length / 4) * 100).toFixed(2);
        if(area>this.area){
            this.touch = false;
            this.ctx.clearRect(0,0,this.width,this.height);
            this.ctx.globalCompositeOperation = 'source-over';
            this.succuss();
        }
    }
    //再来一次(重置)
    reset(){
        this.isPrize = false;
        this.drawBg();
    }
}
View Code

(第一次写博客,有错请见谅)

转载于:https://www.cnblogs.com/sublogs/p/10778487.html

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 IT 领域,文档格式转换是常见需求,尤其在处理多种文件类型时。本文将聚焦于利用 Java 技术栈,尤其是 Apache POI 和 iTextPDF 库,实现 doc、xls(涵盖 Excel 2003 及 Excel 2007+)以及 txt、图片等格式文件向 PDF 的转换,并实现在线浏览功能。 先从 Apache POI 说起,它是一个强大的 Java 库,专注于处理 Microsoft Office 格式文件,比如 doc 和 xls。Apache POI 提供了 HSSF 和 XSSF 两个 API,其中 HSSF 用于读写老版本的 BIFF8 格式(Excel 97-2003),XSSF 则针对新的 XML 格式(Excel 2007+)。这两个 API 均具备读取和写入工作表、单元格、公式、样式等功能。读取 Excel 文件时,可通过创建 HSSFWorkbook 或 XSSFWorkbook 对象来打开相应格式的文件,进而遍历工作簿中的每个 Sheet,获取行和列数据。写入 Excel 文件时,创建新的 Workbook 对象,添加 Sheet、Row 和 Cell,即可构建新 Excel 文件。 再看 iTextPDF,它是一个用于生成和修改 PDF 文档的 Java 库,拥有丰富的 API。创建 PDF 文档时,借助 Document 对象,可定义页面尺寸、边距等属性来定制 PDF 外观。添加内容方面,可使用 Paragraph、List、Table 等元素将文本、列表和表格加入 PDF,图片可通过 Image 类加载插入。iTextPDF 支持多种字体和样式,可设置文本颜色、大小、样式等。此外,iTextPDF 的 TextRenderer 类能将 HTML、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值