While in most web apps you’ll likely be rendering to the DOM, there are a few special cases where you might want to use Vue for something else. Say you’re developing an app with WebGL and you’d like to describe it with Vue as a tree of components. While Vue doesn’t explicitly support this at the moment, it’s entirely possible to implement yourself, as you’ll discover below.
We’ll be using pixi.js for this guide, so be sure to install that via npm. This is an advanced article, and as such, it is assumed that you already have a skeleton app prepared with webpack and vue 2.2+. Additionally, explanation will largely happen through code comments due to the complex nature of the components.
The goal will be to produce a set of three Vue components, the renderer, container, and a sprite component in order to draw textures in a 2D WebGL canvas with pixi.js.
The end result should look something like this:
Note: This is not a guide on implementing a complete PIXI renderer in Vue, just the basics. You’ll have to handle anything further yourself if you intend to do something more serious.
This is the component that initializes our PIXI stage and provides the PIXI objects to all of its descendants. (via. Vue 2.2+'s provide / inject system.)
This component primarily does two things.
The container component can contain an arbitrary amount of sprites or other containers, allowing for group nesting.
The container component takes two propertis, x and y, for position and emits two events, pointerdown and tick to handle clicking and frame updates, respectively. It can also have any number of containers or sprites as children.
The sprite component is almost the same as the container component, but with some extra tweaks for PIXI’s Sprite API.
The sprite is pretty much the same as a container, except that it has an imagePath prop which allows you to choose what image to load and display from the server.
A simple Vue app that uses these three components to render the image at the start of the article:
👉 Hopefully your knowledge of what can be done with Vue components has now been significantly expanded and the ideas are flowing like a waterfall. These are practically unexplored grounds, so there’s plenty you could choose to do!
Tread carefully! (or don’t!)
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Almost 4 years old article but very interesting. I would like to deepen your work and continue what you have started. Can you tell me why the tickers are slow? Do you have any idea how to improve the performance to be as close as that of PIXI? Cheers!