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。
答案 0 :(得分:0)
这是一个简单的例子,它使用“目的地输出”合成来擦除前景填充以显示背景图像。
目的地合成将“擦除”绘制新笔画的现有内容。
在这个例子中:
演示: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>
答案 1 :(得分:0)
如果你有白色背景,你可以轻松使用
之类的东西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()">
如果您有背景图像,则可以使用此图像擦除背景图像。