pixijs / pixi-react

Write PIXI apps using React declarative style
https://pixijs.io/pixi-react/
MIT License
2.42k stars 180 forks source link

Bug: How to mask sprites with sprites in pixi-react? #484

Closed realwugang closed 7 months ago

realwugang commented 7 months ago

Current Behavior

image

Expected Behavior

image

https://pixijs.com/7.x/playground?state=undefined&exampleId=masks.sprite

Steps to Reproduce

https://codesandbox.io/p/sandbox/mask-xzyqxx?file=%2Fsrc%2FApp.js%3A19%2C36

Environment

"dependencies": {
    "react": "^18.0.0",
    "react-dom": "^18.0.0",
    "react-scripts": "^5.0.0",
    "pixi.js-legacy": "7.4.2",
    "@pixi/react": "7.1.2"
  }
import { Application as PixiApplication } from "@pixi/app";
import { useRef, useCallback } from "react";
import { Stage, Container, Graphics, _ReactPixi, Sprite } from "@pixi/react";

export default function App() {
  const spriteRef = useRef();
  const graphicsRef = useRef();

  const draw = useCallback((g) => {
    g.beginFill("#eeeeee");
    g.drawRect(0, 0, 100, 100);
  }, []);

  return (
    <div className="App">
      <Stage
        width={window.innerWidth}
        height={window.innerHeight}
        options={{
          backgroundAlpha: 0,
          antialias: true,
          resolution: window.devicePixelRatio,
          forceCanvas: true
        }}
      >
        <Container>
          <Sprite
            mask={spriteRef.current}
            image="https://pixijs.com/assets/cells.png"
            scale={1.5}
          />
          <Sprite
            ref={spriteRef}
            image="https://pixijs.com/assets/flowerTop.png"
            x={310}
            y={190}
            anchor={0.5}
          />
          <Graphics ref={graphicsRef} draw={draw} />
        </Container>
      </Stage>
    </div>
  );
}

Possible Solution

No response

Additional Information

No response

realwugang commented 7 months ago

help!