316daefccb2bc95093f5731f1d79d0556971d721
[raphael] / README.markdown
1 # Raphaël
2
3 Cross-browser vector graphics the easy way.
4
5 ## What is it?
6
7 Raphaël is a small JavaScript library that should simplify your work with vector graphics on the web. If you want to create your own specific chart or image crop and rotate widget, for example, you can achieve it simply and easily with this library.
8
9 Raphaël uses the [SVG W3C Recommendation](http://www.w3.org/TR/SVG/) and [VML](http://msdn.microsoft.com/en-us/library/bb264280.aspx) (a mostly equivalent implementation for Internet Explorer) as a base for drawing graphics. This means every graphical object you create is also a DOM object, so you can attach JavaScript event handlers or modify them later. Raphaël’s goal is to provide an adapter that will make drawing vector art cross-browser compatible and easy to do.
10
11 Raphaël currently supports Firefox 3.0+, Safari 3.0+, Opera 9.5+ and Internet Explorer 6.0+.
12
13 ## How to use it?
14
15 Download and include `raphael.js` (or, `raphael-packed.js` for a minimized version) into your HTML page. When it's loaded, use it as simply as:
16
17     // Creates canvas 320 × 200 at 10, 50
18     var paper = Raphael(10, 50, 320, 200);
19     // Creates circle at x = 50, y = 40, with radius 10
20     var circle = paper.circle(50, 40, 10);
21     // Sets the fill attribute of the circle to red (#f00)
22     circle.attr("fill", "#f00");
23     // Sets the stroke attribute of the circle to white (#fff)
24     circle.attr("stroke", "#fff");
25     
26 ## Reference
27
28 This section provides a function reference for the Raphaël JavaScript library.
29
30 ### Main Function
31
32 #### Raphael
33
34 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.
35
36 ##### Parameters
37
38 1. container HTMLElement or string
39 2. width number
40 3. height number
41
42 or
43
44 1. x number
45 2. y number
46 3. width number
47 4. height number
48
49 ##### Usage
50
51     // Each of the following examples create a canvas that is 320px wide by 200px high
52     // Canvas is created at the viewport's 10,50 coordinate
53     var paper = Raphael(10, 50, 320, 200);
54     // Canvas is created at the top left corner of the #notepad element (or its top right corner in dir="rtl" elements)
55     var paper = Raphael(document.getElementById("notepad"), 320, 200);
56     // Same as above
57     var paper = Raphael("notepad", 320, 200);
58     
59 ### Element’s generic methods
60
61 Each object created on the canvas shares these same generic methods:
62
63 #### node
64
65 Gives you a reference to the DOM object, so you can assign event handlers or just mess around.
66
67 ##### Usage
68
69     var c = paper.circle(10, 10, 10); // draw a circle at coordinate 10,10 with radius of 10
70     c.node.onclick = function () { c.attr("fill", "red"); };
71
72 #### rotate
73
74 Rotates the element by the given degree from either its 0,0 corner or its centre point.
75
76 ##### Parameters
77
78 1. degree number Degree of rotation (0 – 360°)
79 2. isAbsolute boolean \[optional\] Specifies the rotation point. Use `true` to rotate the element around its center point. The default, `false`, rotates the element from its 0,0 coordinate.
80
81 ##### Usage
82
83     var c = paper.circle(10, 10, 10);
84     c.rotate(45);        // rotation is relative
85     c.rotate(45, true);  // rotation is absolute
86
87 #### translate
88
89 Moves the element around the canvas by the given distances.
90
91 ##### Parameters
92
93 1. dx number Pixels of translation by X axis
94 2. dy number Pixels of translation by Y axis
95
96 ##### Usage
97
98     var c = paper.circle(10, 10, 10);
99     c.translate(10, 10); // moves the circle down the canvas, in a diagonal line
100
101 #### scale
102
103 Resizes the element by the given multiplier.
104
105 ##### Parameters
106
107 1. Xtimes number Amount to scale horizontally
108 2. Ytimes number Amount to scale vertically
109
110 ##### Usage
111
112     var c = paper.circle(10, 10, 10);
113     c.scale(1.5, 1.5); // makes the circle 1.5 times larger
114     c.scale(.5, .75);  // makes the circle half as wide, and 75% as high
115
116 #### attr
117
118 Sets the attributes of elements directly.
119
120 ##### Parameters
121
122 1. attributeName string
123 2. value string
124
125 or
126
127 1. params object
128
129 ###### Possible parameters
130
131 Please refer to the [SVG specification](http://www.w3.org/TR/SVG/) for an explanation of these parameters.
132
133 * cx number
134 * cy number
135 * dasharray string \[“-”, “.”, “-.”, “-..”, “. ”, “- ”, “--”, “- .”, “--.”, “--..”\]
136 * fill colour
137 * fill-opacity number
138 * font string
139 * font-family string
140 * font-size number
141 * font-weight string
142 * gradient object
143 * height number
144 * opacity number
145 * path pathString
146 * r number
147 * rotation number
148 * rx number
149 * ry number
150 * scale CSV
151 * stroke colour
152 * stroke-linecap string \[“butt”, “square”, “round”, “miter”\]
153 * stroke-linejoin string \[“butt”, “square”, “round”, “miter”\]
154 * stroke-miterlimit number
155 * stroke-opacity number
156 * stroke-width number
157 * translation CSV
158 * width number
159 * x number
160 * y number
161
162 ##### Usage
163
164     var c = paper.circle(10, 10, 10);
165     c.attr("fill", "black");                              // using strings
166     c.attr({fill: "#000", stroke: "#f00", opacity: 0.5}); // using params object
167
168 #### animate
169
170 Linearly changes an attribute from its current value to its specified value in the given amount of milliseconds.
171
172 ##### Parameters
173
174 1. newAttrs object A parameters object of the animation results.
175 2. ms number The duration of the animation, given in milliseconds.
176 3. callback function \[optional\]
177
178 ##### Usage
179
180     var c = paper.circle(10, 10, 10);
181     c.animate({cx: 20, r: 20}, 2000);
182
183 #### getBBox
184
185 Returns the dimensions of an element.
186
187 ##### Usage
188
189     var c = paper.circle(10, 10, 10);
190     var width = c.getBBox().width;
191
192 #### toFront
193
194 Moves the element so it is the closest to the viewer’s eyes, on top of other elements.
195
196 ##### Usage
197
198     var c = paper.circle(10, 10, 10);
199     c.toFront();
200
201 #### toBack
202
203 Moves the element so it is the furthest from the viewer’s eyes, behind other elements.
204
205 ##### Usage
206
207     var c = paper.circle(10, 10, 10);
208     c.toBack();
209     
210 #### insertBefore
211
212 Inserts current object before the given one
213
214 ##### Usage
215
216     var r = paper.rect(10, 10, 10, 10);
217     var c = paper.circle(10, 10, 10);
218     c.insertBefore(r);
219
220 #### insertAfter
221
222 Inserts current object after the given one
223
224 ##### Usage
225
226     var c = paper.circle(10, 10, 10);
227     var r = paper.rect(10, 10, 10, 10);
228     c.insertAfter(r);
229
230 ### Graphic Primitives
231
232 #### circle
233
234 Draws a circle.
235
236 ##### Parameters
237
238 1. x number X coordinate of the centre
239 2. y number Y coordinate of the centre
240 3. r number radius
241
242 ##### Usage
243
244     var c = paper.circle(10, 10, 10);
245
246 #### rect
247
248 Draws a rectangle.
249
250 ##### Parameters
251
252 1. x number X coordinate of top left corner
253 2. y number Y coordinate of top left corner
254 3. width number
255 4. height number
256 5. r number \[optional\] radius for rounded corners, default is 0
257
258 ##### Usage
259
260     // regular rectangle
261     var c = paper.rect(10, 10, 10, 10);
262     // rectangle with rounded corners
263     var c = paper.rect(10, 10, 100, 50, 10);
264
265 #### ellipse
266
267 Draws an ellipse.
268
269 ##### Parameters
270
271 1. x number X coordinate of the centre
272 2. y number X coordinate of the centre
273 3. rx number Horisontal radius
274 4. ry number Vertical radius
275
276 ##### Usage
277
278     var c = paper.ellipse(100, 100, 30, 40);
279
280 #### image
281
282 Embeds an image into the SVG/VML canvas.
283
284 ##### Parameters
285
286 1. src string URI of the source image
287 2. x number X coordinate position
288 3. y number Y coordinate position
289 4. width number Width of the image
290 5. height number Height of the image
291
292 ##### Usage
293
294     var c = paper.image("apple.png", 10, 10, 100, 100);
295
296 #### text
297
298 Draws a text string.
299
300 ##### Parameters
301
302 1. x number X coordinate position
303 2. y number Y coordinate position
304 3. text string The text string to draw.
305
306 ##### Usage
307
308     var t = paper.text(10, 10, "Look mom, I'm scalable!");
309
310 #### path
311
312 Initialises path drawing. Typically, this function returns an empty `path` object and to draw paths you use its built-in methods like `lineTo` and `curveTo`. However, you can also specify a path literally by supplying the path data as a second argument.
313
314 ##### Parameters
315
316 1. params object Attributes for the resulting path as described in the `attr` reference.
317 2. pathString string \[optional\] Path data in [SVG path string format](http://www.w3.org/TR/SVG/paths.html#PathData).
318
319 ##### Usage
320
321     var c = paper.path({stroke: "#036"}).moveTo(10, 10).lineTo(50, 50); // draw a diagonal line
322     var c = paper.path({stroke: "#036"}, "M 10 10 L 50 50");            // same
323
324 ### Path Methods
325
326 #### absolutely
327
328 Sets a trigger to count all following units as absolute ones, unless said otherwise. (This is on by default.)
329
330 ##### Usage
331
332     var c = paper.path({stroke: "#036"}).absolutely()
333         .moveTo(10, 10).lineTo(50, 50);
334
335 #### relatively
336
337 Sets a trigger to count all following units as relative ones, unless said otherwise.
338
339 ##### Usage
340
341     var c = paper.path({stroke: "#036"}).relatively()
342         .moveTo(10, 10).lineTo(50, 50);
343
344 #### moveTo
345
346 Moves the drawing point to the given coordinates.
347
348 ##### Parameters
349
350 1. x number X coordinate
351 2. y number Y coordinate
352
353 ##### Usage
354
355     // Begins drawing the path at coordinate 10,10
356     var c = paper.path({stroke: "#036"}).moveTo(10, 10).lineTo(50, 50);
357
358 #### lineTo
359
360 Draws a straight line to the given coordinates.
361
362 ##### Parameters
363
364 1. x number X coordinate
365 2. y number Y coordinate
366
367 ##### Usage
368
369     // Draws a line starting from 10,10 to 50,50
370     var c = paper.path({stroke: "#036"}).moveTo(10, 10).lineTo(50, 50);
371
372 #### cplineTo
373
374 Draws a curved line to the given coordinates. The line will have horizontal anchors on start and on finish.
375
376 ##### Parameters
377
378 1. x number
379 2. y number
380 3. width number
381
382 ##### Usage
383
384     var c = paper.path({stroke: "#036"}).moveTo(10, 10).cplineTo(50, 50);
385
386 #### curveTo
387
388 Draws a bicubic curve to the given coordinates.
389
390 ##### Parameters
391
392 1. x1 number
393 2. y1 number
394 3. x2 number
395 4. y2 number
396 5. x3 number
397 6. y3 number
398
399 ##### Usage
400
401     var c = paper.path({stroke: "#036"}).moveTo(10, 10).curveTo(10, 15, 45, 45, 50, 50);
402
403 #### qcurveTo
404
405 Draws a quadratic curve to the given coordinates.
406
407 ##### Parameters
408
409 1. x1 number
410 2. y1 number
411 3. x2 number
412 4. y2 number
413
414 ##### Usage
415
416     var c = paper.path({stroke: "#036"}).moveTo(10, 10).curveTo(10, 15, 45, 45, 50, 50);
417
418 #### addRoundedCorner
419
420 Draws a quarter of a circle from the current drawing point.
421
422 ##### Parameters
423
424 1. r number
425 2. dir string Two-letter directional instruction, as described below.
426
427 Possible dir values
428
429 * dl: down left
430 * dr: down right
431 * ld: left down
432 * lu: left up
433 * rd: right down
434 * ru: right up
435 * ul: up left
436 * ur: up right
437
438 ##### Usage
439
440     var c = paper.path({stroke: "#036"}).moveTo(10, 10).addRoundedCorner(10, "rd");
441
442 #### andClose
443
444 Closes the path being drawn.
445
446 ##### Usage
447
448     var c = paper.path({stroke: "#036"}).moveTo(10, 10).andClose();
449
450 ## License
451
452 [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php)
453
454 Copyright (c) 2008 Dmitry Baranovskiy ([http://raphaeljs.com](http://raphaeljs.com/))