如何在 KonvaJS 中的图层上绘制倒置元素
How to draw an inverted element on a layer in KonvaJS
有没有什么方法可以在 KonvaJS 中添加一个绘制在其边界之外的形状?基本上是图层中的 "hole",这样我就可以通过使圆圈周围的所有内容变暗(不透明度低的黑色)来突出显示 canvas 中的一个点。
非常感谢任何帮助!
干杯
编辑:
这是我的意思的图像(我还不允许嵌入它):
https://i.imgur.com/gvTqgN0.jpg
我希望可能有这样的东西:
Konva.Circle({
x: 100,
y: 100,
radius: 50,
opacity: 0.3,
fill: 'black',
inverted: true
})
然后这又会 "not draw" 一个圆圈,但它周围的一切都会采用给定的属性。在这种情况下,除了圆圈之外,它都会变暗一点。
您可以使用自定义形状执行此操作:
const shape = new Konva.Shape({
width: stage.width(),
height: stage.height(),
// what is color of background ?
fill: 'rgba(0,0,0,0.2)',
sceneFunc: (ctx, shape) => {
// draw background
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(shape.width(), 0);
ctx.lineTo(shape.width(), shape.height());
ctx.lineTo(0, shape.height());
ctx.lineTo(0, 0);
// now cut circle from the background
// we can do this by useing arc
const x = 200;
const y = 200;
const radius = 10;
// but it must be counter-clockwise
// so the last argument true is very important here
ctx.arc(200, 200, 100, 0, Math.PI * 2, true);
ctx.fillStrokeShape(shape);
},
// remove shape from hit graph, so it is not visible for events
listening: false
});
有没有什么方法可以在 KonvaJS 中添加一个绘制在其边界之外的形状?基本上是图层中的 "hole",这样我就可以通过使圆圈周围的所有内容变暗(不透明度低的黑色)来突出显示 canvas 中的一个点。
非常感谢任何帮助!
干杯
编辑: 这是我的意思的图像(我还不允许嵌入它): https://i.imgur.com/gvTqgN0.jpg
我希望可能有这样的东西:
Konva.Circle({
x: 100,
y: 100,
radius: 50,
opacity: 0.3,
fill: 'black',
inverted: true
})
然后这又会 "not draw" 一个圆圈,但它周围的一切都会采用给定的属性。在这种情况下,除了圆圈之外,它都会变暗一点。
您可以使用自定义形状执行此操作:
const shape = new Konva.Shape({
width: stage.width(),
height: stage.height(),
// what is color of background ?
fill: 'rgba(0,0,0,0.2)',
sceneFunc: (ctx, shape) => {
// draw background
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(shape.width(), 0);
ctx.lineTo(shape.width(), shape.height());
ctx.lineTo(0, shape.height());
ctx.lineTo(0, 0);
// now cut circle from the background
// we can do this by useing arc
const x = 200;
const y = 200;
const radius = 10;
// but it must be counter-clockwise
// so the last argument true is very important here
ctx.arc(200, 200, 100, 0, Math.PI * 2, true);
ctx.fillStrokeShape(shape);
},
// remove shape from hit graph, so it is not visible for events
listening: false
});