1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6 <title>Raphaël Reference</title>
7 <meta name="author" content="Dmitry Baranovskiy">
8 <meta name="description" content="Vector Graphics JavaScript™ Library">
9 <link rel="stylesheet" href="raphael.css" type="text/css" charset="utf-8" media="screen,projection">
10 <link rel="stylesheet" type="text/css" media="print" href="/dmitry-print.css">
11 <link rel="shortcut icon" href="/favicon16.png" type="image/x-icon">
12 <link rel="apple-touch-icon" href="/favicon.png">
13 <script src="../jquery.js" type="text/javascript" charset="utf-8"></script>
14 <script src="../dmitry.js" type="text/javascript" charset="utf-8"></script>
16 <body class="raphael" id="raphael.dmitry.baranovskiy.com">
18 <a href="http://twitter.com/statuses/user_timeline/17180567.atom" id="rss" name="rss">rss</a>
20 Raphaël—JavaScript Library
27 <h2>Main Function</h2>
32 Creates a canvas object on which to draw. You must do this first, as all future calls to drawing methods from this instance will be bound to this canvas.
36 <li>container <em>HTMLElement</em> or <em>string</em></li>
37 <li>width <em>number</em></li>
38 <li>height <em>number</em></li>
42 <li>x <em>number</em></li>
43 <li>y <em>number</em></li>
44 <li>width <em>number</em></li>
45 <li>height <em>number</em></li>
48 <pre class="javascript code"><code>// Each of the following examples create a canvas that is 320px wide by 200px high
49 // Canvas is created at the viewport's 10,50 coordinate
50 var paper = Raphael(10, 50, 320, 200);
51 // Canvas is created at the top left corner of the #notepad element (or its top right corner in dir="rtl" elements)
52 var paper = Raphael(document.getElementById("notepad"), 320, 200);
54 var paper = Raphael("notepad", 320, 200);</code></pre>
56 Element’s generic methods
59 Each object created on the canvas shares these same generic methods:
65 Gives you a reference to the DOM object, so you can assign event handlers or just mess around.
68 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10); // draw a circle at coordinate 10,10 with radius of 10
69 c.node.onclick = function () { c.attr("fill", "red"); };</code></pre>
74 Removes element from the DOM. You can’t use it after this method call.
77 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);
78 c.remove();</code></pre>
83 Makes element invisible.
86 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);
87 c.hide();</code></pre>
92 Makes element visible.
95 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);
96 c.show();</code></pre>
101 Rotates the element by the given degree from its center point.
105 <li>degree <em>number</em> Degree of rotation (0 – 360°)</li>
106 <li>isAbsolute <em>boolean</em> [optional] Specifies is degree is relative to previous position (<code>false</code>) or is it absolute angle (<code>true</code>)</li>
109 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);
110 c.rotate(45); // rotation is relative
111 c.rotate(45, true); // rotation is absolute</code></pre>
116 Moves the element around the canvas by the given distances.
120 <li>dx <em>number</em> Pixels of translation by X axis</li>
121 <li>dy <em>number</em> Pixels of translation by Y axis</li>
124 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);
125 c.translate(10, 10); // moves the circle down the canvas, in a diagonal line</code></pre>
130 Resizes the element by the given multiplier.
134 <li>Xtimes <em>number</em> Amount to scale horizontally</li>
135 <li>Ytimes <em>number</em> Amount to scale vertically</li>
138 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);
139 c.scale(1.5, 1.5); // makes the circle 1.5 times larger
140 c.scale(.5, .75); // makes the circle half as wide, and 75% as high</code></pre>
145 Sets the attributes of elements directly.
149 <li>attributeName <em>string</em></li>
150 <li>value <em>string</em></li>
154 <li>params <em>object</em></li>
158 <li>attributeName <em>string</em> in this case method returns current value for given attribute name</li>
162 <li>attributeNames <em>array</em> in this case method returns array of current values for given attribute names</li>
164 <h4>Possible parameters</h4>
165 <p>Please refer to the <a href="http://www.w3.org/TR/SVG/" title="The W3C Recommendation for the SVG language describes these properties in detail.">SVG specification</a> for an explanation of these parameters.</p>
167 <li>cx <em>number</em></li>
168 <li>cy <em>number</em></li>
169 <li>fill <em>colour</em></li>
170 <li>fill-opacity <em>number</em></li>
171 <li>font <em>string</em></li>
172 <li>font-family <em>string</em></li>
173 <li>font-size <em>number</em></li>
174 <li>font-weight <em>string</em></li>
175 <li>gradient <em>object</em></li>
176 <li>height <em>number</em></li>
177 <li>opacity <em>number</em></li>
178 <li>path <em>pathString</em></li>
179 <li>r <em>number</em></li>
180 <li>rotation <em>number</em></li>
181 <li>rx <em>number</em></li>
182 <li>ry <em>number</em></li>
183 <li>scale <em>CSV</em></li>
184 <li>stroke <em>colour</em></li>
185 <li>stroke-dasharray <em>string</em> [“-”, “.”, “-.”, “-..”, “. ”, “- ”, “--”, “- .”, “--.”, “--..”]</li>
186 <li>stroke-linecap <em>string</em> [“butt”, “square”, “round”, “miter”]</li>
187 <li>stroke-linejoin <em>string</em> [“butt”, “square”, “round”, “miter”]</li>
188 <li>stroke-miterlimit <em>number</em></li>
189 <li>stroke-opacity <em>number</em></li>
190 <li>stroke-width <em>number</em></li>
191 <li>translation <em>CSV</em></li>
192 <li>width <em>number</em></li>
193 <li>x <em>number</em></li>
194 <li>y <em>number</em></li>
197 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);
198 c.attr("fill", "black"); // using strings
199 c.attr({fill: "#000", stroke: "#f00", opacity: 0.5}); // using params object</code></pre>
204 Linearly changes an attribute from its current value to its specified value in the given amount of milliseconds.
208 <li>newAttrs <em>object</em> A parameters object of the animation results. (Not all attributes can be animated.)</li>
209 <li>ms <em>number</em> The duration of the animation, given in milliseconds.</li>
210 <li>callback <em>function</em> [optional]</li>
212 <h4>Attributes that can be animated</h4>
213 <p>The <code>newAttrs</code> parameter accepts an object whose properties are the attributes to animate. However, not all attributes listed in the <code>attr</code> method reference can be animated. The following is a list of those properties that can be animated:</p>
215 <li>cx <em>number</em></li>
216 <li>cy <em>number</em></li>
217 <li>fill <em>colour</em></li>
218 <li>fill-opacity <em>number</em></li>
219 <li>font-size <em>number</em></li>
220 <li>height <em>number</em></li>
221 <li>opacity <em>number</em></li>
222 <li>path <em>pathString</em></li>
223 <li>r <em>number</em></li>
224 <li>rotation <em>number</em></li>
225 <li>rx <em>number</em></li>
226 <li>ry <em>number</em></li>
227 <li>scale <em>CSV</em></li>
228 <li>stroke <em>colour</em></li>
229 <li>stroke-opacity <em>number</em></li>
230 <li>stroke-width <em>number</em></li>
231 <li>translation <em>CSV</em></li>
232 <li>width <em>number</em></li>
233 <li>x <em>number</em></li>
234 <li>y <em>number</em></li>
237 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);</code>
238 <code>c.animate({cx: 20, r: 20}, 2000);</code></pre>
243 Returns the dimensions of an element.
246 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);</code>
247 <code>var width = c.getBBox().width;</code></pre>
252 Moves the element so it is the closest to the viewer’s eyes, on top of other elements.
255 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);</code>
256 <code>c.toFront();</code></pre>
261 Moves the element so it is the furthest from the viewer’s eyes, behind other elements.
264 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);</code>
265 <code>c.toBack();</code></pre>
266 <h3 id="insertBefore">
270 Inserts current object before the given one.
273 <pre class="javascript code"><code>var r = paper.rect(10, 10, 10, 10);</code>
274 <code>var c = paper.circle(10, 10, 10);</code>
275 <code>c.insertBefore(r);</code></pre>
276 <h3 id="insertAfter">
280 Inserts current object after the given one
283 <pre class="javascript code"><code>var r = paper.rect(10, 10, 10, 10);</code>
284 <code>var c = paper.circle(10, 10, 10);</code>
285 <code>r.insertAfter(c);</code></pre>
286 <h2>Graphic Primitives</h2>
295 <li>x <em>number</em> X coordinate of the centre</li>
296 <li>y <em>number</em> Y coordinate of the centre</li>
297 <li>r <em>number</em> radius</li>
300 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);</code></pre>
309 <li>x <em>number</em> X coordinate of top left corner</li>
310 <li>y <em>number</em> Y coordinate of top left corner</li>
311 <li>width <em>number</em></li>
312 <li>height <em>number</em></li>
313 <li>r <em>number</em> [optional] radius for rounded corners, default is 0</li>
316 <pre class="javascript code"><code>// regular rectangle</code>
317 <code>var c = paper.rect(10, 10, 10, 10);</code>
318 <code>// rectangle with rounded corners</code>
319 <code>var c = paper.rect(10, 10, 100, 50, 10);</code></pre>
328 <li>x <em>number</em> X coordinate of the centre</li>
329 <li>y <em>number</em> X coordinate of the centre</li>
330 <li>rx <em>number</em> Horisontal radius</li>
331 <li>ry <em>number</em> Vertical radius</li>
334 <pre class="javascript code"><code>var c = paper.ellipse(100, 100, 30, 40);</code></pre>
339 Embeds an image into the SVG/VML canvas.
343 <li>src <em>string</em> URI of the source image</li>
344 <li>x <em>number</em> X coordinate position</li>
345 <li>y <em>number</em> Y coordinate position</li>
346 <li>width <em>number</em> Width of the image</li>
347 <li>height <em>number</em> Height of the image</li>
350 <pre class="javascript code"><code>var c = paper.image("apple.png", 10, 10, 100, 100);</code></pre>
359 <li>x <em>number</em> X coordinate position.</li>
360 <li>y <em>number</em> Y coordinate position.</li>
361 <li>text <em>string</em> The text string to draw.</li>
364 <pre class="javascript code"><code>var t = paper.text(10, 10, "Look mom, I'm scalable!");</code></pre>
369 Initialises path drawing. Typically, this function returns an empty <code>path</code> object and to draw paths you use its built-in methods like <code>lineTo</code> and <code>curveTo</code>. However, you can also specify a path literally by supplying the path data as a second argument.
373 <li>params <em>object</em> Attributes for the resulting path as described in the <code><a href="#attr">attr</a></code> reference.</li>
374 <li>pathString <em>string</em> [optional] Path data in <a href="http://www.w3.org/TR/SVG/paths.html#PathData" title="Details of a path's data attribute's format are described in the SVG specification.">SVG path string format</a>.</li>
377 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).lineTo(50, 50); // draw a diagonal line
378 var c = paper.path({stroke: "#036"}, "M 10 10 L 50 50"); // same</code></pre>
379 <h2>Path Methods</h2>
384 Sets a trigger to count all following units as absolute ones, unless said otherwise. (This is on by default.)
387 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).absolutely()
388 .moveTo(10, 10).lineTo(50, 50);</code></pre>
393 Sets a trigger to count all following units as relative ones, unless said otherwise.
396 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).relatively()
397 .moveTo(10, 10).lineTo(50, 50);</code></pre>
402 Moves the drawing point to the given coordinates.
406 <li>x <em>number</em> X coordinate</li>
407 <li>y <em>number</em> Y coordinate</li>
410 <pre class="javascript code"><code>// Begins drawing the path at coordinate 10,10
411 var c = paper.path({stroke: "#036"}).moveTo(10, 10).lineTo(50, 50);</code></pre>
416 Draws a straight line to the given coordinates.
420 <li>x <em>number</em> X coordinate</li>
421 <li>y <em>number</em> Y coordinate</li>
424 <pre class="javascript code"><code>// Draws a line starting from 10,10 to 50,50
425 var c = paper.path({stroke: "#036"}).moveTo(10, 10).lineTo(50, 50);</code></pre>
430 Draws a curved line to the given coordinates. The line will have horizontal anchors on start and on finish.
434 <li>x <em>number</em></li>
435 <li>y <em>number</em></li>
436 <li>width <em>number</em></li>
439 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).cplineTo(50, 50);</code></pre>
444 Draws a bicubic curve to the given coordinates.
448 <li>x1 <em>number</em></li>
449 <li>y1 <em>number</em></li>
450 <li>x2 <em>number</em></li>
451 <li>y2 <em>number</em></li>
452 <li>x3 <em>number</em></li>
453 <li>y3 <em>number</em></li>
456 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).curveTo(10, 15, 45, 45, 50, 50);</code></pre>
461 Draws a quadratic curve to the given coordinates.
465 <li>x1 <em>number</em></li>
466 <li>y1 <em>number</em></li>
467 <li>x2 <em>number</em></li>
468 <li>y2 <em>number</em></li>
471 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).curveTo(10, 15, 45, 45, 50, 50);</code></pre>
472 <h3 id="addRoundedCorner">
476 Draws a quarter of a circle from the current drawing point.
480 <li>r <em>number</em></li>
481 <li>dir <em>string</em> Two-letter directional instruction, as described below.</li>
483 <h4>Possible <code>dir</code> values</h4>
503 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).addRoundedCorner(10, "rd");</code></pre>
508 Closes the path being drawn.
511 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).andClose();</code></pre>
516 Returns a colour object for the next colour in spectrum
520 <li>value <em>number</em> brightness [optional]</li>
523 <pre class="javascript code"><code>var c = paper.path({stroke: Raphael.getColor()}, "M10,10L100,100")</code></pre>
524 <h3 id="getColor-reset">
528 Resets getColor function, so it will start from the beginning
533 <a href="index.html">Home</a>
540 <a href="reference.html#Raphael"><code>Raphael</code></a>
543 <a href="reference.html#node"><code>node</code></a>
546 <a href="reference.html#remove"><code>remove</code></a>
549 <a href="reference.html#hide"><code>hide</code></a>
552 <a href="reference.html#show"><code>show</code></a>
555 <a href="reference.html#rotate"><code>rotate</code></a>
558 <a href="reference.html#translate"><code>translate</code></a>
561 <a href="reference.html#scale"><code>scale</code></a>
564 <a href="reference.html#attr"><code>attr</code></a>
567 <a href="reference.html#animate"><code>animate</code></a>
570 <a href="reference.html#getBBox"><code>getBBox</code></a>
573 <a href="reference.html#toFront"><code>toFront</code></a>
576 <a href="reference.html#toBack"><code>toBack</code></a>
579 <a href="reference.html#circle"><code>circle</code></a>
582 <a href="reference.html#rect"><code>rect</code></a>
585 <a href="reference.html#ellipse"><code>ellipse</code></a>
588 <a href="reference.html#image"><code>image</code></a>
591 <a href="reference.html#path"><code>path</code></a>
594 <a href="reference.html#absolutely"><code>absolutely</code></a>
597 <a href="reference.html#relatively"><code>relatively</code></a>
600 <a href="reference.html#moveTo"><code>moveTo</code></a>
603 <a href="reference.html#lineTo"><code>lineTo</code></a>
606 <a href="reference.html#cplineTo"><code>cplineTo</code></a>
609 <a href="reference.html#curveTo"><code>curveTo</code></a>
612 <a href="reference.html#qcurveTo"><code>qcurveTo</code></a>
615 <a href="reference.html#addRoundedCorner"><code>addRoundedCorner</code></a>
618 <a href="reference.html#andClose"><code>andClose</code></a>
623 <a href="#getColor"><code>getColor</code></a>
626 <a href="#getColor-reset"><code>getColor.reset</code></a>
632 <a href="http://creativecommons.org/licenses/by-sa/3.0/" title="Creative Commons Attribution-ShareAlike 3.0 Unported" rel="license">Some Rights Reserved</a> by <a href="http://dmitry.baranovskiy.com/">Dmitry Baranovskiy</a>
635 A font by <a href="http://www.exljbris.nl">Jos Buivenga</a> | Logo by <a href="http://wasabicube.com/">Wasabicube</a>
641 <script type="text/javascript">
642 var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
643 document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
645 <script type="text/javascript">
646 var pageTracker = _gat._getTracker("UA-616618-3");
647 pageTracker._trackPageview();