Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I scale an SVG polygon in ems?

Tags:

html

svg

I'm embedding an <svg> element directly in an HTML5 document, and I want to scale a repeating background pattern in terms of the page's font size. No problem, SVG supports CSS units, so I can just specify the size in ems, right?

The SVG spec claims "All coordinates and lengths in SVG can be specified with or without a unit identifier." And, indeed, both of these do exactly what I was hoping for:

<rect x='1em' y='2em' width='3em' height='4em' />
<circle cx='6em' cy='7em' r='8em' />

But polygons (for example) have their own completely different definition of the word "coordinate," such that this raises an error instead of drawing a 1 em triangle:

<polygon points='0 0, 0 1em, 1em 0' />

Paths are the same way -- understandably, since they're already using the letters for a different purpose.

And transformations such as "scale" expect a "number," not a "coordinate" or "length", so this isn't allowed either (my browser seems to silently ignore the "transform" attribute):

<polygon points='0 0, 0 1, 1 0' transform='scale(1em)' />

So I guess I can't even figure out how to draw a 1 em triangle, much less anything more complicated. Am I overlooking a reasonable way of doing it? What about an unreasonable way? Should I give up and use a <canvas> element instead, or would that be even worse?

like image 722
zaphod Avatar asked Dec 15 '11 05:12

zaphod


People also ask

Does SVG have scale?

SVG with a viewBox will scale to fit the height and width you give it. But what about auto-sizing? With raster images, you can set width or height , and have the other scale to match.

How do SVG polygon points work?

For <polygon> , points defines a list of points, each representing a vertex of the shape to be drawn. Each point is define by a X and Y coordinate in the user coordinate system. Note: A polygon is a closed shape, meaning the last point is connected to the first point.

How do you make a SVG polygon?

To draw a polygon in HTML SVG, use the SVG <polygon> element. The <polygon> element creates a graphic containing at least three sides. The points attribute is the x and y coordinates for each corner of the polygon.

What is an SVG viewBox?

The viewBox attribute defines the position and dimension, in user space, of an SVG viewport. The value of the viewBox attribute is a list of four numbers: min-x , min-y , width and height .


2 Answers

You can wrap your polygons in an inner <svg> element to scale them as you wish. The viewBox controls the coordinate system of the objects it contains and the height and width attributes control how big it looks.

<svg xmlns="http://www.w3.org/2000/svg">
  <svg viewBox="0 0 1 1" height="1em" width="1em" y="7em">
      <polygon points='0 0, 0 1, 1 0' />
  </svg>
  <circle cx='6em' cy='7em' r='2em' />
</svg>
like image 61
Robert Longson Avatar answered Sep 28 '22 10:09

Robert Longson


You can apply transforms to any item and use em or any other units.

For example, this works just fine:

<polygon points='0 0, 0 1, 1 0' transform='scale(2em 1.5em)' />
like image 28
Angelica Steele Avatar answered Sep 28 '22 10:09

Angelica Steele