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>
110 <li>degree <em>number</em> Degree of rotation (0 – 360°)</li>
111 <li>cx <em>number</em> [optional] X coordinate of the origin of rotation</li>
112 <li>cY <em>number</em> [optional] Y coordinate of the origin of rotation</li>
115 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);
116 c.rotate(45); // rotation is relative
117 c.rotate(45, true); // rotation is absolute</code></pre>
122 Moves the element around the canvas by the given distances.
126 <li>dx <em>number</em> Pixels of translation by X axis</li>
127 <li>dy <em>number</em> Pixels of translation by Y axis</li>
130 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);
131 c.translate(10, 10); // moves the circle down the canvas, in a diagonal line</code></pre>
136 Resizes the element by the given multiplier.
140 <li>Xtimes <em>number</em> Amount to scale horizontally</li>
141 <li>Ytimes <em>number</em> Amount to scale vertically</li>
144 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);
145 c.scale(1.5, 1.5); // makes the circle 1.5 times larger
146 c.scale(.5, .75); // makes the circle half as wide, and 75% as high</code></pre>
151 Sets the attributes of elements directly.
155 <li>attributeName <em>string</em></li>
156 <li>value <em>string</em></li>
160 <li>params <em>object</em></li>
164 <li>attributeName <em>string</em> in this case method returns current value for given attribute name</li>
168 <li>attributeNames <em>array</em> in this case method returns array of current values for given attribute names</li>
170 <h4>Possible parameters</h4>
171 <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>
173 <li>cx <em>number</em></li>
174 <li>cy <em>number</em></li>
175 <li>fill <em>colour</em></li>
176 <li>fill-opacity <em>number</em></li>
177 <li>font <em>string</em></li>
178 <li>font-family <em>string</em></li>
179 <li>font-size <em>number</em></li>
180 <li>font-weight <em>string</em></li>
181 <li>gradient <em>object|string</em> "‹angle›-‹colour›[-‹colour›[:‹offset›]]*-‹colour›"</li>
182 <li>height <em>number</em></li>
183 <li>opacity <em>number</em></li>
184 <li>path <em>pathString</em></li>
185 <li>r <em>number</em></li>
186 <li>rotation <em>number</em></li>
187 <li>rx <em>number</em></li>
188 <li>ry <em>number</em></li>
189 <li>scale <em>CSV</em></li>
190 <li>src <em>string</em> (URL)</li>
191 <li>stroke <em>colour</em></li>
192 <li>stroke-dasharray <em>string</em> [“-”, “.”, “-.”, “-..”, “. ”, “- ”, “--”, “- .”, “--.”, “--..”]</li>
193 <li>stroke-linecap <em>string</em> [“butt”, “square”, “round”, “miter”]</li>
194 <li>stroke-linejoin <em>string</em> [“butt”, “square”, “round”, “miter”]</li>
195 <li>stroke-miterlimit <em>number</em></li>
196 <li>stroke-opacity <em>number</em></li>
197 <li>stroke-width <em>number</em></li>
198 <li>translation <em>CSV</em></li>
199 <li>width <em>number</em></li>
200 <li>x <em>number</em></li>
201 <li>y <em>number</em></li>
204 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);
205 c.attr("fill", "black"); // using strings
206 c.attr({fill: "#000", stroke: "#f00", opacity: 0.5}); // using params object</code></pre>
211 Linearly changes an attribute from its current value to its specified value in the given amount of milliseconds.
215 <li>newAttrs <em>object</em> A parameters object of the animation results. (Not all attributes can be animated.)</li>
216 <li>ms <em>number</em> The duration of the animation, given in milliseconds.</li>
217 <li>callback <em>function</em> [optional]</li>
219 <h4>Attributes that can be animated</h4>
220 <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>
222 <li>cx <em>number</em></li>
223 <li>cy <em>number</em></li>
224 <li>fill <em>colour</em></li>
225 <li>fill-opacity <em>number</em></li>
226 <li>font-size <em>number</em></li>
227 <li>height <em>number</em></li>
228 <li>opacity <em>number</em></li>
229 <li>path <em>pathString</em></li>
230 <li>r <em>number</em></li>
231 <li>rotation <em>number</em></li>
232 <li>rx <em>number</em></li>
233 <li>ry <em>number</em></li>
234 <li>scale <em>CSV</em></li>
235 <li>stroke <em>colour</em></li>
236 <li>stroke-opacity <em>number</em></li>
237 <li>stroke-width <em>number</em></li>
238 <li>translation <em>CSV</em></li>
239 <li>width <em>number</em></li>
240 <li>x <em>number</em></li>
241 <li>y <em>number</em></li>
244 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);</code>
245 <code>c.animate({cx: 20, r: 20}, 2000);</code></pre>
250 Returns the dimensions of an element.
253 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);</code>
254 <code>var width = c.getBBox().width;</code></pre>
259 Moves the element so it is the closest to the viewer’s eyes, on top of other elements.
262 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);</code>
263 <code>c.toFront();</code></pre>
268 Moves the element so it is the furthest from the viewer’s eyes, behind other elements.
271 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);</code>
272 <code>c.toBack();</code></pre>
273 <h3 id="insertBefore">
277 Inserts current object before the given one.
280 <pre class="javascript code"><code>var r = paper.rect(10, 10, 10, 10);</code>
281 <code>var c = paper.circle(10, 10, 10);</code>
282 <code>c.insertBefore(r);</code></pre>
283 <h3 id="insertAfter">
287 Inserts current object after the given one
290 <pre class="javascript code"><code>var r = paper.rect(10, 10, 10, 10);</code>
291 <code>var c = paper.circle(10, 10, 10);</code>
292 <code>r.insertAfter(c);</code></pre>
293 <h2>Graphic Primitives</h2>
302 <li>x <em>number</em> X coordinate of the centre</li>
303 <li>y <em>number</em> Y coordinate of the centre</li>
304 <li>r <em>number</em> radius</li>
307 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);</code></pre>
316 <li>x <em>number</em> X coordinate of top left corner</li>
317 <li>y <em>number</em> Y coordinate of top left corner</li>
318 <li>width <em>number</em></li>
319 <li>height <em>number</em></li>
320 <li>r <em>number</em> [optional] radius for rounded corners, default is 0</li>
323 <pre class="javascript code"><code>// regular rectangle</code>
324 <code>var c = paper.rect(10, 10, 10, 10);</code>
325 <code>// rectangle with rounded corners</code>
326 <code>var c = paper.rect(10, 10, 100, 50, 10);</code></pre>
335 <li>x <em>number</em> X coordinate of the centre</li>
336 <li>y <em>number</em> X coordinate of the centre</li>
337 <li>rx <em>number</em> Horisontal radius</li>
338 <li>ry <em>number</em> Vertical radius</li>
341 <pre class="javascript code"><code>var c = paper.ellipse(100, 100, 30, 40);</code></pre>
346 Embeds an image into the SVG/VML canvas.
350 <li>src <em>string</em> URI of the source image</li>
351 <li>x <em>number</em> X coordinate position</li>
352 <li>y <em>number</em> Y coordinate position</li>
353 <li>width <em>number</em> Width of the image</li>
354 <li>height <em>number</em> Height of the image</li>
357 <pre class="javascript code"><code>var c = paper.image("apple.png", 10, 10, 100, 100);</code></pre>
362 Creates array-like object to keep and operate couple of elements at once. Warning: it doesn’t create any elements for itself in the page.
365 <pre class="javascript code"><code>var st = paper.set();</code>
366 <code>st.push(paper.circle(10, 10, 5));</code>
367 <code>st.push(paper.circle(30, 10, 5));</code>
368 <code>st.attr({fill: "red"});</code></pre>
373 Draws a text string. If you need line breaks, put “\n” in the string.
377 <li>x <em>number</em> X coordinate position.</li>
378 <li>y <em>number</em> Y coordinate position.</li>
379 <li>text <em>string</em> The text string to draw.</li>
382 <pre class="javascript code"><code>var t = paper.text(10, 10, "Look mom, I'm scalable!");</code></pre>
387 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.
391 <li>params <em>object</em> Attributes for the resulting path as described in the <code><a href="#attr">attr</a></code> reference.</li>
392 <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>
395 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).lineTo(50, 50); // draw a diagonal line
396 var c = paper.path({stroke: "#036"}, "M 10 10 L 50 50"); // same</code></pre>
397 <h2>Path Methods</h2>
402 Sets a trigger to count all following units as absolute ones, unless said otherwise. (This is on by default.)
405 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).absolutely()
406 .moveTo(10, 10).lineTo(50, 50);</code></pre>
411 Sets a trigger to count all following units as relative ones, unless said otherwise.
414 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).relatively()
415 .moveTo(10, 10).lineTo(50, 50);</code></pre>
420 Moves the drawing point to the given coordinates.
424 <li>x <em>number</em> X coordinate</li>
425 <li>y <em>number</em> Y coordinate</li>
428 <pre class="javascript code"><code>// Begins drawing the path at coordinate 10,10
429 var c = paper.path({stroke: "#036"}).moveTo(10, 10).lineTo(50, 50);</code></pre>
434 Draws a straight line to the given coordinates.
438 <li>x <em>number</em> X coordinate</li>
439 <li>y <em>number</em> Y coordinate</li>
442 <pre class="javascript code"><code>// Draws a line starting from 10,10 to 50,50
443 var c = paper.path({stroke: "#036"}).moveTo(10, 10).lineTo(50, 50);</code></pre>
448 Draws a curved line to the given coordinates. The line will have horizontal anchors on start and on finish.
452 <li>x <em>number</em></li>
453 <li>y <em>number</em></li>
454 <li>width <em>number</em></li>
457 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).cplineTo(50, 50);</code></pre>
462 Draws a bicubic curve to the given coordinates.
466 <li>x1 <em>number</em></li>
467 <li>y1 <em>number</em></li>
468 <li>x2 <em>number</em></li>
469 <li>y2 <em>number</em></li>
470 <li>x3 <em>number</em></li>
471 <li>y3 <em>number</em></li>
474 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).curveTo(10, 15, 45, 45, 50, 50);</code></pre>
479 Draws a quadratic curve to the given coordinates.
483 <li>x1 <em>number</em></li>
484 <li>y1 <em>number</em></li>
485 <li>x2 <em>number</em></li>
486 <li>y2 <em>number</em></li>
489 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).curveTo(10, 15, 45, 45, 50, 50);</code></pre>
490 <h3 id="addRoundedCorner">
494 Draws a quarter of a circle from the current drawing point.
498 <li>r <em>number</em></li>
499 <li>dir <em>string</em> Two-letter directional instruction, as described below.</li>
501 <h4>Possible <code>dir</code> values</h4>
521 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).addRoundedCorner(10, "rd");</code></pre>
526 Closes the path being drawn.
529 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).andClose();</code></pre>
534 If you need to change dimensions of the canvas call this method
538 <li>width <em>number</em> new width of the canvas</li>
539 <li>height <em>number</em> new height of the canvas</li>
545 Should be called before main Raphael method. Sets which window should be used for drawing. Default is the current one. You need to use it if you want to draw inside <code>iframe</code>
549 <li>window <em>object</em></li>
555 Returns a colour object for the next colour in spectrum
559 <li>value <em>number</em> brightness [optional]</li>
562 <pre class="javascript code"><code>var c = paper.path({stroke: Raphael.getColor()}, "M10,10L100,100")</code></pre>
563 <h3 id="getColor-reset">
567 Resets getColor function, so it will start from the beginning
572 <a href="index.html">Home</a>
579 <a href="#Raphael"><code>Raphael</code></a>
582 <a href="#node"><code>node</code></a>
585 <a href="#remove"><code>remove</code></a>
588 <a href="#hide"><code>hide</code></a>
591 <a href="#show"><code>show</code></a>
594 <a href="#rotate"><code>rotate</code></a>
597 <a href="#translate"><code>translate</code></a>
600 <a href="#scale"><code>scale</code></a>
603 <a href="#attr"><code>attr</code></a>
606 <a href="#animate"><code>animate</code></a>
609 <a href="#getBBox"><code>getBBox</code></a>
612 <a href="#toFront"><code>toFront</code></a>
615 <a href="#toBack"><code>toBack</code></a>
618 <a href="#circle"><code>circle</code></a>
621 <a href="#rect"><code>rect</code></a>
624 <a href="#ellipse"><code>ellipse</code></a>
627 <a href="#image"><code>image</code></a>
630 <a href="#set"><code>set</code></a>
633 <a href="#text"><code>text</code></a>
636 <a href="#path"><code>path</code></a>
639 <a href="#absolutely"><code>absolutely</code></a>
642 <a href="#relatively"><code>relatively</code></a>
645 <a href="#moveTo"><code>moveTo</code></a>
648 <a href="#lineTo"><code>lineTo</code></a>
651 <a href="#cplineTo"><code>cplineTo</code></a>
654 <a href="#curveTo"><code>curveTo</code></a>
657 <a href="#qcurveTo"><code>qcurveTo</code></a>
660 <a href="#addRoundedCorner"><code>addRoundedCorner</code></a>
663 <a href="#andClose"><code>andClose</code></a>
668 <a href="#setSize"><code>setSize</code></a>
671 <a href="#setWindow"><code>setWindow</code></a>
674 <a href="#getColor"><code>getColor</code></a>
677 <a href="#getColor-reset"><code>getColor.reset</code></a>
683 <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>
686 Font by <a href="http://www.exljbris.nl">Jos Buivenga</a> · Logo by <a href="http://wasabicube.com/">Wasabicube</a> ·
687 Work at this project started as 20% time at <a href="http://www.atlassian.com/" title="Atlassian — Software to Track, Test & Collaborate, from Enterprises to Open Source Projects.">Atlassian</a>
693 <script type="text/javascript">
694 var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
695 document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
697 <script type="text/javascript">
698 var pageTracker = _gat._getTracker("UA-616618-3");
699 pageTracker._trackPageview();