原文链接

SVG and canvas are both technologies that can draw stuff in web browsers, so they are worth comparing and understanding when one is more suitable than the other. Even a light understanding of them makes the choice of choosing one over the other pretty clear.

  • A little flat-color icon? That’s clearly SVG territory.
  • An interactive console-like game? That’s clearly canvas territory.

I know we didn’t cover why yet, but I hope that will become clear as we dig into it.

SVG is vector and declarative

If you know you need vector art, SVG is the choice. Vector art is visually crisp and tends to be a smaller file size than raster graphics like JPG.

That makes logos a very common SVG use case. SVG code can go right within HTML, and are like declarative drawing instructions:

Canvas is a JavaScript drawing API

You put a <canvas> element in HTML, then do the drawing in JavaScript. In other words, you issue commands to tell it how to draw (which is more imperative than declarative).

  1. <canvas id="myCanvas" width="578" height="200"></canvas>
  2. <script>
  3. var canvas = document.getElementById('myCanvas');
  4. var context = canvas.getContext('2d');
  5. var centerX = canvas.width / 2;
  6. var centerY = canvas.height / 2;
  7. var radius = 70;
  8. context.beginPath();
  9. context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
  10. context.fillStyle = 'green';
  11. context.fill();
  12. </script>

SVG is in the DOM

If you’re familiar with DOM events like click and mouseDown and whatnot, those are available in SVG as well. A <circle> isn’t terribly different than a <div> in that respect.

  1. <svg viewBox="0 0 100 100">
  2. <circle cx="50" cy="50" r="50" />
  3. <script>
  4. document.querySelector('circle').addEventListener('click', e => {
  5. e.target.style.fill = "red";
  6. });
  7. </script>
  8. </svg>

SVG for accessibility

You can have a text alternative for canvas:

  1. <canvas aria-label="Hello ARIA World" role="img"></canvas>

You can do that in SVG too, but since SVG and its guts can be right in the DOM, we generally think of SVG as being what you use if you’re trying to build an accessible experience. Explained another way, you can build an SVG that assistive technology can access and find links and sub-elements with their own auditory explanations and such.
Text is also firmly in SVG territory. SVG literally has a <text> element, which is accessible and visually crisp — unlike canvas where text is typically blurry.

Canvas for pixels

As you’ll see in Sarah Dranser’s comparison below, canvas is a way of saying dance, pixels, dance!. That’s a fun way of explaining the concept that drives it home better than any dry technical sentiment could do.
Highly interactive work with lots and lots of complex detail and gradients is the territory of canvas. You’ll see a lot more games built with canvas than SVG for this reason, although there are always exceptions (note the simple vector-y-ness of this game).

CSS can play with SVG

We saw above that SVG can be in the DOM and that JavaScript can get in there and do stuff. The story is similar with CSS.

  1. <svg viewBox="0 0 100 100">
  2. <circle cx="50" cy="50" r="50" />
  3. <style>
  4. circle { fill: blue; }
  5. </style>
  6. </svg>

Note how I’ve put the <script> and <style> blocks within the <svg> for these examples, which is valid. But assuming you’ve put the SVG literally in the HTML, you could move those out, or have other external CSS and JavaScript do the same thing.
We have a massive guide of SVG Properties and CSS. But what is great to know is that the stuff that CSS is great at is still possible in SVG, like :hover states and animation!

  1. <svg viewBox="0 0 100 100">
  2. <circle cx="50" cy="50" r="50" />
  3. <style>
  4. circle { fill: blue; animation: pulse 2s alternate infinite; }
  5. @keyframes pulse {
  6. 100% {
  7. r: 30;
  8. }
  9. }
  10. </style>
  11. <script>
  12. document.querySelector('circle').addEventListener('click', e => {
  13. e.target.style.fill = "red";
  14. });
  15. </script>
  16. </svg>

Sarah Drasner’s comparison

Tablized from this tweet.

DOM/Virtual DOM Canvas
Pros
Great for UI/UX animation Dance, pixels, dance!
Great for SVG that is resolution independent Great for really impressive 3D or immersive stuff
Easier to debug Movement of tons of objects
Cons
Tanks with lots of objects Harder to make accessible
Because you have to care about the way you anmimate Not resolution independent out of the box
Breaks to nothing

Shirley Wu’s comparison

Tablized from this tweet.

SVG Canvas
Pros Easy to get started Very performant
Easier to register user interactions Easy to update
Easy to animate
Cons Potentially complex DOM More work to get started
Not performant for a large number of elements More work to handle interactions
Have to write custom animations

Many folks consider scenarios with a lot of objects (1,000+, as Shirley says) is the territory of canvas.