-
Notifications
You must be signed in to change notification settings - Fork 2
/
raphaelPath.js
82 lines (71 loc) · 2.33 KB
/
raphaelPath.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Raphael path
* Copyright 2011 Guillaume Gautreau
* Licensed under the MIT License
*/
(function(R){
function Path(paper){
this.paper = paper;
this.string = "";
// toString overload
this.toString = function(){
return this.string;
}
// Main method, for applying commands
this.command = function(){
if (arguments.length > 0){
this.string += arguments[0];
[].shift.apply(arguments, []);
this.string += [].join.apply(arguments, [","]);
}
return this;
}
// Draws the path
this.draw = function(){
return this.paper.path(this.toString());
}
// Registers a new path command
// Name: name of the command (public function name)
// Command: SVG path command (one letter)
// Args: number of arguments for this command
// FixedArgs: number of arguments that are not repeated when this command is chained
this.register = function(name, command, args, fixedArgs){
this[name] = function(){
// By default, function arguments are the same as command arguments
var commandArgs = arguments, fixed = fixedArgs || 0;
// But arguments can also be passed in an array
if (arguments.length == 1 && arguments[0] instanceof Array){
commandArgs = arguments[0];
}
// Argument number verification, with support of chained commands
if (commandArgs.length == args
|| (args > 0 && commandArgs.length >= args
&& (commandArgs.length - fixed) % (args - fixed) == 0)) {
// SVG path command is added to arguments
[].reverse.apply(commandArgs, []);
[].push.apply(commandArgs, [command]);
[].reverse.apply(commandArgs, []);
this.command.apply(this, commandArgs);
} else {
throw "Incorrect number of arguments for command " + command;
}
return this;
}
}
this.register("moveTo", "M", 2);
this.register("lineTo", "L", 2);
this.register("close", "Z", 0);
this.register("horizontal", "H", 1);
this.register("vertical", "V", 1);
this.register("curveTo", "C", 6);
this.register("sCurveTo", "S", 4);
this.register("qCurveTo", "Q", 4);
this.register("sqCurveTo", "T", 2);
this.register("arcTo", "A", 7);
this.register("crCurveTo", "R", 4, 2);
}
// Raphael entry point
R.fn.preparePath = function(){
return new Path(this);
}
})(Raphael);