How to Use Raphaël to Create Responsive SVG
Raphaël is a small JavaScript library that should simplify your work with vector graphics on the web.
It uses SVG for creating graphics.
SVG and Canvas
SVG(Scalable Vector Graphics) is an XML-based image format that is used to define two-dimensional vector based graphics for the Web. It is scalability, can be printed with high quality at any resolution.
Canvas is raster based image format, like bitmaps, used to visual images on the fly.
SVG and Canvas have several methods for drawing paths, boxes, circles, text, and graphic images.
SVG:
1
2
3<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>Canvas:
1
2
3
4
5var c = <span class="built_in">document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.moveTo(0,0);
ctx.lineTo(200,100);
ctx.stroke();
SVG can be drawn by other generators, like Illustrator or some online generator. After finished, codes can be included in HTML file. It can be mofified by script and CSS. Canvas image is using JS as a pen to draw something on webpage. It can be modified only by script.
Raphaël elementary
1 | // Each of the following examples create a canvas |
Method 1
Getting window size, and set every graphic parameters into relative length:
1 | var windowWidth = $(window).width(); |
Method 2
Raphaël supplies with .setViewBox(x, y, w, h, fit)
to adjust SVG size.x
(number) new x position, default is 0.y
(number) new y position, default is 0.w
(number) new width of the canvash
(number) new height of the canvasfit
(boolean) true
if you want graphics to fit into new boundary box
1 | var windowWidth = $(window).width(); |
Reference
[1] How Can i scale Raphael js elements on window resize using jquery
[2] What is HTML5 Canvas?
[3] Raphaël Reference