question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use PIXI.Application() when there is already a canvas element in HTML?

See original GitHub issue

Good evening,

my issue is not a bug, but more my difficulty to understand the documentation.

I am using pixi.js version 5.2.0 to create a word app at Facebook and your library is great, thank you all very much for developing it.

However recently I have migrated my code from pixi.js 4 to 5 and I am not sure how to change my code:

        var ratio = BOARD_WIDTH / (BOARD_HEIGHT + SmallTile.HEIGHT);
        var renderer = new PIXI.Renderer({
                width: BOARD_WIDTH,
                height: BOARD_HEIGHT + SmallTile.HEIGHT, 
                view: document.getElementById('board')
        });

        window.onresize = function(event) {
            var w = Math.min(window.innerWidth, screen.width) - 2 * MENU_WIDTH;
            var h = Math.min(window.innerHeight, Math.round(.7 * screen.height));

            if (w / h >= ratio) {
                w = Math.round(h * ratio);
            } else {
                h = Math.round(w / ratio);
            }

            renderer.view.style.width = (w - 2 * PADDING) + 'px';
            renderer.view.style.height = (h - 2 * PADDING) + 'px';
        };

        window.onresize();

My problem is that the v5 doc suggests:

const app = new PIXI.Application();

// The application will create a canvas element for you that you
// can then insert into the DOM
document.body.appendChild(app.view);

But I already have a canvas element in my HTML file:

   <TABLE WIDTH="100%"><TR>
   <TD ALIGN="center"><CANVAS ID="board" STYLE="border: 1px dotted white;"></CANVAS></TD>
   </TR></TABLE>

How can I please use the new PIXI.Application, but attach it to the existing canvas element?

As you see probably see from my code I am not very proficient in the latest Javascript syntax, but I think my issue is very simple, I am just missing some guidance. Thank you.

Environment

Screenshot 2020-02-06 at 21 13 17

Here the error reported by some of my game users (using Opera browser or maybe Yandex browser - the communication with the user is challenging, she is an elderly lady):

Screenshot 2020-02-06 at 21 30 05

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
bigtimebuddycommented, Feb 6, 2020

Do something like you were doing in v4, pass the view option to the Application constructor:

const app = new Application({ view: document.getElementById(“board”) })

Check out the docs: http://pixijs.io/docs

1reaction
eXponentacommented, Feb 6, 2020

Pixi (pixi legacy) must automatically detect supported renderer (‘forceCanvas:false’) when you use Application.

If it isn’t true, it is bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use PIXI.Application() when there is already a canvas ...
I am using pixi.js version 5.2.0 to create a word app at Facebook and your library ... But I already have a canvas...
Read more >
Use an existing canvas without losing contents in pIxi.js
Update Pixi's animations and call it's renderer; Clear the third (on-screen) canvas; Draw the contents of the first canvas to the third; Draw ......
Read more >
PIXI.Application - PixiJS API Documentation
Class: Application. PIXI.Application. Convenience class to create a new PixiJS application. This class automatically creates the renderer, ...
Read more >
Getting Started - PixiJS
There are only a few steps required to write a PixiJS application: ... Create a new folder named pixi-test , then copy and...
Read more >
Getting started with Pixi.js - Ihatetomatoes
I am using CodePen for the final demo, you can find the PIXI code in the JS pane of the ... Now we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found