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

Implementation in Gatsby #7

Open
MrUltimate opened this issue Dec 31, 2019 · 11 comments
Open

Implementation in Gatsby #7

MrUltimate opened this issue Dec 31, 2019 · 11 comments
Assignees
Labels
bug Something isn't working

Comments

@MrUltimate
Copy link

I'm trying to add this into my GatsbyJS site, however, on every page change, the script getting added again which ruins the performance of the sim. Is there a way to handle that change? Is there a way to perhaps remove the script and then add it back when someone navigates back to the page with the sim?

@x8BitRain
Copy link
Owner

You can try call gl.getExtension('WEBGL_lose_context').loseContext(); on page change, that will stop the sim from running, otherwise removing the canvas from the DOM and adding it again will reset the sim too. I'd like to try make the script a function that you can call whenever at some point, but I hope that helps.

@MrUltimate
Copy link
Author

Thanks for the response!

So put gl.getExtension('WEBGL_lose_context').loseContext(); under a ComponentWillUnMount()?

@MrUltimate
Copy link
Author

If it helps, this is the error I get in console when I navigate back to the page with the canvas element:

Screen Shot 2019-12-31 at 6 59 13 PM

@x8BitRain
Copy link
Owner

You can also try:

componentDidMount() {
    const script = document.createElement("script");
    script.src = "./assets/js/fluid-init.js";
    script.id = "fluid-src";    // give the script tag an ID
    script.async = true;
    script.onload = () => this.fluidLoaded();
    document.body.appendChild(script);
  }

componentDidUnMount () {
    gl.getExtension('WEBGL_lose_context').loseContext();
    document.querySelector("#fluid-src").remove();
}

So when the component unmounts it removes the script and removes the GL context, then when it mounts again it will re-add the script and re-execute the script.

gl.getExtension('WEBGL_lose_context').loseContext(); might not be needed with this approach but give it a shot.

@MrUltimate
Copy link
Author

Thanks! And Happy New Year!

Unfortunately, I've already tried that approach by that doesn't seem to work. gl is undefined in that context so that doesn't work from the component. If I copy the declarations from the script, then getWebGLContext() is returned as undefined. At that point, so get that to work, I'd have to copy the entire script and create it into a function, like you mentioned earlier. I don't have much experience in this unfortunately. Would you be able to help out?

@x8BitRain
Copy link
Owner

Sure, if could your code with me I can try and find a way to make it work then explain how I did it and make some changes to this repo to make it work better in multi page contexts.

@MrUltimate
Copy link
Author

MrUltimate commented Jan 2, 2020

@x8BitRain
Copy link
Owner

I couldn't get your site to run with gatsby develop because your datoCMS API key is missing from .env and it just seems to break further if I try remove the datoCMS packages. I don't really want to reverse it into a standalone react site so If you're okay with sending me your .env file you can email to me.

@MrUltimate
Copy link
Author

Just sent it to you!

@x8BitRain x8BitRain added the bug Something isn't working label Jan 6, 2020
@x8BitRain x8BitRain self-assigned this Jan 6, 2020
@x8BitRain
Copy link
Owner

Thanks, I changed the script so that the fluid sim can be called and started with fluid() and stopped by calling fluidStop(). But even with those functions working just fine the weird stacking problem still happens. I think it's something to do with React's pagination that keeps the sim running in the background regardless of the page change. I guess I'll have to look deeper into webGL garbage collection so the stop script will properly kill the process on navigating away from the page it's on.

The only short term solution I can think of is just setting your home page link to an absolute URL like
<Link className="link home" to={"https://helloshivam.com/"}>
That way the entire page reloads fully and the script will run properly without overlapping.
Or you could try make a basic HTML with the sim running in it and embed that in an iframe then replace the canvas with that.
I made a PR here with the changes to the script https://github.com/MrUltimate/portfolio-2020/pull/1

Sorry I couldn't get it working perfectly but I'll try and make this implementation work correctly with page changes soon.

@MrUltimate
Copy link
Author

Thanks for all the help, looks like with this update, the WebGL canvas fails to render completely, with lots of errors that I don't understand. ☹️

Screen Shot 2020-01-10 at 12 28 15 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants