JS實現(xiàn)canvas仿ps橡皮擦刮卡效果詳解
更新時間:2021年11月22日 11:11:07 作者:java李楊勇
這篇文章主要為大家詳細(xì)介紹了使用js中的Canvas實現(xiàn)橡皮擦效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
效果演示:

主要JS代碼實現(xiàn)
<div class="box" id="bb">
<canvas id="cas" width="1366" height="651"></canvas>
</div>
<script type="text/javascript" charset="utf-8">
var canvas = document.getElementById("cas"),
ctx = canvas.getContext("2d");
var x1, y1, a = 30,
timeout, totimes = 100,
jiange = 30;
canvas.width = document.getElementById("bb").clientWidth;
canvas.height = document.getElementById("bb").clientHeight;
var img = new Image();
img.src = "sha.jpg";
img.onload = function() {
ctx.drawImage(img, 0, 0, canvas.width, canvas.height)
//ctx.fillRect(0,0,canvas.width,canvas)
tapClip()
}
//通過修改globalCompositeOperation來達(dá)到擦除的效果
function tapClip() {
var hastouch = "ontouchstart" in window ? true : false,
tapstart = hastouch ? "touchstart" : "mousedown",
tapmove = hastouch ? "touchmove" : "mousemove",
tapend = hastouch ? "touchend" : "mouseup";
ctx.lineCap = "round";
ctx.lineJoin = "round";
ctx.lineWidth = a * 2;
ctx.globalCompositeOperation = "destination-out";
canvas.addEventListener(tapstart, function(e) {
clearTimeout(timeout)
e.preventDefault();
x1 = hastouch ? e.targetTouches[0].pageX : e.clientX - canvas.offsetLeft;
y1 = hastouch ? e.targetTouches[0].pageY : e.clientY - canvas.offsetTop;
ctx.save();
ctx.beginPath()
ctx.arc(x1, y1, 1, 0, 2 * Math.PI);
ctx.fill();
ctx.restore();
canvas.addEventListener(tapmove, tapmoveHandler);
canvas.addEventListener(tapend, function() {
canvas.removeEventListener(tapmove, tapmoveHandler);
timeout = setTimeout(function() {
var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var dd = 0;
for (var x = 0; x < imgData.width; x += jiange) {
for (var y = 0; y < imgData.height; y += jiange) {
var i = (y * imgData.width + x) * 4;
if (imgData.data[i + 3] > 0) {
dd++
}
}
}
if (dd / (imgData.width * imgData.height / (jiange * jiange)) < 0.4) {
canvas.className = "noOp";
}
}, totimes)
});
canvas.addEventListener(tapmove, tapmoveHandler);
canvas.addEventListener(tapend, function() {
canvas.removeEventListener(tapmove, tapmoveHandler);
timeout = setTimeout(function() {
var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var dd = 0;
for (var x = 0; x < imgData.width; x += jiange) {
for (var y = 0; y < imgData.height; y += jiange) {
var i = (y * imgData.width + x) * 4;
if (imgData.data[i + 3] > 0) {
dd++
}
}
}
}, totimes)
});
}
</script>
<script type="text/javascript">
window.setTimeout('CountDown()', 100);
// End
</script>
以上就是JS實現(xiàn)canvas仿ps橡皮擦刮卡效果詳解的詳細(xì)內(nèi)容,更多關(guān)于JS 的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決html input驗證只能輸入數(shù)字,不能輸入其他的問題
下面小編就為大家?guī)硪黄鉀Qhtml input驗證只能輸入數(shù)字,不能輸入其他的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
javascript實現(xiàn)省市區(qū)三級聯(lián)動下拉框菜單
這篇文章主要為大家詳細(xì)介紹了javascript實現(xiàn)省市區(qū)三級聯(lián)動下拉框菜單很詳細(xì)的代碼,解決了大家實現(xiàn)javascript省市區(qū)三級聯(lián)動下拉框菜單的問題,感興趣的小伙伴們可以參考一下2015-11-11
javascript HTML5文件上傳FileReader API
這篇文章主要介紹了javascript HTML5文件上傳FileReader API的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-04-04
Bootstrap中表單控件狀態(tài)(驗證狀態(tài))
這篇文章主要介紹了Bootstrap中表單控件狀態(tài)(驗證狀態(tài)) 的相關(guān)資料,還給大家介紹了在Bootstrap框架中提供的機制驗證效果,非常不錯,需要的朋友可以參考下2016-08-08

