[英]javascript canvas erasing tool doesnt do anything
http://joeybabcock.me/tests/php/5.php我上面有一個基於phpacademy的示例的繪圖應用程序,如果繼續進行下去,您將在最后看到黑色的,它應該是一種擦除工具,但無法正常工作,我有以下內容:
var swetch = document.createElement('div');
swetch.className = 'swetch';
swetch.style.backgroundColor = "rgba(0,0,0,0)";
swetch.addEventListener('click', setEraser);
document.getElementById('colors').appendChild(swetch);
並且:
function setEraser(){
context.fillStyle = "rgba(0,0,0,0)";
context.globalCompositeOperation = "destination-out";
context.strokeStyle = "rgba(0,0,0,0)";
swetch.className += ' active';
var active = document.getElementsByClassName('active')[0];
if(active){
active.className = 'swatch';
}
}
我試過創建一個簡單的色板,它具有css屬性“透明”,並且還帶有“ rgba(0,0,0,0)”,但是都沒有用。
我已經嘗試了關於stackoverflow和google的所有答案,以及許多不同的方法(如果使用globalCompositeOperation)。
這是一個簡單的示例,該示例使用“目標輸出”合成來擦除前景填充以顯示背景圖像。
無論在哪里繪制新筆畫,目的地輸出合成都會“擦除”現有內容。
在此示例中:
演示: http : //jsfiddle.net/m1erickson/fjTLF/
碼:
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
body{ background-color: ivory; }
#wrapper{
position:relative;
width:300px;
height:300px;
}
#canvasTop,#canvasBottom{
position:absolute; top:0px; left:0px;
border:1px solid green;
width:100%;
height:100%;
}
#canvasTop{
border:1px solid red;
}
</style>
<script>
$(function(){
var canvas=document.getElementById("canvasTop");
var ctx=canvas.getContext("2d");
var canvas2=document.getElementById("canvasBottom");
var ctx2=canvas2.getContext("2d");
var canvasOffset=$("#canvasTop").offset();
var offsetX=canvasOffset.left;
var offsetY=canvasOffset.top;
var startX;
var startY;
var isDown=false;
var img=new Image();
img.onload=function(){
canvas.width=canvas2.width=img.width;
canvas.height=canvas2.height=img.height;
ctx.lineWidth=10;
ctx.lineCap = "round";
ctx.lineJoin = "round";
ctx.fillStyle="skyblue";
ctx.fillRect(0,0,canvas.width,canvas.height);
ctx2.drawImage(img,0,0);
};
img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/KoolAidMan.png";
function handleMouseDown(e){
e.preventDefault();
startX=parseInt(e.clientX-offsetX);
startY=parseInt(e.clientY-offsetY);
isDown=true;
}
function handleMouseUp(e){
e.preventDefault();
isDown=false;
}
function handleMouseOut(e){
e.preventDefault();
isDown=false;
}
function handleMouseMove(e){
if(!isDown){return;}
mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);
// Put your mousemove stuff here
ctx.save();
ctx.globalCompositeOperation="destination-out";
ctx.beginPath();
ctx.moveTo(startX,startY);
ctx.lineTo(mouseX,mouseY);
ctx.stroke();
startX=mouseX;
startY=mouseY;
}
$("#canvasTop").mousedown(function(e){handleMouseDown(e);});
$("#canvasTop").mousemove(function(e){handleMouseMove(e);});
$("#canvasTop").mouseup(function(e){handleMouseUp(e);});
$("#canvasTop").mouseout(function(e){handleMouseOut(e);});
}); // end $(function(){});
</script>
</head>
<body>
<div id="wrapper">
<canvas id="canvasBottom" width=300 height=300></canvas>
<canvas id="canvasTop" width=300 height=300></canvas>
</div>
</body>
</html>
如果您的背景是白色,則可以輕松使用
function eraser(){
context.fillStyle = "rgb(255, 255, 255)";
context.strokeStyle = "rgb(255, 255, 255)";
context.fontStyle = "rgb(255, 255, 255)";
}
您可以在其中包含帶有橡皮擦工具或其他東西的div,然后使用onclick =“”函數
<div id="eraser" class="eraser" onClick="eraser()">
如果您有背景圖片,則可以始終使用它來擦除背景圖片。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.