summaryrefslogtreecommitdiffstats
path: root/src/transform.js
blob: 7c4902b4a117c2c8648aae3832352d3d4b1d9bef (plain)
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
SVG.extend(SVG.Element, SVG.FX, {
	// Add transformations
	transform: function(o) {
		// get target in case of the fx module, otherwise reference this
		var target = this.target || this

		// full getter
		if (o == null)
			return target.ctm().extract()

		// singular getter
		else if (typeof o === 'string')
			return target.ctm().extract()[o]
		
		// get current matrix
		var matrix = new SVG.Matrix(target)
		
		// act on matrix
		if (o.a != null)
			matrix = matrix.multiply(new SVG.Matrix(o))
		
		// act on rotate
		else if (o.rotation)
			matrix = matrix.rotate(
				o.rotation
			, o.cx == null ? target.bbox().cx : o.cx
			, o.cy == null ? target.bbox().cy : o.cy
			)
		
		// act on scale
		else if (o.scale != null || o.scaleX != null || o.scaleY != null)
			matrix = matrix.scale(
				o.scale != null ? o.scale : o.scaleX != null ? o.scaleX : 1
			, o.scale != null ? o.scale : o.scaleY != null ? o.scaleY : 1
			, o.cx 		== null ? target.bbox().x : o.cx
			, o.cy 		== null ? target.bbox().y : o.cy
			)

		// act on skew
		else if (o.skewX || o.skewY)
			matrix = matrix.skew(
				o.skewX
			, o.skewY
			, o.cx == null ? target.bbox().cx : o.cx
			, o.cy == null ? target.bbox().cy : o.cy
			)

		// act on flip
		else if (o.flip)
			matrix = matrix.flip(
				o.flip
			, o.offset == null ? target.bbox()['c' + o.flip] : o.offset
			)

		// act on translate
		else if (o.x || o.y)
			matrix = matrix.translate(o.x, o.y)

		return this.attr('transform', matrix)
	}
})

SVG.extend(SVG.Element, {
	// Reset all transformations
  untransform: function() {
		return this.attr('transform', null)
	}
})