js canvas實(shí)現(xiàn)橡皮擦效果
本文實(shí)例為大家分享了canvas實(shí)現(xiàn)橡皮擦效果的具體代碼,供大家參考,具體內(nèi)容如下
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="images/background.png" class="background resizeContainer"/>
<div id="J_cover" class="resizeContainer"></div>
<script type="text/javascript" src="js/zepto.js"></script>
<script type="text/javascript" src="js/lottery.js"></script>
<script type="text/javascript">
var canvas = {
init : function(){
var self = this;
var node = document.getElementById('J_cover'),
canvas_url = 'images/cover.png',
type = 'image';
var lottery = new Lottery(node, canvas_url, type, window_w, window_h, self.callback);
lottery.init();
},
callback : function(){
$('#J_cover').hide();
}
}
var window_h, window_w;
$(document).ready(function(){
window_w = $(window).width();
window_h = $(window).height();
$('.resizeContainer').width(window_w).height(window_h);
canvas.init();
});
</script>
</body>
</html>
lottery.js
function Lottery(node, cover, coverType, width, height, drawPercentCallback) {
//node:canvas的id,cover:上面一層的圖片地址,coverType:'image'or'color',width:canvas寬, height:canvas高, drawPercentCallback:回調(diào)函數(shù)
//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) ? 21 : 20;
},
bindEvent: function() {
var _this = this;
var device = (/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(navigator.userAgent.toLowerCase()));
var clickEvtName = device ? 'touchstart' : 'mousedown';
var moveEvtName = device ? 'touchmove' : 'mousemove';
if (!device) {
var isMouseDown = false;
_this.conNode.addEventListener('mouseup', function(e) {
e.preventDefault();
isMouseDown = false;
var per = _this.getTransparentPercent(_this.maskCtx, _this.width, _this.height);
if (per >= 80) {//在大于等于80%的時(shí)候調(diào)用回調(diào)函數(shù)
if (typeof(_this.drawPercentCallback) == 'function') _this.drawPercentCallback();
}
}, false);
} else {
_this.conNode.addEventListener("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%的時(shí)候調(diào)用回調(diào)函數(shù)
if (typeof(_this.drawPercentCallback) == 'function') _this.drawPercentCallback();
}
}, false);
}
this.mask.addEventListener(clickEvtName, function(e) {
e.preventDefault();
isMouseDown = true;
var x = (device ? e.touches[0].pageX : e.pageX || e.x);
var y = (device ? e.touches[0].pageY : e.pageY || e.y);
_this.drawPoint(x, y, isMouseDown);
}, false);
this.mask.addEventListener(moveEvtName, function(e) {
e.preventDefault();
if (!isMouseDown) return false;
e.preventDefault();
var x = (device ? e.touches[0].pageX : e.pageX || e.x);
var y = (device ? e.touches[0].pageY : e.pageY || e.y);
_this.drawPoint(x, y, isMouseDown);
}, false);
},
drawLottery: function() {
if (this.lotteryType == 'image') {
var image = new Image(),
_this = this;
image.onload = function() {
this.width = _this.width;
this.height = _this.height;
_this.resizeCanvas(_this.background, _this.width, _this.height);
_this.backCtx.drawImage(this, 0, 0, _this.width, _this.height);
_this.drawMask();
}
image.src = this.lottery;
} else if (this.lotteryType == 'text') {
this.width = this.width;
this.height = this.height;
this.resizeCanvas(this.background, this.width, this.height);
this.backCtx.save();
this.backCtx.fillStyle = '#FFF';
this.backCtx.fillRect(0, 0, this.width, this.height);
this.backCtx.restore();
this.backCtx.save();
var fontSize = 30;
this.backCtx.font = 'Bold ' + fontSize + 'px Arial';
this.backCtx.textAlign = 'center';
this.backCtx.fillStyle = '#F60';
this.backCtx.fillText(this.lottery, this.width / 2, this.height / 2 + fontSize / 2);
this.backCtx.restore();
this.drawMask();
}
},
drawMask: function() {
if (this.coverType == 'color') {
this.maskCtx.fillStyle = this.cover;
this.maskCtx.fillRect(0, 0, this.width, this.height);
this.maskCtx.globalCompositeOperation = 'destination-out';
} else if (this.coverType == 'image') {
var image = new Image(),
_this = this;
image.onload = function() {
_this.resizeCanvas(_this.mask, _this.width, _this.height);
var android = (/android/i.test(navigator.userAgent.toLowerCase()));
_this.maskCtx.globalAlpha = 1;//上面一層的透明度,1為不透明
_this.maskCtx.drawImage(this, 0, 0, this.width, this.height, 0, 0, _this.width, _this.height);
//---以下一段為在上面一層上寫(xiě)字
// var fontSize = 50;
// var txt = '123123';
// var gradient = _this.maskCtx.createLinearGradient(0, 0, _this.width, 0);
// gradient.addColorStop("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);
}
}
另一個(gè)zepto.js是庫(kù)函數(shù)文件,可網(wǎng)上自行查找
出來(lái)的效果如圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS實(shí)現(xiàn)方向鍵切換輸入框焦點(diǎn)的方法
這篇文章主要介紹了JS實(shí)現(xiàn)方向鍵切換輸入框焦點(diǎn)的方法,涉及javascript鼠標(biāo)事件及頁(yè)面元素焦點(diǎn)的切換實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
js中訪問(wèn)html中iframe的文檔對(duì)象的代碼[IE6,IE7,IE8,FF]
W3C的標(biāo)準(zhǔn)告訴我們,可以通過(guò)Dom對(duì)象的contentDocument屬性來(lái)返回文檔對(duì)象。2011-01-01
javascript 正則表達(dá)式觸發(fā)函數(shù)進(jìn)行高級(jí)替換
如果在正則表達(dá)式中定義了子匹配,那么參數(shù)的長(zhǎng)度會(huì)隨著子匹配的個(gè)數(shù)改變,如果沒(méi)有定義子匹配,那么長(zhǎng)度是固定的。2010-03-03
JavaScript中的finally()方法和Filter()方法詳解
finally是 JavaScript 構(gòu)造中使用的方法try-catch,Filter() 是 JavaScript 中的一種方法,可以通過(guò)處理數(shù)組輕松提供過(guò)濾后的輸出數(shù)據(jù),本文就給大家詳細(xì)的介紹一下JavaScript中的finally()方法和Filter()方法,需要的朋友可以參考下2023-08-08
JavaScript實(shí)現(xiàn)自動(dòng)跳轉(zhuǎn)文本功能
這篇文章主要為大家詳細(xì)介紹了JavaScript自動(dòng)跳轉(zhuǎn)文本功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
深入理解JavaScript中的尾調(diào)用(Tail Call)
尾調(diào)用(Tail Call)是函數(shù)式編程的一個(gè)重要概念,下面這篇文章主要給大家深入的介紹了關(guān)于JavaScript中尾調(diào)用的相關(guān)資料,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,有需要的朋友們下面來(lái)一起看看吧。2017-02-02

