Raphael JavaScript Library

17
Raphaël JavaScript Library By Jax

Transcript of Raphael JavaScript Library

Page 1: Raphael JavaScript Library

Raphaël JavaScript Library

By Jax

Page 2: Raphael JavaScript Library

Step

• Over View

• Create Paper

• Create Element

• Sample run 1

• Element animate

• Element event

• Sample run 2

• drag

• Sample run 3

Page 3: Raphael JavaScript Library

Over View

• Raphael

• Paper

• Element

• Animation

• Matrix

• Set

Page 4: Raphael JavaScript Library

Create Paper

var paper = Raphael("elementId", 840, 480);

var paper = Raphael(element, 840, 480);

var paper = Raphael(10, 10, 840, 480);

Page 5: Raphael JavaScript Library

Create Elementpaper.circle(x, y, radius) paper.ellipse(x, y, radiusX, radiusY)

paper.rect(x, y, width, height, [radius])

paper.text(x, y, text)

paper.path("M10 10 L90 90")

paper.image(src, x, y, width, height)

Page 6: Raphael JavaScript Library

Paper.path

http://raphaeljs.com/reference.html#Paper.path

Command Name Parameters

M moveto (x y)+

Z closepath (none)

L lineto (x y)+

H horizontal lineto x+

V vertical lineto y+

C curveto (x1 y1 x2 y2 x y)+

S smooth curveto (x2 y2 x y)+

Q quadratic Bézier curveto (x1 y1 x y)+

T smooth quadratic Bézier curveto (x y)+

A elliptical arc(rx ry x-axis-rotation large-arc-flag sweep-flag x y)+

R Catmull-Rom curveto* x1 y1 (x y)+

Page 7: Raphael JavaScript Library

Raphael.fn

Raphael.fn.arrow = function (x1, y1, x2, y2, size) {  return this.path( ... ); 

}; 

var paper = Raphael(10, 10, 630, 480); 

// then use it  paper.arrow(10, 10, 30, 30, 5); 

Page 8: Raphael JavaScript Library

Element.attr

// get Element.attr('attrName') Element.attr(['attrName1', 'attrName2'])

// set Element.attr('attrName', 'value') Element.attr({ 'attrName': 'value' })

Page 9: Raphael JavaScript Library

Element attributes • cx • cy • r • rx • ry • x • y

• fill • fill-opacity • height • width • opacity • path • stroke • stroke-opacity • stroke-width

http://raphaeljs.com/reference.html#Element.attr

Page 10: Raphael JavaScript Library
Page 11: Raphael JavaScript Library

Sample run 1• In JsBin

<script src="https://rawgithub.com/DmitryBaranovskiy/raphael/master/raphael-min.js" type="text/javascript" charset="utf-8"></script>

Page 12: Raphael JavaScript Library

Element animate

Element.animate(attrObj, ms, [easing])

• “linear” • “<” or “easeIn” or “ease-in” • “>” or “easeOut” or “ease-out” • “<>” or “easeInOut” or “ease-in-out” • “backIn” or “back-in” • “backOut” or “back-out” • “elastic” • “bounce”

Page 13: Raphael JavaScript Library

Element event

• click (handler)• dblclick (handler)• drag(onmove, onstart, onend, [mcontext], [scontext],

[econtext])• hover(f_in, f_out, [in_context], [out_context])• mousedown (handler)• mousemove (handler)• mouseout (handler)• mouseover (handler)• mouseup (handler)• onDragOver (handler)

Page 14: Raphael JavaScript Library

Sample run 2

• Use animate 200ms on hover

Page 15: Raphael JavaScript Library

Element Other Method

• data(key, [value])• removeData([key])• glow([glow])• clone()• remove()• show()• hide()• toBack()• toFront()

• rotate(deg, [cx], [cy])• scale(sx, sy, [cx], [cy])• transform([tstr])• translate(dx, dy)

• resume([anim])• status([anim], [value])• stop([anim])

Page 16: Raphael JavaScript Library

drag

paper.circle(50, 50, 20).attr('fill', 'red').drag(function (dx, dy, x, y, event) {

this.attr({ cx: this.ox + dx, cy: this.oy + dy }); }, function (x, y, event) {

this.ox = this.attr("cx"); this.oy = this.attr("cy");

});

Page 17: Raphael JavaScript Library

Sample run 3