gpt4 book ai didi

html - Canvas 'Clip' 反向 Action ?

转载 作者:太空狗 更新时间:2023-10-29 14:59:42 25 4
gpt4 key购买 nike

假设我有:

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();

这是有效的,除了你可以看到“洞”不是剪辑,所以如果背景不是实心的,你根本无法设置洞的“填充”颜色来匹配背景。

因此我需要剪掉这个洞。但是,当我这样做时,显示的“P”的唯一部分是夹子“孔”绑定(bind)的部分。我需要反过来。我需要“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();

感谢您的帮助!

最佳答案

我知道你刚才问过这个问题,但我有一个答案要给你。

您的第一个示例只对了一半。使用 destination-out 会起作用,但是为了不打扰您要绘制的 Canvas ,我们创建一个新 Canvas 并在其中绘制。

然后,一旦我们用剖面图在上面绘制了我们的形状,我们就将整个 Canvas 绘制到原始 Canvas 上。由于新 Canvas 没有背景,它将保持透明。

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);
}

关于html - Canvas 'Clip' 反向 Action ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4821679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com