Improved matrix and getBBox for span elements

master
Dmitry Baranovskiy 2016-07-30 21:18:45 +10:00
parent 378b3a56b6
commit 2825d0ce65
2 changed files with 56 additions and 30 deletions

View File

@ -47,6 +47,9 @@ Snap.plugin(function (Snap, Element, Paper, glob, Fragment) {
o }
\*/
elproto.getBBox = function (isWithoutTransform) {
if (this.type == "tspan") {
return Snap._.box(this.node.getClientRects().item(0));
}
if (!Snap.Matrix || !Snap.path) {
return this.node.getBBox();
}

View File

@ -1,11 +1,11 @@
// Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -58,30 +58,48 @@ Snap.plugin(function (Snap, Element, Paper, glob, Fragment) {
- matrix (object) @Matrix
\*/
matrixproto.add = function (a, b, c, d, e, f) {
var out = [[], [], []],
m = [[this.a, this.c, this.e], [this.b, this.d, this.f], [0, 0, 1]],
matrix = [[a, c, e], [b, d, f], [0, 0, 1]],
x, y, z, res;
if (a && a instanceof Matrix) {
matrix = [[a.a, a.c, a.e], [a.b, a.d, a.f], [0, 0, 1]];
return this.add(a.a, a.b, a.c, a.d, a.e, a.f);
}
var aNew = a * this.a + b * this.c,
bNew = a * this.b + b * this.d;
this.e += e * this.a + f * this.c;
this.f += e * this.b + f * this.d;
this.c = c * this.a + d * this.c;
this.d = c * this.b + d * this.d;
for (x = 0; x < 3; x++) {
for (y = 0; y < 3; y++) {
res = 0;
for (z = 0; z < 3; z++) {
res += m[x][z] * matrix[z][y];
}
out[x][y] = res;
}
this.a = aNew;
this.b = bNew;
return this;
};
/*\
* Matrix.multLeft
[ method ]
**
* Multiplies a passed affine transform to the left: M * this.
- a (number)
- b (number)
- c (number)
- d (number)
- e (number)
- f (number)
* or
- matrix (object) @Matrix
\*/
Matrix.prototype.multLeft = function (a, b, c, d, e, f) {
if (a && a instanceof Matrix) {
return this.multLeft(a.a, a.b, a.c, a.d, a.e, a.f);
}
this.a = out[0][0];
this.b = out[1][0];
this.c = out[0][1];
this.d = out[1][1];
this.e = out[0][2];
this.f = out[1][2];
var aNew = a * this.a + c * this.b,
cNew = a * this.c + c * this.d,
eNew = a * this.e + c * this.f + e;
this.b = b * this.a + d * this.b;
this.d = b * this.c + d * this.d;
this.f = b * this.e + d * this.f + f;
this.a = aNew;
this.c = cNew;
this.e = eNew;
return this;
};
/*\
@ -115,7 +133,9 @@ Snap.plugin(function (Snap, Element, Paper, glob, Fragment) {
- y (number) vertical offset distance
\*/
matrixproto.translate = function (x, y) {
return this.add(1, 0, 0, 1, x, y);
this.e += tx * this.a + ty * this.c;
this.f += tx * this.b + ty * this.d;
return this;
};
/*\
* Matrix.scale
@ -129,10 +149,13 @@ Snap.plugin(function (Snap, Element, Paper, glob, Fragment) {
* Default cx, cy is the middle point of the element.
\*/
matrixproto.scale = function (x, y, cx, cy) {
y == null && (y = x);
(cx || cy) && this.add(1, 0, 0, 1, cx, cy);
this.add(x, 0, 0, y, 0, 0);
(cx || cy) && this.add(1, 0, 0, 1, -cx, -cy);
sy == null && (sy = sx);
(tx || ty) && this.translate(tx, ty);
this.a *= sx;
this.b *= sx;
this.c *= sy;
this.d *= sy;
(tx || ty) && this.translate(-tx, -ty);
return this;
};
/*\
@ -307,7 +330,7 @@ Snap.plugin(function (Snap, Element, Paper, glob, Fragment) {
s.scalex = +s.scalex.toFixed(4);
s.scaley = +s.scaley.toFixed(4);
s.rotate = +s.rotate.toFixed(4);
return (s.dx || s.dy ? "t" + [+s.dx.toFixed(4), +s.dy.toFixed(4)] : E) +
return (s.dx || s.dy ? "t" + [+s.dx.toFixed(4), +s.dy.toFixed(4)] : E) +
(s.scalex != 1 || s.scaley != 1 ? "s" + [s.scalex, s.scaley, 0, 0] : E) +
(s.rotate ? "r" + [+s.rotate.toFixed(4), 0, 0] : E);
} else {
@ -343,4 +366,4 @@ Snap.plugin(function (Snap, Element, Paper, glob, Fragment) {
Snap.matrix = function (a, b, c, d, e, f) {
return new Matrix(a, b, c, d, e, f);
};
});
});