Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: changing the hitArea prop does not change the hit area of the underlying pixi element #563

Open
CaptainTux opened this issue Jan 6, 2025 · 0 comments
Labels
v8 Issues related to Pixi React v8

Comments

@CaptainTux
Copy link

Current Behavior

When supplying the hitArea prop with hitArea={new Rectangle(0, 0, width, height)} to an element, the hit area does not actually change if the width or height change after the first render of the component.

Expected Behavior

The hit area of the element corresponds to the rectangle supplied to the hitArea prop.
As a workaround I created this hook, which is rather inconvenient, but it works.

import { Rectangle } from "pixi.js";
import { useLayoutEffect, useRef } from "react";

export const useHitArea = (x: number, y: number, width: number, height: number) => {
  const rectRef = useRef(new Rectangle(x, y, width, height));

  useLayoutEffect(() => {
    rectRef.current.copyFrom(new Rectangle(x, y, width, height));
  }, [x, y, width, height]);

  return rectRef;
};

Steps to Reproduce

const MyContainer = () => {
  const width = 600;
  const [height, setHeight] = useState(600);
  return (
    <graphics
      eventMode="static"
      onMouseDown={() => {
        setHeight((prev) => prev - 100);
      }}
      hitArea={new Rectangle(0, 0, width, height)}
      draw={(g) => {
        g.clear();
        g.rect(0, 0, width, height).fill(0xff0000);
      }}
    />
  );
};

After clicking at the bottom of the rectangle, one can still click outside of the rectangle after it shrinks.
Unfortunately I cannot get the sandbox to run, but this example should demonstrate the problem.

Environment

Possible Solution

No response

Additional Information

No response

@trezy trezy added the v8 Issues related to Pixi React v8 label Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v8 Issues related to Pixi React v8
Projects
None yet
Development

No branches or pull requests

2 participants