博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用canvas画一个刮刮卡
阅读量:6952 次
发布时间:2019-06-27

本文共 1347 字,大约阅读时间需要 4 分钟。

思路

  1. 可以为canvas设置一个background的作为需要显示的目标图像,然后canvas绘制一个矩形作为遮罩层。或者通过position使用canvas覆盖在image标签上。

  2. 利用canvas的globalCompositeOperation属性可以很方便的操作绘图间的关系,详细可参考

  3. 因为消除后的图形为透明图形。可以使用getImageData抓取canvas中的图像数据来判断已消除的面积。

实现

把目标图像设置为background

this.canvas.style.background = `url(${this.CONFIG.backgroundUrl}) no-repeat center center`this.canvas.style.backgroundSize = 'cover'复制代码

绘制一个遮罩层

drawGrayMask(){  this.context.fillStyle = this.CONFIG.fillColor  this.context.fillRect(0,0,this.CONFIG.width, this.CONFIG.height)}复制代码

设置绘图效果

if (this.start){  // 鼠标/手势 移动时,获取当前坐标   let x = e.touches ? e.touches[0].clientX : e.pageX  let y = e.touches ? e.touches[0].clientY : e.pageY  // 设置canvas属性  this.context.globalCompositeOperation = 'destination-out'  this.drawLine(x, y)  this.CONFIG.startX = x  this.CONFIG.startY = y}复制代码

判断绘制的图像占整个canvas的比例

// 获取当前透明的面积for (let i=0; i
this.CONFIG.width*this.CONFIG.height*0.5){ this.run()}复制代码

清除整个遮罩层

clearCanvas(){  // 绘制一个面积超过canvas面积的圆  this.context.beginPath()  this.context.globalCompositeOperation = 'destination-out'  this.context.arc(this.CONFIG.width/2, this.CONFIG.height/2, this.CONFIG.currentRadius, 0, 2*Math.PI)  this.context.fill()  this.context.closePath()}update(){   if (this.CONFIG.currentRadius < this.CONFIG.width || this.CONFIG.currentRadius

转载于:https://juejin.im/post/5c874d03e51d455aa333e10b

你可能感兴趣的文章
magento 语言包替换
查看>>
jquery操作<select>标签大全
查看>>
Centos7源码安装MongoDB-3.6
查看>>
SQL Server 2008 R2数据库镜像部署
查看>>
让ssh客户端直接上传和下载文件
查看>>
Linux 防火墙
查看>>
简练软考知识点整理-外指赶快先提投降
查看>>
32 MySQL主从
查看>>
HanLP-分类模块的分词器介绍
查看>>
Raid5磁盘阵列修复方法介绍
查看>>
技术解析系列 | PouchContainer 支持 LXCFS 实现高可靠容器隔离
查看>>
linux中web服务器的基本配置
查看>>
linux服务器之间设置ssh免密登录
查看>>
如何将M4A格式的音频转换为MP3格式?只需一步搞定
查看>>
APP项目资源对接平台有那几家
查看>>
微信自定义网页分享链接(可自定义链接 图片 内容介绍)
查看>>
Oracle管理表空间(三)--Oracle UNDO表空间
查看>>
Oracle使用rman进行表空间基于时间点的恢复
查看>>
DNS 多网段的反向记录
查看>>
mac效率工具
查看>>