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 Rotates the element by the given degree from either its 0,0 corner or its center point.
78 <li>degree <em>number</em> Degree of rotation (0 – 360°)</li>
79 <li>isAbsolute <em>boolean</em> [optional] Specifies the rotation point. Use <code>true</code> to rotate the element around its center point. The default, <code>false</code>, rotates the element from its 0,0 coordinate.</li>
82 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);
83 c.rotate(45); // rotation is relative
84 c.rotate(45, true); // rotation is absolute</code></pre>
89 Moves the element around the canvas by the given distances.
93 <li>dx <em>number</em> Pixels of translation by X axis</li>
94 <li>dy <em>number</em> Pixels of translation by Y axis</li>
97 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);
98 c.translate(10, 10); // moves the circle down the canvas, in a diagonal line</code></pre>
103 Resizes the element by the given multiplier.
107 <li>Xtimes <em>number</em> Amount to scale horizontally</li>
108 <li>Ytimes <em>number</em> Amount to scale vertically</li>
111 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);
112 c.scale(1.5, 1.5); // makes the circle 1.5 times larger
113 c.scale(.5, .75); // makes the circle half as wide, and 75% as high</code></pre>
118 Sets the attributes of elements directly.
122 <li>attributeName <em>string</em></li>
123 <li>value <em>string</em></li>
127 <li>params <em>object</em></li>
129 <h4>Possible parameters</h4>
130 <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>
132 <li>cx <em>number</em></li>
133 <li>cy <em>number</em></li>
134 <li>fill <em>colour</em></li>
135 <li>fill-opacity <em>number</em></li>
136 <li>font <em>string</em></li>
137 <li>font-family <em>string</em></li>
138 <li>font-size <em>number</em></li>
139 <li>font-weight <em>string</em></li>
140 <li>gradient <em>object</em></li>
141 <li>height <em>number</em></li>
142 <li>opacity <em>number</em></li>
143 <li>path <em>pathString</em></li>
144 <li>r <em>number</em></li>
145 <li>rotation <em>number</em></li>
146 <li>rx <em>number</em></li>
147 <li>ry <em>number</em></li>
148 <li>scale <em>CSV</em></li>
149 <li>stroke <em>colour</em></li>
150 <li>stroke-dasharray <em>string</em> [“-”, “.”, “-.”, “-..”, “. ”, “- ”, “--”, “- .”, “--.”, “--..”]</li>
151 <li>stroke-linecap <em>string</em> [“butt”, “square”, “round”, “miter”]</li>
152 <li>stroke-linejoin <em>string</em> [“butt”, “square”, “round”, “miter”]</li>
153 <li>stroke-miterlimit <em>number</em></li>
154 <li>stroke-opacity <em>number</em></li>
155 <li>stroke-width <em>number</em></li>
156 <li>translation <em>CSV</em></li>
157 <li>width <em>number</em></li>
158 <li>x <em>number</em></li>
159 <li>y <em>number</em></li>
162 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);
163 c.attr("fill", "black"); // using strings
164 c.attr({fill: "#000", stroke: "#f00", opacity: 0.5}); // using params object</code></pre>
169 Linearly changes an attribute from its current value to its specified value in the given amount of milliseconds.
173 <li>newAttrs <em>object</em> A parameters object of the animation results.</li>
174 <li>ms <em>number</em> The duration of the animation, given in milliseconds.</li>
175 <li>callback <em>function</em> [optional]</li>
178 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);</code>
179 <code>c.animate({cx: 20, r: 20}, 2000);</code></pre>
184 Returns the dimensions of an element.
187 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);</code>
188 <code>var width = c.getBBox().width;</code></pre>
193 Moves the element so it is the closest to the viewer’s eyes, on top of other elements.
196 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);</code>
197 <code>c.toFront();</code></pre>
202 Moves the element so it is the furthest from the viewer’s eyes, behind other elements.
205 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);</code>
206 <code>c.toBack();</code></pre>
207 <h3 id="insertBefore">
211 Inserts current object before the given one.
214 <pre class="javascript code"><code>var r = paper.rect(10, 10, 10, 10);</code>
215 <code>var c = paper.circle(10, 10, 10);</code>
216 <code>c.insertBefore(r);</code></pre>
217 <h3 id="insertAfter">
221 Inserts current object after the given one
224 <pre class="javascript code"><code>var r = paper.rect(10, 10, 10, 10);</code>
225 <code>var c = paper.circle(10, 10, 10);</code>
226 <code>r.insertAfter(c);</code></pre>
227 <h2>Graphic Primitives</h2>
236 <li>x <em>number</em> X coordinate of the centre</li>
237 <li>y <em>number</em> Y coordinate of the centre</li>
238 <li>r <em>number</em> radius</li>
241 <pre class="javascript code"><code>var c = paper.circle(10, 10, 10);</code></pre>
250 <li>x <em>number</em> X coordinate of top left corner</li>
251 <li>y <em>number</em> Y coordinate of top left corner</li>
252 <li>width <em>number</em></li>
253 <li>height <em>number</em></li>
254 <li>r <em>number</em> [optional] radius for rounded corners, default is 0</li>
257 <pre class="javascript code"><code>// regular rectangle</code>
258 <code>var c = paper.rect(10, 10, 10, 10);</code>
259 <code>// rectangle with rounded corners</code>
260 <code>var c = paper.rect(10, 10, 100, 50, 10);</code></pre>
269 <li>x <em>number</em> X coordinate of the centre</li>
270 <li>y <em>number</em> X coordinate of the centre</li>
271 <li>rx <em>number</em> Horisontal radius</li>
272 <li>ry <em>number</em> Vertical radius</li>
275 <pre class="javascript code"><code>var c = paper.ellipse(100, 100, 30, 40);</code></pre>
280 Embeds an image into the SVG/VML canvas.
284 <li>src <em>string</em> URI of the source image</li>
285 <li>x <em>number</em> X coordinate position</li>
286 <li>y <em>number</em> Y coordinate position</li>
287 <li>width <em>number</em> Width of the image</li>
288 <li>height <em>number</em> Height of the image</li>
291 <pre class="javascript code"><code>var c = paper.image("apple.png", 10, 10, 100, 100);</code></pre>
296 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.
300 <li>params <em>object</em> Attributes for the resulting path as described in the <code><a href="#attr">attr</a></code> reference.</li>
301 <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>
304 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).lineTo(50, 50); // draw a diagonal line
305 var c = paper.path({stroke: "#036"}, "M 10 10 L 50 50"); // same</code></pre>
306 <h2>Path Methods</h2>
311 Sets a trigger to count all following units as absolute ones, unless said otherwise. (This is on by default.)
314 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).absolutely()
315 .moveTo(10, 10).lineTo(50, 50);</code></pre>
320 Sets a trigger to count all following units as relative ones, unless said otherwise.
323 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).relatively()
324 .moveTo(10, 10).lineTo(50, 50);</code></pre>
329 Moves the drawing point to the given coordinates.
333 <li>x <em>number</em> X coordinate</li>
334 <li>y <em>number</em> Y coordinate</li>
337 <pre class="javascript code"><code>// Begins drawing the path at coordinate 10,10
338 var c = paper.path({stroke: "#036"}).moveTo(10, 10).lineTo(50, 50);</code></pre>
343 Draws a straight line to the given coordinates.
347 <li>x <em>number</em> X coordinate</li>
348 <li>y <em>number</em> Y coordinate</li>
351 <pre class="javascript code"><code>// Draws a line starting from 10,10 to 50,50
352 var c = paper.path({stroke: "#036"}).moveTo(10, 10).lineTo(50, 50);</code></pre>
357 Draws a curved line to the given coordinates. The line will have horizontal anchors on start and on finish.
361 <li>x <em>number</em></li>
362 <li>y <em>number</em></li>
363 <li>width <em>number</em></li>
366 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).cplineTo(50, 50);</code></pre>
371 Draws a bicubic curve to the given coordinates.
375 <li>x1 <em>number</em></li>
376 <li>y1 <em>number</em></li>
377 <li>x2 <em>number</em></li>
378 <li>y2 <em>number</em></li>
379 <li>x3 <em>number</em></li>
380 <li>y3 <em>number</em></li>
383 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).curveTo(10, 15, 45, 45, 50, 50);</code></pre>
388 Draws a quadratic curve to the given coordinates.
392 <li>x1 <em>number</em></li>
393 <li>y1 <em>number</em></li>
394 <li>x2 <em>number</em></li>
395 <li>y2 <em>number</em></li>
398 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).curveTo(10, 15, 45, 45, 50, 50);</code></pre>
399 <h3 id="addRoundedCorner">
403 Draws a quarter of a circle from the current drawing point.
407 <li>r <em>number</em></li>
408 <li>dir <em>string</em> Two-letter directional instruction, as described below.</li>
410 <h4>Possible <code>dir</code> values</h4>
430 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).addRoundedCorner(10, "rd");</code></pre>
435 Closes the path being drawn.
438 <pre class="javascript code"><code>var c = paper.path({stroke: "#036"}).moveTo(10, 10).andClose();</code></pre>
442 <a href="index.html">Home</a>
449 <a href="reference.html#Raphael"><code>Raphael</code></a>
452 <a href="reference.html#node"><code>node</code></a>
455 <a href="reference.html#rotate"><code>rotate</code></a>
458 <a href="reference.html#translate"><code>translate</code></a>
461 <a href="reference.html#scale"><code>scale</code></a>
464 <a href="reference.html#attr"><code>attr</code></a>
467 <a href="reference.html#attr"><code>animate</code></a>
470 <a href="reference.html#getBBox"><code>getBBox</code></a>
473 <a href="reference.html#toFront"><code>toFront</code></a>
476 <a href="reference.html#toBack"><code>toBack</code></a>
479 <a href="reference.html#circle"><code>circle</code></a>
482 <a href="reference.html#rect"><code>rect</code></a>
485 <a href="reference.html#ellipse"><code>ellipse</code></a>
488 <a href="reference.html#image"><code>image</code></a>
491 <a href="reference.html#path"><code>path</code></a>
494 <a href="reference.html#absolutely"><code>absolutely</code></a>
497 <a href="reference.html#relatively"><code>relatively</code></a>
500 <a href="reference.html#moveTo"><code>moveTo</code></a>
503 <a href="reference.html#lineTo"><code>lineTo</code></a>
506 <a href="reference.html#cplineTo"><code>cplineTo</code></a>
509 <a href="reference.html#curveTo"><code>curveTo</code></a>
512 <a href="reference.html#qcurveTo"><code>qcurveTo</code></a>
515 <a href="reference.html#addRoundedCorner"><code>addRoundedCorner</code></a>
518 <a href="reference.html#andClose"><code>andClose</code></a>
526 <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>
529 A font by <a href="http://www.exljbris.nl">Jos Buivenga</a> | Logo by <a href="http://wasabicube.com/">Wasabicube</a>
535 <script type="text/javascript">
536 var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
537 document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
539 <script type="text/javascript">
540 var pageTracker = _gat._getTracker("UA-616618-3");
541 pageTracker._trackPageview();