JSDoc
git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2727 eee81c28-f429-11dd-99c0-75d572ba1dddmaster
parent
8f1c74260b
commit
2ea686a118
185
editor/math.js
185
editor/math.js
|
@ -12,6 +12,14 @@
|
||||||
// Dependencies:
|
// Dependencies:
|
||||||
// None.
|
// None.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef AngleCoord45
|
||||||
|
* @type {object}
|
||||||
|
* @property {number} x - The angle-snapped x value
|
||||||
|
* @property {number} y - The angle-snapped y value
|
||||||
|
* @property {number} a - The angle at which to snap
|
||||||
|
*/
|
||||||
|
|
||||||
(function() {'use strict';
|
(function() {'use strict';
|
||||||
|
|
||||||
if (!svgedit.math) {
|
if (!svgedit.math) {
|
||||||
|
@ -24,44 +32,37 @@ var NEAR_ZERO = 1e-14;
|
||||||
// Throw away SVGSVGElement used for creating matrices/transforms.
|
// Throw away SVGSVGElement used for creating matrices/transforms.
|
||||||
var svg = document.createElementNS(svgedit.NS.SVG, 'svg');
|
var svg = document.createElementNS(svgedit.NS.SVG, 'svg');
|
||||||
|
|
||||||
// Function: svgedit.math.transformPoint
|
/**
|
||||||
// A (hopefully) quicker function to transform a point by a matrix
|
* A (hopefully) quicker function to transform a point by a matrix
|
||||||
// (this function avoids any DOM calls and just does the math)
|
* (this function avoids any DOM calls and just does the math)
|
||||||
//
|
* @param {number} x - Float representing the x coordinate
|
||||||
// Parameters:
|
* @param {number} y - Float representing the y coordinate
|
||||||
// x - Float representing the x coordinate
|
* @param {SVGMatrix} m - Matrix object to transform the point with
|
||||||
// y - Float representing the y coordinate
|
* @returns {object} An x, y object representing the transformed point
|
||||||
// m - Matrix object to transform the point with
|
*/
|
||||||
// Returns a x,y object representing the transformed point
|
|
||||||
svgedit.math.transformPoint = function (x, y, m) {
|
svgedit.math.transformPoint = function (x, y, m) {
|
||||||
return { x: m.a * x + m.c * y + m.e, y: m.b * x + m.d * y + m.f};
|
return { x: m.a * x + m.c * y + m.e, y: m.b * x + m.d * y + m.f};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Function: svgedit.math.isIdentity
|
/**
|
||||||
// Helper function to check if the matrix performs no actual transform
|
* Helper function to check if the matrix performs no actual transform
|
||||||
// (i.e. exists for identity purposes)
|
* (i.e. exists for identity purposes)
|
||||||
//
|
* @param {SVGMatrix} m - The matrix object to check
|
||||||
// Parameters:
|
* @returns {boolean} Indicates whether or not the matrix is 1,0,0,1,0,0
|
||||||
// m - The matrix object to check
|
*/
|
||||||
//
|
|
||||||
// Returns:
|
|
||||||
// Boolean indicating whether or not the matrix is 1,0,0,1,0,0
|
|
||||||
svgedit.math.isIdentity = function (m) {
|
svgedit.math.isIdentity = function (m) {
|
||||||
return (m.a === 1 && m.b === 0 && m.c === 0 && m.d === 1 && m.e === 0 && m.f === 0);
|
return (m.a === 1 && m.b === 0 && m.c === 0 && m.d === 1 && m.e === 0 && m.f === 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Function: svgedit.math.matrixMultiply
|
/**
|
||||||
// This function tries to return a SVGMatrix that is the multiplication m1*m2.
|
* This function tries to return a SVGMatrix that is the multiplication m1*m2.
|
||||||
// We also round to zero when it's near zero
|
* We also round to zero when it's near zero
|
||||||
//
|
* @param {...SVGMatrix} matr - Two or more matrix objects to multiply
|
||||||
// Parameters:
|
* @returns {SVGMatrix} The matrix object resulting from the calculation
|
||||||
// >= 2 Matrix objects to multiply
|
*/
|
||||||
//
|
svgedit.math.matrixMultiply = function (matr) {
|
||||||
// Returns:
|
|
||||||
// The matrix object resulting from the calculation
|
|
||||||
svgedit.math.matrixMultiply = function() {
|
|
||||||
var args = arguments, i = args.length, m = args[i-1];
|
var args = arguments, i = args.length, m = args[i-1];
|
||||||
|
|
||||||
while (i-- > 1) {
|
while (i-- > 1) {
|
||||||
|
@ -78,14 +79,11 @@ svgedit.math.matrixMultiply = function() {
|
||||||
return m;
|
return m;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function: svgedit.math.hasMatrixTransform
|
/**
|
||||||
// See if the given transformlist includes a non-indentity matrix transform
|
* See if the given transformlist includes a non-indentity matrix transform
|
||||||
//
|
* @param {object} [tlist] - The transformlist to check
|
||||||
// Parameters:
|
* @returns {boolean} Whether or not a matrix transform was found
|
||||||
// tlist - The transformlist to check
|
*/
|
||||||
//
|
|
||||||
// Returns:
|
|
||||||
// Boolean on whether or not a matrix transform was found
|
|
||||||
svgedit.math.hasMatrixTransform = function (tlist) {
|
svgedit.math.hasMatrixTransform = function (tlist) {
|
||||||
if (!tlist) {return false;}
|
if (!tlist) {return false;}
|
||||||
var num = tlist.numberOfItems;
|
var num = tlist.numberOfItems;
|
||||||
|
@ -96,27 +94,24 @@ svgedit.math.hasMatrixTransform = function(tlist) {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function: svgedit.math.transformBox
|
/**
|
||||||
// Transforms a rectangle based on the given matrix
|
* Transforms a rectangle based on the given matrix
|
||||||
//
|
* @param {number} l - Float with the box's left coordinate
|
||||||
// Parameters:
|
* @param {number} t - Float with the box's top coordinate
|
||||||
// l - Float with the box's left coordinate
|
* @param {number} w - Float with the box width
|
||||||
// t - Float with the box's top coordinate
|
* @param {number} h - Float with the box height
|
||||||
// w - Float with the box width
|
* @param {SVGMatrix} m - Matrix object to transform the box by
|
||||||
// h - Float with the box height
|
* @returns {object} An object with the following values:
|
||||||
// m - Matrix object to transform the box by
|
* tl - The top left coordinate (x,y object)
|
||||||
//
|
* tr - The top right coordinate (x,y object)
|
||||||
// Returns:
|
* bl - The bottom left coordinate (x,y object)
|
||||||
// An object with the following values:
|
* br - The bottom right coordinate (x,y object)
|
||||||
// * tl - The top left coordinate (x,y object)
|
* aabox - Object with the following values:
|
||||||
// * tr - The top right coordinate (x,y object)
|
* x - Float with the axis-aligned x coordinate
|
||||||
// * bl - The bottom left coordinate (x,y object)
|
* y - Float with the axis-aligned y coordinate
|
||||||
// * br - The bottom right coordinate (x,y object)
|
* width - Float with the axis-aligned width coordinate
|
||||||
// * aabox - Object with the following values:
|
* height - Float with the axis-aligned height coordinate
|
||||||
// * Float with the axis-aligned x coordinate
|
*/
|
||||||
// * Float with the axis-aligned y coordinate
|
|
||||||
// * Float with the axis-aligned width coordinate
|
|
||||||
// * Float with the axis-aligned height coordinate
|
|
||||||
svgedit.math.transformBox = function (l, t, w, h, m) {
|
svgedit.math.transformBox = function (l, t, w, h, m) {
|
||||||
var transformPoint = svgedit.math.transformPoint,
|
var transformPoint = svgedit.math.transformPoint,
|
||||||
|
|
||||||
|
@ -144,19 +139,17 @@ svgedit.math.transformBox = function(l, t, w, h, m) {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function: svgedit.math.transformListToTransform
|
/**
|
||||||
// This returns a single matrix Transform for a given Transform List
|
* This returns a single matrix Transform for a given Transform List
|
||||||
// (this is the equivalent of SVGTransformList.consolidate() but unlike
|
* (this is the equivalent of SVGTransformList.consolidate() but unlike
|
||||||
// that method, this one does not modify the actual SVGTransformList)
|
* that method, this one does not modify the actual SVGTransformList)
|
||||||
// This function is very liberal with its min,max arguments
|
* This function is very liberal with its min, max arguments
|
||||||
//
|
* @param {object} tlist - The transformlist object
|
||||||
// Parameters:
|
* @param {integer} [min=0] - Optional integer indicating start transform position
|
||||||
// tlist - The transformlist object
|
* @param {integer} [max] - Optional integer indicating end transform position;
|
||||||
// min - Optional integer indicating start transform position
|
* defaults to one less than the tlist's numberOfItems
|
||||||
// max - Optional integer indicating end transform position
|
* @returns {object} A single matrix transform object
|
||||||
//
|
*/
|
||||||
// Returns:
|
|
||||||
// A single matrix transform object
|
|
||||||
svgedit.math.transformListToTransform = function (tlist, min, max) {
|
svgedit.math.transformListToTransform = function (tlist, min, max) {
|
||||||
if (tlist == null) {
|
if (tlist == null) {
|
||||||
// Or should tlist = null have been prevented before this?
|
// Or should tlist = null have been prevented before this?
|
||||||
|
@ -180,35 +173,26 @@ svgedit.math.transformListToTransform = function(tlist, min, max) {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Function: svgedit.math.getMatrix
|
/**
|
||||||
// Get the matrix object for a given element
|
* Get the matrix object for a given element
|
||||||
//
|
* @param {Element} elem - The DOM element to check
|
||||||
// Parameters:
|
* @returns {SVGMatrix} The matrix object associated with the element's transformlist
|
||||||
// elem - The DOM element to check
|
*/
|
||||||
//
|
|
||||||
// Returns:
|
|
||||||
// The matrix object associated with the element's transformlist
|
|
||||||
svgedit.math.getMatrix = function (elem) {
|
svgedit.math.getMatrix = function (elem) {
|
||||||
var tlist = svgedit.transformlist.getTransformList(elem);
|
var tlist = svgedit.transformlist.getTransformList(elem);
|
||||||
return svgedit.math.transformListToTransform(tlist).matrix;
|
return svgedit.math.transformListToTransform(tlist).matrix;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Function: svgedit.math.snapToAngle
|
/**
|
||||||
// Returns a 45 degree angle coordinate associated with the two given
|
* Returns a 45 degree angle coordinate associated with the two given
|
||||||
// coordinates
|
* coordinates
|
||||||
//
|
* @param {number} x1 - First coordinate's x value
|
||||||
// Parameters:
|
* @param {number} x2 - Second coordinate's x value
|
||||||
// x1 - First coordinate's x value
|
* @param {number} y1 - First coordinate's y value
|
||||||
// x2 - Second coordinate's x value
|
* @param {number} y2 - Second coordinate's y value
|
||||||
// y1 - First coordinate's y value
|
* @returns {AngleCoord45}
|
||||||
// y2 - Second coordinate's y value
|
*/
|
||||||
//
|
|
||||||
// Returns:
|
|
||||||
// Object with the following values:
|
|
||||||
// x - The angle-snapped x value
|
|
||||||
// y - The angle-snapped y value
|
|
||||||
// snapangle - The angle at which to snap
|
|
||||||
svgedit.math.snapToAngle = function (x1, y1, x2, y2) {
|
svgedit.math.snapToAngle = function (x1, y1, x2, y2) {
|
||||||
var snap = Math.PI / 4; // 45 degrees
|
var snap = Math.PI / 4; // 45 degrees
|
||||||
var dx = x2 - x1;
|
var dx = x2 - x1;
|
||||||
|
@ -225,15 +209,12 @@ svgedit.math.snapToAngle = function(x1, y1, x2, y2) {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Function: rectsIntersect
|
/**
|
||||||
// Check if two rectangles (BBoxes objects) intersect each other
|
* Check if two rectangles (BBoxes objects) intersect each other
|
||||||
//
|
* @param {SVGRect} r1 - The first BBox-like object
|
||||||
// Paramaters:
|
* @param {SVGRect} r2 - The second BBox-like object
|
||||||
// r1 - The first BBox-like object
|
* @returns {boolean} True if rectangles intersect
|
||||||
// r2 - The second BBox-like object
|
*/
|
||||||
//
|
|
||||||
// Returns:
|
|
||||||
// Boolean that's true if rectangles intersect
|
|
||||||
svgedit.math.rectsIntersect = function (r1, r2) {
|
svgedit.math.rectsIntersect = function (r1, r2) {
|
||||||
return r2.x < (r1.x + r1.width) &&
|
return r2.x < (r1.x + r1.width) &&
|
||||||
(r2.x + r2.width) > r1.x &&
|
(r2.x + r2.width) > r1.x &&
|
||||||
|
|
Loading…
Reference in New Issue