Transform.js module

This commit is contained in:
Tim Mickel 2016-01-13 17:40:50 -05:00
parent 535e61f628
commit 53ac780598

View file

@ -1,10 +1,15 @@
var Transform = function () {};
////////////////////////////////// //////////////////////////////////
// SVG Transforms // SVG Transforms
////////////////////////////////// //////////////////////////////////
Transform.getList = function (elem) { import SVGTools from './SVGTools';
import SVG2Canvas from './SVG2Canvas';
import Paint from './Paint';
import Vector from '../geom/Vector';
import {gn, DEGTOR} from '../utils/lib';
export default class Transform {
static getList (elem) {
if (elem == undefined) { if (elem == undefined) {
return null; return null;
} }
@ -14,9 +19,9 @@ Transform.getList = function (elem) {
return elem.gradientTransform.baseVal; return elem.gradientTransform.baseVal;
} }
return null; return null;
}; }
Transform.extract = function (elem, n) { static extract (elem, n) {
var tl = Transform.getList(elem); var tl = Transform.getList(elem);
for (var i = 0; i < tl.numberOfItems; ++i) { for (var i = 0; i < tl.numberOfItems; ++i) {
if (tl.getItem(i).type == n) { if (tl.getItem(i).type == n) {
@ -24,9 +29,9 @@ Transform.extract = function (elem, n) {
} }
} }
return Paint.root.createSVGTransform(); return Paint.root.createSVGTransform();
}; }
Transform.getIndex = function (elem, n) { static getIndex (elem, n) {
var tl = Transform.getList(elem); var tl = Transform.getList(elem);
for (var i = 0; i < tl.numberOfItems; ++i) { for (var i = 0; i < tl.numberOfItems; ++i) {
if (tl.getItem(i).type == n) { if (tl.getItem(i).type == n) {
@ -34,24 +39,24 @@ Transform.getIndex = function (elem, n) {
} }
} }
return null; return null;
}; }
Transform.point = function (x, y, m) { static point (x, y, m) {
return Transform.newPoint(x, y).matrixTransform(m); return Transform.newPoint(x, y).matrixTransform(m);
}; }
Transform.newPoint = function (x, y) { static newPoint (x, y) {
var pt = Paint.root.createSVGPoint(); var pt = Paint.root.createSVGPoint();
pt.x = x; pt.x = x;
pt.y = y; pt.y = y;
return pt; return pt;
}; }
//////////////////////////// ////////////////////////////
// Element translation // Element translation
//////////////////////////// ////////////////////////////
Transform.translateTo = function (elem, xform) { static translateTo (elem, xform) {
if (elem == undefined) { if (elem == undefined) {
return; return;
} }
@ -102,9 +107,9 @@ Transform.translateTo = function (elem, xform) {
} }
Transform.updateAll(elem); Transform.updateAll(elem);
Transform.updateRotationCenter(elem); Transform.updateRotationCenter(elem);
}; }
Transform.updateRotationCenter = function (elem) { static updateRotationCenter (elem) {
if (Transform.getRotationAngle(elem) == 0) { if (Transform.getRotationAngle(elem) == 0) {
return; return;
} }
@ -114,9 +119,9 @@ Transform.updateRotationCenter = function (elem) {
var center = SVGTools.getBoxCenter(elem); var center = SVGTools.getBoxCenter(elem);
center = Transform.point(center.x, center.y, mtx); center = Transform.point(center.x, center.y, mtx);
rot.setRotate(angle, center.x, center.y); rot.setRotate(angle, center.x, center.y);
}; }
Transform.line = function (elem, mtx) { static line (elem, mtx) {
var pt = Paint.root.createSVGPoint(); var pt = Paint.root.createSVGPoint();
pt.x = Number(elem.getAttribute('x1')); pt.x = Number(elem.getAttribute('x1'));
pt.y = Number(elem.getAttribute('y1')); pt.y = Number(elem.getAttribute('y1'));
@ -128,9 +133,9 @@ Transform.line = function (elem, mtx) {
pt = pt.matrixTransform(mtx); pt = pt.matrixTransform(mtx);
elem.setAttribute('x2', pt.x); elem.setAttribute('x2', pt.x);
elem.setAttribute('y2', pt.y); elem.setAttribute('y2', pt.y);
}; }
Transform.eleminateTranslates = function (elem) { static eleminateTranslates (elem) {
var tl = Transform.getList(elem); var tl = Transform.getList(elem);
for (var i = 0; i < tl.numberOfItems; ++i) { for (var i = 0; i < tl.numberOfItems; ++i) {
if (tl.getItem(i).type == 2) { if (tl.getItem(i).type == 2) {
@ -146,9 +151,9 @@ Transform.eleminateTranslates = function (elem) {
} }
} }
}; }
Transform.eliminateAll = function (spr) { static eliminateAll (spr) {
var tl = Transform.getList(spr); var tl = Transform.getList(spr);
if (tl && tl.numberOfItems > 0) { if (tl && tl.numberOfItems > 0) {
var k = tl.numberOfItems; var k = tl.numberOfItems;
@ -157,9 +162,9 @@ Transform.eliminateAll = function (spr) {
} }
} }
return tl; return tl;
}; }
Transform.combineAll = function (elem) { static combineAll (elem) {
var tl = Transform.getList(elem); var tl = Transform.getList(elem);
if (tl == null) { if (tl == null) {
return Paint.root.createSVGMatrix(); return Paint.root.createSVGMatrix();
@ -171,9 +176,9 @@ Transform.combineAll = function (elem) {
m = m.multiply(mtom.matrix); m = m.multiply(mtom.matrix);
} }
return m; return m;
}; }
Transform.appendForMove = function (elem, t) { static appendForMove (elem, t) {
var tl = Transform.getList(elem); var tl = Transform.getList(elem);
if (tl == null) { if (tl == null) {
return; return;
@ -183,28 +188,28 @@ Transform.appendForMove = function (elem, t) {
} else { } else {
tl.insertItemBefore(t, 0); tl.insertItemBefore(t, 0);
} }
}; }
Transform.getTranslateTransform = function () { static getTranslateTransform () {
var res = Paint.root.createSVGTransform(); var res = Paint.root.createSVGTransform();
res.setTranslate(0, 0); res.setTranslate(0, 0);
return res; return res;
}; }
Transform.applyRotation = function (elem, angle) { static applyRotation (elem, angle) {
var rot = Paint.root.createSVGTransform(); var rot = Paint.root.createSVGTransform();
var box = SVGTools.getBox(elem); var box = SVGTools.getBox(elem);
var cx = box.x + box.width / 2; var cx = box.x + box.width / 2;
var cy = box.y + box.height / 2; var cy = box.y + box.height / 2;
rot.setRotate(angle, cx, cy); rot.setRotate(angle, cx, cy);
Transform.getList(elem).appendItem(rot); Transform.getList(elem).appendItem(rot);
}; }
////////////////////////////////// //////////////////////////////////
// SVG Transforms // SVG Transforms
////////////////////////////////// //////////////////////////////////
Transform.getRotationAngle = function (elem, to_rad) { static getRotationAngle (elem, to_rad) {
var tl = Transform.getList(elem); var tl = Transform.getList(elem);
if (!tl) { if (!tl) {
return 0; return 0;
@ -217,9 +222,9 @@ Transform.getRotationAngle = function (elem, to_rad) {
} }
} }
return 0.0; return 0.0;
}; }
Transform.getRotation = function (elem) { static getRotation (elem) {
//console.log ("Transform.getRotation", elem); //console.log ("Transform.getRotation", elem);
var tl = Transform.getList(elem); var tl = Transform.getList(elem);
var num = tl.numberOfItems; var num = tl.numberOfItems;
@ -239,9 +244,9 @@ Transform.getRotation = function (elem) {
Transform.getList(elem).insertItemBefore(rot, 0); Transform.getList(elem).insertItemBefore(rot, 0);
} }
return rot; return rot;
}; }
Transform.getValid = function (elem) { static getValid (elem) {
if (!elem) { if (!elem) {
return; return;
} }
@ -279,9 +284,9 @@ Transform.getValid = function (elem) {
return null; return null;
} }
return tl; return tl;
}; }
Transform.getCombinedMatrices = function (elem) { static getCombinedMatrices (elem) {
var tl = Transform.getList(elem); var tl = Transform.getList(elem);
if (tl == null) { if (tl == null) {
return Paint.root.createSVGMatrix(); return Paint.root.createSVGMatrix();
@ -297,9 +302,9 @@ Transform.getCombinedMatrices = function (elem) {
} }
} }
return m; return m;
}; }
Transform.hasScaleMatrix = function (elem) { static hasScaleMatrix (elem) {
var tl = Transform.getList(elem); var tl = Transform.getList(elem);
if (tl == null) { if (tl == null) {
return false; return false;
@ -310,39 +315,35 @@ Transform.hasScaleMatrix = function (elem) {
} }
} }
return false; return false;
}; }
Transform.getScaleMatrix = function (e) { static getScaleMatrix (e) {
var tl = Transform.getList(e); var tl = Transform.getList(e);
var scaleIndex = Transform.getIndex(e, 3); var scaleIndex = Transform.getIndex(e, 3);
if (scaleIndex != null) { if (scaleIndex != null) {
return tl.getItem(scaleIndex).matrix; return tl.getItem(scaleIndex).matrix;
} }
return Paint.root.createSVGMatrix(); return Paint.root.createSVGMatrix();
}; }
Transform.updateAll = function (elem) { static updateAll (elem) {
var newtl = Transform.getList(elem); var newtl = Transform.getList(elem);
if (newtl && newtl.numberOfItems == 0) { if (newtl && newtl.numberOfItems == 0) {
elem.removeAttribute('transform'); elem.removeAttribute('transform');
} }
}; }
Transform.applyMatrix = function (elem, matrix) { static applyMatrix (elem, matrix) {
var m = Paint.root.createSVGTransform(); var m = Paint.root.createSVGTransform();
m.setMatrix(matrix); m.setMatrix(matrix);
Transform.getList(elem).appendItem(m); Transform.getList(elem).appendItem(m);
}; }
SVGMatrix.prototype.isIdentity = function () {
return (this.a == 1 && this.b == 0 && this.c == 0 && this.d == 1 && this.e == 0 && this.f == 0);
};
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Paths data structure // Paths data structure
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Transform.applyToCmds = function (shape, mtx) { static applyToCmds (shape, mtx) {
var d = shape.getAttribute('d'); var d = shape.getAttribute('d');
var list = SVG2Canvas.getCommandList(d); var list = SVG2Canvas.getCommandList(d);
var plist = []; var plist = [];
@ -356,9 +357,9 @@ Transform.applyToCmds = function (shape, mtx) {
} }
var path = SVG2Canvas.arrayToString(plist); var path = SVG2Canvas.arrayToString(plist);
shape.setAttribute('d', path); shape.setAttribute('d', path);
}; }
Transform.getModifiedCmd = function (cmd, mtx) { static getModifiedCmd (cmd, mtx) {
var pt = Transform.newPoint(0, 0); var pt = Transform.newPoint(0, 0);
if (cmd.length < 2) { if (cmd.length < 2) {
return cmd; return cmd;
@ -381,14 +382,14 @@ Transform.getModifiedCmd = function (cmd, mtx) {
cmd[i + 1] = pt.y; cmd[i + 1] = pt.y;
} }
return cmd; return cmd;
}; }
//////////////////////////////////////////////// ////////////////////////////////////////////////
// Element Rotation // Element Rotation
/////////////////////////////////////////////// ///////////////////////////////////////////////
Transform.rotateFromPoint = function (erot, node) { static rotateFromPoint (erot, node) {
var pname = node.tagName; var pname = node.tagName;
var rot = Transform.getRotation(node); var rot = Transform.getRotation(node);
var c, p, delta, mtx, cx, cy; var c, p, delta, mtx, cx, cy;
@ -501,4 +502,9 @@ Transform.rotateFromPoint = function (erot, node) {
Transform.applyToCmds(node, Transform.combineAll(node)); Transform.applyToCmds(node, Transform.combineAll(node));
Transform.eliminateAll(node); Transform.eliminateAll(node);
} }
}
}
SVGMatrix.prototype.isIdentity = function () {
return (this.a == 1 && this.b == 0 && this.c == 0 && this.d == 1 && this.e == 0 && this.f == 0);
}; };