首页javascriptdrawing_settingsCanvas - 如何使用复合目的地输出擦除现有图像

Canvas - 如何使用复合目的地输出擦除现有图像

我们想知道如何使用复合目的地输出擦除现有图像。



<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>//<![CDATA[ 
window.onload=function(){
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var img = new Image();
img.onload = function () {
    start();
}
img.src = "http://www.w3cschool.cn/style/download.png";
function start() {
    ctx.drawImage(img, 0, 0);
    ctx.globalCompositeOperation = "destination-out";
    ctx.beginPath();
    ctx.moveTo(0, 0);
    ctx.lineTo(300, 300);
    ctx.moveTo(300, 0);
    ctx.lineTo(0, 300);
    ctx.lineWidth = 30;
    ctx.fillStyle = "blue";
    ctx.stroke();
}
}//]]>  
</script>
</head>
<body>
  <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>