Skip to content

Commit 5cae606

Browse files
committed
added README
1 parent 2faad86 commit 5cae606

File tree

2 files changed

+139
-24
lines changed

2 files changed

+139
-24
lines changed

README.markdown

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
jQuery.path
2+
========
3+
4+
Provides animation along bezier and circular arcs.
5+
6+
The animation engine in jQuery is focussed on single dimensional animation - hence it's difficult to animate two variables along a path.
7+
8+
This simple plugin provides a method of multidimensional animation, and in particular provides a method for animating along bezier curves and arcs.
9+
10+
Bezier
11+
---
12+
13+
Example: animate along a bezier path
14+
15+
<pre>
16+
var bezier_params = {
17+
start: {
18+
x: 185,
19+
y: 185,
20+
angle: 10
21+
},
22+
end: {
23+
x:540,
24+
y:110,
25+
angle: -10,
26+
length: 0.25
27+
}
28+
}
29+
30+
$("my_elem").animate({path : new $.path.bezier(bezier_params)})
31+
</pre>
32+
33+
Bezier curves are made form a start point, an end point each with a control point
34+
35+
* start is starting point
36+
* end is the final point
37+
* x,y indicate the coordinates at that point. Required
38+
* angle is the angle of the control point from the line joining the start and end. Optional, default is 0
39+
* length is the distance from the point to it's control point as a ratio of the distance from start to end. Optional, default is 1/3
40+
41+
Arc
42+
---
43+
44+
Exampe: animate along an arc
45+
46+
<pre>
47+
48+
var arc_params = {
49+
center: [285,185],
50+
radius: 100,
51+
start: 30,
52+
end: 200,
53+
dir: -1
54+
}
55+
56+
$("my_elem").animate({path : new $.path.arc(arc_params)})
57+
</pre>
58+
59+
* center is the coords of the centre of an imaginary circle that contains the start and end point
60+
* radius is the radius of this circle
61+
* start is the angle of the start point. 0 is "North", measured clockwise
62+
* end is the angle of the start point. 0 is "North", measured clockwise
63+
* dir is the direction to move in. Only required if not obvious from start and end (e.g. if start is 100, end is 30, but you want to animate clockwise)
64+
65+
Other Paths
66+
----
67+
68+
It is trivial to create other paths, or even animate other parameters. E.g:
69+
70+
<code>
71+
72+
var SineWave = function() {
73+
this.css = function(p) {
74+
var s = Math.sin(p*20)
75+
var x = 500 - p * 300
76+
var y = s * 50 + 150
77+
var o = ((s+2)/4+0.1)
78+
return {top: y + "px", left: x + "px", opacity: o}
79+
}
80+
};
81+
82+
$("my_elem").animate({path : new SineWave})
83+
</code>
84+
85+
Links
86+
----
87+
88+
* Demo at http://weepy.github.com/jquery.path
89+
* Source at http://github.com/weepy/jquery.path
90+
* Issue Tracker at http://github.com/weepy/jquery.path/issues
91+

test/test.html

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
42
<script src='../jquery.path.js'></script>
53
<link type="text/css" rel="stylesheet" media="screen" href="http://weepy.github.com/style.css"/>
@@ -9,7 +7,7 @@
97
<style>
108
#arc { position: relative; width: 600px; height: 300px; }
119
#bezier { position: relative; width: 600px; height: 300px; }
12-
10+
#sine { position: relative; width: 600px; height: 300px; }
1311
.pixel { position: absolute; width: 150px; height: 150px; background: red; -moz-border-radius: 75px; -webkit-border-radius: 75px; opacity: 0.5; filter: alpha(opacity=50); top: -15px; left: 185px;}
1412
.dot {width: 1px; position: absolute; height: 1px; background: #aaa; }
1513
a { cursor: pointer;}
@@ -19,10 +17,21 @@
1917

2018
<script>
2119

20+
var SineWave = function() {
21+
this.css = function(p) {
22+
var s = Math.sin(p*20)
23+
var x = 500 - p * 300
24+
var y = s * 50 + 150
25+
var o = ((s+2)/4+0.1)
26+
return {top: y + "px", left: x + "px", opacity: o}
27+
}
28+
};
29+
30+
2231
$().ready(function() {
2332

2433

25-
var Paths = {"arc":[], "bezier":[]}
34+
var Paths = {"arc":[], "bezier":[], "sine": []}
2635

2736
var path_fns = {
2837
arc: function(i) {
@@ -40,36 +49,42 @@
4049
start: { x:185, y:185, angle: i*20 * x},
4150
end: {x:540,y:110, angle: i*10, length: i / 4.0}
4251
})
52+
},
53+
sine : function() {
54+
return new SineWave
4355
}
4456
}
4557

4658
for(var type in Paths) {
4759

4860
for(var i=0; i<10; i++ ) {
49-
61+
if(type == "sine" && i != 9)
62+
continue
63+
5064
var path = path_fns[type](i)
5165
Paths[type].push(path)
5266

5367
var css = {
5468
"backgroundColor": (i%2) ? "red" : "yellow",
55-
width: 20*i,
56-
height: 20*i,
57-
"-moz-border-radius": 10*i,
58-
"-webkit-border-radius": 10*i,
59-
marginLeft: -10*i, // offset the div, so center is at origin
60-
marginTop: -10*i
69+
width: 20*(1+i),
70+
height: 20*(1+i),
71+
"-moz-border-radius": 10*(1+i),
72+
"-webkit-border-radius": 10*(1+i),
73+
marginLeft: -10*(1+i), // offset the div, so center is at origin
74+
marginTop: -10*(1+i)
6175
}
6276

6377
var $$ = $("<span class=pixel></span>").css(css)
6478

6579
// initialize to start
6680
$$.css(path.css(1))
67-
$("#" + type).append($$)
81+
$("#" + type).append($$)
6882
}
6983

7084
}
7185

7286

87+
7388
$(".start").click(function() {
7489
var i =0;
7590
var type = $(this).closest("div").attr("id")
@@ -81,19 +96,19 @@
8196
})
8297

8398

84-
$(".plot").click(function() {
85-
86-
var $$ = $("<div>")
87-
var type = $(this).closest("div").attr("id")
88-
89-
for(var t=0; t<1;t+= 0.01) {
90-
for(var i in Paths[type]) {
91-
var d = $("<span class=dot></span>").css(Paths[type][i].css(t))
92-
$$.append(d)
93-
}
99+
$(".plot").click(function() {
100+
101+
var $$ = $("<div>")
102+
var type = $(this).closest("div").attr("id")
103+
104+
for(var t=0; t<1;t+= 0.01) {
105+
for(var i in Paths[type]) {
106+
var d = $("<span class=dot></span>").css(Paths[type][i].css(t))
107+
$$.append(d)
94108
}
95-
$("#" + type ).append($$)
96-
})
109+
}
110+
$("#" + type ).append($$)
111+
})
97112

98113

99114
})
@@ -117,6 +132,15 @@ <h2>ten beziers</h2>
117132
<p><a class=plot >Plot</a> </p>
118133
</div>
119134

135+
136+
<h2>custom path: sine wave</h2>
137+
<div id=sine>
138+
<p><a class=start >Start</a> </p>
139+
<p><a class=plot >Plot</a> </p>
140+
</div>
141+
142+
143+
120144
<h2>links</h2>
121145
<ul>
122146
<li><a href='http://github.com/weepy/jquery.path/raw/master/jquery.path.js'>Javascript file</a></li>

0 commit comments

Comments
 (0)