【问题标题】:Using globalCompositeOperation to mask Group of shapes, in React Konva在 React Konva 中使用 globalCompositeOperation 来掩盖形状组
【发布时间】:2019-02-09 16:28:04
【问题描述】:

我的项目使用 React Konva (https://github.com/konvajs/react-konva)

我正在尝试将多个形状绘制到 Group 中,并使用它来掩盖“下方”的图像。

当我的组件使用globalCompositeOperation 绘制单个形状时,它会产生预期的结果。代码如下:

render() {
        return (

            <Group >
                <Group>
                    <Image 
                        image={this.state.image} 
                        ref={node => { this.image = node; }} 
                    />
                    <Group>
                        <Rect fill={"#555555"} 
                            width={200} height={200} 
                            x={100} y={100} 
                            globalCompositeOperation={"destination-in"}
                        />
                    </Group>
                </Group>
            </Group>
        )

    }

结果:

注意图像是如何被裁剪成矩形的,露出下面的文本层。

但是,只要形状在组内移动,并且我在那里应用 globalCompositeOperation,就不会发生遮罩。相关部分代码:

<Group>
            <Image 
                image={this.state.image} 
                ref={node => { this.image = node; }} 
            />
            <Group globalCompositeOperation={"destination-in"}>
                <Rect fill={"#555555"} 
                    width={200} height={200} 
                    x={100} y={100} 
                />
            </Group>
        </Group>

结果:

这很奇怪,因为 Konva 文档表明 Group 实际上有一个属性 globalCompositeOperation(请参阅 https://konvajs.github.io/api/Konva.Group.html#globalCompositeOperation__anchor)。

知道如何让 (React) Konva 在Group 级别应用globalCompositeOperation 而不仅仅是在Shape 级别吗?

【问题讨论】:

    标签: javascript reactjs html5-canvas konvajs


    【解决方案1】:

    啊,刚刚找到解决办法了。

    似乎在应用globalCompositeOperation之前需要缓存整个Group。我假设这意味着该组首先被扁平化/光栅化。

    在我的 React 组件中,我实现了如下解决方案:

     import React from 'react';
    import { Image, Group, Rect } from 'react-konva';
    
    class CutoutImage extends React.Component {
        state = {
            image: null,
            mask: null
        }
    
        componentDidMount() {
            const image = new window.Image();
            image.src = "/images/frisbee.jpg";
            image.onload = () => {
                this.setState({ image });
            }
        }
    
        componentDidUpdate() {
            if (this.image) {
                this.image.cache();
            }
            if (this.mask) {
                this.mask.cache();
            }
        }
    
        render() {
            return (
    
                <Group>
                    <Image 
                        image={this.state.image} 
                        ref={node => { this.image = node; }} 
                    />
                    <Group 
                        globalCompositeOperation={"destination-in"} 
                        ref={node => {this.mask = node; }}
                        >
                        <Rect fill={"#555555"} 
                            width={200} height={200} 
                            x={100} y={100} 
                        />
                        <Rect fill={"#dddddd"} 
                            width={200} height={200} 
                            x={300} y={300} 
                        />
                        <Rect fill={"#dddddd"} 
                            width={200} height={100} 
                            x={150} y={350} 
                        />
                    </Group>
                </Group>
            )
    
        }
    
    }
    export default CutoutImage;
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-08
      • 2019-12-26
      • 1970-01-01
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 2020-12-07
      相关资源
      最近更新 更多