Categories: Project Tips and Tricks
Tags: Components, SVG or CANVAS, VUE
SVG or CANVAS: Choosing the Right One to Use
How does one choose between SVG and CANVAS? Let’s consider some tasks and look at which would be better.
Creating a Grid
Let’s begin with our first task — to create a component in the form of a grid. The component should respond to the click by creating a square. There should also be a reaction to drag and drop on an empty field and on the created square.
You already know you’ll be working with VUE. You know for a fact that you won’t use block HTML to create and interact with the grid, because a div that’s made up of a thousand other divs is a bad idea. And now you are faced with a choice: SVG or CANVAS?
The first important question: how and where is it planned to use the component?
If we are talking only about the desktop, then there is no significant difference in this choice between SVG and CANVAS. However, if the list of requirements includes mobiles and tablets, and especially if mobile is first, then SVG gains +1 point for better scalability. There is a possibility that the user will want to stretch your component with his or her fingers, zooming in many times. You have to be ready for it. SVG has high quality at any resolution.
So you’ve created a field, and it’s ready to go.
Next stage: click response.
If in your case the correct click response is a painted pixel, then CANVAS wins. But since SVG is vector based and composed of shapes and our reaction to a click is the creation of a certain graphic shape, SVG will gain +1 point.
Next question: drag and drop.
In our case, the reaction to drag and drop on an empty field should be the creation of a square, one of the corners of which will follow the mouse/finger. Drag and drop on an already created square will move it around the field. I put +1 for SVG.
The general rule, in this case, is: If you work with vectors, with shapes, your choice is SVG. If your task is more about drawing by pixels – your choice is CANVAS.
The next point to pay attention to is making changes using CSS.
Just keep in mind that this is impossible in CANVAS, and you are depriving yourself of a convenient tool. Also, with SVG you can access your shapes in the DOM while working with CANVAS is limited in this regard.
SVG uses an approach where you simply input your drawing instructions and the browser’s graphics API creates the model in memory and translates it into drawing commands for your browser. Unlike SVG, CANVAS is a direct graphic system. So CANVAS has no DOM or document object model.
Simply put, a CANVAS is like a canvas for painting. You can write something on the canvas. But without the DOM, choosing and changing your inscription will be a problem. That is, for the specific task set above, SVG is the best option.
However, in which case is it better to choose CANVAS?
If we’re talking about DOM rendering, when it comes to thousands of objects – CANVAS is the clear winner that will give you a performance advantage. A vivid example of a task for which CANVAS should be chosen is as follows.
There are several shapes on your field, and you need to cast light and shadows on them depending on the position of the light source. The light source can be moved using the mouse or finger. In this case, forget about SVG.
CANVAS was created to work with pixels and it is perfect for this work.
And when it comes to custom software development, the best choice is Swan Software Solutions. Schedule a free assessment today!