本文实例为大家分享了canvas实现橡皮擦效果的具体代码,供大家参考,具体内容如下
html部分
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html" charset="utf-8" /> <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <title>My Canvas 0.1</title> <style type="text/css"> html,body,div,img{ margin:0; padding:0; } a,a:hover{ text-decoration:none; } .background{ width:100%; position:fixed; top:0; left:0; } </style> </head> <body> <img src="/UploadFiles/2021-04-02/background.png">lottery.js
function Lottery(node, cover, coverType, width, height, drawPercentCallback) { //node:canvas的id,cover:上面一层的图片地址,coverType:'image'or'color',width:canvas宽, height:canvas高, drawPercentCallback:回调函数 //canvas this.conNode = node; this.background = null; this.backCtx = null; this.mask = null; this.maskCtx = null; this.lottery = null; this.lotteryType = 'image'; this.cover = cover || "#000"; this.coverType = coverType; this.pixlesData = null; this.width = width; this.height = height; this.lastPosition = null; this.drawPercentCallback = drawPercentCallback; this.vail = false; } Lottery.prototype = { createElement: function(tagName, attributes) { var ele = document.createElement(tagName); for (var key in attributes) { ele.setAttribute(key, attributes[key]); } return ele; }, getTransparentPercent: function(ctx, width, height) { var imgData = ctx.getImageData(0, 0, width, height), pixles = imgData.data, transPixs = []; for (var i = 0, j = pixles.length; i < j; i += 4) { var a = pixles[i + 3]; if (a < 128) { transPixs.push(i); } } return (transPixs.length / (pixles.length / 4) * 100).toFixed(2); }, resizeCanvas: function(canvas, width, height) { canvas.width = width; canvas.height = height; canvas.getContext('2d').clearRect(0, 0, width, height); }, resizeCanvas_w: function(canvas, width, height) { canvas.width = width; canvas.height = height; canvas.getContext('2d').clearRect(0, 0, width, height); if (this.vail) this.drawLottery(); else this.drawMask(); }, drawPoint: function(x, y, fresh) { this.maskCtx.beginPath(); this.maskCtx.arc(x, y, 20, 0, Math.PI * 2); this.maskCtx.fill(); this.maskCtx.beginPath(); this.maskCtx.lineWidth = 60; this.maskCtx.lineCap = this.maskCtx.lineJoin = 'round'; if (this.lastPosition) { this.maskCtx.moveTo(this.lastPosition[0], this.lastPosition[1]); } this.maskCtx.lineTo(x, y); this.maskCtx.stroke(); this.lastPosition = [x, y]; this.mask.style.zIndex = (this.mask.style.zIndex == 20) "touchmove", function(e) { if (isMouseDown) { e.preventDefault(); } if (e.cancelable) { e.preventDefault(); } else { window.event.returnValue = false; } }, false); _this.conNode.addEventListener('touchend', function(e) { isMouseDown = false; var per = _this.getTransparentPercent(_this.maskCtx, _this.width, _this.height); if (per >= 80) {//在大于等于80%的时候调用回调函数 if (typeof(_this.drawPercentCallback) == 'function') _this.drawPercentCallback(); } }, false); } this.mask.addEventListener(clickEvtName, function(e) { e.preventDefault(); isMouseDown = true; var x = (device "0", "#fff"); // gradient.addColorStop("1.0", "#000"); // _this.maskCtx.font = 'Bold ' + fontSize + 'px Arial'; // _this.maskCtx.textAlign = 'left'; // _this.maskCtx.fillStyle = gradient; // _this.maskCtx.fillText(txt, _this.width / 2 - _this.maskCtx.measureText(txt).width / 2, 100); // _this.maskCtx.globalAlpha = 1; _this.maskCtx.globalCompositeOperation = 'destination-out'; } image.src = this.cover; } }, init: function(lottery, lotteryType) { if (lottery) { this.lottery = lottery; this.lottery.width = this.width; this.lottery.height = this.height; this.lotteryType = lotteryType || 'image'; this.vail = true; } if (this.vail) { this.background = this.background || this.createElement('canvas', { style: 'position:fixed;top:0;left:0;background-color:transparent;' }); } this.mask = this.mask || this.createElement('canvas', { style: 'position:fixed;top:0;left:0;background-color:transparent;' }); this.mask.style.zIndex = 20; if (!this.conNode.innerHTML.replace(/[\w\W]| /g, '')) { if (this.vail) this.conNode.appendChild(this.background); this.conNode.appendChild(this.mask); this.bindEvent(); } if (this.vail) this.backCtx = this.backCtx || this.background.getContext('2d'); this.maskCtx = this.maskCtx || this.mask.getContext('2d'); if (this.vail) this.drawLottery(); else this.drawMask(); var _this = this; window.addEventListener('resize', function() { _this.width = document.documentElement.clientWidth; _this.height = document.documentElement.clientHeight; _this.resizeCanvas_w(_this.mask, _this.width, _this.height); }, false); } }另一个zepto.js是库函数文件,可网上自行查找
出来的效果如图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月16日
2024年11月16日
- 第五街的士高《印度激情版》3CD [WAV+CUE][2.4G]
- 三国志8重制版哪个武将智力高 三国志8重制版智力武将排行一览
- 三国志8重制版哪个武将好 三国志8重制版武将排行一览
- 三国志8重制版武将图像怎么保存 三国志8重制版武将图像设置方法
- 何方.1990-我不是那种人【林杰唱片】【WAV+CUE】
- 张惠妹.1999-妹力新世纪2CD【丰华】【WAV+CUE】
- 邓丽欣.2006-FANTASY【金牌大风】【WAV+CUE】
- 饭制《黑神话》蜘蛛四妹手办
- 《燕云十六声》回应跑路:年内公测版本完成95%
- 网友发现国内版《双城之战》第二季有删减:亲亲环节没了!
- 邓丽君2024-《漫步人生路》头版限量编号MQA-UHQCD[WAV+CUE]
- SergeProkofievplaysProkofiev[Dutton][FLAC+CUE]
- 永恒英文金曲精选4《TheBestOfEverlastingFavouritesVol.4》[WAV+CUE]
- 群星《国风超有戏 第9期》[320K/MP3][13.63MB]
- 群星《国风超有戏 第9期》[FLAC/分轨][72.56MB]