Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG polygon points with percentage units support

Tags:

svg

I am trying to have a fluid SVG canvas that can resize easily. So far I'm using percentages everywhere. However it seems like SVG polygon and paths do not support percentages in point attribute. Here's an example:

(jsFiddle)

<svg width='90%' height='90%' style='background-color: whitesmoke'>     <rect x='40%' y='40%' width='25%' height='25%' />      <polygon points="0,0 0,100 30,20 30,0" />     <polygon points="30,0 30,20 60,0 60,0" />     <polygon points="60,0 60,0 90,30 90,0" /> </svg> 

However if I start to change numbers in points attribute to percentages it fails with parsing error in console. I am looking for a way to have the polygon that can resize with the <svg> element.

like image 582
ahmet alp balkan Avatar asked Jul 06 '13 01:07

ahmet alp balkan


People also ask

Can you use percentages in SVG?

Nearly every length measurement in SVG can be expressed by a percentage. With a few exceptions, those percentages are relative to the SVG coordinate system size—the size defined by the viewBox of the nearest ancestor <svg> or <symbol> (or by its actual width and height, if it doesn't have a viewBox ).

Which attribute is used with polygon in SVG to define the edges of any polygon?

The points attribute defines the x and y coordinates for each corner of the polygon.

How do you make a SVG polygon?

Most of the web browsers can display SVG just like they can display PNG, GIF, and JPG. 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 a viewBox SVG?

The viewBox is an attribute of the SVG element in HTML. It is used to scale the SVG element that means we can set the coordinates as well as width and height. Syntax: viewBox = "min-x min-y width height" Attribute Values: min-x: It is used to set the horizontal axis.


2 Answers

By using viewBox and a container element (of whatever size) I think you can achieve the effect you're looking for: http://jsfiddle.net/davegaeddert/WpeH4/

<div id="svg-container" style="width:100%;height:100%;">     <svg width='100%' height='100%' viewBox="0 0 100 100" preserveAspectRatio="none" style='background-color: whitesmoke'>         <rect x='40%' y='40%' width='25%' height='25%' />          <polygon points="0,0 0,100 30,20 30,0" />         <polygon points="30,0 30,20 60,0 60,0" />         <polygon points="60,0 60,0 90,30 90,0" />     </svg> </div> 

If you give the viewBox a size of 0 0 100 100 then the points can be written like percentages and the shape will scale with the svg.

like image 138
davegaeddert Avatar answered Sep 20 '22 08:09

davegaeddert


You can group the elements together with g and use a transform:

<svg width='90%' height='90%' style='background-color: whitesmoke'>     <rect x='40%' y='40%' width='25%' height='25%' />      <g transform="scale(0.4 0.4)">         <polygon points="0,0 0,100 30,20 30,0"/>         <polygon points="30,0 30,20 60,0 60,0"/>         <polygon points="60,0 60,0 90,30 90,0"/>     </g> </svg> 
like image 36
simonzack Avatar answered Sep 19 '22 08:09

simonzack