Refactor a few more functions into the math and path modules. Update MIME type on all JS files

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1984 eee81c28-f429-11dd-99c0-75d572ba1ddd
This commit is contained in:
Jeff Schiller
2011-02-09 06:14:47 +00:00
parent 0705f0b5c8
commit 99a73cc06f
5 changed files with 96 additions and 92 deletions

View File

@@ -183,6 +183,20 @@ svgedit.math.transformListToTransform = function(tlist, min, max) {
};
// Function: svgedit.math.getMatrix
// Get the matrix object for a given element
//
// Parameters:
// elem - The DOM element to check
//
// Returns:
// The matrix object associated with the element's transformlist
svgedit.math.getMatrix = function(elem) {
var tlist = svgedit.transformlist.getTransformList(elem);
return svgedit.math.transformListToTransform(tlist).matrix;
};
// Function: svgedit.math.snapToAngle
// Returns a 45 degree angle coordinate associated with the two given
// coordinates

View File

@@ -108,8 +108,8 @@ svgedit.path.getGripPt = function(seg, alt_pt) {
out = pt;
}
out.x *= editorContext_.currentZoom();
out.y *= editorContext_.currentZoom();
out.x *= editorContext_.getCurrentZoom();
out.y *= editorContext_.getCurrentZoom();
return out;
};
@@ -126,8 +126,8 @@ svgedit.path.getPointFromGrip = function(pt, path) {
out.y = pt.y;
}
out.x /= editorContext_.currentZoom();
out.y /= editorContext_.currentZoom();
out.x /= editorContext_.getCurrentZoom();
out.y /= editorContext_.getCurrentZoom();
return out;
};
@@ -342,6 +342,57 @@ svgedit.path.getSegSelector = function(seg, update) {
return segLine;
};
// Function: smoothControlPoints
// Takes three points and creates a smoother line based on them
//
// Parameters:
// ct1 - Object with x and y values (first control point)
// ct2 - Object with x and y values (second control point)
// pt - Object with x and y values (third point)
//
// Returns:
// Array of two "smoothed" point objects
svgedit.path.smoothControlPoints = this.smoothControlPoints = function(ct1, ct2, pt) {
// each point must not be the origin
var x1 = ct1.x - pt.x,
y1 = ct1.y - pt.y,
x2 = ct2.x - pt.x,
y2 = ct2.y - pt.y;
if ( (x1 != 0 || y1 != 0) && (x2 != 0 || y2 != 0) ) {
var anglea = Math.atan2(y1,x1),
angleb = Math.atan2(y2,x2),
r1 = Math.sqrt(x1*x1+y1*y1),
r2 = Math.sqrt(x2*x2+y2*y2),
nct1 = editorContext_.getSVGRoot().createSVGPoint(),
nct2 = editorContext_.getSVGRoot().createSVGPoint();
if (anglea < 0) { anglea += 2*Math.PI; }
if (angleb < 0) { angleb += 2*Math.PI; }
var angleBetween = Math.abs(anglea - angleb),
angleDiff = Math.abs(Math.PI - angleBetween)/2;
var new_anglea, new_angleb;
if (anglea - angleb > 0) {
new_anglea = angleBetween < Math.PI ? (anglea + angleDiff) : (anglea - angleDiff);
new_angleb = angleBetween < Math.PI ? (angleb - angleDiff) : (angleb + angleDiff);
}
else {
new_anglea = angleBetween < Math.PI ? (anglea - angleDiff) : (anglea + angleDiff);
new_angleb = angleBetween < Math.PI ? (angleb + angleDiff) : (angleb - angleDiff);
}
// rotate the points
nct1.x = r1 * Math.cos(new_anglea) + pt.x;
nct1.y = r1 * Math.sin(new_anglea) + pt.y;
nct2.x = r2 * Math.cos(new_angleb) + pt.x;
nct2.y = r2 * Math.sin(new_angleb) + pt.y;
return [nct1, nct2];
}
return undefined;
};
svgedit.path.Segment = function(index, item) {
this.selected = false;
this.index = index;
@@ -805,4 +856,23 @@ svgedit.path.Path.prototype.selectPt = function(pt, ctrl_num) {
}
};
// Update position of all points
svgedit.path.Path.prototype.update = function() {
var elem = this.elem;
if(svgedit.utilities.getRotationAngle(elem)) {
this.matrix = svgedit.math.getMatrix(elem);
this.imatrix = this.matrix.inverse();
} else {
this.matrix = null;
this.imatrix = null;
}
this.eachSeg(function(i) {
this.item = elem.pathSegList.getItem(i);
this.update();
});
return this;
};
})();

View File

