英文:
How to clip several complex Images as Shapes in Konva.js, I just found only one complex Image solution in stackoverflow
问题
Hello @lavrton,我在stackoverflow上找到一个类似的问题链接,你提到的解决方案是一个形状,但它无法同时呈现多个形状。因为sceneFunc和hitFunc都使用整个画布上下文,当我绘制新的形状时,旧的形状不会显示在正确的位置,而会显示在上下文的对应位置。如何处理这个问题?
多次使用下面的片段构建包含蒙版的多个节点,但会遇到上面提到的问题
const mask = new Konva.Shape({
id: KONVA_NODE_ID.MASK,
x: 0.5 * width * (1 - range),
y: 0.5 * height * (1 - range),
width: width * range,
height: height * range,
sceneFunc: function (context, shape) {
context.drawImage(imageObj, 0, 0, shape.width(), shape.height())
context.globalCompositeOperation = 'source-in'
context.drawImage(backObj, 0, 0, shape.width(), shape.height())
},
hitFunc: function (context, shape) {
context.fillStrokeShape(shape)
},
})
group.add(mask)
英文:
hello @lavrton ,I found a similar problem in stackoverflow Link, your mentioned solution is a shape, but it can't render several shapes in the meantime. because the sceneFunc and hitFunc both use the whole canvas context, when I draw the new one, the old one will not show in the correct place, but in the counterpart of the context. How to handle it?
use the snippets below several times to build several nodes containing mask, but it meets the problem above
const mask = new Konva.Shape({
id: KONVA_NODE_ID.MASK,
x: 0.5 * width * (1 - range),
y: 0.5 * height * (1 - range),
width: width * range,
height: height * range,
sceneFunc: function (context, shape) {
context.drawImage(imageObj, 0, 0, shape.width(), shape.height())
context.globalCompositeOperation = 'source-in'
context.drawImage(backObj, 0, 0, shape.width(), shape.height())
},
hitFunc: function (context, shape) {
context.fillStrokeShape(shape)
},
})
group.add(mask)
答案1
得分: 1
如果您想进行遮罩处理,最好在 Konva 外部使用外部画布元素进行处理,然后将该画布用于 Konva.Image
:
英文:
If you want to have masking, it is better to do it outside Konva with an external canvas element and then just use that canvas for Konva.Image
:
const canvas = document.createElement('canvas');
canvas.width = imageObj.width;
canvas.height = imageObj.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(imageObj, 0, 0)
ctx.globalCompositeOperation = 'source-in'
ctx.drawImage(backObj, 0, 0);
const image = new Konva.Image({
image: canvas
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论