drawing.js
4.16 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Module dependencies.
*/
var escape = require('./utils').escape;
/**
* Extend proto.
*/
module.exports = function (proto) {
// http://www.graphicsmagick.org/GraphicsMagick.html#details-fill
proto.fill = function fill (color) {
return this.out("-fill", color || "none");
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-stroke
proto.stroke = function stroke (color, width) {
if (width) {
this.strokeWidth(width);
}
return this.out("-stroke", color || "none");
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-strokewidth
proto.strokeWidth = function strokeWidth (width) {
return this.out("-strokewidth", width);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-font
proto.font = function font (font, size) {
if (size) {
this.fontSize(size);
}
return this.out("-font", font);
}
// http://www.graphicsmagick.org/GraphicsMagick.html
proto.fontSize = function fontSize (size) {
return this.out("-pointsize", size);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.draw = function draw (args) {
return this.out("-draw", [].slice.call(arguments).join(" "));
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawPoint = function drawPoint (x, y) {
return this.draw("point", x +","+ y);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawLine = function drawLine (x0, y0, x1, y1) {
return this.draw("line", x0+","+y0, x1+","+y1);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawRectangle = function drawRectangle (x0, y0, x1, y1, wc, hc) {
var shape = "rectangle"
, lastarg;
if ("undefined" !== typeof wc) {
shape = "roundRectangle";
if ("undefined" === typeof hc) {
hc = wc;
}
lastarg = wc+","+hc;
}
return this.draw(shape, x0+","+y0, x1+","+y1, lastarg);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawArc = function drawArc (x0, y0, x1, y1, a0, a1) {
return this.draw("arc", x0+","+y0, x1+","+y1, a0+","+a1);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawEllipse = function drawEllipse (x0, y0, rx, ry, a0, a1) {
if (a0 == undefined) a0 = 0;
if (a1 == undefined) a1 = 360;
return this.draw("ellipse", x0+","+y0, rx+","+ry, a0+","+a1);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawCircle = function drawCircle (x0, y0, x1, y1) {
return this.draw("circle", x0+","+y0, x1+","+y1);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawPolyline = function drawPolyline () {
return this.draw("polyline", formatPoints(arguments));
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawPolygon = function drawPolygon () {
return this.draw("polygon", formatPoints(arguments));
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawBezier = function drawBezier () {
return this.draw("bezier", formatPoints(arguments));
}
proto._gravities = [
"northwest"
, "north"
, "northeast"
, "west"
, "center"
, "east"
, "southwest"
, "south"
, "southeast"];
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawText = function drawText (x0, y0, text, gravity) {
var gravity = String(gravity || "").toLowerCase()
, arg = ["text " + x0 + "," + y0 + " " + escape(text)];
if (~this._gravities.indexOf(gravity)) {
arg.unshift("gravity", gravity);
}
return this.draw.apply(this, arg);
}
proto._drawProps = ["color", "matte"];
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.setDraw = function setDraw (prop, x, y, method) {
prop = String(prop || "").toLowerCase();
if (!~this._drawProps.indexOf(prop)) {
return this;
}
return this.draw(prop, x+","+y, method);
}
}
function formatPoints (points) {
var len = points.length
, result = []
, i = 0;
for (; i < len; ++i) {
result.push(points[i].join(","));
}
return result;
}