假设我有:
var context = document.getElementById('test').getContext('2d');
// Background
context.fillStyle = '#000';
context.fillRect(0,0,300,300);
// 'P'
context.beginPath();
context.moveTo(90,89);
context.lineTo(161,89);
context.quadraticCurveTo(200,89,200,127);
context.quadraticCurveTo(200,166,148,166);
context.lineTo(115,166);
context.lineTo(108,210);
context.lineTo(69,210);
context.lineTo(90,89);
context.fillStyle = "#eee";
context.fill();
context.closePath();
context.globalCompositeOperation = 'destination-out';
// 'P' hole
context.beginPath();
context.moveTo(124,117);
context.lineTo(146,117);
context.quadraticCurveTo(160,117,160,127);
context.quadraticCurveTo(160,145,146,145);
context.lineTo(120,145);
context.lineTo(124,117);
context.fillStyle = '#ffffff';
context.fill();
context.closePath();
Run Code Online (Sandbox Code Playgroud)
这是有效的,除非您可以看到"洞"不是剪辑,因此如果背景不是实心,您根本无法设置孔的"填充"颜色以匹配背景.
因此我需要修剪这个洞.然而,当我这样做时,显示的'P'的唯一部分是由剪辑'孔'限定的部分.我需要反过来.我需要'P'来显示,但是用"洞"剪切部分,这样任何背景都会显示出来.
这是我得到的,但不完全在那里:
var context = document.getElementById('test').getContext('2d');
// Background
context.fillStyle = '#000';
context.fillRect(0,0,300,300);
// 'P' hole clip
context.beginPath();
context.moveTo(124,117);
context.lineTo(146,117);
context.quadraticCurveTo(160,117,160,127);
context.quadraticCurveTo(160,145,146,145);
context.lineTo(120,145);
context.lineTo(124,117);
context.clip();
context.closePath();
// 'P'
context.beginPath();
context.moveTo(90,89);
context.lineTo(161,89);
context.quadraticCurveTo(200,89,200,127);
context.quadraticCurveTo(200,166,148,166);
context.lineTo(115,166);
context.lineTo(108,210);
context.lineTo(69,210);
context.lineTo(90,89);
context.fillStyle = "#eee";
context.fill();
context.closePath();
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!
我知道你刚才问过这个问题,但我有一个答案.
你的第一个例子是半正确的.使用目标输出将起作用,但是为了不打扰您想要绘制的画布,我们创建一个新画布并在其中绘制它.
然后,一旦我们用切口绘制了我们的形状,我们就将整个画布绘制到原始画布上.由于新画布没有背景,因此它将保持透明度.
var canvas = document.getElementById('test'),
context = canvas.getContext('2d'),
// New canvas - we will draw the letter P on here
newCanvas = document.createElement('canvas'),
newContext = newCanvas.getContext('2d');
// Make sure you have enough room on your new canvas
newCanvas.width = canvas.width;
newCanvas.height = canvas.height;
with(newContext) {
// 'P'
beginPath();
moveTo(90,89);
lineTo(161,89);
quadraticCurveTo(200,89,200,127);
quadraticCurveTo(200,166,148,166);
lineTo(115,166);
lineTo(108,210);
lineTo(69,210);
lineTo(90,89);
fillStyle = "#eee";
fill();
closePath();
globalCompositeOperation = 'destination-out';
// 'P' hole
beginPath();
moveTo(124,117);
lineTo(146,117);
quadraticCurveTo(160,117,160,127);
quadraticCurveTo(160,145,146,145);
lineTo(120,145);
lineTo(124,117);
fillStyle = '#000';
fill();
closePath();
}
with(context) {
// Background
fillStyle = '#000';
fillRect(0,0,300,300);
// Simply reference the canvas element when drawing
drawImage(newCanvas, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)