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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
SVG.Element = SVG.invent({
// Initialize node
create: function(node) {
/* make stroke value accessible dynamically */
this._stroke = SVG.defaults.attrs.stroke
/* initialize transformation store with defaults */
this.trans = SVG.defaults.trans()
/* create circular reference */
if (this.node = node) {
this.type = node.nodeName
this.node.instance = this
}
}
// Add class methods
, extend: {
// Move over x-axis
x: function(x) {
if (x != null) {
x = new SVG.Number(x)
x.value /= this.trans.scaleX
}
return this.attr('x', x)
}
// Move over y-axis
, y: function(y) {
if (y != null) {
y = new SVG.Number(y)
y.value /= this.trans.scaleY
}
return this.attr('y', y)
}
// Move by center over x-axis
, cx: function(x) {
return x == null ? this.x() + this.width() / 2 : this.x(x - this.width() / 2)
}
// Move by center over y-axis
, cy: function(y) {
return y == null ? this.y() + this.height() / 2 : this.y(y - this.height() / 2)
}
// Move element to given x and y values
, move: function(x, y) {
return this.x(x).y(y)
}
// Move element by its center
, center: function(x, y) {
return this.cx(x).cy(y)
}
// Set width of element
, width: function(width) {
return this.attr('width', width)
}
// Set height of element
, height: function(height) {
return this.attr('height', height)
}
// Set element size to given width and height
, size: function(width, height) {
var p = proportionalSize(this.bbox(), width, height)
return this.attr({
width: new SVG.Number(p.width)
, height: new SVG.Number(p.height)
})
}
// Clone element
, clone: function() {
var clone , attr
, type = this.type
/* invoke shape method with shape-specific arguments */
clone = type == 'rect' || type == 'ellipse' ?
this.parent[type](0,0) :
type == 'line' ?
this.parent[type](0,0,0,0) :
type == 'image' ?
this.parent[type](this.src) :
type == 'text' ?
this.parent[type](this.content) :
type == 'path' ?
this.parent[type](this.attr('d')) :
type == 'polyline' || type == 'polygon' ?
this.parent[type](this.attr('points')) :
type == 'g' ?
this.parent.group() :
this.parent[type]()
/* apply attributes attributes */
attr = this.attr()
delete attr.id
clone.attr(attr)
/* copy transformations */
clone.trans = this.trans
/* apply attributes and translations */
return clone.transform({})
}
// Remove element
, remove: function() {
if (this.parent)
this.parent.removeElement(this)
return this
}
// Replace element
, replace: function(element) {
this.after(element).remove()
return element
}
// Add element to given container and return self
, addTo: function(parent) {
return parent.put(this)
}
// Add element to given container and return container
, putIn: function(parent) {
return parent.add(this)
}
// Get parent document
, doc: function(type) {
return this._parent(type || SVG.Doc)
}
// Set svg element attribute
, attr: function(a, v, n) {
if (a == null) {
/* get an object of attributes */
a = {}
v = this.node.attributes
for (n = v.length - 1; n >= 0; n--)
a[v[n].nodeName] = SVG.regex.isNumber.test(v[n].nodeValue) ? parseFloat(v[n].nodeValue) : v[n].nodeValue
return a
} else if (typeof a == 'object') {
/* apply every attribute individually if an object is passed */
for (v in a) this.attr(v, a[v])
} else if (v === null) {
/* remove value */
this.node.removeAttribute(a)
} else if (v == null) {
/* act as a getter if the first and only argument is not an object */
v = this.node.getAttribute(a)
return v == null ?
SVG.defaults.attrs[a] :
SVG.regex.isNumber.test(v) ?
parseFloat(v) : v
} else if (a == 'style') {
/* redirect to the style method */
return this.style(v)
} else {
/* BUG FIX: some browsers will render a stroke if a color is given even though stroke width is 0 */
if (a == 'stroke-width')
this.attr('stroke', parseFloat(v) > 0 ? this._stroke : null)
else if (a == 'stroke')
this._stroke = v
/* convert image fill and stroke to patterns */
if (a == 'fill' || a == 'stroke') {
if (SVG.regex.isImage.test(v))
v = this.doc().defs().image(v, 0, 0)
if (v instanceof SVG.Image)
v = this.doc().defs().pattern(0, 0, function() {
this.add(v)
})
}
/* ensure correct numeric values (also accepts NaN and Infinity) */
if (typeof v === 'number')
v = new SVG.Number(v)
/* ensure full hex color */
else if (SVG.Color.isColor(v))
v = new SVG.Color(v)
/* parse array values */
else if (Array.isArray(v))
v = new SVG.Array(v)
/* if the passed attribute is leading... */
if (a == 'leading') {
/* ... call the leading method instead */
if (this.leading)
this.leading(v)
} else {
/* set given attribute on node */
typeof n === 'string' ?
this.node.setAttributeNS(n, a, v.toString()) :
this.node.setAttribute(a, v.toString())
}
/* rebuild if required */
if (this.rebuild && (a == 'font-size' || a == 'x'))
this.rebuild(a, v)
}
return this
}
// Manage transformations
, transform: function(o, v) {
if (arguments.length == 0) {
/* act as a getter if no argument is given */
return this.trans
} else if (typeof o === 'string') {
/* act as a getter if only one string argument is given */
if (arguments.length < 2)
return this.trans[o]
/* apply transformations as object if key value arguments are given*/
var transform = {}
transform[o] = v
return this.transform(transform)
}
/* ... otherwise continue as a setter */
var transform = []
/* parse matrix */
o = parseMatrix(o)
/* merge values */
for (v in o)
if (o[v] != null)
this.trans[v] = o[v]
/* compile matrix */
this.trans.matrix = this.trans.a
+ ' ' + this.trans.b
+ ' ' + this.trans.c
+ ' ' + this.trans.d
+ ' ' + this.trans.e
+ ' ' + this.trans.f
/* alias current transformations */
o = this.trans
/* add matrix */
if (o.matrix != SVG.defaults.matrix)
transform.push('matrix(' + o.matrix + ')')
/* add rotation */
if (o.rotation != 0)
transform.push('rotate(' + o.rotation + ' ' + (o.cx == null ? this.bbox().cx : o.cx) + ' ' + (o.cy == null ? this.bbox().cy : o.cy) + ')')
/* add scale */
if (o.scaleX != 1 || o.scaleY != 1)
transform.push('scale(' + o.scaleX + ' ' + o.scaleY + ')')
/* add skew on x axis */
if (o.skewX != 0)
transform.push('skewX(' + o.skewX + ')')
/* add skew on y axis */
if (o.skewY != 0)
transform.push('skewY(' + o.skewY + ')')
/* add translation */
if (o.x != 0 || o.y != 0)
transform.push('translate(' + new SVG.Number(o.x / o.scaleX) + ' ' + new SVG.Number(o.y / o.scaleY) + ')')
/* update transformations, even if there are none */
if (transform.length == 0)
this.node.removeAttribute('transform')
else
this.node.setAttribute('transform', transform.join(' '))
return this
}
// Dynamic style generator
, style: function(s, v) {
if (arguments.length == 0) {
/* get full style */
return this.node.style.cssText || ''
} else if (arguments.length < 2) {
/* apply every style individually if an object is passed */
if (typeof s == 'object') {
for (v in s) this.style(v, s[v])
} else if (SVG.regex.isCss.test(s)) {
/* parse css string */
s = s.split(';')
/* apply every definition individually */
for (v = 0; v < s.length; v++) {
v = s[v].split(':')
this.style(v[0].replace(/\s+/g, ''), v[1])
}
} else {
/* act as a getter if the first and only argument is not an object */
return this.node.style[camelCase(s)]
}
} else {
this.node.style[camelCase(s)] = v === null || SVG.regex.isBlank.test(v) ? '' : v
}
return this
}
// Get bounding box
, bbox: function() {
return new SVG.BBox(this)
}
// Get rect box
, rbox: function() {
return new SVG.RBox(this)
}
// Checks whether the given point inside the bounding box of the element
, inside: function(x, y) {
var box = this.bbox()
return x > box.x
&& y > box.y
&& x < box.x + box.width
&& y < box.y + box.height
}
// Show element
, show: function() {
return this.style('display', '')
}
// Hide element
, hide: function() {
return this.style('display', 'none')
}
// Is element visible?
, visible: function() {
return this.style('display') != 'none'
}
// Return id on string conversion
, toString: function() {
return this.attr('id')
}
// Return array of classes on the node
, classes: function() {
var classAttr = this.node.getAttribute('class')
if (classAttr === null) {
return []
} else {
return classAttr.trim().split(/\s+/)
}
}
// Return true if class exists on the node, false otherwise
, hasClass: function(className) {
return this.classes().indexOf(className) != -1
}
// Add class to the node
, addClass: function(className) {
var classArray
if (!(this.hasClass(className))) {
classArray = this.classes()
classArray.push(className)
this.node.setAttribute('class', classArray.join(' '))
}
return this
}
// Remove class from the node
, removeClass: function(className) {
var classArray
if (this.hasClass(className)) {
classArray = this.classes().filter(function(c) {
return c != className
})
this.node.setAttribute('class', classArray.join(' '))
}
return this
}
// Toggle the presence of a class on the node
, toggleClass: function(className) {
if (this.hasClass(className)) {
this.removeClass(className)
} else {
this.addClass(className)
}
return this
}
// Private: find svg parent by instance
, _parent: function(parent) {
var element = this
while (element != null && !(element instanceof parent))
element = element.parent
return element
}
}
})
|