reactjspixi.js

Masking in React-Pixi inlet/react-pixi


The image I'm using for the sprite is much larger than required (910px). I need to dynamically change the height of the container, masking the image.

Does anyone know how to do this?

<Container
      x={x}
      y={y}
      width={width}
      height={height}>
   <Sprite
        image={image}
        anchor={[0, 0]}
        x={0}
        y={0}
        width={width}
        height={910}
        rotation={0} />
 </Container>
 @inlet/react-pixi: "^1.1.9"
 pixi.js: "^5.2.0"
 react: "^16.12.0"
 react-dom: "^16.12.0"

This unfortunately doesnt work either as the textures stays static whilst the rectangle moves around

 <Graphics
      draw={(g) => {
        g.clear()
        const texture = new PIXI.Texture.from(image)
        g.beginTextureFill(texture)
        g.drawRect(x, y, width, height)
        g.endFill()
      }} /> 

Solution

  • Try this:

    const maskRef = useRef()
    
    <Container mask={maskRef?.current} x={x} y={y} width={width} height={height}>
      <Graphics
        name="mask"
        draw={React.useCallback(
          (g) => {
            g.beginFill(0x000000)
            g.drawRect(0, 0, size.width + 3, size.height + 3)
            g.endFill()
          },
          [width, height]
        )}
        ref={maskRef}
      />
      <Sprite
        image={image}
        anchor={[0, 0]}
        x={0}
        y={0}
        width={width}
        height={910}
        rotation={0}
      />
    </Container>
    

    Official doc at here, explaining how it works and limitations