@@ -1509,9 +1509,9 @@
if(svgCanvas.getHref(elem).indexOf('data:') !== 0) {
promptImgURL();
}
} else if(elname == 'text') {
} /*else if(elname == 'text') {
// TODO: Do something here for new text
}
}*/
}
if(!is_node && currentMode != 'pathedit') {

View File

@@ -321,6 +321,7 @@ var matrixMultiply = canvas.matrixMultiply = svgedit.math.matrixMultiply;
var hasMatrixTransform = canvas.hasMatrixTransform = svgedit.math.hasMatrixTransform;
var transformListToTransform = canvas.transformListToTransform = svgedit.math.transformListToTransform;
var snapToAngle = svgedit.math.snapToAngle;
var getMatrix = svgedit.math.getMatrix;
// initialize from units.js
// send in an object implementing the ElementContainer interface (see units.js)
@@ -438,7 +439,8 @@ var selectorManager = this.selectorManager = svgedit.select.getSelectorManager()
// Import from path.js
svgedit.path.init({
currentZoom: function() { return current_zoom; }
getCurrentZoom: function() { return current_zoom; },
getSVGRoot: function() { return svgroot; }
});
// Function: snapToGrid
@@ -2154,19 +2156,6 @@ var recalculateDimensions = this.recalculateDimensions = function(selected) {
// Root Current Transformation Matrix in user units
var root_sctm = null;
// Function: getMatrix
// Get the matrix object for a given element
//
// Parameters:
// elem - The DOM element to check
//
// Returns:
// The matrix object associated with the element's transformlist
var getMatrix = function(elem) {
var tlist = getTransformList(elem);
return transformListToTransform(tlist).matrix;
}
// Group: Selection
// Function: clearSelection
@@ -2311,58 +2300,6 @@ this.selectAllInCurrentLayer = function() {
}
};
// Function: smoothControlPoints
// Takes three points and creates a smoother line based on them
//
// Parameters:
// ct1 - Object with x and y values (first control point)
// ct2 - Object with x and y values (second control point)
// pt - Object with x and y values (third point)
//
// Returns:
// Array of two "smoothed" point objects
var smoothControlPoints = this.smoothControlPoints = function(ct1, ct2, pt) {
// each point must not be the origin
var x1 = ct1.x - pt.x,
y1 = ct1.y - pt.y,
x2 = ct2.x - pt.x,
y2 = ct2.y - pt.y;
if ( (x1 != 0 || y1 != 0) && (x2 != 0 || y2 != 0) ) {
var anglea = Math.atan2(y1,x1),
angleb = Math.atan2(y2,x2),
r1 = Math.sqrt(x1*x1+y1*y1),
r2 = Math.sqrt(x2*x2+y2*y2),
nct1 = svgroot.createSVGPoint(),
nct2 = svgroot.createSVGPoint();
if (anglea < 0) { anglea += 2*Math.PI; }
if (angleb < 0) { angleb += 2*Math.PI; }
var angleBetween = Math.abs(anglea - angleb),
angleDiff = Math.abs(Math.PI - angleBetween)/2;
var new_anglea, new_angleb;
if (anglea - angleb > 0) {
new_anglea = angleBetween < Math.PI ? (anglea + angleDiff) : (anglea - angleDiff);
new_angleb = angleBetween < Math.PI ? (angleb - angleDiff) : (angleb + angleDiff);
}
else {
new_anglea = angleBetween < Math.PI ? (anglea - angleDiff) : (anglea + angleDiff);
new_angleb = angleBetween < Math.PI ? (angleb + angleDiff) : (angleb - angleDiff);
}
// rotate the points
nct1.x = r1 * Math.cos(new_anglea) + pt.x;
nct1.y = r1 * Math.sin(new_anglea) + pt.y;
nct2.x = r2 * Math.cos(new_angleb) + pt.x;
nct2.y = r2 * Math.sin(new_angleb) + pt.y;
return [nct1, nct2];
}
return undefined;
};
// Function: getMouseTarget
// Gets the desired element from a mouse event
//
@@ -3994,25 +3931,6 @@ var pathActions = canvas.pathActions = function() {
}
// TODO: Move into path.js
// Update position of all points
svgedit.path.Path.prototype.update = function() {
var elem = this.elem;
if(getRotationAngle(elem)) {
this.matrix = getMatrix(elem);
this.imatrix = this.matrix.inverse();
} else {
this.matrix = null;
this.imatrix = null;
}
this.eachSeg(function(i) {
this.item = elem.pathSegList.getItem(i);
this.update();
});
return this;
}
svgedit.path.Path.prototype.endChanges = function(text) {
if(svgedit.browser.isWebkit()) resetD(this.elem);
var cmd = new ChangeElementCommand(this.elem, {d: this.last_d}, text);
@@ -4091,7 +4009,7 @@ var pathActions = canvas.pathActions = function() {
// if the previous segment had a control point, we want to smooth out
// the control points on both sides
if (prevCtlPt) {
var newpts = smoothControlPoints( prevCtlPt, ct1, curpos );
var newpts = svgedit.path.smoothControlPoints( prevCtlPt, ct1, curpos );
if (newpts && newpts.length == 2) {
var prevArr = d[d.length-1].split(',');
prevArr[2] = newpts[0].x;