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.

Poor performance with single physics object (Firefox?)

See original GitHub issue

Hi,

Iā€™m getting pretty choppy performance using Firefox with a very simple implementation of matter to simulate one object.

Either this is an issue with Firefox (?) or perhaps my code which Iā€™ll add below. I really didnā€™t think that one physics object would lag the page like it does.

// --- matter.js ---
// Initialize world
let Engine = Matter.Engine,
Render = Matter.Render,
World = Matter.World,
Vertices = Matter.Vertices,
Bodies = Matter.Bodies;

let engine = Engine.create(),
    world = engine.world;

// Setup when init() is called from a button press
function init() {
    let width = window.innerWidth;
    let height = Math.max(
        document.body.scrollHeight, document.documentElement.scrollHeight,
        document.body.offsetHeight, document.documentElement.offsetHeight,
        document.body.clientHeight, document.documentElement.clientHeight
    );

    let wrapper = document.querySelector('.wrapper-medium'),
        text = document.querySelector('p'),
        pic = document.getElementById('trigger');

    let wrapperBounds = wrapper.getBoundingClientRect(),
        textBounds = text.getBoundingClientRect(),
        picBounds = pic.getBoundingClientRect();

    let wrapWidth = wrapperBounds.width,
        textTop = textBounds.top + (575 / 2) + window.scrollY,
        picMiddle = (picBounds.bottom + picBounds.top) / 2;

    let picRatioX = pic.clientWidth/pic.naturalWidth,
        picRatioY = pic.clientHeight/pic.naturalHeight; 
        
    // Some random force
    let xforce = (Math.random() * 8) - 4,
        yforce = (Math.random() * 8) - 4;
    
    engine.events = {};
    World.clear(engine.world);
    Engine.clear(engine);
    
    engine = Engine.create();

    let render = Render.create({
        element: document.querySelector('#matter'),
        engine: engine,
        options: {
            wireframes: false,
            background: 'transparent',
            width: width,
            height: height
        }
    });
    
    // Custom object
    let facePath = Vertices.fromPath('387 30 202 11 92 63 67 259 169 472 250 489 347 440 440 136');
    Vertices.scale(facePath, picRatioX, picRatioY);

    let face = Bodies.fromVertices(width / 2, picMiddle, facePath,
    {
        density: 0.0025,
        restitution: 0.75,
        friction: 0.75,
        force: { x: xforce, y: yforce },
        render: {
            sprite: {
                texture: '../assets/images/profile-small.png', // set texture here
                xScale: picRatioX,
                yScale: picRatioY,
            }
        }
    }
    );

    // Add objects
    World.add(engine.world, [
        Bodies.rectangle(width / 2, height + 50, width, 100, {
            isStatic: true
        }),
        Bodies.rectangle(width / 2, -50 - window.scrollY, width, 100, {
            isStatic: true
        }),
        Bodies.rectangle(-50, height / 2, 100, height, {
            isStatic: true
        }),
        Bodies.rectangle(width + 50, height / 2, 100, height, {
            isStatic: true
        }),
        Bodies.rectangle(width / 2, textTop, wrapWidth, 575, {
            isStatic: true,
            friction: 0.75,
            render: {
            visible: false
            }
        }),
        face,
    ]);
    Engine.run(engine);
    Render.run(render);
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tommers-walkercommented, Jul 27, 2021

Hi Liam, firstly thank you for such an awesome library! šŸ‘ Not sure whether to open a new issue or revisit this one, but weā€™re having issues with performance in Firefox. Iā€™ve made a codepen and turned on debug mode: https://codepen.io/niceandserious/pen/e0448a9f65be6cf9e9526ba9a80795cc?editors=1010 In Chrome and Safari the fps stays at 60 but Firefox seems to really struggle, sometimes almost dropping to half that as the number of bodies increases. I tested it in wireframe mode as per your suggestion above and sure enough Firefox behaved. So I guess my question is is there a particular reason why Firefox struggles when using sprites?

0reactions
liabrucommented, Sep 6, 2021

@tommers-walker sorry Iā€™d missed this earlier - interested to know if you did get to the bottom of it?

The way sprites work is pretty straightforward, just drawing an image to canvas in the usual way, so it could just be that Firefox has trouble with that so not sure if thereā€™s something I can fix.

If it is just a browser limitation then you may need to look into alternatives like pixi.js which use WebGL and GPU instancing and might be better (but Iā€™m not sure).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Firefox's performance settings - Mozilla Support
Firefox automatically uses settings that work best with your computer. ... In the General panel, go down to the Performance section.
Read more >
Firefox/Tweaks - ArchWiki - Arch Linux
Performance. Improving Firefox's performance is divided into parameters that can be inputted while running Firefox or otherwise modifyingĀ ...
Read more >
Optimizing Scenes Ā· Hubs by Mozilla
One way to improve performance for everyone is to reduce the complexity of a scene. This could involve reducing the number of objects...
Read more >
My game lags in mobile devices. - Construct 3
Physics objects will slow down a lot if too many like more than 10 - 20 ... in one of my projects, and...
Read more >
How Low-e Glass Works - Vitro Glass Education Center
One way this performance is achieved is through the use of passive and solar control low-e coatings. So, what is low-e glass? In...
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