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

_requestAnimationFrame TypeError when running default engine/render loops in Firefox Extension #652

Closed
calebchalmers opened this issue Aug 30, 2018 · 1 comment

Comments

@calebchalmers
Copy link

I created a script using the minimal example from the Getting Started wiki page, and included it and matter.js as content scripts in a basic Firefox Extension.

When I run the extension on a webpage, the line Engine.run(engine); raises the exception TypeError: 'requestAnimationFrame' called on an object that does not implement interface Window.

After inspecting the matter.js source I found that the problem comes from this line:
image

The same problem occurs with Render.run(render); and the corresponding Runner.run function in matter.js.

This seems to be because the global object in the Firefox extension's content scripts is not window but rather a different sandbox object, therefore _requestAnimationFrame is not being called on window and raises the exception.

To prove this, I replaced _requestAnimationFrame(render) with window.requestAnimationFrame(render) in both Runner.run functions and the loops successfully ran without any errors.

Using _replaceAnimationFrame.call(window, render) also works, and still maintains the cross-browser support that the original method provides.

This was tested using Firefox v61.0.2, and both Matter v0.14.2 and v0.10.0.

@liabru
Copy link
Owner

liabru commented Jun 24, 2024

I'm not sure if this is still the case but leaving this here in case someone runs into something similar:

  • try polyfill window.requestAnimationFrame for your environment
  • or fork and customise Matter.Runner
  • or create your own game loop that calls Engine.update as needed

As of 0.20.0 you can also now:

Thanks for the report.

@liabru liabru closed this as completed Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants