davidfig / pixi-viewport

A highly configurable viewport/2D camera designed to work with pixi.js
https://davidfig.github.io/pixi-viewport/
MIT License
1.04k stars 174 forks source link

fix window references during SSR #434

Closed RJWadley closed 1 year ago

RJWadley commented 1 year ago

fixes #432

milkyskies commented 1 year ago

Thank you! I hope this gets merged.

ahmafi commented 1 year ago

I have tried this on a next.js project, but I'll get Cannot read properties of undefined (reading 'shared').

I'm using it like this in a useEffect hook:


  useEffect(() => {
    if (typeof window === 'undefined') return;
    const app = new PIXI.Application({ width: 500, height: 400 });
    containerRef.current.appendChild(app.view);

    // create viewport
    const viewport = new Viewport({
      screenWidth: window.innerWidth,
      screenHeight: window.innerHeight,
      worldWidth: 1000,
      worldHeight: 1000,

      events: app.renderer.events, // the interaction module is important for wheel to work properly when renderer.view is placed or scaled
    });

    // add the viewport to the stage
    app.stage.addChild(viewport);

    // activate plugins
    viewport.drag().pinch().wheel().decelerate();

    // add a red box
    const sprite = viewport.addChild(new PIXI.Sprite(PIXI.Texture.WHITE));
    sprite.tint = 0xff0000;
    sprite.width = sprite.height = 100;
    sprite.position.set(100, 100);
  }, []);

I have also tested with dynamic import:

  useEffect(() => {
    (async () => {
      if (typeof window === 'undefined') return;

      const pv = await import('pixi-viewport');

      const app = new PIXI.Application({ width: 500, height: 400 });
      containerRef.current.appendChild(app.view);

      // create viewport
      const viewport = new pv.Viewport({
        screenWidth: window.innerWidth,
        screenHeight: window.innerHeight,
        worldWidth: 1000,
        worldHeight: 1000,

        events: app.renderer.events, // the interaction module is important for wheel to work properly when renderer.view is placed or scaled
      });

      // add the viewport to the stage
      app.stage.addChild(viewport);

      // activate plugins
      viewport.drag().pinch().wheel().decelerate();

      // add a red box
      const sprite = viewport.addChild(new PIXI.Sprite(PIXI.Texture.WHITE));
      sprite.tint = 0xff0000;
      sprite.width = sprite.height = 100;
      sprite.position.set(100, 100);
    })();
  }, []);

What am I missing something?

pixi.js: 6.5.9
pixi-viewport: 5.0.1
RJWadley commented 1 year ago

@ahmafi I believe the latest version of pixi-viewport uses pixi.js v7, and it looks like you're using pixi.js v6. Could you try it with 7?

VasiliKubarka commented 1 year ago

@RJWadley I suppose merging will go faster if you will clean up yarn.lock changes

rbozan commented 1 year ago

can confirm it works for Sveltekit

artokun commented 1 year ago

bump

artokun commented 1 year ago

@RJWadley you should rebase + fix your yarn.lock diff

himrocks33 commented 1 year ago

Merge please!

Dayonel commented 1 year ago

Bump

davidfig commented 1 year ago

Thanks!

pietrovismara commented 9 months ago

Was this released on NPM? I'm on v5.0.2 and I still get errors from the window references.

RJWadley commented 6 months ago

yeah, looks like this was never published. I think the library would probably benefit a lot from an auto-publish action on push to main.

@davidfig I understand you might be a bit busy, would you like help maintaining pixi-viewport?

h-a-s-k commented 4 months ago

viewport viewport2 Still erroring out around these, had to compile myself