Skip to content
Advertisement

Vue JS – How do I get WebGL to render via Vue without disappearing?

Question

I’m trying to use Vue JS to display some very simple WebGL. I use a Vue method call inside my canvas element to start the function that renders the WebGL. It all works fine, except that the rendered image flashes on the screen in a flash, then disappears. The image is a simple triangle that’s supposed to just sit onthe screen once rendered. I can’t find a solution to it?

As I’m very novice, I might have made some obvious mistakes that are there the problem, there may be I’m guessing some standardised way to incorporate simple, basic WebGl into the HTML via Vue, but I’ve just not been able to find it?

What are the possible solutions? Unfortunately, I’m supposed to use just basic WebGL (not Three.js or anything), but via basic Vue JS.

I’ll explain the details, below.

Background


I’m fairly new to Vue JS, and very new to WebGL. I’m following this series of tutorials on YouTube for my first intro to WebGL, with the particular video linked there containing the code I followed for this problem.

The only, perhaps significantly, different thing to the vid I did was of course to use Vue JS as a UI framework, rather than writing plain HTML and Javascript files, then manually using node js to set up a server.

In the tutorial, he links via a tag in the html a separate .js file containing the WebGL code. In this script, Javascript just uses query selector to grab the canvas and its context, and assign it as the place to output the render.

Inside Vue JS, I wasn’t sure how to tell Vue to just run an external script that wasn’t a method or computed etc, so I put the WebGL code in a method, and output it to the screen by calling the method inside the canvas tag in the vue template:

JavaScript
JavaScript

Now this actually works, and the little red triangle is actually rendered – except as soon as it’s rendered to the screen, it disappears.


My Thoughts As To Why

Here’s what I think happens: when the vue component first loads, it runs the runWebGL() method I’ve put there, creates and then displays the lil red triangle in the canvas. When the method has finished however, well it’s no longer running so everything in it is destroyed and there’s no WebGL to output.

If I put an alert function for example at the end of runWebGL() to prevent the function from automatically ending, you can see the triangle staying outputted to the screen until the user closes the alert.

What I’m assuming then is that when, as the guy has done in the video, you attach some script directly to the html via tags, be they local or externally referenced scripts, once they’re finished they don’t close. So the data etc in them is still available, even though they’ve been parsed to the end?

How do I solve this in Vue JS, then? Am I supposed to implement some way of keeping Vue JS methods ‘running’ even when the code in them has finished executing? Or is there some other, more standardised method for meshing WebGL and Vue JS?

I’m aware that there are some WebGL frameworks out there, and that there are some that try to blend Vue and WebGL, but unfortunately I’m not supposed to use them – I’ve got to get it as close to native VueJS and plain WebGL as I can.

Thanks very much indeed for your help!


The Javascript and WebGL code, which is identical to the one from the video, which I put inside runWebGL() is here:

JavaScript

Advertisement

Answer

You need to learn more about the life cycle of the vue-instance. For example try the mounted hook:

JavaScript

[ https://jsfiddle.net/stdob__/fyojs1vn/ ]

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement