aboutsummaryrefslogtreecommitdiffstats
path: root/src/matrix.js
blob: 52fb080a304982b6655efb2eaa20ae9c9fb6985e (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
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
SVG.Matrix = SVG.invent({
  // Initialize
  create: function(source) {
    var i, base = arrayToMatrix([1, 0, 0, 1, 0, 0])

    // ensure source as object
    source = source instanceof SVG.Element ?
      source.matrixify() :
    typeof source === 'string' ?
      arrayToMatrix(source.split(SVG.regex.delimiter).map(parseFloat)) :
    arguments.length == 6 ?
      arrayToMatrix([].slice.call(arguments)) :
    Array.isArray(source) ?
      arrayToMatrix(source) :
    typeof source === 'object' ?
      source : base

    // merge source
    for (i = abcdef.length - 1; i >= 0; --i)
      this[abcdef[i]] = source[abcdef[i]] != null ?
        source[abcdef[i]] : base[abcdef[i]]
  }

  // Add methods
, extend: {

    // Convert an object of affine parameters into a matrix
    compose: function (o, cx, cy) {

      // Set the defaults
      var tx = o.translateX || 0
        , ty = o.translateY || 0
        , theta = o.theta || 0
        , sx = o.scaleX || 1
        , sy = o.scaleY || 1
        , lam = o.shear || 0
        , cx = cx || 0
        , cy = cy || 0

      // Calculate the trigonometric values
      var ct = Math.cos(theta * Math.PI / 180)
        , st Math.sin(theta * Math.PI / 180)

      // Calculate the matrix components directly
      var a = sx * ct
        , b = sx * st
        , c = lam * sx * ct - sy * st
        , d = lam * sx * st + sy * ct
        , e = - sx * ct * (cx + cy * lam) + sy * st * cy + tx + cx
        , f = - sx * st * (cx + cy * lam) - sy * ct * cy + ty + cy

      // Construct a new matrix and return it
      var matrix = new SVG.Matrix([a, b, c, d, e, f])
      return matrix
    }
    // Decompose a matrix into the affine parameters needed to form it
  , decompose: function (matrix, cx, cy) {

      // Get the paramaters of the current matrix
      var a = matrix.a
        , b = matrix.b
        , c = matrix.c
        , d = matrix.d
        , e = matrix.e
        , f = matrix.f

      // Project the first basis vector onto the unit circle
      var circle = unitCircle (a, b)
        , theta = circle.theta
        , ct = circle.cos
        , st = circle.sin

      // Work out the transformation parameters
      var signX = Math.sign(a * ct + b * st)
        , sx = signX * mag (a, b)
        , lam = (st * d + ct * c) / (ct * a + st * b)
        , signY = Math.sign(- c * st + d * ct)
        , sy = mag (lam * a - c, d - lam * b)
        , tx = e - cx + cx * ct * sx + cy * (lam * ct * sx - st * sy)
        , ty = f - cy + cx * st * sx + cy * (lam * st * sx + ct * sy)

      // Package and return the parameters
      return {

        // Bundle the affine parameters
        translateX: tx
      , translateY: ty
      , theta: theta
      , scaleX: sx
      , scaleY: sy
      , shear: lam

      // Bundle the matrix parameters
      , a: this.a
      , b: this.b
      , c: this.c
      , d: this.d
      , e: this.e
      , f: this.f

      // Return the new origin point
      , x: this.e
      , y: this.f

      // Store the matrix
      , matrix: new SVG.Matrix(this)
      }
    }
    // Clone matrix
  , form: function (o) {

    // Get all of the parameters required to form the matrix
    var flipX = o.flip && (o.flip == "x" || o.flip == "both") ? -1 : 1
      , flipY = o.flip && (o.flip == "y" || o.flip == "both") ? -1 : 1
      , kX = o.skew.length ? o.skew[0]
        : isFinite(o.skew) ? o.skew
        : isFinite(o.skewX) ? o.skewX
        : 0
      , kY = o.skew.length ? o.skew[1]
        : isFinite(o.skew) ? o.skew
        : isFinite(o.skewY) ? o.skewY
        : 0
      , skewX = o.scale.length ? o.scale[0] * flipX
        : isFinite(o.scale) ? o.scale * flipX
        : isFinite(o.scaleX) ? o.scaleX * flipX
        : flipX
      , skewY = o.scale.length ? o.scale[1] * flipY
        : isFinite(o.scale) ? o.scale * flipY
        : isFinite(o.scaleY) ? o.scaleY * flipY
        : flipY
      , kx = Math.tan(SVG.utils.radians(skewX))
      , ky = Math.tan(SVG.utils.radians(skewY))
      , lam = o.shear || 0
      , theta = SVG.utils.radians(o.rotate || 0)
      , st = Math.sin(theta)
      , ct = Math.cos(theta)
      , ox = o.origin.length ? o.origin[0] : o.ox || 0
      , oy = o.origin.length ? o.origin[1] : o.oy || 0
      , px = o.position.length ? o.position[0] : o.px || ox
      , py = o.position.length ? o.position[1] : o.py || oy
      , tx = o.translate.length ? o.translate[0] : o.tx || 0
      , ty = o.translate.length ? o.translate[1] : o.ty || 0

    // Form the matrix parameters... aka. welcome to wonderland! (used wolfram)
    var a = ct*sx + ky*st*sy
      , b = -st*sx+ct*ky*sy
      , c = ct*kx*sx+st*sy + lam*(ct*sx+ky*st*sy)
      , d = -kx*st*sx + ct*sy + lam*(-st*sx + ct*ky*sy)
      , e = px + tx + cx*(ct*sx+ky*st*sy) + cy*(ct*kx*sx+st*sy+lam*(ct*sx+ky*st*sy))
      , f = py + ty + cx*(-st*sx + ct*ky*sy) + cy*(-kx*st*sx + ct*sy + lam*(-st*sx + ct*ky*sy))
      , result = new Matrix(a, b, c, d, e, f)
    return result
  }
  , clone: function() {
      return new SVG.Matrix(this)
    }
    // Morph one matrix into another
  , morph: function(matrix) {
      // store new destination
      this.destination = new SVG.Matrix(matrix)

      return this
    }
    // Get morphed matrix at a given position
  , at: function(pos) {
      // make sure a destination is defined
      if (!this.destination) return this

      // calculate morphed matrix at a given position
      var matrix = new SVG.Matrix({
        a: this.a + (this.destination.a - this.a) * pos
      , b: this.b + (this.destination.b - this.b) * pos
      , c: this.c + (this.destination.c - this.c) * pos
      , d: this.d + (this.destination.d - this.d) * pos
      , e: this.e + (this.destination.e - this.e) * pos
      , f: this.f + (this.destination.f - this.f) * pos
      })

      return matrix
    }
    // Multiplies by given matrix
  , multiply: function(matrix) {
      return new SVG.Matrix(this.native().multiply(parseMatrix(matrix).native()))
    }
    // Inverses matrix
  , inverse: function() {
      return new SVG.Matrix(this.native().inverse())
    }
    // Translate matrix
  , translate: function(x, y) {
      return new SVG.Matrix(this.native().translate(x || 0, y || 0))
    }
    // Scale matrix
  , scale: function(x, y, cx, cy) {
      // support uniformal scale
      if (arguments.length == 1) {
        y = x
      } else if (arguments.length == 3) {
        cy = cx
        cx = y
        y = x
      }

      return this.around(cx, cy, new SVG.Matrix(x, 0, 0, y, 0, 0))
    }
    // Rotate matrix
  , rotate: function(r, cx, cy) {
      // convert degrees to radians
      r = SVG.utils.radians(r)

      return this.around(cx, cy, new SVG.Matrix(Math.cos(r), Math.sin(r), -Math.sin(r), Math.cos(r), 0, 0))
    }
    // Flip matrix on x or y, at a given offset
  , flip: function(a, o) {
      return a == 'x' ?
          this.scale(-1, 1, o, 0) :
        a == 'y' ?
          this.scale(1, -1, 0, o) :
          this.scale(-1, -1, a, o != null ? o : a)
    }
    // Skew
  , shear: function(a, cx, cy) {
    return this.around(cx, cy, new SVG.Matrix(1, a, 0, 1, 0, 0))
  }
  , skew: function(x, y, cx, cy) {
      // support uniformal skew
      if (arguments.length == 1) {
        y = x
      } else if (arguments.length == 3) {
        cy = cx
        cx = y
        y = x
      }

      // convert degrees to radians
      x = SVG.utils.radians(x)
      y = SVG.utils.radians(y)

      return this.around(cx, cy, new SVG.Matrix(1, Math.tan(y), Math.tan(x), 1, 0, 0))
    }
    // SkewX
  , skewX: function(x, cx, cy) {
      return this.skew(x, 0, cx, cy)
    }
    // SkewY
  , skewY: function(y, cx, cy) {
      return this.skew(0, y, cx, cy)
    }
    // Transform around a center point
  , around: function(cx, cy, matrix) {
      return this
        .multiply(new SVG.Matrix(1, 0, 0, 1, cx || 0, cy || 0))
        .multiply(matrix)
        .multiply(new SVG.Matrix(1, 0, 0, 1, -cx || 0, -cy || 0))
    }
    // Convert to native SVGMatrix
  , native: function() {
      // create new matrix
      var matrix = SVG.parser.nodes.svg.node.createSVGMatrix()

      // update with current values
      for (var i = abcdef.length - 1; i >= 0; i--)
        matrix[abcdef[i]] = this[abcdef[i]]

      return matrix
    }
    // Convert matrix to string
  , toString: function() {
      return 'matrix(' + this.a + ',' + this.b + ',' + this.c + ',' + this.d + ',' + this.e + ',' + this.f + ')'
    }
  }

  // Define parent
, parent: SVG.Element

  // Add parent method
, construct: {
    // Get current matrix
    ctm: function() {
      return new SVG.Matrix(this.node.getCTM())
    },
    // Get current screen matrix
    screenCTM: function() {
      /* https://bugzilla.mozilla.org/show_bug.cgi?id=1344537
         This is needed because FF does not return the transformation matrix
         for the inner coordinate system when getScreenCTM() is called on nested svgs.
         However all other Browsers do that */
      if(this instanceof SVG.Nested) {
        var rect = this.rect(1,1)
        var m = rect.node.getScreenCTM()
        rect.remove()
        return new SVG.Matrix(m)
      }
      return new SVG.Matrix(this.node.getScreenCTM())
    }

  }

})