2009-10-18 20:46:32 +00:00
|
|
|
/*
|
|
|
|
TODOs for Localizing:
|
|
|
|
|
|
|
|
- rename tool_path to tool_fhpath in all localization files (already updated in UI and script)
|
|
|
|
- rename tool_poly to tool_path in all localization files (already updated in UI and script)
|
|
|
|
- rename poly_node_x to path_node_x globally
|
|
|
|
- rename poly_node_y to path_node_y globally
|
|
|
|
- rename svninfo_dim to svginfo_dim globally (typo)
|
|
|
|
- provide translations in all other non-EN lang.XX.js files for:
|
|
|
|
- path_node_x
|
|
|
|
- path_node_y
|
|
|
|
- seg_type
|
|
|
|
- straight_segments
|
|
|
|
- curve_segments
|
|
|
|
- tool_node_clone
|
|
|
|
- tool_node_delete
|
|
|
|
- selLayerLabel
|
|
|
|
- selLayerNames
|
|
|
|
- sidepanel_handle
|
|
|
|
*/
|
2009-10-27 20:54:29 +00:00
|
|
|
var isWebkit = navigator.userAgent.indexOf("AppleWebKit") != -1;
|
2009-06-23 05:17:53 +00:00
|
|
|
if(!window.console) {
|
2009-09-22 21:50:55 +00:00
|
|
|
window.console = {};
|
|
|
|
window.console.log = function(str) {};
|
|
|
|
window.console.dir = function(str) {};
|
|
|
|
}
|
|
|
|
if( window.opera ) {
|
|
|
|
window.console.log = function(str) {opera.postError(str);}
|
2009-06-23 05:17:53 +00:00
|
|
|
}
|
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// this defines which elements and attributes that we support
|
2009-08-14 22:50:34 +00:00
|
|
|
// TODO: add <a> elements to this
|
2009-09-05 22:20:36 +00:00
|
|
|
// TODO: add <tspan> to this
|
2009-07-10 05:04:06 +00:00
|
|
|
var svgWhiteList = {
|
2009-10-19 20:13:52 +00:00
|
|
|
"circle": ["cx", "cy", "fill", "fill-opacity", "fill-rule", "id", "opacity", "r", "requiredFeatures", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "systemLanguage", "transform"],
|
2009-07-14 12:52:39 +00:00
|
|
|
"defs": [],
|
2009-10-17 20:29:56 +00:00
|
|
|
"desc": [],
|
2009-10-19 20:13:52 +00:00
|
|
|
"ellipse": ["cx", "cy", "fill", "fill-opacity", "fill-rule", "id", "opacity", "requiredFeatures", "rx", "ry", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "systemLanguage", "transform"],
|
2009-10-17 20:29:56 +00:00
|
|
|
"g": ["id", "display", "requiredFeatures", "systemLanguage", "transform"],
|
|
|
|
"image": ["height", "id", "opacity", "requiredFeatures", "systemLanguage", "transform", "width", "x", "xlink:href", "xlink:title", "y"],
|
2009-10-19 20:13:52 +00:00
|
|
|
"line": ["fill", "fill-opacity", "fill-rule", "id", "opacity", "requiredFeatures", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "systemLanguage", "transform", "x1", "x2", "y1", "y2"],
|
2009-10-17 20:29:56 +00:00
|
|
|
"linearGradient": ["id", "gradientTransform", "gradientUnits", "requiredFeatures", "spreadMethod", "systemLanguage", "x1", "x2", "y1", "y2"],
|
2009-10-19 20:13:52 +00:00
|
|
|
"path": ["d", "fill", "fill-opacity", "fill-rule", "id", "opacity", "requiredFeatures", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "systemLanguage", "transform"],
|
|
|
|
"polygon": ["id", "fill", "fill-opacity", "fill-rule", "id", "opacity", "points", "requiredFeatures", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "systemLanguage", "transform"],
|
|
|
|
"polyline": ["id", "fill", "fill-opacity", "fill-rule", "opacity", "points", "requiredFeatures", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "systemLanguage", "transform"],
|
2009-10-17 20:29:56 +00:00
|
|
|
"radialGradient": ["id", "cx", "cy", "fx", "fy", "gradientTransform", "gradientUnits", "r", "requiredFeatures", "spreadMethod", "systemLanguage"],
|
2009-10-19 20:13:52 +00:00
|
|
|
"rect": ["fill", "fill-opacity", "fill-rule", "height", "id", "opacity", "requiredFeatures", "rx", "ry", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "systemLanguage", "transform", "width", "x", "y"],
|
2009-10-17 20:29:56 +00:00
|
|
|
"stop": ["id", "offset", "requiredFeatures", "stop-color", "stop-opacity", "systemLanguage"],
|
|
|
|
"switch": ["id", "requiredFeatures", "systemLanguage"],
|
|
|
|
"svg": ["id", "height", "requiredFeatures", "systemLanguage", "transform", "viewBox", "width", "xmlns", "xmlns:xlink"],
|
2009-10-19 20:13:52 +00:00
|
|
|
"text": ["fill", "fill-opacity", "fill-rule", "font-family", "font-size", "font-style", "font-weight", "id", "opacity", "requiredFeatures", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "systemLanguage", "transform", "text-anchor", "x", "xml:space", "y"],
|
2009-09-20 23:04:57 +00:00
|
|
|
"title": [],
|
2009-08-17 06:56:55 +00:00
|
|
|
};
|
2009-07-10 05:04:06 +00:00
|
|
|
|
2009-10-20 13:49:53 +00:00
|
|
|
function SvgCanvas(c)
|
|
|
|
{
|
|
|
|
|
2009-10-05 08:16:41 +00:00
|
|
|
var toXml = function(str) {
|
|
|
|
return str.replace("&", "&").replace("<", "<").replace(">",">");
|
|
|
|
};
|
|
|
|
var fromXml = function(str) {
|
|
|
|
return str.replace(">", ">").replace("<", "<").replace("&", "&");
|
|
|
|
};
|
|
|
|
|
2009-10-02 15:05:31 +00:00
|
|
|
var pathFuncsStrs = ['Moveto','Lineto','CurvetoCubic','CurvetoQuadratic','Arc','LinetoHorizontal','LinetoVertical','CurvetoCubicSmooth','CurvetoQuadraticSmooth']
|
|
|
|
var pathFuncs = [0,'ClosePath'];
|
|
|
|
$.each(pathFuncsStrs,function(i,s){pathFuncs.push(s+'Abs');pathFuncs.push(s+'Rel');});
|
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
// These command objects are used for the Undo/Redo stack
|
|
|
|
// attrs contains the values that the attributes had before the change
|
|
|
|
function ChangeElementCommand(elem, attrs, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text ? ("Change " + elem.tagName + " " + text) : ("Change " + elem.tagName);
|
|
|
|
this.newValues = {};
|
|
|
|
this.oldValues = attrs;
|
|
|
|
for (attr in attrs) {
|
|
|
|
if (attr == "#text") this.newValues[attr] = elem.textContent;
|
|
|
|
else this.newValues[attr] = elem.getAttribute(attr);
|
|
|
|
}
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.apply = function() {
|
2009-09-06 03:51:16 +00:00
|
|
|
var bChangedTransform = false;
|
2009-06-25 02:54:07 +00:00
|
|
|
for( attr in this.newValues ) {
|
|
|
|
if (this.newValues[attr]) {
|
|
|
|
if (attr == "#text") this.elem.textContent = this.newValues[attr];
|
|
|
|
else this.elem.setAttribute(attr, this.newValues[attr]);
|
|
|
|
}
|
|
|
|
else {
|
2009-09-30 20:11:08 +00:00
|
|
|
if (attr == "#text") this.elem.textContent = "";
|
|
|
|
else {
|
|
|
|
this.elem.setAttribute(attr, "");
|
|
|
|
this.elem.removeAttribute(attr);
|
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-09-06 03:51:16 +00:00
|
|
|
if (attr == "transform") { bChangedTransform = true; }
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-09-04 05:10:48 +00:00
|
|
|
// relocate rotational transform, if necessary
|
2009-09-06 03:51:16 +00:00
|
|
|
if(!bChangedTransform) {
|
2009-09-04 05:10:48 +00:00
|
|
|
var angle = canvas.getRotationAngle(elem);
|
|
|
|
if (angle) {
|
|
|
|
var bbox = elem.getBBox();
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(bbox.x + bbox.width/2),
|
|
|
|
cy = round(bbox.y + bbox.height/2);
|
2009-09-04 05:10:48 +00:00
|
|
|
var rotate = ["rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
|
|
|
if (rotate != elem.getAttribute("transform")) {
|
|
|
|
elem.setAttribute("transform", rotate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-22 02:42:31 +00:00
|
|
|
// if we are changing layer names, re-identify all layers
|
2009-10-21 15:12:32 +00:00
|
|
|
if (this.elem.tagName == "title" && this.elem.parentNode.parentNode == svgcontent) {
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 02:42:31 +00:00
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
return true;
|
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.unapply = function() {
|
2009-09-06 03:51:16 +00:00
|
|
|
var bChangedTransform = false;
|
2009-06-25 02:54:07 +00:00
|
|
|
for( attr in this.oldValues ) {
|
|
|
|
if (this.oldValues[attr]) {
|
|
|
|
if (attr == "#text") this.elem.textContent = this.oldValues[attr];
|
|
|
|
else this.elem.setAttribute(attr, this.oldValues[attr]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (attr == "#text") this.elem.textContent = "";
|
|
|
|
else this.elem.removeAttribute(attr);
|
|
|
|
}
|
2009-09-06 03:51:16 +00:00
|
|
|
if (attr == "transform") { bChangedTransform = true; }
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-09-04 05:10:48 +00:00
|
|
|
// relocate rotational transform, if necessary
|
2009-09-06 03:51:16 +00:00
|
|
|
if(!bChangedTransform) {
|
2009-09-04 05:10:48 +00:00
|
|
|
var angle = canvas.getRotationAngle(elem);
|
|
|
|
if (angle) {
|
|
|
|
var bbox = elem.getBBox();
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(bbox.x + bbox.width/2),
|
|
|
|
cy = round(bbox.y + bbox.height/2);
|
2009-09-04 05:10:48 +00:00
|
|
|
var rotate = ["rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
|
|
|
if (rotate != elem.getAttribute("transform")) {
|
|
|
|
elem.setAttribute("transform", rotate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-22 02:42:31 +00:00
|
|
|
// if we are changing layer names, re-identify all layers
|
2009-10-21 15:12:32 +00:00
|
|
|
if (this.elem.tagName == "title" && this.elem.parentNode.parentNode == svgcontent) {
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 02:42:31 +00:00
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
return true;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.elements = function() { return [this.elem]; }
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function InsertElementCommand(elem, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text || ("Create " + elem.tagName);
|
|
|
|
this.parent = elem.parentNode;
|
|
|
|
|
2009-09-22 01:00:24 +00:00
|
|
|
this.apply = function() {
|
|
|
|
this.elem = this.parent.insertBefore(this.elem, this.elem.nextSibling);
|
2009-10-21 15:12:32 +00:00
|
|
|
if (this.parent == svgcontent) {
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 01:00:24 +00:00
|
|
|
}
|
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.unapply = function() {
|
|
|
|
this.parent = this.elem.parentNode;
|
|
|
|
this.elem = this.elem.parentNode.removeChild(this.elem);
|
2009-10-21 15:12:32 +00:00
|
|
|
if (this.parent == svgcontent) {
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 01:00:24 +00:00
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.elements = function() { return [this.elem]; };
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function RemoveElementCommand(elem, parent, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text || ("Delete " + elem.tagName);
|
|
|
|
this.parent = parent;
|
|
|
|
|
|
|
|
this.apply = function() {
|
|
|
|
this.parent = this.elem.parentNode;
|
2009-06-29 14:38:30 +00:00
|
|
|
this.elem = this.parent.removeChild(this.elem);
|
2009-10-21 15:12:32 +00:00
|
|
|
if (this.parent == svgcontent) {
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 01:00:24 +00:00
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
};
|
|
|
|
|
2009-09-22 01:00:24 +00:00
|
|
|
this.unapply = function() {
|
|
|
|
this.elem = this.parent.insertBefore(this.elem, this.elem.nextSibling);
|
2009-10-21 15:12:32 +00:00
|
|
|
if (this.parent == svgcontent) {
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 01:00:24 +00:00
|
|
|
}
|
|
|
|
};
|
2009-07-01 21:28:22 +00:00
|
|
|
|
|
|
|
this.elements = function() { return [this.elem]; };
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function MoveElementCommand(elem, oldNextSibling, oldParent, text) {
|
|
|
|
this.elem = elem;
|
2009-09-06 03:51:16 +00:00
|
|
|
this.text = text ? ("Move " + elem.tagName + " to " + text) : ("Move " + elem.tagName);
|
2009-06-25 02:54:07 +00:00
|
|
|
this.oldNextSibling = oldNextSibling;
|
|
|
|
this.oldParent = oldParent;
|
|
|
|
this.newNextSibling = elem.nextSibling;
|
|
|
|
this.newParent = elem.parentNode;
|
2009-06-29 14:38:30 +00:00
|
|
|
|
|
|
|
this.apply = function() {
|
2009-06-25 02:54:07 +00:00
|
|
|
this.elem = this.newParent.insertBefore(this.elem, this.newNextSibling);
|
2009-10-21 15:12:32 +00:00
|
|
|
if (this.newParent == svgcontent) {
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 01:00:24 +00:00
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
|
|
|
this.unapply = function() {
|
2009-06-25 02:54:07 +00:00
|
|
|
this.elem = this.oldParent.insertBefore(this.elem, this.oldNextSibling);
|
2009-10-21 15:12:32 +00:00
|
|
|
if (this.oldParent == svgcontent) {
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 01:00:24 +00:00
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
};
|
2009-07-01 21:28:22 +00:00
|
|
|
|
|
|
|
this.elements = function() { return [this.elem]; };
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
|
|
|
|
2009-06-29 03:58:34 +00:00
|
|
|
// TODO: create a 'typing' command object that tracks changes in text
|
|
|
|
// if a new Typing command is created and the top command on the stack is also a Typing
|
|
|
|
// and they both affect the same element, then collapse the two commands into one
|
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
// this command object acts an arbitrary number of subcommands
|
|
|
|
function BatchCommand(text) {
|
|
|
|
this.text = text || "Batch Command";
|
|
|
|
this.stack = [];
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.apply = function() {
|
2009-07-08 00:03:08 +00:00
|
|
|
var len = this.stack.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
2009-07-01 21:28:22 +00:00
|
|
|
this.stack[i].apply();
|
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.unapply = function() {
|
|
|
|
for (var i = this.stack.length-1; i >= 0; i--) {
|
|
|
|
this.stack[i].unapply();
|
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
this.elements = function() {
|
|
|
|
// iterate through all our subcommands and find all the elements we are changing
|
|
|
|
var elems = [];
|
|
|
|
var cmd = this.stack.length;
|
|
|
|
while (cmd--) {
|
|
|
|
var thisElems = this.stack[cmd].elements();
|
|
|
|
var elem = thisElems.length;
|
|
|
|
while (elem--) {
|
|
|
|
if (elems.indexOf(thisElems[elem]) == -1) elems.push(thisElems[elem]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return elems;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-02 04:50:24 +00:00
|
|
|
this.addSubCommand = function(cmd) { this.stack.push(cmd); };
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.isEmpty = function() { return this.stack.length == 0; };
|
|
|
|
}
|
2009-06-29 03:58:34 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// private members
|
|
|
|
|
|
|
|
// **************************************************************************************
|
|
|
|
// FIXME: what's the right way to make this 'class' private to the SelectorManager?
|
|
|
|
function Selector(id, elem) {
|
|
|
|
// this is the selector's unique number
|
|
|
|
this.id = id;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this holds a reference to the element for which this selector is being used
|
|
|
|
this.selectedElement = elem;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this is a flag used internally to track whether the selector is being used or not
|
2009-06-29 03:58:34 +00:00
|
|
|
this.locked = true;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this function is used to reset the id and element that the selector is attached to
|
|
|
|
this.reset = function(e) {
|
|
|
|
this.locked = true;
|
|
|
|
this.selectedElement = e;
|
2009-07-01 15:34:15 +00:00
|
|
|
this.resize();
|
2009-08-14 18:35:48 +00:00
|
|
|
selectorManager.update();
|
2009-06-29 13:20:39 +00:00
|
|
|
this.selectorGroup.setAttribute("display", "inline");
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this holds a reference to the <g> element that holds all visual elements of the selector
|
|
|
|
this.selectorGroup = addSvgElementFromJson({ "element": "g",
|
|
|
|
"attr": {"id": ("selectorGroup"+this.id)}
|
|
|
|
});
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this holds a reference to <rect> element
|
|
|
|
this.selectorRect = this.selectorGroup.appendChild( addSvgElementFromJson({
|
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"id": ("selectedBox"+this.id),
|
|
|
|
"fill": "none",
|
|
|
|
"stroke": "blue",
|
|
|
|
"stroke-width": "1",
|
|
|
|
"stroke-dasharray": "5,5",
|
|
|
|
"width": 1,
|
|
|
|
"height": 1,
|
|
|
|
// need to specify this so that the rect is not selectable
|
2009-08-04 03:14:23 +00:00
|
|
|
"style": "pointer-events:none"
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
|
|
|
}) );
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this holds a reference to the grip elements for this selector
|
|
|
|
this.selectorGrips = { "nw":null,
|
|
|
|
"n":null,
|
|
|
|
"ne":null,
|
|
|
|
"e":null,
|
2009-08-26 13:59:25 +00:00
|
|
|
"se":null,
|
2009-06-29 13:20:39 +00:00
|
|
|
"s":null,
|
2009-08-26 13:59:25 +00:00
|
|
|
"sw":null,
|
|
|
|
"w":null
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-08-26 19:09:13 +00:00
|
|
|
this.rotateGripConnector = this.selectorGroup.appendChild( addSvgElementFromJson({
|
|
|
|
"element": "line",
|
|
|
|
"attr": {
|
|
|
|
"id": ("selectorGrip_rotate_connector_" + this.id),
|
|
|
|
"stroke": "blue",
|
2009-09-28 18:19:22 +00:00
|
|
|
"stroke-width": "1"
|
2009-08-26 19:09:13 +00:00
|
|
|
}
|
|
|
|
}) );
|
|
|
|
this.rotateGrip = this.selectorGroup.appendChild( addSvgElementFromJson({
|
|
|
|
"element": "circle",
|
|
|
|
"attr": {
|
|
|
|
"id": ("selectorGrip_rotate_" + this.id),
|
|
|
|
"fill": "lime",
|
|
|
|
"r": 4,
|
|
|
|
"stroke": "blue",
|
2009-09-28 18:19:22 +00:00
|
|
|
"stroke-width": 2,
|
|
|
|
"style": "cursor:url(images/rotate.png) 12 12, auto;"
|
2009-08-26 19:09:13 +00:00
|
|
|
}
|
|
|
|
}) );
|
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// add the corner grips
|
|
|
|
for (dir in this.selectorGrips) {
|
2009-07-01 21:59:11 +00:00
|
|
|
this.selectorGrips[dir] = this.selectorGroup.appendChild(
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"id": ("selectorGrip_" + dir + "_" + this.id),
|
|
|
|
"fill": "blue",
|
|
|
|
"width": 6,
|
|
|
|
"height": 6,
|
|
|
|
"style": ("cursor:" + dir + "-resize"),
|
|
|
|
// This expands the mouse-able area of the grips making them
|
|
|
|
// easier to grab with the mouse.
|
|
|
|
// This works in Opera and WebKit, but does not work in Firefox
|
|
|
|
// see https://bugzilla.mozilla.org/show_bug.cgi?id=500174
|
|
|
|
"stroke-width": 2,
|
|
|
|
"pointer-events":"all",
|
|
|
|
"display":"none"
|
|
|
|
}
|
|
|
|
}) );
|
2009-06-29 13:20:39 +00:00
|
|
|
$('#'+this.selectorGrips[dir].id).mousedown( function() {
|
|
|
|
current_mode = "resize";
|
2009-07-01 19:28:08 +00:00
|
|
|
current_resize_mode = this.id.substr(13,this.id.indexOf("_",13)-13);
|
2009-06-29 13:20:39 +00:00
|
|
|
});
|
2009-08-26 19:45:28 +00:00
|
|
|
$('#selectorGrip_rotate_'+id).mousedown( function() {
|
|
|
|
current_mode = "rotate";
|
|
|
|
});
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
this.showGrips = function(show) {
|
2009-07-30 22:45:59 +00:00
|
|
|
// TODO: use suspendRedraw() here
|
2009-08-26 19:09:13 +00:00
|
|
|
var bShow = show ? "inline" : "none";
|
2009-08-27 14:44:23 +00:00
|
|
|
this.rotateGrip.setAttribute("display", bShow);
|
|
|
|
this.rotateGripConnector.setAttribute("display", bShow);
|
|
|
|
var elem = this.selectedElement;
|
2009-09-13 15:50:43 +00:00
|
|
|
if(elem && (elem.tagName == "text" || elem.tagName == "g")) bShow = "none";
|
2009-06-29 13:20:39 +00:00
|
|
|
for (dir in this.selectorGrips) {
|
2009-08-26 19:09:13 +00:00
|
|
|
this.selectorGrips[dir].setAttribute("display", bShow);
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
2009-08-31 20:05:52 +00:00
|
|
|
if(elem) this.updateGripCursors(canvas.getRotationAngle(elem));
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-08-26 13:59:25 +00:00
|
|
|
|
|
|
|
// Updates cursors for corner grips on rotation so arrows point the right way
|
|
|
|
this.updateGripCursors = function(angle) {
|
|
|
|
var dir_arr = [];
|
|
|
|
var steps = Math.round(angle / 45);
|
2009-08-27 12:37:02 +00:00
|
|
|
if(steps < 0) steps += 8;
|
2009-08-26 13:59:25 +00:00
|
|
|
for (dir in this.selectorGrips) {
|
|
|
|
dir_arr.push(dir);
|
|
|
|
}
|
|
|
|
while(steps > 0) {
|
|
|
|
dir_arr.push(dir_arr.shift());
|
|
|
|
steps--;
|
|
|
|
}
|
|
|
|
var i = 0;
|
|
|
|
for (dir in this.selectorGrips) {
|
|
|
|
this.selectorGrips[dir].setAttribute('style', ("cursor:" + dir_arr[i] + "-resize"));
|
|
|
|
i++;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2009-08-21 15:53:36 +00:00
|
|
|
this.resize = function(cur_bbox) {
|
2009-06-29 13:20:39 +00:00
|
|
|
var selectedBox = this.selectorRect;
|
|
|
|
var selectedGrips = this.selectorGrips;
|
|
|
|
var selected = this.selectedElement;
|
2009-09-17 14:47:54 +00:00
|
|
|
var sw = round(selected.getAttribute("stroke-width"));
|
|
|
|
var offset = 1/canvas.getZoom();
|
|
|
|
if (selected.getAttribute("stroke") != "none" && !isNaN(sw)) {
|
2009-06-29 13:20:39 +00:00
|
|
|
offset += sw/2;
|
|
|
|
}
|
|
|
|
if (selected.tagName == "text") {
|
2009-09-17 14:47:54 +00:00
|
|
|
offset += 2/canvas.getZoom();
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
2009-08-27 04:01:19 +00:00
|
|
|
var oldbox = canvas.getBBox(this.selectedElement);
|
|
|
|
var bbox = cur_bbox || oldbox;
|
2009-06-29 13:20:39 +00:00
|
|
|
var l=bbox.x-offset, t=bbox.y-offset, w=bbox.width+(offset<<1), h=bbox.height+(offset<<1);
|
2009-08-24 19:19:27 +00:00
|
|
|
var sr_handle = svgroot.suspendRedraw(100);
|
2009-09-09 14:18:24 +00:00
|
|
|
l*=current_zoom;
|
|
|
|
t*=current_zoom;
|
|
|
|
w*=current_zoom;
|
|
|
|
h*=current_zoom;
|
2009-08-24 18:39:22 +00:00
|
|
|
assignAttributes(selectedBox, {
|
|
|
|
'x': l,
|
|
|
|
'y': t,
|
|
|
|
'width': w,
|
|
|
|
'height': h
|
|
|
|
});
|
2009-08-24 19:19:27 +00:00
|
|
|
|
2009-08-24 18:39:22 +00:00
|
|
|
var gripCoords = {
|
|
|
|
nw: [l-3, t-3],
|
|
|
|
ne: [l+w-3, t-3],
|
|
|
|
sw: [l-3, t+h-3],
|
|
|
|
se: [l+w-3, t+h-3],
|
|
|
|
n: [l+w/2-3, t-3],
|
|
|
|
w: [l-3, t+h/2-3],
|
|
|
|
e: [l+w-3, t+h/2-3],
|
|
|
|
s: [l+w/2-3, t+h-3]
|
|
|
|
};
|
|
|
|
$.each(gripCoords, function(dir, coords) {
|
|
|
|
assignAttributes(selectedGrips[dir], {
|
|
|
|
x: coords[0], y: coords[1]
|
|
|
|
});
|
|
|
|
});
|
2009-08-21 15:53:36 +00:00
|
|
|
|
2009-08-26 19:09:13 +00:00
|
|
|
assignAttributes(this.rotateGripConnector, { x1: l+w/2, y1: t-20, x2: l+w/2, y2: t });
|
|
|
|
assignAttributes(this.rotateGrip, { cx: l+w/2, cy: t-20 });
|
|
|
|
|
2009-08-21 15:53:36 +00:00
|
|
|
// empty out the transform attribute
|
|
|
|
this.selectorGroup.setAttribute("transform", "");
|
|
|
|
this.selectorGroup.removeAttribute("transform");
|
|
|
|
|
|
|
|
// align selector group with element coordinate axes
|
2009-08-27 04:01:19 +00:00
|
|
|
var elem = this.selectedElement;
|
|
|
|
var transform = elem.getAttribute("transform");
|
|
|
|
var angle = canvas.getRotationAngle(elem);
|
|
|
|
if (angle) {
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(oldbox.x + oldbox.width/2) * current_zoom
|
|
|
|
cy = round(oldbox.y + oldbox.height/2) * current_zoom;
|
2009-08-27 04:01:19 +00:00
|
|
|
this.selectorGroup.setAttribute("transform", "rotate("+angle+" " + cx + "," + cy + ")");
|
2009-08-21 15:53:36 +00:00
|
|
|
}
|
2009-08-24 19:19:27 +00:00
|
|
|
svgroot.unsuspendRedraw(sr_handle);
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 15:34:15 +00:00
|
|
|
// now initialize the selector
|
|
|
|
this.reset(elem);
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-06-01 17:35:22 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
function SelectorManager() {
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this will hold the <g> element that contains all selector rects/grips
|
|
|
|
this.selectorParentGroup = null;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-30 06:24:41 +00:00
|
|
|
// this is a special rect that is used for multi-select
|
|
|
|
this.rubberBandBox = null;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this will hold objects of type Selector (see above)
|
|
|
|
this.selectors = [];
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-30 06:37:39 +00:00
|
|
|
// this holds a map of SVG elements to their Selector object
|
|
|
|
this.selectorMap = {};
|
2009-06-30 06:24:41 +00:00
|
|
|
|
|
|
|
// local reference to this object
|
|
|
|
var mgr = this;
|
2009-09-04 06:36:37 +00:00
|
|
|
|
|
|
|
this.initGroup = function() {
|
2009-09-29 17:01:28 +00:00
|
|
|
// remove old selector parent group if it existed
|
|
|
|
if (mgr.selectorParentGroup && mgr.selectorParentGroup.parentNode) {
|
|
|
|
mgr.selectorParentGroup.parentNode.removeChild(mgr.selectorParentGroup);
|
|
|
|
}
|
|
|
|
// create parent selector group and add it to svgroot
|
|
|
|
mgr.selectorParentGroup = svgdoc.createElementNS(svgns, "g");
|
|
|
|
mgr.selectorParentGroup.setAttribute("id", "selectorParentGroup");
|
|
|
|
svgroot.appendChild(mgr.selectorParentGroup);
|
2009-09-04 17:44:08 +00:00
|
|
|
mgr.selectorMap = {};
|
|
|
|
mgr.selectors = [];
|
|
|
|
mgr.rubberBandBox = null;
|
2009-09-10 20:34:20 +00:00
|
|
|
mgr.update();
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
this.requestSelector = function(elem) {
|
2009-07-01 15:34:15 +00:00
|
|
|
if (elem == null) return null;
|
2009-06-29 13:20:39 +00:00
|
|
|
var N = this.selectors.length;
|
2009-06-30 14:29:28 +00:00
|
|
|
// if we've already acquired one for this element, return it
|
2009-07-01 21:59:11 +00:00
|
|
|
if (typeof(this.selectorMap[elem.id]) == "object") {
|
|
|
|
this.selectorMap[elem.id].locked = true;
|
|
|
|
return this.selectorMap[elem.id];
|
2009-06-30 14:29:28 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
for (var i = 0; i < N; ++i) {
|
|
|
|
if (this.selectors[i] && !this.selectors[i].locked) {
|
|
|
|
this.selectors[i].locked = true;
|
2009-07-01 15:34:15 +00:00
|
|
|
this.selectors[i].reset(elem);
|
2009-07-01 21:59:11 +00:00
|
|
|
this.selectorMap[elem.id] = this.selectors[i];
|
2009-06-29 13:20:39 +00:00
|
|
|
return this.selectors[i];
|
2009-06-29 03:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-29 13:20:39 +00:00
|
|
|
// if we reached here, no available selectors were found, we create one
|
|
|
|
this.selectors[N] = new Selector(N, elem);
|
|
|
|
this.selectorParentGroup.appendChild(this.selectors[N].selectorGroup);
|
2009-07-01 21:59:11 +00:00
|
|
|
this.selectorMap[elem.id] = this.selectors[N];
|
2009-06-29 13:20:39 +00:00
|
|
|
return this.selectors[N];
|
|
|
|
};
|
2009-07-01 15:34:15 +00:00
|
|
|
this.releaseSelector = function(elem) {
|
|
|
|
if (elem == null) return;
|
2009-06-29 13:20:39 +00:00
|
|
|
var N = this.selectors.length;
|
2009-07-01 21:59:11 +00:00
|
|
|
var sel = this.selectorMap[elem.id];
|
2009-06-29 13:20:39 +00:00
|
|
|
for (var i = 0; i < N; ++i) {
|
|
|
|
if (this.selectors[i] && this.selectors[i] == sel) {
|
|
|
|
if (sel.locked == false) {
|
|
|
|
console.log("WARNING! selector was released but was already unlocked");
|
|
|
|
}
|
2009-07-01 21:59:11 +00:00
|
|
|
delete this.selectorMap[elem.id];
|
2009-06-29 13:20:39 +00:00
|
|
|
sel.locked = false;
|
|
|
|
sel.selectedElement = null;
|
2009-07-01 19:28:08 +00:00
|
|
|
sel.showGrips(false);
|
2009-06-29 13:20:39 +00:00
|
|
|
|
|
|
|
// remove from DOM and store reference in JS but only if it exists in the DOM
|
|
|
|
try {
|
|
|
|
sel.selectorGroup.setAttribute("display", "none");
|
|
|
|
} catch(e) { }
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this keeps the selector groups as the last child in the document
|
|
|
|
this.update = function() {
|
|
|
|
this.selectorParentGroup = svgroot.appendChild(this.selectorParentGroup);
|
2009-08-07 05:44:55 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-30 06:24:41 +00:00
|
|
|
this.getRubberBandBox = function() {
|
|
|
|
if (this.rubberBandBox == null) {
|
2009-06-30 17:50:08 +00:00
|
|
|
this.rubberBandBox = this.selectorParentGroup.appendChild(
|
2009-07-01 19:28:08 +00:00
|
|
|
addSvgElementFromJson({ "element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"id": "selectorRubberBand",
|
|
|
|
"fill": "blue",
|
|
|
|
"fill-opacity": 0.15,
|
|
|
|
"stroke": "blue",
|
|
|
|
"stroke-width": 0.5,
|
|
|
|
"display": "none",
|
2009-08-04 03:14:23 +00:00
|
|
|
"style": "pointer-events:none"
|
2009-07-01 19:28:08 +00:00
|
|
|
}
|
|
|
|
}));
|
2009-06-30 06:24:41 +00:00
|
|
|
}
|
|
|
|
return this.rubberBandBox;
|
2009-08-07 05:44:55 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-04 06:36:37 +00:00
|
|
|
this.initGroup();
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
|
|
|
// **************************************************************************************
|
2009-06-25 08:22:30 +00:00
|
|
|
|
2009-10-27 20:54:29 +00:00
|
|
|
// **************************************************************************************
|
|
|
|
// SVGTransformList implementation for Webkit
|
|
|
|
// These methods do not currently raise any exceptions.
|
|
|
|
// These methods also do not check that transforms are being inserted or handle if
|
|
|
|
// a transform is already in the list, etc. This is basically implementing as much
|
|
|
|
// of SVGTransformList that we need to get the job done.
|
|
|
|
//
|
|
|
|
// interface SVGEditTransformList {
|
2009-10-28 14:09:46 +00:00
|
|
|
// attribute unsigned long numberOfItems;
|
|
|
|
// void clear ( )
|
|
|
|
// SVGTransform initialize ( in SVGTransform newItem )
|
|
|
|
// SVGTransform getItem ( in unsigned long index )
|
|
|
|
// SVGTransform insertItemBefore ( in SVGTransform newItem, in unsigned long index )
|
|
|
|
// SVGTransform replaceItem ( in SVGTransform newItem, in unsigned long index )
|
|
|
|
// SVGTransform removeItem ( in unsigned long index )
|
|
|
|
// SVGTransform appendItem ( in SVGTransform newItem )
|
|
|
|
// SVGTransform createSVGTransformFromMatrix ( in SVGMatrix matrix );
|
|
|
|
// SVGTransform consolidate ( );
|
|
|
|
// }
|
2009-10-27 20:54:29 +00:00
|
|
|
// **************************************************************************************
|
|
|
|
var svgTransformLists = {};
|
|
|
|
var SVGEditTransformList = function(elem) {
|
|
|
|
this._elem = elem || null;
|
|
|
|
this._xforms = [];
|
2009-10-28 14:09:46 +00:00
|
|
|
this._update = function() {
|
|
|
|
var tstr = "";
|
|
|
|
for (var i = 0; i < _this.numberOfItems; ++i) {
|
|
|
|
var xform = _this.getItem(i);
|
|
|
|
var m = xform.matrix;
|
|
|
|
switch (xform.type) {
|
|
|
|
case 2: // translate
|
|
|
|
tstr += "translate(" + m.e + "," + m.f + ") ";
|
|
|
|
break;
|
|
|
|
case 3: // scale
|
|
|
|
if (m.a == m.d) tstr += "scale(" + m.a + ") ";
|
|
|
|
else tstr += "scale(" + m.a + "," + m.d + ") ";
|
|
|
|
break;
|
|
|
|
case 4: // rotate (with a translate?)
|
|
|
|
// TODO: can i get the center right here and now from bbox?
|
|
|
|
tstr += "rotate(" + xform.angle + ") ";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_this._elem.setAttribute("transform", tstr);
|
|
|
|
};
|
2009-10-27 20:54:29 +00:00
|
|
|
_this = this;
|
|
|
|
|
|
|
|
this.numberOfItems = 0;
|
|
|
|
this.clear = function() {
|
|
|
|
_this.numberOfItems = 0;
|
|
|
|
_this._xforms = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
this.initialize = function(newItem) {
|
|
|
|
_this.numberOfItems = 1;
|
|
|
|
_this._xforms = [newItem];
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getItem = function(index) {
|
|
|
|
if (index < _this.numberOfItems && index >= 0) {
|
|
|
|
return _this._xforms[index];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.insertItemBefore = function(newItem, index) {
|
|
|
|
var retValue = null;
|
|
|
|
if (index >= 0) {
|
|
|
|
if (index < _this.numberOfItems) {
|
|
|
|
var newxforms = new Array(_this.numberOfItems + 1);
|
|
|
|
// TODO: use array copying and slicing
|
|
|
|
for ( var i = 0; i < index; ++i) {
|
|
|
|
newxforms[i] = _this._xforms[i];
|
|
|
|
}
|
|
|
|
newxforms[i] = newItem;
|
|
|
|
for ( var j = i+1; i < _this.numberOfItems; ++j, ++i) {
|
|
|
|
newxforms[j] = _this._xforms[i];
|
|
|
|
}
|
|
|
|
_this.numberOfItems++;
|
|
|
|
_this._xforms = newxforms;
|
|
|
|
retValue = newItem;
|
2009-10-28 14:09:46 +00:00
|
|
|
_this._update();
|
2009-10-27 20:54:29 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
retValue = _this.appendItem(newItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.replaceItem = function(newItem, index) {
|
|
|
|
var retValue = null;
|
|
|
|
if (index < _this.numberOfItems && index >= 0) {
|
|
|
|
_this._xforms[index] = newItem;
|
|
|
|
retValue = newItem;
|
2009-10-28 14:09:46 +00:00
|
|
|
_this._update();
|
2009-10-27 20:54:29 +00:00
|
|
|
}
|
|
|
|
return retValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.removeItem = function(index) {
|
|
|
|
var retValue = null;
|
|
|
|
if (index < _this.numberOfItems && index >= 0) {
|
|
|
|
var retValue = _this._xforms[index];
|
|
|
|
var newxforms = new Array(_this.numberOfItems - 1);
|
|
|
|
for (var i = 0; i < index; ++i) {
|
|
|
|
newxforms[i] = _this._xforms[i];
|
|
|
|
}
|
|
|
|
for (var j = i; j < _this.numberOfItems-1; ++j, ++i) {
|
|
|
|
newxforms[j] = _this._xforms[i+1];
|
|
|
|
}
|
|
|
|
_this.numberOfItems--;
|
|
|
|
_this._xforms = newxforms;
|
2009-10-28 14:09:46 +00:00
|
|
|
_this._update();
|
2009-10-27 20:54:29 +00:00
|
|
|
}
|
|
|
|
return retValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.appendItem = function(newItem) {
|
|
|
|
_this._xforms.push(newItem);
|
|
|
|
_this.numberOfItems++;
|
2009-10-28 14:09:46 +00:00
|
|
|
_this._update();
|
2009-10-27 20:54:29 +00:00
|
|
|
return newItem;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// **************************************************************************************
|
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
var addSvgElementFromJson = function(data) {
|
|
|
|
return canvas.updateElementFromJson(data)
|
|
|
|
};
|
|
|
|
|
2009-08-24 19:19:27 +00:00
|
|
|
var assignAttributes = function(node, attrs, suspendLength) {
|
|
|
|
if(!suspendLength) suspendLength = 0;
|
2009-10-14 03:03:13 +00:00
|
|
|
// Opera has a problem with suspendRedraw() apparently
|
|
|
|
var handle = null;
|
|
|
|
if (!window.opera) svgroot.suspendRedraw(suspendLength);
|
2009-08-24 19:19:27 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
for (i in attrs) {
|
2009-10-16 13:17:23 +00:00
|
|
|
var ns = (i.substr(0,4) == "xml:" ? xmlns :
|
|
|
|
i.substr(0,6) == "xlink:" ? xlinkns : null);
|
2009-10-13 00:51:15 +00:00
|
|
|
node.setAttributeNS(ns, i, attrs[i]);
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
2009-08-24 19:19:27 +00:00
|
|
|
|
2009-10-14 03:03:13 +00:00
|
|
|
if (!window.opera) svgroot.unsuspendRedraw(handle);
|
2009-08-07 05:44:55 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
// remove unneeded attributes
|
|
|
|
// makes resulting SVG smaller
|
|
|
|
var cleanupElement = function(element) {
|
|
|
|
var handle = svgroot.suspendRedraw(60);
|
|
|
|
if (element.getAttribute('fill-opacity') == '1')
|
|
|
|
element.removeAttribute('fill-opacity');
|
|
|
|
if (element.getAttribute('opacity') == '1')
|
|
|
|
element.removeAttribute('opacity');
|
|
|
|
if (element.getAttribute('stroke') == 'none')
|
|
|
|
element.removeAttribute('stroke');
|
|
|
|
if (element.getAttribute('stroke-dasharray') == 'none')
|
|
|
|
element.removeAttribute('stroke-dasharray');
|
|
|
|
if (element.getAttribute('stroke-opacity') == '1')
|
|
|
|
element.removeAttribute('stroke-opacity');
|
|
|
|
if (element.getAttribute('stroke-width') == '1')
|
|
|
|
element.removeAttribute('stroke-width');
|
|
|
|
if (element.getAttribute('rx') == '0')
|
|
|
|
element.removeAttribute('rx')
|
|
|
|
if (element.getAttribute('ry') == '0')
|
|
|
|
element.removeAttribute('ry')
|
2009-10-08 17:02:23 +00:00
|
|
|
if (element.getAttribute('display') == 'inline')
|
|
|
|
element.removeAttribute('display');
|
2009-08-07 05:44:55 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
2009-08-17 06:56:55 +00:00
|
|
|
};
|
2009-08-07 05:44:55 +00:00
|
|
|
|
|
|
|
this.updateElementFromJson = function(data) {
|
|
|
|
var shape = svgdoc.getElementById(data.attr.id);
|
|
|
|
// if shape is a path but we need to create a rect/ellipse, then remove the path
|
|
|
|
if (shape && data.element != shape.tagName) {
|
2009-09-21 02:05:58 +00:00
|
|
|
current_layer.removeChild(shape);
|
2009-08-07 05:44:55 +00:00
|
|
|
shape = null;
|
|
|
|
}
|
|
|
|
if (!shape) {
|
|
|
|
shape = svgdoc.createElementNS(svgns, data.element);
|
2009-09-21 02:05:58 +00:00
|
|
|
if (current_layer) {
|
|
|
|
current_layer.appendChild(shape);
|
|
|
|
}
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(shape, data.attr, 100);
|
2009-08-07 05:44:55 +00:00
|
|
|
cleanupElement(shape);
|
|
|
|
return shape;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-13 12:55:15 +00:00
|
|
|
// TODO: declare the variables and set them as null, then move this setup stuff to
|
|
|
|
// an initialization function - probably just use clear()
|
2009-06-09 22:13:13 +00:00
|
|
|
var canvas = this;
|
2009-06-07 00:17:21 +00:00
|
|
|
var container = c;
|
2009-06-01 21:24:30 +00:00
|
|
|
var svgns = "http://www.w3.org/2000/svg";
|
2009-09-04 23:08:27 +00:00
|
|
|
var xlinkns = "http://www.w3.org/1999/xlink";
|
2009-10-13 05:41:32 +00:00
|
|
|
var xmlns = "http://www.w3.org/XML/1998/namespace";
|
2009-06-12 06:45:37 +00:00
|
|
|
var idprefix = "svg_";
|
2009-06-07 00:17:21 +00:00
|
|
|
var svgdoc = c.ownerDocument;
|
|
|
|
var svgroot = svgdoc.createElementNS(svgns, "svg");
|
2009-06-07 10:27:55 +00:00
|
|
|
svgroot.setAttribute("width", 640);
|
|
|
|
svgroot.setAttribute("height", 480);
|
2009-06-08 15:05:12 +00:00
|
|
|
svgroot.setAttribute("id", "svgroot");
|
2009-06-08 08:09:15 +00:00
|
|
|
svgroot.setAttribute("xmlns", svgns);
|
2009-09-04 23:08:27 +00:00
|
|
|
svgroot.setAttribute("xmlns:xlink", xlinkns);
|
2009-06-07 00:17:21 +00:00
|
|
|
container.appendChild(svgroot);
|
2009-10-21 15:12:32 +00:00
|
|
|
var svgcontent = svgdoc.createElementNS(svgns, "svg");
|
|
|
|
svgcontent.setAttribute('id', 'svgcontent');
|
|
|
|
svgcontent.setAttribute('viewBox', '0 0 640 480');
|
|
|
|
svgcontent.setAttribute("xmlns", svgns);
|
|
|
|
svgcontent.setAttribute("xmlns:xlink", xlinkns);
|
|
|
|
svgroot.appendChild(svgcontent);
|
2009-10-13 01:40:38 +00:00
|
|
|
// TODO: make this string optional and set by the client
|
|
|
|
// TODO: make sure this is always at the top of the SVG file right underneath the <svg> element
|
|
|
|
var comment = svgdoc.createComment(" Created with SVG-edit - http://svg-edit.googlecode.com/ ");
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent.appendChild(comment);
|
2009-09-23 14:28:03 +00:00
|
|
|
// TODO For Issue 208: this is a start on a thumbnail
|
|
|
|
// var svgthumb = svgdoc.createElementNS(svgns, "use");
|
|
|
|
// svgthumb.setAttribute('width', '100');
|
|
|
|
// svgthumb.setAttribute('height', '100');
|
2009-10-21 15:12:32 +00:00
|
|
|
// svgthumb.setAttributeNS(xlinkns, 'href', '#svgcontent');
|
2009-09-23 14:28:03 +00:00
|
|
|
// svgroot.appendChild(svgthumb);
|
2009-09-21 22:16:44 +00:00
|
|
|
// z-ordered array of tuples containing layer names and <g> elements
|
2009-09-23 03:05:27 +00:00
|
|
|
// the first layer is the one at the bottom of the rendering
|
2009-09-21 22:16:44 +00:00
|
|
|
var all_layers = [];
|
2009-09-21 02:05:58 +00:00
|
|
|
// pointer to the current layer <g>
|
|
|
|
var current_layer = null;
|
|
|
|
|
2009-06-02 16:40:33 +00:00
|
|
|
var d_attr = null;
|
|
|
|
var started = false;
|
2009-06-01 21:24:30 +00:00
|
|
|
var obj_num = 1;
|
2009-06-02 16:40:33 +00:00
|
|
|
var start_x = null;
|
|
|
|
var start_y = null;
|
2009-06-27 03:18:50 +00:00
|
|
|
var current_mode = "select";
|
2009-06-23 05:17:53 +00:00
|
|
|
var current_resize_mode = "none";
|
2009-08-31 14:54:59 +00:00
|
|
|
|
|
|
|
var all_properties = {
|
|
|
|
shape: {
|
|
|
|
fill: "#FF0000",
|
|
|
|
fill_paint: null,
|
|
|
|
fill_opacity: 1,
|
|
|
|
stroke: "#000000",
|
|
|
|
stroke_paint: null,
|
|
|
|
stroke_opacity: 1,
|
|
|
|
stroke_width: 5,
|
|
|
|
stroke_style: 'none',
|
|
|
|
opacity: 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
all_properties.text = $.extend(true, {}, all_properties.shape);
|
|
|
|
$.extend(all_properties.text, {
|
|
|
|
fill: "#000000",
|
|
|
|
stroke_width: 0,
|
2009-10-20 15:50:58 +00:00
|
|
|
font_size: 24,
|
2009-08-31 14:54:59 +00:00
|
|
|
font_family: 'serif'
|
|
|
|
});
|
|
|
|
|
|
|
|
var cur_shape = all_properties.shape;
|
|
|
|
var cur_text = all_properties.text;
|
2009-08-31 15:59:42 +00:00
|
|
|
var cur_properties = cur_shape;
|
2009-08-31 14:54:59 +00:00
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
var freehand_min_x = null;
|
|
|
|
var freehand_max_x = null;
|
|
|
|
var freehand_min_y = null;
|
|
|
|
var freehand_max_y = null;
|
2009-10-18 20:46:32 +00:00
|
|
|
var current_path = null;
|
|
|
|
var current_path_pts = [];
|
|
|
|
var current_path_pt = -1;
|
|
|
|
var current_path_pt_drag = -1;
|
|
|
|
var current_path_oldd = null;
|
2009-09-25 17:59:32 +00:00
|
|
|
var current_ctrl_pt_drag = -1;
|
2009-09-09 14:18:24 +00:00
|
|
|
var current_zoom = 1;
|
2009-06-29 22:22:13 +00:00
|
|
|
// this will hold all the currently selected elements
|
|
|
|
// default size of 1 until it needs to grow bigger
|
|
|
|
var selectedElements = new Array(1);
|
2009-06-30 06:24:41 +00:00
|
|
|
// this holds the selected's bbox
|
2009-07-01 19:28:08 +00:00
|
|
|
var selectedBBoxes = new Array(1);
|
2009-09-06 23:25:12 +00:00
|
|
|
var justSelected = null;
|
2009-06-30 06:24:41 +00:00
|
|
|
// this object manages selectors for us
|
2009-06-29 13:20:39 +00:00
|
|
|
var selectorManager = new SelectorManager();
|
2009-06-30 06:24:41 +00:00
|
|
|
var rubberBox = null;
|
2009-06-06 18:34:42 +00:00
|
|
|
var events = {};
|
2009-06-25 02:54:07 +00:00
|
|
|
var undoStackPointer = 0;
|
|
|
|
var undoStack = [];
|
2009-08-26 19:59:52 +00:00
|
|
|
var curBBoxes = [];
|
|
|
|
|
2009-09-17 14:47:54 +00:00
|
|
|
// This method rounds the incoming value to the nearest value based on the current_zoom
|
|
|
|
var round = function(val){
|
|
|
|
return parseInt(val*current_zoom)/current_zoom;
|
|
|
|
};
|
|
|
|
|
2009-07-02 04:50:24 +00:00
|
|
|
// This method sends back an array or a NodeList full of elements that
|
2009-09-24 03:13:25 +00:00
|
|
|
// intersect the multi-select rubber-band-box on the current_layer only.
|
2009-07-02 04:50:24 +00:00
|
|
|
//
|
|
|
|
// Since the only browser that supports the SVG DOM getIntersectionList is Opera,
|
|
|
|
// we need to provide an implementation here. We brute-force it for now.
|
|
|
|
//
|
|
|
|
// Reference:
|
2009-07-01 19:28:08 +00:00
|
|
|
// Firefox does not implement getIntersectionList(), see https://bugzilla.mozilla.org/show_bug.cgi?id=501421
|
|
|
|
// Webkit does not implement getIntersectionList(), see https://bugs.webkit.org/show_bug.cgi?id=11274
|
2009-07-01 15:34:15 +00:00
|
|
|
var getIntersectionList = function(rect) {
|
2009-07-02 04:50:24 +00:00
|
|
|
if (rubberBox == null) { return null; }
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-26 19:59:52 +00:00
|
|
|
if(!curBBoxes.length) {
|
|
|
|
// Cache all bboxes
|
2009-09-24 03:13:25 +00:00
|
|
|
curBBoxes = canvas.getVisibleElements(current_layer, true);
|
2009-07-01 15:34:15 +00:00
|
|
|
}
|
2009-08-26 19:59:52 +00:00
|
|
|
|
|
|
|
var resultList = null;
|
|
|
|
try {
|
2009-09-21 02:05:58 +00:00
|
|
|
resultList = current_layer.getIntersectionList(rect, null);
|
2009-08-26 19:59:52 +00:00
|
|
|
} catch(e) { }
|
|
|
|
|
|
|
|
if (resultList == null || typeof(resultList.item) != "function") {
|
|
|
|
resultList = [];
|
|
|
|
|
|
|
|
var rubberBBox = rubberBox.getBBox();
|
2009-09-10 19:20:26 +00:00
|
|
|
$.each(rubberBBox, function(key, val) {
|
|
|
|
rubberBBox[key] = val / current_zoom;
|
|
|
|
});
|
2009-08-26 19:59:52 +00:00
|
|
|
var i = curBBoxes.length;
|
|
|
|
while (i--) {
|
|
|
|
if (Utils.rectsIntersect(rubberBBox, curBBoxes[i].bbox)) {
|
|
|
|
resultList.push(curBBoxes[i].elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-02 04:50:24 +00:00
|
|
|
// addToSelection expects an array, but it's ok to pass a NodeList
|
|
|
|
// because using square-bracket notation is allowed:
|
|
|
|
// http://www.w3.org/TR/DOM-Level-2-Core/ecma-script-binding.html
|
2009-07-01 15:34:15 +00:00
|
|
|
return resultList;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 14:38:30 +00:00
|
|
|
// FIXME: we MUST compress consecutive text changes to the same element
|
2009-06-25 02:54:07 +00:00
|
|
|
// (right now each keystroke is saved as a separate command that includes the
|
|
|
|
// entire text contents of the text element)
|
|
|
|
// TODO: consider limiting the history that we store here (need to do some slicing)
|
2009-06-30 06:24:41 +00:00
|
|
|
var addCommandToHistory = function(cmd) {
|
2009-06-25 02:54:07 +00:00
|
|
|
// if our stack pointer is not at the end, then we have to remove
|
|
|
|
// all commands after the pointer and insert the new command
|
|
|
|
if (undoStackPointer < undoStack.length && undoStack.length > 0) {
|
|
|
|
undoStack = undoStack.splice(0, undoStackPointer);
|
|
|
|
}
|
2009-07-02 04:50:24 +00:00
|
|
|
undoStack.push(cmd);
|
2009-06-25 02:54:07 +00:00
|
|
|
undoStackPointer = undoStack.length;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-25 02:54:07 +00:00
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
// private functions
|
2009-06-05 12:33:02 +00:00
|
|
|
var getId = function() {
|
2009-06-25 08:22:30 +00:00
|
|
|
if (events["getid"]) return call("getid", obj_num);
|
|
|
|
return idprefix + obj_num;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-11 13:15:16 +00:00
|
|
|
var getNextId = function() {
|
|
|
|
// ensure the ID does not exist
|
|
|
|
var id = getId();
|
|
|
|
while (svgdoc.getElementById(id)) {
|
|
|
|
obj_num++;
|
|
|
|
id = getId();
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
return id;
|
2009-07-11 13:15:16 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-06 18:34:42 +00:00
|
|
|
var call = function(event, arg) {
|
|
|
|
if (events[event]) {
|
|
|
|
return events[event](this,arg);
|
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 18:34:42 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// this function sanitizes the input node and its children
|
|
|
|
// this function only keeps what is allowed from our whitelist defined above
|
|
|
|
var sanitizeSvg = function(node) {
|
|
|
|
// we only care about element nodes
|
2009-07-13 02:57:11 +00:00
|
|
|
// automatically return for all comment, etc nodes
|
|
|
|
// for text, we do a whitespace trim
|
|
|
|
if (node.nodeType == 3) {
|
|
|
|
node.nodeValue = node.nodeValue.replace(/^\s+|\s+$/g, "");
|
|
|
|
}
|
2009-07-10 05:04:06 +00:00
|
|
|
if (node.nodeType != 1) return;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
var doc = node.ownerDocument;
|
|
|
|
var parent = node.parentNode;
|
|
|
|
// can parent ever be null here? I think the root node's parent is the document...
|
|
|
|
if (!doc || !parent) return;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
var allowedAttrs = svgWhiteList[node.nodeName];
|
|
|
|
// if this element is allowed
|
|
|
|
if (allowedAttrs != undefined) {
|
|
|
|
var i = node.attributes.length;
|
|
|
|
while (i--) {
|
|
|
|
// if the attribute is not in our whitelist, then remove it
|
|
|
|
// could use jQuery's inArray(), but I don't know if that's any better
|
|
|
|
var attrName = node.attributes.item(i).nodeName;
|
|
|
|
if (allowedAttrs.indexOf(attrName) == -1) {
|
|
|
|
// TODO: do I need to call setAttribute(..., "") here for Fx2?
|
|
|
|
node.removeAttribute(attrName);
|
|
|
|
}
|
2009-10-23 15:21:27 +00:00
|
|
|
if(attrName == 'd') {
|
|
|
|
// Convert to absolute
|
|
|
|
node.setAttribute('d',convertPath(node));
|
|
|
|
}
|
2009-07-10 05:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// recurse to children
|
|
|
|
i = node.childNodes.length;
|
|
|
|
while (i--) { sanitizeSvg(node.childNodes.item(i)); }
|
|
|
|
}
|
|
|
|
// else, remove this element
|
|
|
|
else {
|
|
|
|
// remove all children from this node and insert them before this node
|
|
|
|
var children = [];
|
|
|
|
while (node.hasChildNodes()) {
|
|
|
|
children.push(parent.insertBefore(node.firstChild, node));
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// remove this node from the document altogether
|
|
|
|
parent.removeChild(node);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// call sanitizeSvg on each of those children
|
|
|
|
var i = children.length;
|
|
|
|
while (i--) { sanitizeSvg(children[i]); }
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
}
|
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
var removeUnusedGrads = function() {
|
2009-10-21 15:12:32 +00:00
|
|
|
var defs = svgcontent.getElementsByTagNameNS(svgns, "defs");
|
2009-08-25 12:40:58 +00:00
|
|
|
if(!defs || !defs.length) return;
|
2009-08-19 17:08:05 +00:00
|
|
|
|
2009-10-21 15:12:32 +00:00
|
|
|
var all_els = svgcontent.getElementsByTagNameNS(svgns, '*');
|
2009-08-19 17:08:05 +00:00
|
|
|
var grad_uses = [];
|
|
|
|
|
|
|
|
$.each(all_els, function(i, el) {
|
|
|
|
var fill = el.getAttribute('fill');
|
|
|
|
if(fill && fill.indexOf('url(#') == 0) {
|
|
|
|
//found gradient
|
|
|
|
grad_uses.push(fill);
|
|
|
|
}
|
|
|
|
|
|
|
|
var stroke = el.getAttribute('stroke');
|
|
|
|
if(stroke && stroke.indexOf('url(#') == 0) {
|
|
|
|
//found gradient
|
|
|
|
grad_uses.push(stroke);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2009-10-21 15:12:32 +00:00
|
|
|
var lgrads = svgcontent.getElementsByTagNameNS(svgns, "linearGradient");
|
2009-08-19 17:08:05 +00:00
|
|
|
var grad_ids = [];
|
|
|
|
var i = lgrads.length;
|
|
|
|
while (i--) {
|
|
|
|
var grad = lgrads[i];
|
|
|
|
var id = grad.getAttribute('id');
|
|
|
|
var url_id = 'url(#' + id + ')';
|
|
|
|
if($.inArray(url_id, grad_uses) == -1) {
|
|
|
|
// Not found, so remove
|
|
|
|
grad.parentNode.removeChild(grad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove defs if empty
|
|
|
|
var i = defs.length;
|
|
|
|
while (i--) {
|
|
|
|
var def = defs[i];
|
|
|
|
if(!def.getElementsByTagNameNS(svgns,'*').length) {
|
|
|
|
def.parentNode.removeChild(def);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 14:18:24 +00:00
|
|
|
|
|
|
|
var svgCanvasToString = function() {
|
|
|
|
removeUnusedGrads();
|
2009-10-18 20:46:32 +00:00
|
|
|
canvas.clearPath(true);
|
2009-10-29 19:43:34 +00:00
|
|
|
$.each(svgcontent.childNodes, function(i, node) {
|
|
|
|
if(i && node.nodeType == 8 && node.data.indexOf('Created with') != -1) {
|
|
|
|
svgcontent.insertBefore(node, svgcontent.firstChild);
|
|
|
|
}
|
|
|
|
});
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent.removeAttribute('id');
|
|
|
|
var output = svgToString(svgcontent, 0);
|
|
|
|
svgcontent.id = 'svgcontent';
|
2009-09-09 14:18:24 +00:00
|
|
|
return output;
|
|
|
|
}
|
2009-08-19 17:08:05 +00:00
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
var svgToString = function(elem, indent) {
|
2009-07-10 05:04:06 +00:00
|
|
|
var out = new Array();
|
2009-06-01 21:24:30 +00:00
|
|
|
if (elem) {
|
2009-10-08 17:02:23 +00:00
|
|
|
cleanupElement(elem);
|
2009-06-01 21:24:30 +00:00
|
|
|
var attrs = elem.attributes;
|
|
|
|
var attr;
|
|
|
|
var i;
|
|
|
|
var childs = elem.childNodes;
|
2009-07-10 05:04:06 +00:00
|
|
|
for (i=0; i<indent; i++) out.push(" ");
|
|
|
|
out.push("<"); out.push(elem.nodeName);
|
2009-06-01 21:24:30 +00:00
|
|
|
for (i=attrs.length-1; i>=0; i--) {
|
|
|
|
attr = attrs.item(i);
|
2009-10-23 15:21:27 +00:00
|
|
|
var attrVal = attr.nodeValue;
|
|
|
|
if (attrVal != "") {
|
|
|
|
if(attrVal.indexOf('pointer-events') == 0) continue;
|
2009-09-05 11:59:25 +00:00
|
|
|
out.push(" ");
|
2009-10-23 15:21:27 +00:00
|
|
|
if(attr.localName == 'd') attrVal = convertPath(elem, true);
|
|
|
|
if(!isNaN(attrVal)) {
|
|
|
|
attrVal = shortFloat(attrVal);
|
|
|
|
}
|
2009-09-05 18:13:20 +00:00
|
|
|
// map various namespaces to our fixed namespace prefixes
|
|
|
|
// TODO: put this into a map and do a look-up instead of if-else
|
2009-10-13 05:41:32 +00:00
|
|
|
if (attr.namespaceURI == xlinkns) {
|
2009-09-05 11:59:25 +00:00
|
|
|
out.push('xlink:');
|
|
|
|
}
|
2009-09-05 18:13:20 +00:00
|
|
|
else if(attr.namespaceURI == 'http://www.w3.org/2000/xmlns/' && attr.localName != 'xmlns') {
|
|
|
|
out.push('xmlns:');
|
|
|
|
}
|
2009-10-13 05:41:32 +00:00
|
|
|
else if(attr.namespaceURI == xmlns) {
|
|
|
|
out.push('xml:');
|
|
|
|
}
|
2009-09-05 18:13:20 +00:00
|
|
|
out.push(attr.localName); out.push("=\"");
|
2009-10-23 15:21:27 +00:00
|
|
|
out.push(attrVal); out.push("\"");
|
2009-06-16 02:52:36 +00:00
|
|
|
}
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
if (elem.hasChildNodes()) {
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push(">");
|
2009-06-01 21:24:30 +00:00
|
|
|
indent++;
|
2009-06-27 03:42:12 +00:00
|
|
|
var bOneLine = false;
|
2009-06-01 21:24:30 +00:00
|
|
|
for (i=0; i<childs.length; i++)
|
|
|
|
{
|
2009-06-30 17:50:08 +00:00
|
|
|
var child = childs.item(i);
|
|
|
|
if (child.id == "selectorParentGroup") continue;
|
2009-07-13 03:23:58 +00:00
|
|
|
switch(child.nodeType) {
|
|
|
|
case 1: // element node
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push("\n");
|
|
|
|
out.push(svgToString(childs.item(i), indent));
|
2009-07-13 03:23:58 +00:00
|
|
|
break;
|
|
|
|
case 3: // text node
|
2009-07-13 02:57:11 +00:00
|
|
|
var str = child.nodeValue.replace(/^\s+|\s+$/g, "");
|
|
|
|
if (str != "") {
|
|
|
|
bOneLine = true;
|
2009-10-05 08:16:41 +00:00
|
|
|
out.push(toXml(str) + "");
|
2009-07-13 00:17:15 +00:00
|
|
|
}
|
2009-07-13 03:23:58 +00:00
|
|
|
break;
|
|
|
|
case 8: // comment
|
|
|
|
out.push("\n");
|
|
|
|
out.push(new Array(indent+1).join(" "));
|
|
|
|
out.push("<!--");
|
|
|
|
out.push(child.data);
|
|
|
|
out.push("-->");
|
|
|
|
break;
|
|
|
|
} // switch on node type
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
indent--;
|
2009-06-29 14:38:30 +00:00
|
|
|
if (!bOneLine) {
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push("\n");
|
2009-07-30 22:45:59 +00:00
|
|
|
for (i=0; i<indent; i++) out.push(" ");
|
2009-06-27 03:42:12 +00:00
|
|
|
}
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push("</"); out.push(elem.nodeName); out.push(">");
|
2009-06-01 21:24:30 +00:00
|
|
|
} else {
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push("/>");
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-10 05:04:06 +00:00
|
|
|
return out.join('');
|
2009-06-30 06:24:41 +00:00
|
|
|
}; // end svgToString()
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
var recalculateAllSelectedDimensions = function() {
|
2009-07-01 21:28:22 +00:00
|
|
|
var text = (current_resize_mode == "none" ? "position" : "size");
|
|
|
|
var batchCmd = new BatchCommand(text);
|
2009-07-01 19:54:51 +00:00
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
var i = selectedElements.length;
|
|
|
|
while(i--) {
|
2009-09-05 23:09:46 +00:00
|
|
|
var cmd = recalculateDimensions(selectedElements[i],selectedBBoxes[i]);
|
2009-07-08 00:03:08 +00:00
|
|
|
if (cmd) {
|
|
|
|
batchCmd.addSubCommand(cmd);
|
2009-07-01 19:54:51 +00:00
|
|
|
}
|
2009-07-08 00:03:08 +00:00
|
|
|
}
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
if (!batchCmd.isEmpty()) {
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
call("changed", selectedElements);
|
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-10-19 20:13:52 +00:00
|
|
|
/*
|
|
|
|
Changes need to handle resizing of groups and any number of transformations:
|
|
|
|
|
|
|
|
- while being manipulated with a mouse, shapes can have any number of transforms applied
|
|
|
|
- when moused up, we will traverse the transform list of the element and see if we can
|
|
|
|
reduce the transform list to as small as possible (preferably a zero-length transform list)
|
|
|
|
- since translations completely preserve all shapes and orientations, we can ALWAYS remove
|
|
|
|
translates (note that this doesn't apply to translates that are part of a scale operation)
|
|
|
|
- since rotations do not preserve any orientations, it is not possible to preserve rotations
|
|
|
|
unless the rotation degrees is a multiple of 90, thus we will always keep rotational
|
|
|
|
transforms around
|
|
|
|
- non-uniform scales (sx!=sy) preserve the shape and orientation ONLY if the shape has
|
|
|
|
not been rotated, thus we will reduce non-uniform scales when we haven't rotated the shape
|
|
|
|
but otherwise we are forced to keep them around
|
|
|
|
- uniform scales (sx==sy) preserve the shape and orientation, so we can ALWAYS remove
|
|
|
|
uniform scales, even when the shape has been rotated
|
|
|
|
- does the removal of translate or scale transforms affect the rotational center of
|
|
|
|
previously visited transforms?
|
|
|
|
- ungrouping has the effect of transferring the transform list of the group down to its
|
|
|
|
individual children, thus we will then attempt to reduce the transform list further on
|
|
|
|
each child upon ungrouping
|
|
|
|
|
|
|
|
- we traverse the transform list backwards, starting at the nth transform and modifying
|
|
|
|
frame of reference (the ctm) as we move to the next transform
|
|
|
|
- each time we reduce the transform list, we remap the coordinates of the shape
|
|
|
|
- at the end, we have a new transform list and a new set of coordinates for the shape,
|
|
|
|
we can then update the DOM with both of these elements
|
|
|
|
|
|
|
|
|
|
|
|
The Simplest Case:
|
|
|
|
Dragging an axis-aligned rectangle 150 pixels east.
|
|
|
|
|
|
|
|
* Transform list has just one transform: translate(150,0)
|
|
|
|
* we translate the top-left coordinate of the rect (by adding 150 to the 'x' value)
|
|
|
|
and reduce the transform list to empty
|
|
|
|
|
|
|
|
A Simple Case:
|
|
|
|
Dragging an ellipse which has been rotated 30 degrees by 100 pixels south
|
|
|
|
|
|
|
|
* Transform list has two transforms: translate(0,100) rotate(30,cx,cy)
|
|
|
|
* we start at the rotation, since we cannot reduce rotations, we simply update the
|
|
|
|
current transform matrix so that it includes the translate(cx,cy) rotate(30) translate(-cx,-cy)
|
|
|
|
* next, we get to translate(0,100), standalone translates can always be reduced so first
|
|
|
|
we rotate by 30 degrees and it becomes translate(50,86.6), then we move the ellipse's
|
|
|
|
center point by 50,86.6 and then adjust the rotate so that the transform list results
|
|
|
|
as rotate(30,cx',cy')
|
|
|
|
*/
|
|
|
|
|
2009-08-15 04:37:08 +00:00
|
|
|
// this is how we map paths to our preferred relative segment types
|
2009-10-09 20:31:24 +00:00
|
|
|
var pathMap = [ 0, 'z', 'M', 'M', 'L', 'L', 'C', 'C', 'Q', 'Q', 'A', 'A',
|
|
|
|
'L', 'L', 'L', 'L', // TODO: be less lazy below and map them to h and v
|
|
|
|
'S', 'S', 'T', 'T' ];
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-16 14:56:22 +00:00
|
|
|
// this function returns the command which resulted from the selected change
|
2009-09-05 23:09:46 +00:00
|
|
|
var recalculateDimensions = function(selected,selectedBBox) {
|
|
|
|
if (selected == null || selectedBBox == null) return null;
|
2009-08-16 15:07:00 +00:00
|
|
|
var box = canvas.getBBox(selected);
|
2009-07-08 00:03:08 +00:00
|
|
|
|
|
|
|
// if we have not moved/resized, then immediately leave
|
2009-09-06 03:51:16 +00:00
|
|
|
var xform = selected.getAttribute("transform");
|
|
|
|
if ( (!xform || xform == "") && box.x == selectedBBox.x && box.y == selectedBBox.y &&
|
|
|
|
box.width == selectedBBox.width && box.height == selectedBBox.height)
|
|
|
|
{
|
2009-07-08 00:03:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
2009-10-21 14:25:55 +00:00
|
|
|
|
|
|
|
var bScaleMatrix = false;
|
|
|
|
var tlist = selected.transform.baseVal;
|
|
|
|
var t = tlist.numberOfItems;
|
|
|
|
while (t--) {
|
|
|
|
var xform = tlist.getItem(t);
|
|
|
|
if (xform.type == 3) {
|
|
|
|
bScaleMatrix = xform.matrix;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flipping points should only occur for elements without regular x,y vals
|
|
|
|
var multiPoints = (selected.getAttribute('x') === null);
|
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
// after this point, we have some change to this element
|
2009-10-21 14:25:55 +00:00
|
|
|
|
2009-08-26 05:08:34 +00:00
|
|
|
var remap = function(x,y) {
|
2009-09-21 20:23:42 +00:00
|
|
|
// Prevent division by 0
|
|
|
|
if(!box.height) box.height = 1;
|
|
|
|
if(!box.width) box.width = 1;
|
2009-10-21 14:25:55 +00:00
|
|
|
|
|
|
|
var new_x = (((x-box.x)/box.width)*selectedBBox.width + selectedBBox.x);
|
|
|
|
var new_y = (((y-box.y)/box.height)*selectedBBox.height + selectedBBox.y);
|
|
|
|
|
|
|
|
if(multiPoints && bScaleMatrix) {
|
|
|
|
if(bScaleMatrix.a < 0) {
|
|
|
|
new_x = selectedBBox.x + selectedBBox.width - (new_x - selectedBBox.x);
|
|
|
|
}
|
|
|
|
if(bScaleMatrix.d < 0) {
|
|
|
|
new_y = selectedBBox.y + selectedBBox.height - (new_y - selectedBBox.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {x:new_x, y:new_y};
|
2009-08-26 05:08:34 +00:00
|
|
|
};
|
2009-10-20 13:49:53 +00:00
|
|
|
|
2009-09-21 00:03:47 +00:00
|
|
|
var scalew = function(w) {return (w*selectedBBox.width/box.width);}
|
|
|
|
var scaleh = function(h) {return (h*selectedBBox.height/box.height);}
|
2009-07-08 00:03:08 +00:00
|
|
|
|
2009-09-06 03:51:16 +00:00
|
|
|
var batchCmd = new BatchCommand("Transform");
|
|
|
|
|
2009-08-16 14:56:22 +00:00
|
|
|
// if there was a rotation transform, re-set it, otherwise empty out the transform attribute
|
|
|
|
var angle = canvas.getRotationAngle(selected);
|
2009-08-16 19:18:22 +00:00
|
|
|
if (angle) {
|
2009-08-26 05:08:34 +00:00
|
|
|
// this is our old center upon which we have rotated the shape
|
2009-09-17 14:47:54 +00:00
|
|
|
var tr_x = round(box.x + box.width/2),
|
|
|
|
tr_y = round(box.y + box.height/2);
|
2009-08-26 05:08:34 +00:00
|
|
|
var cx = null, cy = null;
|
2009-08-27 04:01:19 +00:00
|
|
|
|
2009-08-26 05:08:34 +00:00
|
|
|
// if this was a resize, find the new cx,cy
|
2009-10-21 14:25:55 +00:00
|
|
|
if (bScaleMatrix) {
|
2009-08-26 05:08:34 +00:00
|
|
|
var alpha = angle * Math.PI / 180.0;
|
|
|
|
|
|
|
|
// rotate new opposite corners of bbox by angle at old center
|
|
|
|
var dx = selectedBBox.x - tr_x,
|
|
|
|
dy = selectedBBox.y - tr_y,
|
|
|
|
r = Math.sqrt(dx*dx + dy*dy),
|
|
|
|
theta = Math.atan2(dy,dx) + alpha;
|
|
|
|
var left = r * Math.cos(theta) + tr_x,
|
|
|
|
top = r * Math.sin(theta) + tr_y;
|
|
|
|
|
|
|
|
dx += selectedBBox.width;
|
|
|
|
dy += selectedBBox.height;
|
|
|
|
r = Math.sqrt(dx*dx + dy*dy);
|
|
|
|
theta = Math.atan2(dy,dx) + alpha;
|
|
|
|
var right = r * Math.cos(theta) + tr_x,
|
|
|
|
bottom = r * Math.sin(theta) + tr_y;
|
|
|
|
|
|
|
|
// now find mid-point of line between top-left and bottom-right to find new center
|
2009-09-17 14:47:54 +00:00
|
|
|
cx = round(left + (right-left)/2);
|
|
|
|
cy = round(top + (bottom-top)/2);
|
2009-08-26 05:08:34 +00:00
|
|
|
|
|
|
|
// now that we know the center and the axis-aligned width/height, calculate the x,y
|
2009-09-17 14:47:54 +00:00
|
|
|
selectedBBox.x = round(cx - selectedBBox.width/2),
|
|
|
|
selectedBBox.y = round(cy - selectedBBox.height/2);
|
2009-08-25 12:40:58 +00:00
|
|
|
}
|
2009-08-26 05:08:34 +00:00
|
|
|
// if it was not a resize, then it was a translation only
|
|
|
|
else {
|
|
|
|
var tx = selectedBBox.x - box.x,
|
|
|
|
ty = selectedBBox.y - box.y;
|
|
|
|
cx = tr_x + tx;
|
|
|
|
cy = tr_y + ty;
|
2009-08-25 12:40:58 +00:00
|
|
|
}
|
|
|
|
|
2009-08-18 16:59:01 +00:00
|
|
|
var rotate = ["rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
|
|
|
selected.setAttribute("transform", rotate);
|
2009-09-06 03:51:16 +00:00
|
|
|
// if we were rotated, store just the old rotation (not other transforms) on the
|
|
|
|
// undo stack
|
|
|
|
var changes = {};
|
|
|
|
changes["transform"] = ["rotate(", angle, " ", tr_x, ",", tr_y, ")"].join('');
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(selected, changes));
|
2009-10-09 15:29:40 +00:00
|
|
|
setPointContainerTransform(rotate);
|
2009-08-16 14:56:22 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-08-28 23:12:46 +00:00
|
|
|
// This fixes Firefox 2- behavior - which does not reset values when the attribute has
|
|
|
|
// been removed, see https://bugzilla.mozilla.org/show_bug.cgi?id=320622
|
2009-08-16 14:56:22 +00:00
|
|
|
selected.setAttribute("transform", "");
|
|
|
|
selected.removeAttribute("transform");
|
2009-10-09 15:29:40 +00:00
|
|
|
setPointContainerTransform("");
|
2009-08-16 14:56:22 +00:00
|
|
|
}
|
2009-09-06 01:59:26 +00:00
|
|
|
|
|
|
|
// if it's a group, transfer the transform attribute to each child element
|
|
|
|
// and recursively call recalculateDimensions()
|
|
|
|
if (selected.tagName == "g") {
|
|
|
|
var children = selected.childNodes;
|
|
|
|
var i = children.length;
|
|
|
|
while (i--) {
|
|
|
|
var child = children.item(i);
|
|
|
|
if (child.nodeType == 1) {
|
2009-09-23 15:29:55 +00:00
|
|
|
try {
|
|
|
|
var childBox = child.getBBox();
|
2009-09-09 15:23:44 +00:00
|
|
|
// TODO: to fix the rotation problem, we must account for the
|
|
|
|
// child's rotation in the bbox adjustment
|
|
|
|
|
|
|
|
// If the child is rotated at all, we should figure out the rotated
|
|
|
|
// bbox before the group's transform, remap all four corners of the bbox
|
|
|
|
// via the group's transform, then determine the new angle and the new center
|
|
|
|
/*
|
|
|
|
var childAngle = canvas.getRotationAngle(child) * Math.PI / 180.0;
|
|
|
|
var left = childBox.x - gcx,
|
|
|
|
top = childBox.y - gcy,
|
|
|
|
right = childBox.x + childBox.width - gcx,
|
|
|
|
bottom = childBox.y + childBox.height - gcy;
|
|
|
|
|
|
|
|
var ptTopLeft = remap(left,top),
|
|
|
|
ptTopRight = remap(right,top),
|
|
|
|
ptBottomLeft = remap(left,bottom),
|
|
|
|
ptBottomRight = remap(right,bottom);
|
|
|
|
*/
|
2009-09-06 01:59:26 +00:00
|
|
|
var pt = remap(childBox.x,childBox.y),
|
|
|
|
w = scalew(childBox.width),
|
|
|
|
h = scaleh(childBox.height);
|
|
|
|
childBox.x = pt.x; childBox.y = pt.y;
|
|
|
|
childBox.width = w; childBox.height = h;
|
|
|
|
batchCmd.addSubCommand(recalculateDimensions(child, childBox));
|
2009-09-23 15:29:55 +00:00
|
|
|
} catch(e) {}
|
2009-09-06 01:59:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return batchCmd;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-06 03:51:16 +00:00
|
|
|
var changes = {};
|
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
switch (selected.tagName)
|
|
|
|
{
|
2009-09-06 01:59:26 +00:00
|
|
|
case "g":
|
|
|
|
// do work here :P
|
|
|
|
break;
|
2009-08-20 05:33:34 +00:00
|
|
|
// NOTE: at the moment, there's no way to create an actual polygon element except by
|
|
|
|
// editing source or importing from somewhere else but we'll cover it here anyway
|
|
|
|
// polygon is handled just like polyline
|
2009-08-15 04:37:08 +00:00
|
|
|
case "polygon":
|
2009-08-07 17:19:32 +00:00
|
|
|
case "polyline":
|
2009-08-20 05:33:34 +00:00
|
|
|
// extract the points from the polygon/polyline, adjust it and write back the new points
|
|
|
|
// but first, save the old points
|
2009-08-07 17:19:32 +00:00
|
|
|
changes["points"] = selected.getAttribute("points");
|
|
|
|
var list = selected.points;
|
|
|
|
var len = list.numberOfItems;
|
|
|
|
var newpoints = "";
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var pt = list.getItem(i);
|
2009-08-26 05:08:34 +00:00
|
|
|
pt = remap(pt.x,pt.y);
|
|
|
|
newpoints += pt.x + "," + pt.y + " ";
|
2009-08-07 17:19:32 +00:00
|
|
|
}
|
|
|
|
selected.setAttributeNS(null, "points", newpoints);
|
|
|
|
break;
|
2009-07-08 00:03:08 +00:00
|
|
|
case "path":
|
|
|
|
// extract the x,y from the path, adjust it and write back the new path
|
|
|
|
// but first, save the old path
|
|
|
|
changes["d"] = selected.getAttribute("d");
|
|
|
|
var M = selected.pathSegList.getItem(0);
|
|
|
|
var curx = M.x, cury = M.y;
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt = remap(curx,cury);
|
|
|
|
var newd = "M" + pt.x + "," + pt.y;
|
2009-07-08 00:03:08 +00:00
|
|
|
var segList = selected.pathSegList;
|
|
|
|
var len = segList.numberOfItems;
|
2009-08-15 04:40:33 +00:00
|
|
|
// for all path segments in the path, we first turn them into relative path segments,
|
|
|
|
// then we remap the coordinates from the resize
|
2009-07-08 00:03:08 +00:00
|
|
|
for (var i = 1; i < len; ++i) {
|
2009-08-15 04:37:08 +00:00
|
|
|
var seg = segList.getItem(i);
|
|
|
|
// if these properties are not in the segment, set them to zero
|
|
|
|
var x = seg.x || 0,
|
|
|
|
y = seg.y || 0,
|
|
|
|
x1 = seg.x1 || 0,
|
|
|
|
y1 = seg.y1 || 0,
|
|
|
|
x2 = seg.x2 || 0,
|
|
|
|
y2 = seg.y2 || 0;
|
|
|
|
|
|
|
|
var type = seg.pathSegType;
|
|
|
|
switch (type) {
|
|
|
|
case 1: // z,Z closepath (Z/z)
|
|
|
|
newd += "z";
|
|
|
|
continue;
|
2009-08-16 06:02:26 +00:00
|
|
|
// turn this into a relative segment then fall through
|
2009-08-15 04:37:08 +00:00
|
|
|
case 3: // relative move (m)
|
|
|
|
case 5: // relative line (l)
|
|
|
|
case 13: // relative horizontal line (h)
|
|
|
|
case 15: // relative vertical line (v)
|
|
|
|
case 19: // relative smooth quad (t)
|
2009-10-09 20:14:00 +00:00
|
|
|
x += curx;
|
|
|
|
y += cury;
|
|
|
|
case 2: // absolute move (M)
|
|
|
|
case 4: // absolute line (L)
|
|
|
|
case 12: // absolute horizontal line (H)
|
|
|
|
case 14: // absolute vertical line (V)
|
|
|
|
case 18: // absolute smooth quad (T)
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
2009-10-09 20:31:24 +00:00
|
|
|
var pt = remap(x,y);
|
|
|
|
newd += [" ", pathMap[type], pt.x, ",", pt.y].join('');
|
2009-08-15 04:37:08 +00:00
|
|
|
break;
|
|
|
|
case 7: // relative cubic (c)
|
2009-10-09 20:14:00 +00:00
|
|
|
x += curx; x1 += curx; x2 += curx;
|
|
|
|
y += cury; y1 += curx; y2 += cury;
|
|
|
|
case 6: // absolute cubic (C)
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
2009-10-09 20:31:24 +00:00
|
|
|
var pt = remap(x,y), pt1 = remap(x1,y1), pt2 = remap(x2,y2);
|
|
|
|
newd += [" ", pathMap[type], pt1.x, ",", pt1.y, " ", pt2.x, ",", pt2.y,
|
|
|
|
" ", pt.x, ",", pt.y].join('');
|
2009-08-15 04:37:08 +00:00
|
|
|
break;
|
|
|
|
case 9: // relative quad (q)
|
2009-10-09 20:14:00 +00:00
|
|
|
x += curx; x1 += curx;
|
|
|
|
y += cury; y1 += cury;
|
|
|
|
case 8: // absolute quad (Q)
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
2009-10-09 20:31:24 +00:00
|
|
|
var pt = remap(x,y), pt1 = remap(x1,y1);
|
|
|
|
newd += [" ", pathMap[type], pt1.x, ",", pt1.y, " ", pt.x, ",", pt.y].join('');
|
2009-08-15 04:37:08 +00:00
|
|
|
break;
|
|
|
|
case 11: // relative elliptical arc (a)
|
2009-10-09 20:14:00 +00:00
|
|
|
x += curx;
|
|
|
|
y += cury;
|
|
|
|
case 10: // absolute elliptical arc (A)
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
2009-10-09 20:31:24 +00:00
|
|
|
var pt = remap(x,y);
|
|
|
|
newd += [" ", pathMap[type], scalew(seg.r1), ",", scaleh(seg.r2), " ", seg.angle, " ",
|
2009-08-15 04:37:08 +00:00
|
|
|
(seg.largeArcFlag ? 1 : 0), " ", (seg.sweepFlag ? 1 : 0), " ",
|
2009-10-09 20:31:24 +00:00
|
|
|
pt.x, ",", pt.y ].join('')
|
2009-08-15 04:37:08 +00:00
|
|
|
break;
|
|
|
|
case 17: // relative smooth cubic (s)
|
2009-10-09 20:14:00 +00:00
|
|
|
x += curx; x2 += curx;
|
|
|
|
y += cury; y2 += cury;
|
2009-10-09 20:31:24 +00:00
|
|
|
var pt = remap(x,y), pt2 = remap(x2,y2);
|
2009-10-09 20:14:00 +00:00
|
|
|
case 16: // absolute smooth cubic (S)
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
2009-10-09 20:31:24 +00:00
|
|
|
newd += [" ", pathMap[type], pt2.x, ",", pt2.y, " ", pt.x, ",", pt.y].join('');
|
2009-08-15 04:37:08 +00:00
|
|
|
break;
|
|
|
|
} // switch on path segment type
|
|
|
|
} // for each segment
|
2009-07-08 00:03:08 +00:00
|
|
|
selected.setAttributeNS(null, "d", newd);
|
|
|
|
break;
|
|
|
|
case "line":
|
2009-08-16 06:02:26 +00:00
|
|
|
changes["x1"] = selected.getAttribute("x1");
|
|
|
|
changes["y1"] = selected.getAttribute("y1");
|
|
|
|
changes["x2"] = selected.getAttribute("x2");
|
|
|
|
changes["y2"] = selected.getAttribute("y2");
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt1 = remap(changes["x1"],changes["y1"]),
|
|
|
|
pt2 = remap(changes["x2"],changes["y2"]);
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(selected, {
|
2009-08-26 05:08:34 +00:00
|
|
|
'x1': pt1.x,
|
|
|
|
'y1': pt1.y,
|
|
|
|
'x2': pt2.x,
|
|
|
|
'y2': pt2.y,
|
2009-08-24 20:00:48 +00:00
|
|
|
}, 1000);
|
2009-07-08 00:03:08 +00:00
|
|
|
break;
|
|
|
|
case "circle":
|
2009-08-16 06:02:26 +00:00
|
|
|
changes["cx"] = selected.getAttribute("cx");
|
|
|
|
changes["cy"] = selected.getAttribute("cy");
|
|
|
|
changes["r"] = selected.getAttribute("r");
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt = remap(changes["cx"], changes["cy"]);
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(selected, {
|
2009-08-26 05:08:34 +00:00
|
|
|
'cx': pt.x,
|
|
|
|
'cy': pt.y,
|
2009-08-24 20:00:48 +00:00
|
|
|
|
|
|
|
// take the minimum of the new selected box's dimensions for the new circle radius
|
2009-09-17 14:47:54 +00:00
|
|
|
'r': round(Math.min(selectedBBox.width/2,selectedBBox.height/2))
|
2009-08-24 20:00:48 +00:00
|
|
|
}, 1000);
|
2009-07-08 00:03:08 +00:00
|
|
|
break;
|
|
|
|
case "ellipse":
|
2009-08-16 06:02:26 +00:00
|
|
|
changes["cx"] = selected.getAttribute("cx");
|
|
|
|
changes["cy"] = selected.getAttribute("cy");
|
|
|
|
changes["rx"] = selected.getAttribute("rx");
|
|
|
|
changes["ry"] = selected.getAttribute("ry");
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt = remap(changes["cx"], changes["cy"]);
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(selected, {
|
2009-08-26 05:08:34 +00:00
|
|
|
'cx': pt.x,
|
|
|
|
'cy': pt.y,
|
2009-08-24 20:00:48 +00:00
|
|
|
'rx': scalew(changes["rx"]),
|
|
|
|
'ry': scaleh(changes["ry"])
|
|
|
|
}, 1000);
|
2009-07-08 00:03:08 +00:00
|
|
|
break;
|
|
|
|
case "text":
|
|
|
|
changes["x"] = selected.getAttribute("x");
|
|
|
|
changes["y"] = selected.getAttribute("y");
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt = remap(changes["x"], changes["y"]);
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(selected, {
|
2009-08-26 05:08:34 +00:00
|
|
|
'x': pt.x,
|
|
|
|
'y': pt.y
|
2009-08-24 20:00:48 +00:00
|
|
|
}, 1000);
|
2009-07-08 00:03:08 +00:00
|
|
|
break;
|
2009-09-04 22:22:40 +00:00
|
|
|
|
2009-09-05 11:59:25 +00:00
|
|
|
case "image":
|
|
|
|
changes["x"] = selected.getAttribute("x");
|
2009-09-04 22:22:40 +00:00
|
|
|
changes["y"] = selected.getAttribute("y");
|
|
|
|
changes["width"] = selected.getAttribute("width");
|
|
|
|
changes["height"] = selected.getAttribute("height");
|
|
|
|
var pt = remap(changes["x"], changes["y"]);
|
|
|
|
assignAttributes(selected, {
|
|
|
|
'x': pt.x,
|
|
|
|
'y': pt.y,
|
|
|
|
'width': scalew(changes["width"]),
|
|
|
|
'height': scaleh(changes["height"])
|
|
|
|
}, 1000);
|
|
|
|
break;
|
2009-07-08 00:03:08 +00:00
|
|
|
case "rect":
|
2009-08-16 06:02:26 +00:00
|
|
|
changes["x"] = selected.getAttribute("x");
|
|
|
|
changes["y"] = selected.getAttribute("y");
|
|
|
|
changes["width"] = selected.getAttribute("width");
|
|
|
|
changes["height"] = selected.getAttribute("height");
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt = remap(changes["x"], changes["y"]);
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(selected, {
|
2009-08-26 05:08:34 +00:00
|
|
|
'x': pt.x,
|
|
|
|
'y': pt.y,
|
2009-08-24 20:00:48 +00:00
|
|
|
'width': scalew(changes["width"]),
|
|
|
|
'height': scaleh(changes["height"])
|
|
|
|
}, 1000);
|
2009-07-08 00:03:08 +00:00
|
|
|
break;
|
|
|
|
default: // rect
|
|
|
|
console.log("Unknown shape type: " + selected.tagName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (changes) {
|
2009-09-06 03:51:16 +00:00
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(selected, changes));
|
2009-06-23 07:48:15 +00:00
|
|
|
}
|
2009-09-06 03:51:16 +00:00
|
|
|
return batchCmd;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 05:17:53 +00:00
|
|
|
|
2009-06-02 12:52:44 +00:00
|
|
|
// public events
|
2009-07-01 15:34:15 +00:00
|
|
|
|
2009-10-13 02:48:27 +00:00
|
|
|
// Group: Selection
|
|
|
|
|
2009-10-13 02:35:36 +00:00
|
|
|
// Function: clearSelection
|
|
|
|
// Clears the selection. The 'selected' handler is then called.
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection = function() {
|
2009-09-22 18:29:15 +00:00
|
|
|
if (selectedElements[0] != null) {
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var elem = selectedElements[i];
|
|
|
|
if (elem == null) break;
|
|
|
|
selectorManager.releaseSelector(elem);
|
|
|
|
selectedElements[i] = null;
|
|
|
|
selectedBBoxes[i] = null;
|
|
|
|
}
|
2009-07-01 15:34:15 +00:00
|
|
|
}
|
2009-07-01 19:28:08 +00:00
|
|
|
call("selected", selectedElements);
|
2009-07-01 15:34:15 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-10 14:27:34 +00:00
|
|
|
// TODO: do we need to worry about selectedBBoxes here?
|
2009-10-13 02:35:36 +00:00
|
|
|
|
|
|
|
// Function: addToSelection
|
|
|
|
// Adds a list of elements to the selection. The 'selected' handler is then called.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// elemsToAdd - an array of DOM elements to add to the selection
|
|
|
|
// showGrips - a boolean flag indicating whether the resize grips should be shown
|
2009-08-21 13:39:23 +00:00
|
|
|
this.addToSelection = function(elemsToAdd, showGrips) {
|
2009-07-01 15:34:15 +00:00
|
|
|
if (elemsToAdd.length == 0) { return; }
|
|
|
|
// find the first null in our selectedElements array
|
|
|
|
var j = 0;
|
|
|
|
while (j < selectedElements.length) {
|
|
|
|
if (selectedElements[j] == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++j;
|
|
|
|
}
|
2009-08-28 19:20:54 +00:00
|
|
|
|
2009-07-01 15:34:15 +00:00
|
|
|
// now add each element consecutively
|
2009-07-03 02:49:42 +00:00
|
|
|
var i = elemsToAdd.length;
|
|
|
|
while (i--) {
|
2009-07-01 15:34:15 +00:00
|
|
|
var elem = elemsToAdd[i];
|
2009-07-01 19:28:08 +00:00
|
|
|
// we ignore any selectors
|
|
|
|
if (elem.id.substr(0,13) == "selectorGrip_") continue;
|
2009-07-01 15:34:15 +00:00
|
|
|
// if it's not already there, add it
|
|
|
|
if (selectedElements.indexOf(elem) == -1) {
|
2009-07-01 19:28:08 +00:00
|
|
|
selectedElements[j] = elem;
|
2009-08-16 15:07:00 +00:00
|
|
|
selectedBBoxes[j++] = this.getBBox(elem);
|
2009-09-06 23:25:12 +00:00
|
|
|
var sel = selectorManager.requestSelector(elem);
|
|
|
|
if (selectedElements.length > 1) {
|
|
|
|
sel.showGrips(false);
|
|
|
|
}
|
2009-07-01 19:28:08 +00:00
|
|
|
call("selected", selectedElements);
|
2009-07-01 15:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-06 23:25:12 +00:00
|
|
|
|
2009-08-21 13:39:23 +00:00
|
|
|
if(showGrips) {
|
2009-08-27 14:44:23 +00:00
|
|
|
selectorManager.requestSelector(selectedElements[0]).showGrips(true);
|
2009-08-21 13:39:23 +00:00
|
|
|
}
|
2009-09-06 23:25:12 +00:00
|
|
|
else if (selectedElements.length > 1) {
|
|
|
|
selectorManager.requestSelector(selectedElements[0]).showGrips(false);
|
|
|
|
}
|
2009-07-01 15:34:15 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-10 14:27:34 +00:00
|
|
|
// TODO: could use slice here to make this faster?
|
2009-10-13 02:35:36 +00:00
|
|
|
// TODO: should the 'selected' handler
|
|
|
|
|
|
|
|
// Function: removeFromSelection
|
|
|
|
// Removes elements from the selection.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// elemsToRemove - an array of elements to remove from selection
|
2009-07-01 15:34:15 +00:00
|
|
|
this.removeFromSelection = function(elemsToRemove) {
|
|
|
|
if (selectedElements[0] == null) { return; }
|
|
|
|
if (elemsToRemove.length == 0) { return; }
|
|
|
|
|
2009-08-17 06:56:55 +00:00
|
|
|
// find every element and remove it from our array copy
|
2009-07-01 15:34:15 +00:00
|
|
|
var newSelectedItems = new Array(selectedElements.length);
|
2009-09-10 14:27:34 +00:00
|
|
|
var newSelectedBBoxes = new Array(selectedBBoxes.length);
|
2009-07-01 15:34:15 +00:00
|
|
|
var j = 0;
|
2009-07-03 02:49:42 +00:00
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
2009-07-01 15:34:15 +00:00
|
|
|
var elem = selectedElements[i];
|
|
|
|
if (elem) {
|
|
|
|
// keep the item
|
|
|
|
if (elemsToRemove.indexOf(elem) == -1) {
|
2009-09-10 14:27:34 +00:00
|
|
|
newSelectedBBoxes[j] = selectedBBoxes[i];
|
2009-07-01 15:34:15 +00:00
|
|
|
newSelectedItems[j++] = elem;
|
|
|
|
}
|
|
|
|
else { // remove the item and its selector
|
|
|
|
selectorManager.releaseSelector(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// the copy becomes the master now
|
|
|
|
selectedElements = newSelectedItems;
|
2009-09-10 14:27:34 +00:00
|
|
|
selectedBBoxes = newSelectedBBoxes;
|
2009-07-01 15:34:15 +00:00
|
|
|
};
|
2009-10-05 19:09:39 +00:00
|
|
|
|
|
|
|
this.addNodeToSelection = function(point) {
|
|
|
|
// Currently only one node can be selected at a time, should allow more later
|
|
|
|
// Should point be the index or the grip element?
|
2009-10-13 14:52:51 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var is_closed = current_path.getAttribute('d').toLowerCase().indexOf('z') != -1;
|
2009-10-12 20:26:03 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
if(is_closed && point == current_path_pts.length/2 - 1) {
|
|
|
|
current_path_pt = 0;
|
2009-10-07 16:04:32 +00:00
|
|
|
} else {
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pt = point;
|
2009-10-07 16:04:32 +00:00
|
|
|
}
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
$('#pathpointgrip_container circle').attr('stroke','#00F');
|
|
|
|
var grip = $('#pathpointgrip_' + point).attr('stroke','#0FF');
|
2009-10-23 17:27:37 +00:00
|
|
|
$('#ctrlpointgrip_container circle').attr('fill', '#EEE');
|
|
|
|
$('#ctrlpointgrip_' + current_path_pt + 'c1, #ctrlpointgrip_' + current_path_pt + 'c2').attr('fill','#0FF');
|
|
|
|
|
2009-10-13 14:52:51 +00:00
|
|
|
updateSegLine();
|
|
|
|
updateSegLine(true);
|
|
|
|
|
2009-10-05 19:09:39 +00:00
|
|
|
call("selected", [grip[0]]);
|
|
|
|
}
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-10-22 03:18:54 +00:00
|
|
|
// Some global variables that we may need to refactor
|
|
|
|
var root_sctm = null;
|
2009-10-22 03:29:49 +00:00
|
|
|
var mouse_target = null;
|
2009-10-22 03:18:54 +00:00
|
|
|
|
|
|
|
// A (hopefully) quicker function to transform a point by a matrix
|
|
|
|
// (this function avoids any DOM calls and just does the math)
|
2009-10-22 13:20:41 +00:00
|
|
|
// Returns a x,y object representing the transformed point
|
2009-10-22 03:18:54 +00:00
|
|
|
var transformPoint = function(x, y, m) {
|
|
|
|
return { x: m.a * x + m.c * y + m.e, y: m.b * x + m.d * y + m.f};
|
|
|
|
};
|
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
// - when we are in a create mode, the element is added to the canvas
|
2009-07-01 19:28:08 +00:00
|
|
|
// but the action is not recorded until mousing up
|
2009-06-29 14:38:30 +00:00
|
|
|
// - when we are in select mode, select the element, remember the position
|
2009-06-25 02:54:07 +00:00
|
|
|
// and do nothing else
|
2009-06-07 00:17:21 +00:00
|
|
|
var mouseDown = function(evt)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-10-22 03:18:54 +00:00
|
|
|
root_sctm = svgroot.getScreenCTM().inverse();
|
|
|
|
var pt = transformPoint( evt.pageX, evt.pageY, root_sctm );
|
2009-10-22 03:07:15 +00:00
|
|
|
var mouse_x = pt.x;
|
|
|
|
var mouse_y = pt.y;
|
2009-09-05 23:09:46 +00:00
|
|
|
evt.preventDefault();
|
2009-09-04 22:22:40 +00:00
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
if($.inArray(current_mode, ['select', 'resize']) == -1) {
|
|
|
|
addGradient();
|
|
|
|
}
|
2009-09-09 14:18:24 +00:00
|
|
|
|
2009-09-11 13:32:56 +00:00
|
|
|
x = mouse_x / current_zoom;
|
|
|
|
y = mouse_y / current_zoom;
|
2009-09-09 14:18:24 +00:00
|
|
|
|
2009-08-24 17:40:41 +00:00
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-10-16 18:01:05 +00:00
|
|
|
|
2009-10-22 03:29:49 +00:00
|
|
|
// find mouse target
|
|
|
|
mouse_target = evt.target;
|
|
|
|
// go up until we hit a child of a layer
|
|
|
|
while (mouse_target.parentNode.parentNode.tagName == "g") {
|
|
|
|
mouse_target = mouse_target.parentNode;
|
|
|
|
}
|
|
|
|
// Webkit bubbles the mouse event all the way up to the div, so we
|
|
|
|
// set the mouse_target to the svgroot like the other browsers
|
|
|
|
if (mouse_target.nodeName.toLowerCase() == "div") {
|
2009-10-22 13:20:41 +00:00
|
|
|
mouse_target = svgroot;
|
2009-10-22 03:29:49 +00:00
|
|
|
}
|
|
|
|
// if it is a selector grip, then it must be a single element selected,
|
|
|
|
// set the mouse_target to that
|
2009-10-22 13:20:41 +00:00
|
|
|
if (mouse_target.parentNode == selectorManager.selectorParentGroup && selectedElements[0] != null) {
|
2009-10-22 03:29:49 +00:00
|
|
|
mouse_target = selectedElements[0];
|
|
|
|
}
|
|
|
|
|
2009-06-03 08:41:25 +00:00
|
|
|
switch (current_mode) {
|
|
|
|
case "select":
|
2009-06-08 02:17:07 +00:00
|
|
|
started = true;
|
2009-06-25 02:54:07 +00:00
|
|
|
current_resize_mode = "none";
|
2009-10-22 13:20:41 +00:00
|
|
|
if (mouse_target != svgroot) {
|
2009-07-01 19:54:51 +00:00
|
|
|
// if this element is not yet selected, clear selection and select it
|
2009-10-22 03:29:49 +00:00
|
|
|
if (selectedElements.indexOf(mouse_target) == -1) {
|
2009-09-06 23:25:12 +00:00
|
|
|
// only clear selection if shift is not pressed (otherwise, add
|
|
|
|
// element to selection)
|
|
|
|
if (!evt.shiftKey) {
|
|
|
|
canvas.clearSelection();
|
|
|
|
}
|
2009-10-22 03:29:49 +00:00
|
|
|
canvas.addToSelection([mouse_target]);
|
|
|
|
justSelected = mouse_target;
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path = null;
|
2009-07-01 19:54:51 +00:00
|
|
|
}
|
2009-10-17 20:29:56 +00:00
|
|
|
// else if it's a path, go into pathedit mode in mouseup
|
2009-06-30 06:24:41 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-07-01 19:28:08 +00:00
|
|
|
canvas.clearSelection();
|
2009-06-30 06:24:41 +00:00
|
|
|
current_mode = "multiselect";
|
2009-07-01 19:28:08 +00:00
|
|
|
if (rubberBox == null) {
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
|
|
|
}
|
2009-09-10 19:20:26 +00:00
|
|
|
start_x *= current_zoom;
|
|
|
|
start_y *= current_zoom;
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': start_x,
|
|
|
|
'y': start_y,
|
|
|
|
'width': 0,
|
|
|
|
'height': 0,
|
|
|
|
'display': 'inline'
|
|
|
|
}, 100);
|
2009-06-07 05:07:23 +00:00
|
|
|
}
|
2009-06-03 08:41:25 +00:00
|
|
|
break;
|
2009-09-16 15:45:50 +00:00
|
|
|
case "zoom":
|
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-09-16 17:18:11 +00:00
|
|
|
if (rubberBox == null) {
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
|
|
|
}
|
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': start_x * current_zoom,
|
|
|
|
'y': start_y * current_zoom,
|
|
|
|
'width': 0,
|
|
|
|
'height': 0,
|
|
|
|
'display': 'inline'
|
|
|
|
}, 100);
|
2009-09-16 15:45:50 +00:00
|
|
|
break;
|
2009-06-23 05:17:53 +00:00
|
|
|
case "resize":
|
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
|
|
|
break;
|
2009-06-03 08:41:25 +00:00
|
|
|
case "fhellipse":
|
|
|
|
case "fhrect":
|
2009-10-18 20:04:26 +00:00
|
|
|
case "fhpath":
|
2009-06-03 08:41:25 +00:00
|
|
|
started = true;
|
2009-06-08 02:17:07 +00:00
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-08-07 17:19:32 +00:00
|
|
|
d_attr = x + "," + y + " ";
|
2009-09-01 19:52:34 +00:00
|
|
|
var stroke_w = cur_shape.stroke_width == 0?1:cur_shape.stroke_width;
|
2009-06-03 08:41:25 +00:00
|
|
|
addSvgElementFromJson({
|
2009-08-07 17:19:32 +00:00
|
|
|
"element": "polyline",
|
2009-06-03 08:41:25 +00:00
|
|
|
"attr": {
|
2009-08-07 17:19:32 +00:00
|
|
|
"points": d_attr,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-06-03 08:41:25 +00:00
|
|
|
"fill": "none",
|
2009-08-31 14:54:59 +00:00
|
|
|
"stroke": cur_shape.stroke,
|
2009-09-01 19:52:34 +00:00
|
|
|
"stroke-width": stroke_w,
|
2009-08-31 14:54:59 +00:00
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
2009-08-16 06:02:26 +00:00
|
|
|
"stroke-linecap": "round",
|
|
|
|
"stroke-linejoin": "round",
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
freehand_min_x = x;
|
|
|
|
freehand_max_x = x;
|
|
|
|
freehand_min_y = y;
|
|
|
|
freehand_max_y = y;
|
|
|
|
break;
|
2009-09-04 22:22:40 +00:00
|
|
|
case "image":
|
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
|
|
|
var newImage = addSvgElementFromJson({
|
|
|
|
"element": "image",
|
|
|
|
"attr": {
|
|
|
|
"x": x,
|
|
|
|
"y": y,
|
|
|
|
"width": 0,
|
|
|
|
"height": 0,
|
|
|
|
"id": getNextId(),
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-09-04 22:22:40 +00:00
|
|
|
}
|
|
|
|
});
|
2009-09-05 11:59:25 +00:00
|
|
|
newImage.setAttributeNS(xlinkns, "href", "images/logo.png");
|
2009-09-04 22:22:40 +00:00
|
|
|
break;
|
2009-06-03 08:41:25 +00:00
|
|
|
case "square":
|
2009-06-25 02:54:07 +00:00
|
|
|
// FIXME: once we create the rect, we lose information that this was a square
|
2009-08-21 12:21:43 +00:00
|
|
|
// (for resizing purposes this could be important)
|
2009-06-03 08:41:25 +00:00
|
|
|
case "rect":
|
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"x": x,
|
|
|
|
"y": y,
|
|
|
|
"width": 0,
|
|
|
|
"height": 0,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "line":
|
|
|
|
started = true;
|
2009-09-01 19:52:34 +00:00
|
|
|
var stroke_w = cur_shape.stroke_width == 0?1:cur_shape.stroke_width;
|
2009-06-03 08:41:25 +00:00
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "line",
|
|
|
|
"attr": {
|
|
|
|
"x1": x,
|
|
|
|
"y1": y,
|
|
|
|
"x2": x,
|
|
|
|
"y2": y,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"stroke": cur_shape.stroke,
|
2009-09-01 19:52:34 +00:00
|
|
|
"stroke-width": stroke_w,
|
2009-08-31 14:54:59 +00:00
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
2009-06-17 17:36:00 +00:00
|
|
|
"fill": "none",
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "circle":
|
2009-06-03 15:00:48 +00:00
|
|
|
started = true;
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "circle",
|
|
|
|
"attr": {
|
|
|
|
"cx": x,
|
|
|
|
"cy": y,
|
|
|
|
"r": 0,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-06-03 15:00:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2009-06-03 08:41:25 +00:00
|
|
|
case "ellipse":
|
|
|
|
started = true;
|
2009-10-16 20:08:36 +00:00
|
|
|
addSvgElementFromJson({
|
2009-06-03 08:41:25 +00:00
|
|
|
"element": "ellipse",
|
|
|
|
"attr": {
|
|
|
|
"cx": x,
|
|
|
|
"cy": y,
|
|
|
|
"rx": 0,
|
|
|
|
"ry": 0,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
2009-06-03 15:00:48 +00:00
|
|
|
break;
|
2009-06-10 03:12:19 +00:00
|
|
|
case "text":
|
|
|
|
started = true;
|
|
|
|
var newText = addSvgElementFromJson({
|
|
|
|
"element": "text",
|
|
|
|
"attr": {
|
|
|
|
"x": x,
|
|
|
|
"y": y,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_text.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_text.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
2009-06-11 15:18:46 +00:00
|
|
|
// fix for bug where text elements were always 50% opacity
|
2009-08-31 14:54:59 +00:00
|
|
|
"opacity": cur_shape.opacity,
|
|
|
|
"font-size": cur_text.font_size,
|
|
|
|
"font-family": cur_text.font_family,
|
2009-09-21 22:16:44 +00:00
|
|
|
"text-anchor": "middle",
|
2009-10-13 00:51:15 +00:00
|
|
|
"style": "pointer-events:inherit",
|
|
|
|
"xml:space": "preserve"
|
2009-06-10 03:12:19 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
newText.textContent = "text";
|
|
|
|
break;
|
2009-10-18 20:46:32 +00:00
|
|
|
case "path":
|
2009-10-12 14:29:21 +00:00
|
|
|
setPointContainerTransform("");
|
2009-08-07 05:44:55 +00:00
|
|
|
started = true;
|
|
|
|
break;
|
2009-10-17 20:29:56 +00:00
|
|
|
case "pathedit":
|
2009-08-08 14:03:14 +00:00
|
|
|
started = true;
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_oldd = current_path.getAttribute("d");
|
2009-08-08 22:17:59 +00:00
|
|
|
var id = evt.target.id;
|
2009-10-18 20:46:32 +00:00
|
|
|
if (id.substr(0,14) == "pathpointgrip_") {
|
2009-10-05 13:50:44 +00:00
|
|
|
// Select this point
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pt_drag = parseInt(id.substr(14));
|
|
|
|
canvas.addNodeToSelection(current_path_pt_drag);
|
2009-10-13 14:52:51 +00:00
|
|
|
updateSegLine();
|
2009-09-25 17:59:32 +00:00
|
|
|
} else if(id.indexOf("ctrlpointgrip_") == 0) {
|
|
|
|
current_ctrl_pt_drag = id.split('_')[1];
|
2009-10-23 17:27:37 +00:00
|
|
|
var node_num = current_ctrl_pt_drag.split('c')[0]-0;
|
|
|
|
canvas.addNodeToSelection(node_num);
|
2009-08-08 22:17:59 +00:00
|
|
|
}
|
2009-08-24 17:40:41 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
if(current_path_pt_drag == -1 && current_ctrl_pt_drag == -1) {
|
2009-10-17 20:29:56 +00:00
|
|
|
// if we haven't moused down on a shape, then go into multiselect mode
|
|
|
|
// otherwise, select it
|
2009-08-24 17:40:41 +00:00
|
|
|
canvas.clearSelection();
|
2009-10-22 03:29:49 +00:00
|
|
|
if (mouse_target.id != "svgroot") {
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path = null;
|
2009-10-22 03:29:49 +00:00
|
|
|
canvas.addToSelection([mouse_target], true);
|
2009-10-17 20:29:56 +00:00
|
|
|
canvas.setMode("select");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
canvas.setMode("multiselect");
|
|
|
|
if (rubberBox == null) {
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
|
|
|
}
|
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': start_x,
|
|
|
|
'y': start_y,
|
|
|
|
'width': 0,
|
|
|
|
'height': 0,
|
|
|
|
'display': 'inline'
|
|
|
|
}, 100);
|
2009-08-24 17:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-26 19:45:28 +00:00
|
|
|
break;
|
|
|
|
case "rotate":
|
|
|
|
started = true;
|
2009-09-04 05:10:48 +00:00
|
|
|
// we are starting an undoable change (a drag-rotation)
|
|
|
|
canvas.beginUndoableChange("transform", selectedElements);
|
2009-08-08 14:03:14 +00:00
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
default:
|
|
|
|
console.log("Unknown mode in mousedown: " + current_mode);
|
|
|
|
break;
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-08-17 03:11:30 +00:00
|
|
|
// in this function we do not record any state changes yet (but we do update
|
2009-06-25 02:54:07 +00:00
|
|
|
// any elements that are still being created, moved or resized on the canvas)
|
2009-07-10 05:04:06 +00:00
|
|
|
// TODO: svgcanvas should just retain a reference to the image being dragged instead
|
|
|
|
// of the getId() and getElementById() funkiness - this will help us customize the ids
|
2009-10-18 20:46:32 +00:00
|
|
|
// a little bit for squares and paths
|
2009-06-07 00:17:21 +00:00
|
|
|
var mouseMove = function(evt)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-06-02 16:40:33 +00:00
|
|
|
if (!started) return;
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-10-22 03:18:54 +00:00
|
|
|
var pt = transformPoint( evt.pageX, evt.pageY, root_sctm );
|
2009-10-22 03:07:15 +00:00
|
|
|
var mouse_x = pt.x;
|
|
|
|
var mouse_y = pt.y;
|
2009-06-05 12:33:02 +00:00
|
|
|
var shape = svgdoc.getElementById(getId());
|
2009-09-04 22:22:40 +00:00
|
|
|
|
2009-09-11 13:32:56 +00:00
|
|
|
x = mouse_x / current_zoom;
|
|
|
|
y = mouse_y / current_zoom;
|
2009-09-09 14:18:24 +00:00
|
|
|
|
2009-09-06 23:25:12 +00:00
|
|
|
evt.preventDefault();
|
2009-09-04 22:22:40 +00:00
|
|
|
|
2009-06-02 16:40:33 +00:00
|
|
|
switch (current_mode)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-06-08 02:17:07 +00:00
|
|
|
case "select":
|
2009-08-30 02:37:16 +00:00
|
|
|
// we temporarily use a translate on the element(s) being dragged
|
2009-07-01 19:28:08 +00:00
|
|
|
// this transform is removed upon mousing up and the element is
|
|
|
|
// relocated to the new location
|
2009-07-01 19:54:51 +00:00
|
|
|
if (selectedElements[0] != null) {
|
2009-06-08 02:17:07 +00:00
|
|
|
var dx = x - start_x;
|
|
|
|
var dy = y - start_y;
|
2009-08-08 14:03:14 +00:00
|
|
|
if (dx != 0 || dy != 0) {
|
|
|
|
var ts = ["translate(",dx,",",dy,")"].join('');
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var selected = selectedElements[i];
|
|
|
|
if (selected == null) break;
|
2009-08-16 15:07:00 +00:00
|
|
|
var box = canvas.getBBox(selected);
|
2009-08-21 15:53:36 +00:00
|
|
|
selectedBBoxes[i].x = box.x + dx;
|
|
|
|
selectedBBoxes[i].y = box.y + dy;
|
2009-08-16 14:56:22 +00:00
|
|
|
var angle = canvas.getRotationAngle(selected);
|
2009-08-16 19:18:22 +00:00
|
|
|
if (angle) {
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(box.x + box.width/2),
|
|
|
|
cy = round(box.y + box.height/2);
|
2009-08-30 02:37:16 +00:00
|
|
|
var xform = ts + [" rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
2009-08-21 15:53:36 +00:00
|
|
|
var r = Math.sqrt( dx*dx + dy*dy );
|
|
|
|
var theta = Math.atan2(dy,dx) - angle * Math.PI / 180.0;
|
2009-08-30 02:37:16 +00:00
|
|
|
selected.setAttribute("transform", xform);
|
|
|
|
box.x += r * Math.cos(theta); box.y += r * Math.sin(theta);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
selected.setAttribute("transform", ts);
|
|
|
|
box.x += dx; box.y += dy;
|
2009-08-16 14:56:22 +00:00
|
|
|
}
|
2009-08-21 15:53:36 +00:00
|
|
|
// update our internal bbox that we're tracking while dragging
|
2009-08-08 14:03:14 +00:00
|
|
|
selectorManager.requestSelector(selected).resize(box);
|
|
|
|
}
|
2009-07-01 19:54:51 +00:00
|
|
|
}
|
2009-06-08 02:17:07 +00:00
|
|
|
}
|
2009-06-10 03:12:19 +00:00
|
|
|
break;
|
2009-06-30 06:24:41 +00:00
|
|
|
case "multiselect":
|
2009-09-10 19:20:26 +00:00
|
|
|
x *= current_zoom;
|
|
|
|
y *= current_zoom;
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': Math.min(start_x,x),
|
2009-08-24 19:20:52 +00:00
|
|
|
'y': Math.min(start_y,y),
|
2009-08-24 19:19:27 +00:00
|
|
|
'width': Math.abs(x-start_x),
|
|
|
|
'height': Math.abs(y-start_y)
|
|
|
|
},100);
|
2009-07-02 04:50:24 +00:00
|
|
|
|
|
|
|
// this code will probably be faster than using getIntersectionList(), but
|
|
|
|
// not as accurate (only grabs an element if the mouse happens to pass over
|
|
|
|
// its bbox and elements would never be released from selection)
|
|
|
|
// var nodeName = evt.target.nodeName.toLowerCase();
|
|
|
|
// if (nodeName != "div" && nodeName != "svg") {
|
|
|
|
// canvas.addToSelection([evt.target]);
|
|
|
|
// }
|
2009-07-03 02:49:42 +00:00
|
|
|
|
|
|
|
// clear out selection and set it to the new list
|
2009-07-02 04:50:24 +00:00
|
|
|
canvas.clearSelection();
|
|
|
|
canvas.addToSelection(getIntersectionList());
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-03 02:49:42 +00:00
|
|
|
/*
|
|
|
|
// for each selected:
|
|
|
|
// - if newList contains selected, do nothing
|
|
|
|
// - if newList doesn't contain selected, remove it from selected
|
|
|
|
// - for any newList that was not in selectedElements, add it to selected
|
|
|
|
var elemsToRemove = [];
|
|
|
|
var newList = getIntersectionList();
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var ind = newList.indexOf(selectedElements[i]);
|
|
|
|
if (ind == -1) {
|
|
|
|
elemsToRemove.push(selectedElements[i]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newList[ind] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (elemsToRemove.length > 0)
|
|
|
|
canvas.removeFromSelection(elemsToRemove);
|
|
|
|
*/
|
2009-06-30 06:24:41 +00:00
|
|
|
break;
|
2009-06-23 05:17:53 +00:00
|
|
|
case "resize":
|
|
|
|
// we track the resize bounding box and translate/scale the selected element
|
|
|
|
// while the mouse is down, when mouse goes up, we use this to recalculate
|
|
|
|
// the shape's coordinates
|
2009-08-16 15:07:00 +00:00
|
|
|
var box=canvas.getBBox(selected), left=box.x, top=box.y, width=box.width,
|
2009-06-23 05:17:53 +00:00
|
|
|
height=box.height, dx=(x-start_x), dy=(y-start_y);
|
2009-08-22 19:08:01 +00:00
|
|
|
|
2009-08-21 15:53:36 +00:00
|
|
|
// if rotated, adjust the dx,dy values
|
2009-08-22 19:08:01 +00:00
|
|
|
var angle = canvas.getRotationAngle(selected);
|
2009-08-21 15:53:36 +00:00
|
|
|
if (angle) {
|
|
|
|
var r = Math.sqrt( dx*dx + dy*dy );
|
2009-08-22 19:08:01 +00:00
|
|
|
var theta = Math.atan2(dy,dx) - angle * Math.PI / 180.0;
|
2009-08-21 15:53:36 +00:00
|
|
|
dx = r * Math.cos(theta);
|
|
|
|
dy = r * Math.sin(theta);
|
|
|
|
}
|
|
|
|
|
2009-08-22 19:08:01 +00:00
|
|
|
// if not stretching in y direction, set dy to 0
|
|
|
|
// if not stretching in x direction, set dx to 0
|
|
|
|
if(current_resize_mode.indexOf("n")==-1 && current_resize_mode.indexOf("s")==-1) {
|
|
|
|
dy = 0;
|
|
|
|
}
|
|
|
|
if(current_resize_mode.indexOf("e")==-1 && current_resize_mode.indexOf("w")==-1) {
|
|
|
|
dx = 0;
|
|
|
|
}
|
|
|
|
|
2009-06-23 05:17:53 +00:00
|
|
|
var ts = null;
|
2009-08-22 19:08:01 +00:00
|
|
|
var tx = 0, ty = 0;
|
2009-09-29 03:10:29 +00:00
|
|
|
var sy = height ? (height+dy)/height : 1,
|
|
|
|
sx = width ? (width+dx)/width : 1;
|
2009-08-27 22:26:47 +00:00
|
|
|
// if we are dragging on the north side, then adjust the scale factor and ty
|
2009-06-23 05:17:53 +00:00
|
|
|
if(current_resize_mode.indexOf("n") != -1) {
|
2009-09-29 03:10:29 +00:00
|
|
|
sy = height ? (height-dy)/height : 1;
|
2009-08-22 19:08:01 +00:00
|
|
|
ty = height;
|
2009-06-23 05:17:53 +00:00
|
|
|
}
|
2009-08-27 22:26:47 +00:00
|
|
|
|
|
|
|
// if we dragging on the east side, then adjust the scale factor and tx
|
2009-08-22 19:08:01 +00:00
|
|
|
if(current_resize_mode.indexOf("w") != -1) {
|
2009-09-29 03:10:29 +00:00
|
|
|
sx = width ? (width-dx)/width : 1;
|
2009-08-22 19:08:01 +00:00
|
|
|
tx = width;
|
2009-06-23 05:17:53 +00:00
|
|
|
}
|
2009-08-22 19:08:01 +00:00
|
|
|
|
|
|
|
// find the rotation transform and prepend it
|
|
|
|
var ts = [" translate(", (left+tx), ",", (top+ty), ") scale(", sx, ",", sy,
|
|
|
|
") translate(", -(left+tx), ",", -(top+ty), ")"].join('');
|
|
|
|
if (angle) {
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(left+width/2),
|
|
|
|
cy = round(top+height/2);
|
2009-08-22 19:08:01 +00:00
|
|
|
ts = ["rotate(", angle, " ", cx, ",", cy, ")", ts].join('')
|
|
|
|
}
|
|
|
|
selected.setAttribute("transform", ts);
|
2009-08-27 22:26:47 +00:00
|
|
|
|
|
|
|
var selectedBBox = selectedBBoxes[0];
|
|
|
|
|
|
|
|
// reset selected bbox top-left position
|
|
|
|
selectedBBox.x = left;
|
|
|
|
selectedBBox.y = top;
|
2009-08-22 19:08:01 +00:00
|
|
|
|
2009-08-27 22:26:47 +00:00
|
|
|
// if this is a translate, adjust the box position
|
2009-08-22 19:08:01 +00:00
|
|
|
if (tx) {
|
2009-08-27 22:26:47 +00:00
|
|
|
selectedBBox.x += dx;
|
2009-08-22 19:08:01 +00:00
|
|
|
}
|
|
|
|
if (ty) {
|
2009-08-27 22:26:47 +00:00
|
|
|
selectedBBox.y += dy;
|
2009-08-22 19:08:01 +00:00
|
|
|
}
|
2009-08-27 22:26:47 +00:00
|
|
|
|
|
|
|
// update box width/height
|
2009-09-17 14:47:54 +00:00
|
|
|
selectedBBox.width = round(width*sx);
|
|
|
|
selectedBBox.height = round(height*sy);
|
2009-08-27 22:26:47 +00:00
|
|
|
|
2009-06-23 05:46:47 +00:00
|
|
|
// normalize selectedBBox
|
|
|
|
if (selectedBBox.width < 0) {
|
2009-08-27 22:26:47 +00:00
|
|
|
selectedBBox.width *= -1;
|
|
|
|
// if we are dragging on the east side and scaled negatively
|
|
|
|
if(current_resize_mode.indexOf("e") != -1 && sx < 0) {
|
|
|
|
selectedBBox.x = box.x - selectedBBox.width;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
selectedBBox.x -= selectedBBox.width;
|
|
|
|
}
|
2009-06-23 05:46:47 +00:00
|
|
|
}
|
|
|
|
if (selectedBBox.height < 0) {
|
2009-08-27 22:26:47 +00:00
|
|
|
selectedBBox.height *= -1;
|
|
|
|
// if we are dragging on the south side and scaled negatively
|
|
|
|
if(current_resize_mode.indexOf("s") != -1 && sy < 0) {
|
|
|
|
selectedBBox.y = box.y - selectedBBox.height;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
selectedBBox.y -= selectedBBox.height;
|
|
|
|
}
|
2009-08-16 14:56:22 +00:00
|
|
|
}
|
2009-08-27 22:26:47 +00:00
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
selectorManager.requestSelector(selected).resize(selectedBBox);
|
2009-06-23 07:48:15 +00:00
|
|
|
break;
|
2009-09-16 17:18:11 +00:00
|
|
|
case "zoom":
|
|
|
|
x *= current_zoom;
|
|
|
|
y *= current_zoom;
|
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': Math.min(start_x*current_zoom,x),
|
|
|
|
'y': Math.min(start_y*current_zoom,y),
|
|
|
|
'width': Math.abs(x-start_x*current_zoom),
|
|
|
|
'height': Math.abs(y-start_y*current_zoom)
|
|
|
|
},100);
|
|
|
|
break;
|
2009-06-10 03:12:19 +00:00
|
|
|
case "text":
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(shape,{
|
|
|
|
'x': x,
|
|
|
|
'y': y
|
|
|
|
},1000);
|
2009-06-10 03:12:19 +00:00
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "line":
|
2009-10-14 03:03:13 +00:00
|
|
|
// Opera has a problem with suspendRedraw() apparently
|
|
|
|
var handle = null;
|
|
|
|
if (!window.opera) svgroot.suspendRedraw(1000);
|
2009-06-02 16:40:33 +00:00
|
|
|
shape.setAttributeNS(null, "x2", x);
|
|
|
|
shape.setAttributeNS(null, "y2", y);
|
2009-10-14 03:03:13 +00:00
|
|
|
if (!window.opera) svgroot.unsuspendRedraw(handle);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "square":
|
|
|
|
var size = Math.max( Math.abs(x - start_x), Math.abs(y - start_y) );
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(shape,{
|
|
|
|
'width': size,
|
|
|
|
'height': size,
|
|
|
|
'x': start_x < x ? start_x : start_x - size,
|
|
|
|
'y': start_y < y ? start_y : start_y - size
|
|
|
|
},1000);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "rect":
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(shape,{
|
|
|
|
'width': Math.abs(x-start_x),
|
|
|
|
'height': Math.abs(y-start_y),
|
|
|
|
'x': Math.min(start_x,x),
|
|
|
|
'y': Math.min(start_y,y)
|
|
|
|
},1000);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-09-04 22:22:40 +00:00
|
|
|
case "image":
|
|
|
|
assignAttributes(shape,{
|
|
|
|
'width': Math.abs(x-start_x),
|
|
|
|
'height': Math.abs(y-start_y),
|
|
|
|
'x': Math.min(start_x,x),
|
|
|
|
'y': Math.min(start_y,y)
|
|
|
|
},1000);
|
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "circle":
|
|
|
|
var cx = shape.getAttributeNS(null, "cx");
|
|
|
|
var cy = shape.getAttributeNS(null, "cy");
|
|
|
|
var rad = Math.sqrt( (x-cx)*(x-cx) + (y-cy)*(y-cy) );
|
2009-06-03 15:00:48 +00:00
|
|
|
shape.setAttributeNS(null, "r", rad);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "ellipse":
|
|
|
|
var cx = shape.getAttributeNS(null, "cx");
|
|
|
|
var cy = shape.getAttributeNS(null, "cy");
|
2009-10-14 03:03:13 +00:00
|
|
|
// Opera has a problem with suspendRedraw() apparently
|
|
|
|
var handle = null;
|
|
|
|
if (!window.opera) svgroot.suspendRedraw(1000);
|
2009-06-02 16:40:33 +00:00
|
|
|
shape.setAttributeNS(null, "rx", Math.abs(x - cx) );
|
|
|
|
shape.setAttributeNS(null, "ry", Math.abs(y - cy) );
|
2009-10-14 03:03:13 +00:00
|
|
|
if (!window.opera) svgroot.unsuspendRedraw(handle);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "fhellipse":
|
|
|
|
case "fhrect":
|
|
|
|
freehand_min_x = Math.min(x, freehand_min_x);
|
|
|
|
freehand_max_x = Math.max(x, freehand_max_x);
|
|
|
|
freehand_min_y = Math.min(y, freehand_min_y);
|
|
|
|
freehand_max_y = Math.max(y, freehand_max_y);
|
2009-06-03 08:41:25 +00:00
|
|
|
// break; missing on purpose
|
2009-10-18 20:04:26 +00:00
|
|
|
case "fhpath":
|
2009-06-08 02:17:07 +00:00
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-08-07 17:19:32 +00:00
|
|
|
d_attr += + x + "," + y + " ";
|
|
|
|
shape.setAttributeNS(null, "points", d_attr);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-10-18 20:46:32 +00:00
|
|
|
// update path stretch line coordinates
|
|
|
|
case "path":
|
|
|
|
var line = document.getElementById("path_stretch_line");
|
2009-08-07 05:44:55 +00:00
|
|
|
if (line) {
|
2009-09-10 20:34:20 +00:00
|
|
|
line.setAttribute("x2", x *= current_zoom);
|
|
|
|
line.setAttribute("y2", y *= current_zoom);
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
|
|
|
break;
|
2009-10-17 20:29:56 +00:00
|
|
|
case "pathedit":
|
2009-08-08 22:17:59 +00:00
|
|
|
// if we are dragging a point, let's move it
|
2009-10-18 20:46:32 +00:00
|
|
|
if (current_path_pt_drag != -1 && current_path) {
|
|
|
|
var old_path_pts = $.map(current_path_pts, function(n){return n/current_zoom;});
|
|
|
|
updatePath(mouse_x, mouse_y, old_path_pts);
|
|
|
|
} else if (current_ctrl_pt_drag != -1 && current_path) {
|
2009-09-25 17:59:32 +00:00
|
|
|
// Moving the control point. Since only one segment is altered,
|
|
|
|
// we only need to do a pathSegList replace.
|
|
|
|
var data = current_ctrl_pt_drag.split('c');
|
|
|
|
var index = data[0]-0;
|
|
|
|
var ctrl_num = data[1]-0;
|
2009-10-18 20:46:32 +00:00
|
|
|
var c_item = current_path.pathSegList.getItem(index+1);
|
2009-09-29 15:58:46 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var angle = canvas.getRotationAngle(current_path) * Math.PI / 180.0;
|
2009-10-07 20:16:35 +00:00
|
|
|
if (angle) {
|
|
|
|
// calculate the shape's old center that was used for rotation
|
|
|
|
var box = selectedBBoxes[0];
|
|
|
|
var cx = round(box.x + box.width/2) * current_zoom,
|
|
|
|
cy = round(box.y + box.height/2) * current_zoom;
|
|
|
|
var dx = mouse_x - cx, dy = mouse_y - cy;
|
|
|
|
var r = Math.sqrt( dx*dx + dy*dy );
|
|
|
|
var theta = Math.atan2(dy,dx) - angle;
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts[i] = mouse_x = cx + r * Math.cos(theta);
|
|
|
|
current_path_pts[i+1] = mouse_y = cy + r * Math.sin(theta);
|
2009-10-07 20:16:35 +00:00
|
|
|
x = mouse_x / current_zoom;
|
|
|
|
y = mouse_y / current_zoom;
|
|
|
|
}
|
|
|
|
|
2009-10-09 19:47:32 +00:00
|
|
|
c_item['x' + ctrl_num] = x;
|
|
|
|
c_item['y' + ctrl_num] = y;
|
|
|
|
replacePathSeg(6, index+1, [c_item.x,c_item.y, c_item.x1,c_item.y1, c_item.x2,c_item.y2]);
|
2009-10-13 14:52:51 +00:00
|
|
|
|
|
|
|
updateSegLine(true);
|
|
|
|
|
2009-09-25 17:59:32 +00:00
|
|
|
var grip = document.getElementById("ctrlpointgrip_" + current_ctrl_pt_drag);
|
|
|
|
if(grip) {
|
|
|
|
grip.setAttribute("cx", mouse_x);
|
|
|
|
grip.setAttribute("cy", mouse_y);
|
|
|
|
|
|
|
|
var line = document.getElementById("ctrlLine_"+current_ctrl_pt_drag);
|
|
|
|
line.setAttribute("x2", mouse_x);
|
|
|
|
line.setAttribute("y2", mouse_y);
|
|
|
|
}
|
2009-08-08 22:17:59 +00:00
|
|
|
}
|
|
|
|
break;
|
2009-08-26 19:45:28 +00:00
|
|
|
case "rotate":
|
2009-09-04 16:48:20 +00:00
|
|
|
var box = canvas.getBBox(selected),
|
2009-09-17 14:47:54 +00:00
|
|
|
cx = round(box.x + box.width/2),
|
|
|
|
cy = round(box.y + box.height/2);
|
|
|
|
var angle = round(((Math.atan2(cy-y,cx-x) * (180/Math.PI))-90) % 360);
|
2009-09-04 05:10:48 +00:00
|
|
|
canvas.setRotationAngle(angle<-180?(360+angle):angle, true);
|
2009-08-26 19:45:28 +00:00
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
default:
|
|
|
|
break;
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-10-23 15:21:27 +00:00
|
|
|
var shortFloat = function(val) {
|
|
|
|
var digits = 5;
|
|
|
|
if(!isNaN(val)) {
|
2009-10-23 17:27:37 +00:00
|
|
|
return Number(Number(val).toFixed(digits));
|
2009-10-23 15:21:27 +00:00
|
|
|
} else if($.isArray(val)) {
|
|
|
|
return shortFloat(val[0]) + ',' + shortFloat(val[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var convertPath = function(path, toRel) {
|
|
|
|
var segList = path.pathSegList;
|
|
|
|
var len = segList.numberOfItems;
|
|
|
|
var curx = 0, cury = 0;
|
|
|
|
var d = "";
|
|
|
|
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var seg = segList.getItem(i);
|
|
|
|
// if these properties are not in the segment, set them to zero
|
|
|
|
var x = seg.x || 0,
|
|
|
|
y = seg.y || 0,
|
|
|
|
x1 = seg.x1 || 0,
|
|
|
|
y1 = seg.y1 || 0,
|
|
|
|
x2 = seg.x2 || 0,
|
|
|
|
y2 = seg.y2 || 0;
|
|
|
|
|
|
|
|
var type = seg.pathSegType;
|
|
|
|
var letter = pathMap[type]['to'+(toRel?'Lower':'Upper')+'Case']();
|
|
|
|
var addToD = function(pnts, more, last) {
|
|
|
|
var str = '';
|
|
|
|
var more = more?' '+more.join(' '):'';
|
|
|
|
var last = last?shortFloat(last):'';
|
|
|
|
$.each(pnts, function(i, pnt) {
|
|
|
|
pnts[i] = shortFloat(pnt);
|
|
|
|
});
|
|
|
|
d += letter + pnts.join(' ') + more + last;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 1: // z,Z closepath (Z/z)
|
|
|
|
d += "z";
|
|
|
|
break;
|
|
|
|
case 2: // absolute move (M)
|
|
|
|
case 4: // absolute line (L)
|
|
|
|
case 12: // absolute horizontal line (H)
|
|
|
|
case 14: // absolute vertical line (V)
|
|
|
|
case 18: // absolute smooth quad (T)
|
|
|
|
x -= curx;
|
|
|
|
y -= cury;
|
|
|
|
case 3: // relative move (m)
|
|
|
|
case 5: // relative line (l)
|
|
|
|
case 13: // relative horizontal line (h)
|
|
|
|
case 15: // relative vertical line (v)
|
|
|
|
case 19: // relative smooth quad (t)
|
|
|
|
if(toRel) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} else {
|
|
|
|
x += curx;
|
|
|
|
y += cury;
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
}
|
|
|
|
addToD([[x,y]]);
|
|
|
|
break;
|
|
|
|
case 6: // absolute cubic (C)
|
|
|
|
x -= curx; x1 -= curx; x2 -= curx;
|
|
|
|
y -= cury; y1 -= cury; y2 -= cury;
|
|
|
|
case 7: // relative cubic (c)
|
|
|
|
if(toRel) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} else {
|
|
|
|
x += curx; x1 += curx; x2 += curx;
|
|
|
|
y += cury; y1 += cury; y2 += cury;
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
}
|
|
|
|
addToD([[x1,y1],[x2,y2],[x,y]]);
|
|
|
|
break;
|
|
|
|
case 8: // absolute quad (Q)
|
|
|
|
x -= curx; x1 -= curx;
|
|
|
|
y -= cury; y1 -= cury;
|
|
|
|
case 9: // relative quad (q)
|
|
|
|
if(toRel) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} else {
|
|
|
|
x += curx; x1 += curx;
|
|
|
|
y += cury; y1 += cury;
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
}
|
|
|
|
addToD([[x1,y1],[x,y]]);
|
|
|
|
break;
|
|
|
|
case 10: // absolute elliptical arc (A)
|
|
|
|
x -= curx;
|
|
|
|
y -= cury;
|
|
|
|
case 11: // relative elliptical arc (a)
|
|
|
|
if(toRel) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} else {
|
|
|
|
x += curx;
|
|
|
|
y += cury;
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
}
|
|
|
|
addToD([[seg.r1,seg.r2]], [
|
|
|
|
seg.angle,
|
|
|
|
(seg.largeArcFlag ? 1 : 0),
|
|
|
|
(seg.sweepFlag ? 1 : 0)
|
|
|
|
],[x,y]
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 16: // absolute smooth cubic (S)
|
|
|
|
x -= curx; x2 -= curx;
|
|
|
|
y -= cury; y2 -= cury;
|
|
|
|
case 17: // relative smooth cubic (s)
|
|
|
|
if(toRel) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} else {
|
|
|
|
x += curx; x2 += curx;
|
|
|
|
y += cury; y2 += cury;
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
}
|
|
|
|
addToD([[x2,y2],[x,y]]);
|
|
|
|
break;
|
|
|
|
} // switch on path segment type
|
|
|
|
} // for each segment
|
|
|
|
return d;
|
|
|
|
};
|
|
|
|
|
2009-10-07 16:04:32 +00:00
|
|
|
var resetPointGrips = function() {
|
|
|
|
var sr = svgroot.suspendRedraw(100);
|
2009-10-18 20:46:32 +00:00
|
|
|
removeAllPointGripsFromPath();
|
|
|
|
recalcPathPoints();
|
|
|
|
addAllPointGripsToPath();
|
2009-10-07 16:04:32 +00:00
|
|
|
svgroot.unsuspendRedraw(sr);
|
|
|
|
};
|
2009-10-07 18:56:03 +00:00
|
|
|
|
2009-10-09 15:29:40 +00:00
|
|
|
var setPointContainerTransform = function(value) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var conts = $('#pathpointgrip_container,#ctrlpointgrip_container');
|
2009-10-09 15:29:40 +00:00
|
|
|
$.each(conts,function() {
|
|
|
|
this.setAttribute("transform", value);
|
|
|
|
if(!value) {
|
|
|
|
this.removeAttribute("transform");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var recalcPathPoints = function() {
|
|
|
|
current_path_pts = [];
|
|
|
|
var segList = current_path.pathSegList;
|
2009-10-07 18:56:03 +00:00
|
|
|
var curx = segList.getItem(0).x, cury = segList.getItem(0).y;
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts.push(curx * current_zoom);
|
|
|
|
current_path_pts.push(cury * current_zoom);
|
2009-10-07 18:56:03 +00:00
|
|
|
var len = segList.numberOfItems;
|
|
|
|
for (var i = 1; i < len; ++i) {
|
|
|
|
var l = segList.getItem(i);
|
|
|
|
var x = l.x, y = l.y;
|
2009-10-18 20:46:32 +00:00
|
|
|
// paths can now be closed, skip Z segments
|
2009-10-07 18:56:03 +00:00
|
|
|
if (l.pathSegType == 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
var type = l.pathSegType;
|
2009-10-18 20:46:32 +00:00
|
|
|
// current_path_pts just holds the absolute coords
|
2009-10-07 18:56:03 +00:00
|
|
|
if (type == 4) {
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
} // type 4 (abs line)
|
|
|
|
else if (type == 5) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} // type 5 (rel line)
|
|
|
|
else if (type == 6) {
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
} // type 6 (abs curve)
|
|
|
|
else if (type == 7) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} // type 7 (rel curve)
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts.push(curx * current_zoom);
|
|
|
|
current_path_pts.push(cury * current_zoom);
|
2009-10-07 18:56:03 +00:00
|
|
|
} // for each segment
|
|
|
|
}
|
2009-10-07 16:04:32 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var removeAllPointGripsFromPath = function() {
|
2009-08-08 14:03:14 +00:00
|
|
|
// loop through and hide all pointgrips
|
2009-10-18 20:46:32 +00:00
|
|
|
$('#pathpointgrip_container > *').attr("display", "none");
|
2009-09-25 17:59:32 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var line = document.getElementById("path_stretch_line");
|
2009-08-30 02:24:20 +00:00
|
|
|
if (line) line.setAttribute("display", "none");
|
2009-09-25 17:59:32 +00:00
|
|
|
|
2009-10-05 19:09:39 +00:00
|
|
|
$('#ctrlpointgrip_container *').attr('display','none');
|
2009-08-08 14:03:14 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var addAllPointGripsToPath = function(pointToSelect) {
|
2009-09-12 04:49:50 +00:00
|
|
|
// loop through and show all pointgrips
|
2009-10-18 20:46:32 +00:00
|
|
|
var len = current_path_pts.length;
|
2009-08-31 18:02:15 +00:00
|
|
|
for (var i = 0; i < len; i += 2) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var grip = document.getElementById("pathpointgrip_"+i/2);
|
2009-08-31 18:02:15 +00:00
|
|
|
if (grip) {
|
|
|
|
assignAttributes(grip, {
|
2009-10-18 20:46:32 +00:00
|
|
|
'cx': current_path_pts[i],
|
|
|
|
'cy': current_path_pts[i+1],
|
2009-08-31 18:02:15 +00:00
|
|
|
'display': 'inline'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2009-10-18 20:46:32 +00:00
|
|
|
addPointGripToPath(current_path_pts[i], current_path_pts[i+1],i/2);
|
2009-08-31 18:02:15 +00:00
|
|
|
}
|
2009-09-25 20:03:20 +00:00
|
|
|
|
|
|
|
var index = i/2;
|
2009-10-18 20:46:32 +00:00
|
|
|
var item = current_path.pathSegList.getItem(index);
|
2009-10-09 19:47:32 +00:00
|
|
|
if(item.pathSegType == 6) {
|
2009-09-25 20:03:20 +00:00
|
|
|
index -= 1;
|
|
|
|
// Same code as when making a curve, needs to be in own function
|
2009-10-18 20:46:32 +00:00
|
|
|
var cur_x = getPathPoint(index)[0];
|
|
|
|
var cur_y = getPathPoint(index)[1];
|
|
|
|
var next_x = getPathPoint(index+1)[0];
|
|
|
|
var next_y = getPathPoint(index+1)[1];
|
2009-10-09 19:47:32 +00:00
|
|
|
addControlPointGrip(item.x1,item.y1, cur_x,cur_y, index+'c1');
|
|
|
|
addControlPointGrip(item.x2,item.y2, next_x,next_y, index+'c2');
|
2009-09-29 15:58:46 +00:00
|
|
|
}
|
2009-08-08 14:03:14 +00:00
|
|
|
}
|
2009-10-18 20:46:32 +00:00
|
|
|
// FIXME: we cannot just use the same transform as the path because we might be
|
2009-09-12 04:49:50 +00:00
|
|
|
// at a different zoom level
|
2009-10-18 20:46:32 +00:00
|
|
|
var angle = canvas.getRotationAngle(current_path);
|
2009-09-12 04:49:50 +00:00
|
|
|
if (angle) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var bbox = canvas.getBBox(current_path);
|
2009-09-12 04:49:50 +00:00
|
|
|
var cx = (bbox.x + bbox.width/2) * current_zoom,
|
|
|
|
cy = (bbox.y + bbox.height/2) * current_zoom;
|
|
|
|
var xform = ["rotate(", angle, " ", cx, ",", cy, ")"].join("");
|
2009-10-09 15:29:40 +00:00
|
|
|
setPointContainerTransform(xform);
|
|
|
|
}
|
|
|
|
if(pointToSelect != null) {
|
|
|
|
canvas.addNodeToSelection(pointToSelect);
|
2009-09-12 04:49:50 +00:00
|
|
|
}
|
2009-08-08 03:36:01 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var addPointGripToPath = function(x,y,index) {
|
2009-08-08 03:36:01 +00:00
|
|
|
// create the container of all the point grips
|
2009-10-18 20:46:32 +00:00
|
|
|
var pointGripContainer = document.getElementById("pathpointgrip_container");
|
2009-08-08 03:36:01 +00:00
|
|
|
if (!pointGripContainer) {
|
|
|
|
var parent = document.getElementById("selectorParentGroup");
|
|
|
|
pointGripContainer = parent.appendChild(document.createElementNS(svgns, "g"));
|
2009-10-18 20:46:32 +00:00
|
|
|
pointGripContainer.id = "pathpointgrip_container";
|
2009-08-08 03:36:01 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var pointGrip = document.getElementById("pathpointgrip_"+index);
|
2009-08-08 03:36:01 +00:00
|
|
|
// create it
|
|
|
|
if (!pointGrip) {
|
|
|
|
pointGrip = document.createElementNS(svgns, "circle");
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(pointGrip, {
|
2009-10-18 20:46:32 +00:00
|
|
|
'id': "pathpointgrip_" + index,
|
2009-08-24 20:00:48 +00:00
|
|
|
'display': "none",
|
|
|
|
'r': 4,
|
2009-10-13 14:52:51 +00:00
|
|
|
'fill': "#0FF",
|
2009-08-24 20:00:48 +00:00
|
|
|
'stroke': "#00F",
|
|
|
|
'stroke-width': 2,
|
|
|
|
'cursor': 'move',
|
2009-10-16 13:17:23 +00:00
|
|
|
'style': 'pointer-events:all',
|
|
|
|
'xlink:title': 'Drag point to move it. Double-click point to change segment type.'
|
2009-08-24 20:00:48 +00:00
|
|
|
});
|
2009-08-08 03:36:01 +00:00
|
|
|
pointGrip = pointGripContainer.appendChild(pointGrip);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var grip = $('#pathpointgrip_'+index);
|
2009-10-13 14:52:51 +00:00
|
|
|
grip.dblclick(function() {
|
2009-10-09 15:29:40 +00:00
|
|
|
canvas.setSegType();
|
2009-09-25 17:59:32 +00:00
|
|
|
});
|
2009-08-08 03:36:01 +00:00
|
|
|
}
|
2009-10-13 14:52:51 +00:00
|
|
|
|
2009-08-08 03:36:01 +00:00
|
|
|
// set up the point grip element and display it
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(pointGrip, {
|
|
|
|
'cx': x,
|
|
|
|
'cy': y,
|
|
|
|
'display': "inline",
|
|
|
|
});
|
2009-08-08 03:36:01 +00:00
|
|
|
};
|
2009-09-25 17:59:32 +00:00
|
|
|
|
2009-10-13 14:52:51 +00:00
|
|
|
var updateSegLine = function(next_node) {
|
|
|
|
// create segment line
|
|
|
|
var segLine = document.getElementById("segline");
|
|
|
|
if(!segLine) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var pointGripContainer = $('#pathpointgrip_container')[0];
|
2009-10-13 14:52:51 +00:00
|
|
|
segLine = document.createElementNS(svgns, "path");
|
|
|
|
assignAttributes(segLine, {
|
|
|
|
'id': "segline",
|
|
|
|
'fill': "none",
|
|
|
|
'stroke': "#0FF",
|
|
|
|
'stroke-width': 2,
|
|
|
|
'style':'pointer-events:none'
|
|
|
|
});
|
|
|
|
segLine = pointGripContainer.appendChild(segLine);
|
|
|
|
}
|
|
|
|
if(!segLine.getAttribute('d')) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var pt = getPathPoint(current_path_pt);
|
2009-10-13 14:52:51 +00:00
|
|
|
segLine.setAttribute('d', 'M' + pt.join(',') + ' 0,0');
|
|
|
|
}
|
|
|
|
segLine.setAttribute('display','inline');
|
|
|
|
|
|
|
|
if(!next_node) {
|
|
|
|
// Replace "M" val
|
2009-10-18 20:46:32 +00:00
|
|
|
replacePathSeg(2, 0, getPathPoint(current_path_pt, true), segLine);
|
2009-10-13 14:52:51 +00:00
|
|
|
} else {
|
2009-10-18 20:46:32 +00:00
|
|
|
var seg = current_path.pathSegList.getItem(current_path_pt+1);
|
2009-10-13 14:52:51 +00:00
|
|
|
var points = [seg.x, seg.y];
|
|
|
|
if(seg.x1 != null && seg.x2 != null) {
|
|
|
|
points.splice(2, 0, seg.x1, seg.y1, seg.x2, seg.y2);
|
|
|
|
}
|
|
|
|
points = $.map(points, function(n){return n*current_zoom;});
|
|
|
|
replacePathSeg(seg.pathSegType, 1, points, segLine);
|
|
|
|
}
|
|
|
|
}
|
2009-10-09 15:29:40 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var updatePath = function(mouse_x, mouse_y, old_path_pts) {
|
2009-10-05 13:50:44 +00:00
|
|
|
var x = mouse_x / current_zoom;
|
|
|
|
var y = mouse_y / current_zoom;
|
2009-10-07 18:56:03 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var is_closed = current_path.getAttribute('d').toLowerCase().indexOf('z') != -1;
|
2009-10-05 13:50:44 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var i = current_path_pt_drag * 2;
|
|
|
|
var last_index = current_path_pts.length/2 - 1;
|
|
|
|
var is_first = current_path_pt_drag == 0 || (is_closed && current_path_pt_drag == last_index);
|
|
|
|
var is_last = !is_closed && current_path_pt_drag == last_index;
|
2009-10-05 13:50:44 +00:00
|
|
|
|
|
|
|
// if the image is rotated, then we must modify the x,y mouse coordinates
|
|
|
|
// and rotate them into the shape's rotated coordinate system
|
|
|
|
// we also re-map mouse_x/y and x/y into the rotated coordinate system
|
2009-10-18 20:46:32 +00:00
|
|
|
var angle = canvas.getRotationAngle(current_path) * Math.PI / 180.0;
|
2009-10-05 13:50:44 +00:00
|
|
|
if (angle) {
|
|
|
|
// calculate the shape's old center that was used for rotation
|
|
|
|
var box = selectedBBoxes[0];
|
|
|
|
var cx = round(box.x + box.width/2) * current_zoom,
|
|
|
|
cy = round(box.y + box.height/2) * current_zoom;
|
|
|
|
var dx = mouse_x - cx, dy = mouse_y - cy;
|
|
|
|
var r = Math.sqrt( dx*dx + dy*dy );
|
|
|
|
var theta = Math.atan2(dy,dx) - angle;
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts[i] = mouse_x = cx + r * Math.cos(theta);
|
|
|
|
current_path_pts[i+1] = mouse_y = cy + r * Math.sin(theta);
|
2009-10-05 13:50:44 +00:00
|
|
|
x = mouse_x / current_zoom;
|
|
|
|
y = mouse_y / current_zoom;
|
|
|
|
}
|
|
|
|
else {
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts[i] = x * current_zoom;
|
|
|
|
current_path_pts[i+1] = y * current_zoom;
|
2009-10-05 13:50:44 +00:00
|
|
|
}
|
|
|
|
|
2009-10-07 18:56:03 +00:00
|
|
|
if(is_first && is_closed) {
|
2009-10-05 13:50:44 +00:00
|
|
|
// Update the first point
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts[0] = current_path_pts[i];
|
|
|
|
current_path_pts[1] = current_path_pts[i+1];
|
|
|
|
current_path_pt_drag = 0;
|
2009-10-05 13:50:44 +00:00
|
|
|
}
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var index = current_path_pt_drag;
|
|
|
|
var abs_x = getPathPoint(index)[0];
|
|
|
|
var abs_y = getPathPoint(index)[1];
|
2009-10-05 13:50:44 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var item = current_path.pathSegList.getItem(index);
|
|
|
|
var x_diff = x - old_path_pts[index*2];
|
|
|
|
var y_diff = y - old_path_pts[index*2 + 1];
|
2009-10-05 13:50:44 +00:00
|
|
|
|
|
|
|
var cur_type = item.pathSegType;
|
|
|
|
var points = [];
|
|
|
|
|
2009-10-09 19:47:32 +00:00
|
|
|
if(cur_type == 6) {
|
|
|
|
points = [abs_x,abs_y, item.x1,item.y1, item.x2 + x_diff,item.y2 + y_diff];
|
2009-10-05 13:50:44 +00:00
|
|
|
} else {
|
|
|
|
if(is_first) {
|
|
|
|
// Need absolute position for first point
|
2009-10-18 20:46:32 +00:00
|
|
|
points = getPathPoint(0);
|
2009-10-05 13:50:44 +00:00
|
|
|
} else {
|
2009-10-09 19:47:32 +00:00
|
|
|
points = [abs_x, abs_y];
|
2009-10-05 13:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
replacePathSeg(cur_type, index, points);
|
|
|
|
|
|
|
|
var setSeg = function(index,first) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var points, item = current_path.pathSegList.getItem(index);
|
2009-10-05 13:50:44 +00:00
|
|
|
var type = item.pathSegType;
|
|
|
|
if(first) {
|
2009-10-09 19:47:32 +00:00
|
|
|
item.x += x_diff;
|
|
|
|
item.y += y_diff;
|
2009-10-05 13:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 1:
|
|
|
|
points = [];
|
|
|
|
break;
|
2009-10-09 19:47:32 +00:00
|
|
|
case 4:
|
|
|
|
points = [item.x, item.y];
|
2009-10-05 13:50:44 +00:00
|
|
|
break;
|
2009-10-09 19:47:32 +00:00
|
|
|
case 6:
|
2009-10-12 18:33:27 +00:00
|
|
|
if(first) {
|
|
|
|
item.x1 -= x_diff;
|
|
|
|
item.y1 -= y_diff;
|
|
|
|
item.x2 += x_diff;
|
|
|
|
item.y2 += y_diff;
|
|
|
|
}
|
|
|
|
|
2009-10-09 19:47:32 +00:00
|
|
|
points = [item.x, item.y, item.x1 + x_diff,item.y1 + y_diff, item.x2,item.y2];
|
2009-10-05 13:50:44 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
replacePathSeg(type, index, points);
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2009-10-07 18:56:03 +00:00
|
|
|
if(is_closed || !is_last) {
|
|
|
|
var next_type = setSeg(index+1);
|
|
|
|
} else {
|
|
|
|
var next_type = 0;
|
|
|
|
}
|
2009-10-05 13:50:44 +00:00
|
|
|
|
2009-10-07 18:56:03 +00:00
|
|
|
if(is_first && is_closed) {
|
2009-10-05 13:50:44 +00:00
|
|
|
var last_type = setSeg(last_index,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// move the point grip
|
2009-10-18 20:46:32 +00:00
|
|
|
var grip = document.getElementById("pathpointgrip_" + current_path_pt_drag);
|
2009-10-05 13:50:44 +00:00
|
|
|
if (grip) {
|
|
|
|
grip.setAttribute("cx", mouse_x);
|
|
|
|
grip.setAttribute("cy", mouse_y);
|
|
|
|
|
2009-10-07 18:56:03 +00:00
|
|
|
if(is_closed && is_first) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var grip = document.getElementById("pathpointgrip_" + last_index);
|
2009-10-05 13:50:44 +00:00
|
|
|
grip.setAttribute("cx", mouse_x);
|
|
|
|
grip.setAttribute("cy", mouse_y);
|
|
|
|
}
|
2009-10-22 19:36:04 +00:00
|
|
|
call("changed", [grip]);
|
2009-10-05 13:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(is_first) cur_type = last_type;
|
|
|
|
|
2009-10-09 19:47:32 +00:00
|
|
|
if(cur_type != 4) {
|
2009-10-05 13:50:44 +00:00
|
|
|
var num = is_first?last_index:index;
|
|
|
|
var id2 = (num-1)+'c2';
|
|
|
|
var line = document.getElementById("ctrlLine_"+id2);
|
|
|
|
if(line) {
|
|
|
|
var x2 = line.getAttribute('x2') - 0 + x_diff*current_zoom;
|
|
|
|
var y2 = line.getAttribute('y2') - 0 + y_diff*current_zoom;
|
|
|
|
addControlPointGrip(x2,y2, mouse_x,mouse_y, id2, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-09 19:47:32 +00:00
|
|
|
if(next_type != 4) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var id1 = (current_path_pt_drag)+'c1';
|
2009-10-05 13:50:44 +00:00
|
|
|
var line = document.getElementById("ctrlLine_"+id1);
|
|
|
|
if(line) {
|
|
|
|
var x2 = line.getAttribute('x2') - 0 + x_diff*current_zoom;
|
|
|
|
var y2 = line.getAttribute('y2') - 0 + y_diff*current_zoom;
|
|
|
|
addControlPointGrip(x2,y2, mouse_x,mouse_y, id1, true);
|
|
|
|
}
|
|
|
|
}
|
2009-10-13 14:52:51 +00:00
|
|
|
updateSegLine();
|
|
|
|
if(next_type != 4) {
|
|
|
|
updateSegLine(true);
|
|
|
|
}
|
2009-10-05 13:50:44 +00:00
|
|
|
}
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var getPathPoint = function(index, raw_val) {
|
|
|
|
var len = current_path_pts.length;
|
2009-09-29 15:58:46 +00:00
|
|
|
var pt_num = len/2;
|
|
|
|
if(index < 0) {
|
|
|
|
index += pt_num;
|
|
|
|
} else if(index >= pt_num) {
|
|
|
|
index -= pt_num;
|
|
|
|
}
|
2009-09-29 18:11:52 +00:00
|
|
|
var z = raw_val?1:current_zoom;
|
2009-10-18 20:46:32 +00:00
|
|
|
return [current_path_pts[index*2] / z, current_path_pts[index*2 + 1] / z];
|
2009-09-29 15:58:46 +00:00
|
|
|
}
|
|
|
|
|
2009-10-02 15:05:31 +00:00
|
|
|
// This replaces the segment at the given index. Type is given as number.
|
2009-10-18 20:46:32 +00:00
|
|
|
var replacePathSeg = function(type, index, pts, path) {
|
|
|
|
if(!path) path = current_path;
|
2009-10-13 14:52:51 +00:00
|
|
|
var func = 'createSVGPathSeg' + pathFuncs[type];
|
2009-10-18 20:46:32 +00:00
|
|
|
var seg = path[func].apply(path, pts);
|
|
|
|
path.pathSegList.replaceItem(seg, index);
|
2009-10-02 15:05:31 +00:00
|
|
|
}
|
|
|
|
|
2009-09-29 18:11:52 +00:00
|
|
|
var addControlPointGrip = function(x, y, source_x, source_y, id, raw_val) {
|
|
|
|
if(!raw_val) {
|
|
|
|
x *= current_zoom; y *= current_zoom;
|
|
|
|
source_x *= current_zoom; source_y *= current_zoom;
|
|
|
|
}
|
|
|
|
|
2009-09-25 17:59:32 +00:00
|
|
|
// create the container of all the control point grips
|
|
|
|
var ctrlPointGripContainer = document.getElementById("ctrlpointgrip_container");
|
|
|
|
if (!ctrlPointGripContainer) {
|
|
|
|
var parent = document.getElementById("selectorParentGroup");
|
|
|
|
ctrlPointGripContainer = parent.appendChild(document.createElementNS(svgns, "g"));
|
|
|
|
ctrlPointGripContainer.id = "ctrlpointgrip_container";
|
|
|
|
}
|
2009-09-25 20:03:20 +00:00
|
|
|
ctrlPointGripContainer.setAttribute("display", "inline");
|
2009-09-25 17:59:32 +00:00
|
|
|
|
|
|
|
var ctrlLine = document.getElementById("ctrlLine_"+id);
|
|
|
|
if (!ctrlLine) {
|
|
|
|
ctrlLine = document.createElementNS(svgns, "line");
|
|
|
|
assignAttributes(ctrlLine, {
|
|
|
|
'id': "ctrlLine_"+id,
|
|
|
|
'stroke': "#555",
|
|
|
|
'stroke-width': 1,
|
|
|
|
"style": "pointer-events:none"
|
|
|
|
});
|
|
|
|
ctrlLine = ctrlPointGripContainer.appendChild(ctrlLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
assignAttributes(ctrlLine, {
|
|
|
|
'x1': source_x,
|
|
|
|
'y1': source_y,
|
|
|
|
'x2': x,
|
|
|
|
'y2': y,
|
|
|
|
'display': "inline"
|
|
|
|
});
|
|
|
|
|
|
|
|
var pointGrip = document.getElementById("ctrlpointgrip_"+id);
|
|
|
|
// create it
|
|
|
|
if (!pointGrip) {
|
|
|
|
pointGrip = document.createElementNS(svgns, "circle");
|
|
|
|
assignAttributes(pointGrip, {
|
|
|
|
'id': "ctrlpointgrip_" + id,
|
|
|
|
'display': "none",
|
2009-09-29 18:11:52 +00:00
|
|
|
'r': 4,
|
2009-10-23 17:27:37 +00:00
|
|
|
'fill': "#0FF",
|
2009-09-25 17:59:32 +00:00
|
|
|
'stroke': "#55F",
|
|
|
|
'stroke-width': 1,
|
|
|
|
'cursor': 'move',
|
2009-10-16 13:17:23 +00:00
|
|
|
'style': 'pointer-events:all',
|
|
|
|
'xlink:title': 'Drag control point to adjust curve properties'
|
2009-09-25 17:59:32 +00:00
|
|
|
});
|
|
|
|
pointGrip = ctrlPointGripContainer.appendChild(pointGrip);
|
|
|
|
}
|
|
|
|
|
|
|
|
assignAttributes(pointGrip, {
|
|
|
|
'cx': x,
|
|
|
|
'cy': y,
|
|
|
|
'display': "inline"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var removeControlPointGrips = function(index) {
|
|
|
|
for(var i=1; i <= 2; i++) {
|
2009-10-05 19:09:39 +00:00
|
|
|
$("#ctrlpointgrip_" + index + "c" + i + ",#ctrlLine_" + index + "c" + i).attr("display", "none");
|
2009-09-25 17:59:32 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-08 03:36:01 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
// - in create mode, the element's opacity is set properly, we create an InsertElementCommand
|
|
|
|
// and store it on the Undo stack
|
|
|
|
// - in move/resize mode, the element's attributes which were affected by the move/resize are
|
|
|
|
// identified, a ChangeElementCommand is created and stored on the stack for those attrs
|
2009-07-08 00:03:08 +00:00
|
|
|
// this is done in when we recalculate the selected dimensions()
|
2009-06-08 02:17:07 +00:00
|
|
|
var mouseUp = function(evt)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-09-06 23:25:12 +00:00
|
|
|
var tempJustSelected = justSelected;
|
|
|
|
justSelected = null;
|
2009-06-02 16:40:33 +00:00
|
|
|
if (!started) return;
|
|
|
|
|
2009-10-22 03:18:54 +00:00
|
|
|
var pt = transformPoint( evt.pageX, evt.pageY, root_sctm );
|
2009-10-22 03:07:15 +00:00
|
|
|
var mouse_x = pt.x;
|
|
|
|
var mouse_y = pt.y;
|
2009-09-11 13:32:56 +00:00
|
|
|
var x = mouse_x / current_zoom;
|
|
|
|
var y = mouse_y / current_zoom;
|
|
|
|
|
2009-06-02 16:40:33 +00:00
|
|
|
started = false;
|
2009-06-05 12:33:02 +00:00
|
|
|
var element = svgdoc.getElementById(getId());
|
2009-06-06 17:53:38 +00:00
|
|
|
var keep = false;
|
2009-06-02 16:40:33 +00:00
|
|
|
switch (current_mode)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-07-01 19:28:08 +00:00
|
|
|
// intentionally fall-through to select here
|
2009-06-23 05:17:53 +00:00
|
|
|
case "resize":
|
2009-07-01 19:28:08 +00:00
|
|
|
case "multiselect":
|
|
|
|
if (rubberBox != null) {
|
|
|
|
rubberBox.setAttribute("display", "none");
|
2009-08-26 19:59:52 +00:00
|
|
|
curBBoxes = [];
|
2009-07-01 19:28:08 +00:00
|
|
|
}
|
2009-06-23 05:17:53 +00:00
|
|
|
current_mode = "select";
|
2009-06-02 16:40:33 +00:00
|
|
|
case "select":
|
2009-07-01 19:28:08 +00:00
|
|
|
if (selectedElements[0] != null) {
|
|
|
|
// if we only have one selected element
|
|
|
|
if (selectedElements[1] == null) {
|
|
|
|
// set our current stroke/fill properties to the element's
|
|
|
|
var selected = selectedElements[0];
|
2009-09-13 15:50:43 +00:00
|
|
|
if (selected.tagName != "g") {
|
|
|
|
cur_shape.fill = selected.getAttribute("fill");
|
|
|
|
cur_shape.fill_opacity = selected.getAttribute("fill-opacity");
|
|
|
|
cur_shape.stroke = selected.getAttribute("stroke");
|
|
|
|
cur_shape.stroke_opacity = selected.getAttribute("stroke-opacity");
|
|
|
|
cur_shape.stroke_width = selected.getAttribute("stroke-width");
|
|
|
|
cur_shape.stroke_style = selected.getAttribute("stroke-dasharray");
|
|
|
|
}
|
2009-07-01 19:28:08 +00:00
|
|
|
if (selected.tagName == "text") {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_text.font_size = selected.getAttribute("font-size");
|
|
|
|
cur_text.font_family = selected.getAttribute("font-family");
|
2009-07-01 19:28:08 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-27 14:44:23 +00:00
|
|
|
selectorManager.requestSelector(selected).showGrips(true);
|
2009-07-01 19:28:08 +00:00
|
|
|
}
|
2009-08-08 14:03:14 +00:00
|
|
|
// if it was being dragged/resized
|
|
|
|
if (x != start_x || y != start_y) {
|
|
|
|
recalculateAllSelectedDimensions();
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) break;
|
|
|
|
selectorManager.requestSelector(selectedElements[i]).resize(selectedBBoxes[i]);
|
|
|
|
}
|
|
|
|
}
|
2009-10-17 20:29:56 +00:00
|
|
|
// no change in position/size, so maybe we should move to pathedit
|
2009-08-08 14:03:14 +00:00
|
|
|
else {
|
2009-09-06 23:25:12 +00:00
|
|
|
var t = evt.target;
|
2009-08-08 14:03:14 +00:00
|
|
|
if (selectedElements[0].nodeName == "path" && selectedElements[1] == null) {
|
2009-10-18 20:46:32 +00:00
|
|
|
if (current_path == t) {
|
2009-10-17 20:29:56 +00:00
|
|
|
current_mode = "pathedit";
|
2009-08-08 14:03:14 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
// recalculate current_path_pts
|
|
|
|
recalcPathPoints();
|
2009-08-08 14:03:14 +00:00
|
|
|
canvas.clearSelection();
|
2009-10-18 20:46:32 +00:00
|
|
|
// save the path's bbox
|
|
|
|
selectedBBoxes[0] = canvas.getBBox(current_path);
|
|
|
|
addAllPointGripsToPath();
|
2009-10-13 14:52:51 +00:00
|
|
|
canvas.addNodeToSelection(0);
|
2009-10-17 20:29:56 +00:00
|
|
|
} // going into pathedit mode
|
2009-08-08 14:03:14 +00:00
|
|
|
else {
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path = t;
|
2009-08-08 14:03:14 +00:00
|
|
|
}
|
2009-09-06 23:25:12 +00:00
|
|
|
} // if it was a path
|
|
|
|
// else, if it was selected and this is a shift-click, remove it from selection
|
|
|
|
else if (evt.shiftKey && tempJustSelected != t) {
|
|
|
|
canvas.removeFromSelection([t]);
|
|
|
|
}
|
|
|
|
} // no change in mouse position
|
2009-08-17 06:56:55 +00:00
|
|
|
}
|
2009-07-13 04:12:25 +00:00
|
|
|
// we return immediately from select so that the obj_num is not incremented
|
|
|
|
return;
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-09-16 15:45:50 +00:00
|
|
|
case "zoom":
|
2009-09-16 17:18:11 +00:00
|
|
|
if (rubberBox != null) {
|
|
|
|
rubberBox.setAttribute("display", "none");
|
|
|
|
}
|
2009-10-28 18:04:38 +00:00
|
|
|
var factor = evt.shiftKey?.5:2;
|
2009-09-16 15:45:50 +00:00
|
|
|
call("zoomed", {
|
|
|
|
'x': Math.min(start_x,x),
|
|
|
|
'y': Math.min(start_y,y),
|
|
|
|
'width': Math.abs(x-start_x),
|
2009-10-28 18:04:38 +00:00
|
|
|
'height': Math.abs(y-start_y),
|
|
|
|
'factor': factor
|
2009-09-16 15:45:50 +00:00
|
|
|
});
|
|
|
|
return;
|
2009-10-18 20:04:26 +00:00
|
|
|
case "fhpath":
|
2009-09-29 13:50:47 +00:00
|
|
|
// Check that the path contains at least 2 points; a degenerate one-point path
|
|
|
|
// causes problems.
|
|
|
|
// Webkit ignores how we set the points attribute with commas and uses space
|
|
|
|
// to separate all coordinates, see https://bugs.webkit.org/show_bug.cgi?id=29870
|
|
|
|
var coords = element.getAttribute('points');
|
|
|
|
var commaIndex = coords.indexOf(',');
|
|
|
|
if (commaIndex >= 0) {
|
|
|
|
keep = coords.indexOf(',', commaIndex+1) >= 0;
|
|
|
|
} else {
|
|
|
|
keep = coords.indexOf(' ', coords.indexOf(' ')+1) >= 0;
|
|
|
|
}
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "line":
|
2009-10-16 20:07:15 +00:00
|
|
|
keep = (element.x1.baseVal.value != element.x2.baseVal.value ||
|
|
|
|
element.y1.baseVal.value != element.y2.baseVal.value);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "square":
|
|
|
|
case "rect":
|
2009-10-16 20:07:15 +00:00
|
|
|
keep = (element.width.baseVal.value && element.height.baseVal.value);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-09-04 22:22:40 +00:00
|
|
|
case "image":
|
2009-10-16 20:07:15 +00:00
|
|
|
keep = (element.width.baseVal.value && element.height.baseVal.value);
|
2009-09-04 22:22:40 +00:00
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "circle":
|
2009-10-16 20:07:15 +00:00
|
|
|
keep = (element.r.baseVal.value);
|
2009-06-03 15:00:48 +00:00
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "ellipse":
|
2009-10-16 20:07:15 +00:00
|
|
|
keep = (element.rx.baseVal.value && element.ry.baseVal.value);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "fhellipse":
|
2009-06-03 08:41:25 +00:00
|
|
|
if ((freehand_max_x - freehand_min_x) > 0 &&
|
|
|
|
(freehand_max_y - freehand_min_y) > 0) {
|
2009-06-28 04:10:03 +00:00
|
|
|
element = addSvgElementFromJson({
|
2009-06-03 08:41:25 +00:00
|
|
|
"element": "ellipse",
|
|
|
|
"attr": {
|
|
|
|
"cx": (freehand_min_x + freehand_max_x) / 2,
|
|
|
|
"cy": (freehand_min_y + freehand_max_y) / 2,
|
|
|
|
"rx": (freehand_max_x - freehand_min_x) / 2,
|
|
|
|
"ry": (freehand_max_y - freehand_min_y) / 2,
|
2009-06-05 12:33:02 +00:00
|
|
|
"id": getId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"opacity": cur_shape.opacity,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-28 04:10:03 +00:00
|
|
|
});
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed",[element]);
|
2009-06-16 02:39:12 +00:00
|
|
|
keep = true;
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "fhrect":
|
2009-06-03 08:41:25 +00:00
|
|
|
if ((freehand_max_x - freehand_min_x) > 0 &&
|
|
|
|
(freehand_max_y - freehand_min_y) > 0) {
|
2009-06-28 04:10:03 +00:00
|
|
|
element = addSvgElementFromJson({
|
2009-06-03 08:41:25 +00:00
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"x": freehand_min_x,
|
|
|
|
"y": freehand_min_y,
|
|
|
|
"width": (freehand_max_x - freehand_min_x),
|
|
|
|
"height": (freehand_max_y - freehand_min_y),
|
2009-06-05 12:33:02 +00:00
|
|
|
"id": getId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"opacity": cur_shape.opacity,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-28 04:10:03 +00:00
|
|
|
});
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed",[element]);
|
2009-06-16 02:39:12 +00:00
|
|
|
keep = true;
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-06-10 03:12:19 +00:00
|
|
|
case "text":
|
|
|
|
keep = true;
|
2009-07-01 19:28:08 +00:00
|
|
|
canvas.clearSelection();
|
2009-06-10 03:12:19 +00:00
|
|
|
break;
|
2009-10-18 20:46:32 +00:00
|
|
|
case "path":
|
2009-08-07 05:44:55 +00:00
|
|
|
// set element to null here so that it is not removed nor finalized
|
|
|
|
element = null;
|
|
|
|
// continue to be set to true so that mouseMove happens
|
|
|
|
started = true;
|
2009-10-18 20:46:32 +00:00
|
|
|
var stretchy = document.getElementById("path_stretch_line");
|
2009-08-07 05:44:55 +00:00
|
|
|
if (!stretchy) {
|
|
|
|
stretchy = document.createElementNS(svgns, "line");
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(stretchy, {
|
2009-10-18 20:46:32 +00:00
|
|
|
'id': "path_stretch_line",
|
2009-08-24 20:00:48 +00:00
|
|
|
'stroke': "blue",
|
|
|
|
'stroke-width': "0.5"
|
|
|
|
});
|
2009-08-07 05:44:55 +00:00
|
|
|
stretchy = document.getElementById("selectorParentGroup").appendChild(stretchy);
|
|
|
|
}
|
|
|
|
stretchy.setAttribute("display", "inline");
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
// if pts array is empty, create path element with M at current point
|
2009-10-18 20:46:32 +00:00
|
|
|
if (current_path_pts.length == 0) {
|
|
|
|
current_path_pts.push(x);
|
|
|
|
current_path_pts.push(y);
|
2009-08-07 05:44:55 +00:00
|
|
|
d_attr = "M" + x + "," + y + " ";
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "path",
|
|
|
|
"attr": {
|
|
|
|
"d": d_attr,
|
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
// set stretchy line to first point
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(stretchy, {
|
2009-09-11 13:32:56 +00:00
|
|
|
'x1': mouse_x,
|
|
|
|
'y1': mouse_y,
|
|
|
|
'x2': mouse_x,
|
|
|
|
'y2': mouse_y
|
2009-08-24 20:00:48 +00:00
|
|
|
});
|
2009-10-18 20:46:32 +00:00
|
|
|
addPointGripToPath(mouse_x,mouse_y,0);
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// determine if we clicked on an existing point
|
2009-10-18 20:46:32 +00:00
|
|
|
var i = current_path_pts.length;
|
2009-09-15 20:50:38 +00:00
|
|
|
var FUZZ = 6/current_zoom;
|
2009-08-07 05:44:55 +00:00
|
|
|
var clickOnPoint = false;
|
|
|
|
while(i) {
|
|
|
|
i -= 2;
|
2009-10-18 20:46:32 +00:00
|
|
|
var px = current_path_pts[i], py = current_path_pts[i+1];
|
2009-08-07 05:44:55 +00:00
|
|
|
// found a matching point
|
|
|
|
if ( x >= (px-FUZZ) && x <= (px+FUZZ) && y >= (py-FUZZ) && y <= (py+FUZZ) ) {
|
|
|
|
clickOnPoint = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-09-10 20:34:20 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
// get path element that we are in the process of creating
|
|
|
|
var path = svgdoc.getElementById(getId());
|
|
|
|
var len = current_path_pts.length;
|
|
|
|
// if we clicked on an existing point, then we are done this path, commit it
|
2009-08-07 05:44:55 +00:00
|
|
|
// (i,i+1) are the x,y that were clicked on
|
|
|
|
if (clickOnPoint) {
|
|
|
|
// if clicked on any other point but the first OR
|
|
|
|
// the first point was clicked on and there are less than 3 points
|
2009-10-18 20:46:32 +00:00
|
|
|
// then leave the path open
|
|
|
|
// otherwise, close the path
|
2009-10-02 15:05:31 +00:00
|
|
|
if (i == 0 && len >= 6) {
|
|
|
|
// Create end segment
|
2009-10-18 20:46:32 +00:00
|
|
|
var abs_x = current_path_pts[0];
|
|
|
|
var abs_y = current_path_pts[1];
|
2009-10-09 19:47:32 +00:00
|
|
|
d_attr += ['L',abs_x,',',abs_y,'z'].join('');
|
2009-10-18 20:46:32 +00:00
|
|
|
path.setAttribute("d", d_attr);
|
2009-10-02 15:05:31 +00:00
|
|
|
} else if(len < 3) {
|
2009-09-24 16:05:23 +00:00
|
|
|
keep = false;
|
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
2009-10-18 20:46:32 +00:00
|
|
|
removeAllPointGripsFromPath();
|
|
|
|
// this will signal to commit the path
|
|
|
|
element = path;
|
|
|
|
current_path_pts = [];
|
2009-08-07 05:44:55 +00:00
|
|
|
started = false;
|
|
|
|
}
|
|
|
|
// else, create a new point, append to pts array, update path element
|
|
|
|
else {
|
2009-10-18 20:46:32 +00:00
|
|
|
var lastx = current_path_pts[len-2], lasty = current_path_pts[len-1];
|
|
|
|
// we store absolute values in our path points array for easy checking above
|
|
|
|
current_path_pts.push(x);
|
|
|
|
current_path_pts.push(y);
|
2009-10-09 19:47:32 +00:00
|
|
|
d_attr += "L" + round(x) + "," + round(y) + " ";
|
2009-10-18 20:46:32 +00:00
|
|
|
path.setAttribute("d", d_attr);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
// set stretchy line to latest point
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(stretchy, {
|
2009-09-11 13:32:56 +00:00
|
|
|
'x1': mouse_x,
|
|
|
|
'y1': mouse_y,
|
|
|
|
'x2': mouse_x,
|
|
|
|
'y2': mouse_y
|
2009-08-24 20:00:48 +00:00
|
|
|
});
|
2009-10-18 20:46:32 +00:00
|
|
|
addPointGripToPath(mouse_x,mouse_y,(current_path_pts.length/2 - 1));
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
|
|
|
keep = true;
|
|
|
|
}
|
|
|
|
break;
|
2009-10-17 20:29:56 +00:00
|
|
|
case "pathedit":
|
2009-08-08 14:03:14 +00:00
|
|
|
keep = true;
|
|
|
|
element = null;
|
2009-10-18 20:46:32 +00:00
|
|
|
// if we were dragging a path point, stop it now
|
|
|
|
if (current_path_pt_drag != -1) {
|
|
|
|
current_path_pt_drag = -1;
|
2009-08-28 23:12:46 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var batchCmd = new BatchCommand("Edit Path");
|
2009-08-29 22:10:52 +00:00
|
|
|
// the attribute changes we want to undo
|
|
|
|
var oldvalues = {};
|
2009-10-18 20:46:32 +00:00
|
|
|
oldvalues["d"] = current_path_oldd;
|
2009-08-29 22:10:52 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
// If the path was rotated, we must now pay the piper:
|
|
|
|
// Every path point must be rotated into the rotated coordinate system of
|
2009-08-28 23:12:46 +00:00
|
|
|
// its old center, then determine the new center, then rotate it back
|
2009-10-18 20:46:32 +00:00
|
|
|
// This is because we want the path to remember its rotation
|
|
|
|
var angle = canvas.getRotationAngle(current_path) * Math.PI / 180.0;
|
2009-10-12 14:29:21 +00:00
|
|
|
|
2009-08-28 23:12:46 +00:00
|
|
|
if (angle) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var box = canvas.getBBox(current_path);
|
2009-08-28 23:12:46 +00:00
|
|
|
var oldbox = selectedBBoxes[0];
|
2009-10-12 14:29:21 +00:00
|
|
|
var oldcx = oldbox.x + oldbox.width/2,
|
|
|
|
oldcy = oldbox.y + oldbox.height/2,
|
|
|
|
newcx = box.x + box.width/2,
|
|
|
|
newcy = box.y + box.height/2;
|
2009-08-28 23:12:46 +00:00
|
|
|
|
2009-08-29 18:38:34 +00:00
|
|
|
// un-rotate the new center to the proper position
|
|
|
|
var dx = newcx - oldcx,
|
|
|
|
dy = newcy - oldcy;
|
|
|
|
var r = Math.sqrt(dx*dx + dy*dy);
|
|
|
|
var theta = Math.atan2(dy,dx) + angle;
|
2009-10-12 14:29:21 +00:00
|
|
|
newcx = r * Math.cos(theta) + oldcx;
|
|
|
|
newcy = r * Math.sin(theta) + oldcy;
|
2009-08-29 18:38:34 +00:00
|
|
|
|
2009-10-12 14:29:21 +00:00
|
|
|
var getRotVals = function(x, y) {
|
|
|
|
dx = x - oldcx;
|
|
|
|
dy = y - oldcy;
|
2009-08-28 23:12:46 +00:00
|
|
|
|
|
|
|
// rotate the point around the old center
|
2009-08-29 18:38:34 +00:00
|
|
|
r = Math.sqrt(dx*dx + dy*dy);
|
|
|
|
theta = Math.atan2(dy,dx) + angle;
|
2009-10-12 14:29:21 +00:00
|
|
|
dx = r * Math.cos(theta) + oldcx;
|
|
|
|
dy = r * Math.sin(theta) + oldcy;
|
2009-08-28 23:12:46 +00:00
|
|
|
|
|
|
|
// dx,dy should now hold the actual coordinates of each
|
|
|
|
// point after being rotated
|
2009-08-29 18:38:34 +00:00
|
|
|
|
2009-08-28 23:12:46 +00:00
|
|
|
// now we want to rotate them around the new center in the reverse direction
|
|
|
|
dx -= newcx;
|
|
|
|
dy -= newcy;
|
|
|
|
|
|
|
|
r = Math.sqrt(dx*dx + dy*dy);
|
|
|
|
theta = Math.atan2(dy,dx) - angle;
|
|
|
|
|
2009-10-12 14:29:21 +00:00
|
|
|
return {'x':(r * Math.cos(theta) + newcx)/1,
|
|
|
|
'y':(r * Math.sin(theta) + newcy)/1};
|
2009-08-28 23:12:46 +00:00
|
|
|
}
|
2009-10-12 14:29:21 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var list = current_path.pathSegList;
|
2009-10-12 14:29:21 +00:00
|
|
|
var i = list.numberOfItems;
|
|
|
|
while (i) {
|
|
|
|
i -= 1;
|
|
|
|
var seg = list.getItem(i);
|
|
|
|
var type = seg.pathSegType;
|
|
|
|
if(type == 1) continue;
|
|
|
|
|
|
|
|
var rvals = getRotVals(seg.x,seg.y);
|
|
|
|
var points = [rvals.x, rvals.y];
|
|
|
|
if(seg.x1 != null && seg.x2 != null) {
|
|
|
|
c_vals1 = getRotVals(seg.x1, seg.y1);
|
|
|
|
c_vals2 = getRotVals(seg.x2, seg.y2);
|
|
|
|
points.splice(points.length, 0, c_vals1.x , c_vals1.y, c_vals2.x, c_vals2.y);
|
|
|
|
}
|
|
|
|
replacePathSeg(type, i, points);
|
|
|
|
} // loop for each point
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
box = canvas.getBBox(current_path);
|
2009-08-29 18:38:34 +00:00
|
|
|
selectedBBoxes[0].x = box.x; selectedBBoxes[0].y = box.y;
|
|
|
|
selectedBBoxes[0].width = box.width; selectedBBoxes[0].height = box.height;
|
2009-08-28 23:12:46 +00:00
|
|
|
|
|
|
|
// now we must set the new transform to be rotated around the new center
|
2009-08-29 18:38:34 +00:00
|
|
|
var rotate = "rotate(" + (angle * 180.0 / Math.PI) + " " + newcx + "," + newcy + ")";
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path.setAttribute("transform", rotate);
|
2009-10-12 14:29:21 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
if(document.getElementById("pathpointgrip_container")) {
|
2009-09-12 04:49:50 +00:00
|
|
|
var pcx = newcx * current_zoom,
|
|
|
|
pcy = newcy * current_zoom;
|
|
|
|
var xform = ["rotate(", (angle*180.0/Math.PI), " ", pcx, ",", pcy, ")"].join("");
|
2009-10-09 15:29:40 +00:00
|
|
|
setPointContainerTransform(xform);
|
2009-08-29 18:38:34 +00:00
|
|
|
}
|
2009-10-12 14:29:21 +00:00
|
|
|
resetPointGrips();
|
2009-10-13 14:52:51 +00:00
|
|
|
updateSegLine(true);
|
2009-10-12 14:29:21 +00:00
|
|
|
|
2009-08-29 18:38:34 +00:00
|
|
|
} // if rotated
|
2009-08-29 22:10:52 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(current_path, oldvalues, "path points"));
|
2009-08-29 22:10:52 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-10-18 20:46:32 +00:00
|
|
|
call("changed", [current_path]);
|
2009-08-29 22:10:52 +00:00
|
|
|
|
2009-10-02 15:05:31 +00:00
|
|
|
// If connected, last point should equal first
|
2009-10-18 20:46:32 +00:00
|
|
|
if(current_path.getAttribute('d').toLowerCase().indexOf('z') != -1) {
|
|
|
|
current_path_pts[current_path_pts.length-2] = getPathPoint(0,true)[0];
|
|
|
|
current_path_pts[current_path_pts.length-1] = getPathPoint(0,true)[1];
|
2009-10-07 18:56:03 +00:00
|
|
|
}
|
2009-10-13 14:52:51 +00:00
|
|
|
updateSegLine();
|
2009-10-02 15:05:31 +00:00
|
|
|
|
2009-08-29 22:10:52 +00:00
|
|
|
// make these changes undo-able
|
2009-10-18 20:46:32 +00:00
|
|
|
} // if (current_path_pt_drag != -1)
|
2009-09-25 17:59:32 +00:00
|
|
|
else if(current_ctrl_pt_drag != -1) {
|
|
|
|
current_ctrl_pt_drag = -1;
|
2009-10-18 20:46:32 +00:00
|
|
|
var batchCmd = new BatchCommand("Edit Path control points");
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(current_path, {d:current_path_oldd}));
|
2009-09-29 19:21:10 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-10-18 20:46:32 +00:00
|
|
|
call("changed", [current_path]);
|
2009-10-02 15:05:31 +00:00
|
|
|
} // else, move back to select mode
|
2009-08-08 22:17:59 +00:00
|
|
|
else {
|
|
|
|
current_mode = "select";
|
2009-10-18 20:46:32 +00:00
|
|
|
removeAllPointGripsFromPath();
|
2009-08-21 12:21:43 +00:00
|
|
|
canvas.clearSelection();
|
2009-08-08 22:17:59 +00:00
|
|
|
canvas.addToSelection([evt.target]);
|
|
|
|
}
|
2009-08-08 14:03:14 +00:00
|
|
|
break;
|
2009-08-26 19:45:28 +00:00
|
|
|
case "rotate":
|
|
|
|
keep = true;
|
|
|
|
element = null;
|
|
|
|
current_mode = "select";
|
2009-09-04 05:10:48 +00:00
|
|
|
var batchCmd = canvas.finishUndoableChange();
|
|
|
|
if (!batchCmd.isEmpty()) {
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
}
|
2009-08-26 19:45:28 +00:00
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
default:
|
|
|
|
console.log("Unknown mode in mouseup: " + current_mode);
|
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
}
|
2009-06-18 05:08:47 +00:00
|
|
|
if (!keep && element != null) {
|
2009-06-10 03:12:19 +00:00
|
|
|
element.parentNode.removeChild(element);
|
|
|
|
element = null;
|
2009-10-16 20:07:15 +00:00
|
|
|
|
|
|
|
var t = evt.target;
|
|
|
|
// if this element is in a group, go up until we reach the top-level group
|
|
|
|
// just below the layer groups
|
|
|
|
// TODO: once we implement links, we also would have to check for <a> elements
|
|
|
|
while (t.parentNode.parentNode.tagName == "g") {
|
|
|
|
t = t.parentNode;
|
|
|
|
}
|
|
|
|
// if we are not in the middle of creating a path, and we've clicked on some shape,
|
|
|
|
// then go to Select mode.
|
|
|
|
// WebKit returns <div> when the canvas is clicked, Firefox/Opera return <svg>
|
2009-10-18 20:46:32 +00:00
|
|
|
if ( (current_mode != "path" || current_path_pts.length == 0) &&
|
2009-10-16 20:07:15 +00:00
|
|
|
t.parentNode.id != "selectorParentGroup" &&
|
|
|
|
t.id != "svgcanvas" && t.id != "svgroot")
|
|
|
|
{
|
|
|
|
// switch into "select" mode if we've clicked on an element
|
|
|
|
canvas.addToSelection([t], true);
|
|
|
|
canvas.setMode("select");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
} else if (element != null) {
|
2009-08-21 13:39:23 +00:00
|
|
|
canvas.addedNew = true;
|
2009-08-31 14:54:59 +00:00
|
|
|
element.setAttribute("opacity", cur_shape.opacity);
|
2009-06-10 03:12:19 +00:00
|
|
|
cleanupElement(element);
|
2009-06-30 17:50:08 +00:00
|
|
|
selectorManager.update();
|
2009-10-18 20:46:32 +00:00
|
|
|
if(current_mode == "path") {
|
|
|
|
current_path = element;
|
2009-10-17 20:29:56 +00:00
|
|
|
current_mode = "pathedit";
|
2009-10-18 20:46:32 +00:00
|
|
|
recalcPathPoints();
|
|
|
|
addAllPointGripsToPath(current_path_pts.length/2 - 1);
|
2009-10-16 18:01:05 +00:00
|
|
|
} else if (current_mode == "text") {
|
|
|
|
// keep us in the tool we were in unless it was a text element
|
2009-10-09 15:29:40 +00:00
|
|
|
canvas.addToSelection([element], true);
|
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
// we create the insert command that is stored on the stack
|
|
|
|
// undo means to call cmd.unapply(), redo means to call cmd.apply()
|
|
|
|
addCommandToHistory(new InsertElementCommand(element));
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed",[element]);
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
|
|
|
// public functions
|
|
|
|
|
2009-10-13 02:48:27 +00:00
|
|
|
// Group: Serialization
|
|
|
|
|
|
|
|
// Function: open
|
|
|
|
// Calls the 'opened' handler and sends the SVG XML text. Clients of the SvgCanvas bind
|
|
|
|
// their load function (typically calls to setSvgString() to the 'opened' event.
|
2009-08-25 16:35:57 +00:00
|
|
|
this.open = function(str) {
|
2009-10-13 02:48:27 +00:00
|
|
|
// Nothing by default, handled by optional widget/extension
|
2009-08-25 16:35:57 +00:00
|
|
|
call("opened", str);
|
2009-08-19 03:42:27 +00:00
|
|
|
};
|
|
|
|
|
2009-10-09 19:07:52 +00:00
|
|
|
// Function: save
|
|
|
|
// Serializes the current drawing into SVG XML text and returns it to the 'saved' handler.
|
2009-10-13 02:48:27 +00:00
|
|
|
// This function also includes the XML prolog. Clients of the SvgCanvas bind their save
|
|
|
|
// function to the 'saved' event.
|
2009-10-09 19:07:52 +00:00
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// Nothing
|
2009-06-06 23:54:53 +00:00
|
|
|
this.save = function() {
|
2009-06-07 05:07:23 +00:00
|
|
|
// remove the selected outline before serializing
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection();
|
2009-08-19 17:08:05 +00:00
|
|
|
|
2009-06-16 02:52:36 +00:00
|
|
|
var str = "<?xml version=\"1.0\" standalone=\"no\"?>\n";
|
2009-07-08 00:03:08 +00:00
|
|
|
// no need for doctype, see http://jwatt.org/svg/authoring/#doctype-declaration
|
2009-09-09 14:18:24 +00:00
|
|
|
str += svgCanvasToString();
|
2009-08-17 21:55:47 +00:00
|
|
|
call("saved", str);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-21 22:16:44 +00:00
|
|
|
var walkTree = function(elem, cbFn){
|
|
|
|
if (elem && elem.nodeType == 1) {
|
|
|
|
cbFn(elem);
|
|
|
|
var i = elem.childNodes.length;
|
|
|
|
while (i--) {
|
|
|
|
walkTree(elem.childNodes.item(i), cbFn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-10-09 19:07:52 +00:00
|
|
|
// Function: getSvgString
|
|
|
|
// Returns the current drawing as raw SVG XML text.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// The current drawing as raw SVG XML text.
|
2009-07-10 05:04:06 +00:00
|
|
|
this.getSvgString = function() {
|
2009-09-09 14:18:24 +00:00
|
|
|
return svgCanvasToString();
|
2009-07-10 05:04:06 +00:00
|
|
|
};
|
|
|
|
|
2009-10-09 19:07:52 +00:00
|
|
|
// Function: setSvgString
|
|
|
|
// This function sets the current drawing as the input SVG XML.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// xmlString - The SVG as XML text.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// This function returns false if the set was unsuccessful, true otherwise.
|
2009-07-10 05:04:06 +00:00
|
|
|
this.setSvgString = function(xmlString) {
|
|
|
|
try {
|
|
|
|
// convert string into XML document
|
|
|
|
var newDoc = Utils.text2xml(xmlString);
|
|
|
|
|
|
|
|
// run it through our sanitizer to remove anything we do not support
|
|
|
|
sanitizeSvg(newDoc.documentElement);
|
|
|
|
|
|
|
|
var batchCmd = new BatchCommand("Change Source");
|
|
|
|
|
2009-09-09 16:43:09 +00:00
|
|
|
// remove old svg document
|
2009-10-21 15:12:32 +00:00
|
|
|
var oldzoom = svgroot.removeChild(svgcontent);
|
2009-09-09 16:43:09 +00:00
|
|
|
batchCmd.addSubCommand(new RemoveElementCommand(oldzoom, svgroot));
|
2009-07-10 05:04:06 +00:00
|
|
|
|
2009-09-09 16:43:09 +00:00
|
|
|
// set new svg document
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent = svgroot.appendChild(svgdoc.importNode(newDoc.documentElement, true));
|
|
|
|
svgcontent.setAttribute('id', 'svgcontent');
|
2009-09-09 17:41:28 +00:00
|
|
|
// determine proper size
|
2009-09-09 18:15:28 +00:00
|
|
|
var w, h;
|
2009-10-21 15:12:32 +00:00
|
|
|
if (svgcontent.getAttribute("viewBox")) {
|
|
|
|
var vb = svgcontent.getAttribute("viewBox").split(' ');
|
2009-09-09 18:15:28 +00:00
|
|
|
w = vb[2];
|
|
|
|
h = vb[3];
|
|
|
|
}
|
|
|
|
// handle old content that doesn't have a viewBox
|
|
|
|
else {
|
2009-10-21 15:12:32 +00:00
|
|
|
w = svgcontent.getAttribute("width");
|
|
|
|
h = svgcontent.getAttribute("height");
|
|
|
|
svgcontent.setAttribute("viewBox", ["0", "0", w, h].join(" "));
|
2009-09-09 18:15:28 +00:00
|
|
|
}
|
2009-09-09 17:41:28 +00:00
|
|
|
// just to be safe, remove any width/height from text so that they are 100%/100%
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent.removeAttribute('width');
|
|
|
|
svgcontent.removeAttribute('height');
|
|
|
|
batchCmd.addSubCommand(new InsertElementCommand(svgcontent));
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-09 17:41:28 +00:00
|
|
|
// update root to the correct size
|
|
|
|
var changes = {};
|
|
|
|
changes['width'] = svgroot.getAttribute('width');
|
|
|
|
changes['height'] = svgroot.getAttribute('height');
|
|
|
|
svgroot.setAttribute('width', w);
|
|
|
|
svgroot.setAttribute('height', h);
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(svgroot, changes));
|
|
|
|
|
|
|
|
// reset zoom
|
|
|
|
current_zoom = 1;
|
2009-09-21 02:05:58 +00:00
|
|
|
|
|
|
|
// identify layers
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-21 22:16:44 +00:00
|
|
|
|
2009-09-09 17:41:28 +00:00
|
|
|
selectorManager.update();
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-10-21 15:12:32 +00:00
|
|
|
call("changed", [svgcontent]);
|
2009-07-10 05:04:06 +00:00
|
|
|
} catch(e) {
|
|
|
|
console.log(e);
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
return true;
|
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-09-22 02:42:31 +00:00
|
|
|
// Layer API Functions
|
2009-10-13 02:48:27 +00:00
|
|
|
|
|
|
|
// Group: Layers
|
|
|
|
|
2009-10-17 20:29:56 +00:00
|
|
|
var identifyLayers = function() {
|
2009-09-22 01:00:24 +00:00
|
|
|
all_layers = [];
|
2009-10-21 15:12:32 +00:00
|
|
|
var numchildren = svgcontent.childNodes.length;
|
|
|
|
// loop through all children of svgcontent
|
2009-09-24 03:13:25 +00:00
|
|
|
var orphans = [], layernames = [];
|
2009-09-22 01:00:24 +00:00
|
|
|
for (var i = 0; i < numchildren; ++i) {
|
2009-10-21 15:12:32 +00:00
|
|
|
var child = svgcontent.childNodes.item(i);
|
2009-09-22 01:00:24 +00:00
|
|
|
// for each g, find its layer name
|
2009-09-24 03:14:43 +00:00
|
|
|
if (child && child.nodeType == 1) {
|
2009-09-24 03:13:25 +00:00
|
|
|
if (child.tagName == "g") {
|
2009-10-17 20:29:56 +00:00
|
|
|
var name = $("title",child).text();
|
2009-09-24 03:13:25 +00:00
|
|
|
// store layer and name in global variable
|
|
|
|
if (name) {
|
|
|
|
layernames.push(name);
|
|
|
|
all_layers.push( [name,child] );
|
|
|
|
current_layer = child;
|
2009-10-13 00:48:05 +00:00
|
|
|
walkTree(child, function(e){e.setAttribute("style", "pointer-events:inherit");});
|
|
|
|
current_layer.setAttribute("style", "pointer-events:none");
|
2009-09-24 03:13:25 +00:00
|
|
|
}
|
2009-09-29 18:00:14 +00:00
|
|
|
// if group did not have a name, it is an orphan
|
|
|
|
else {
|
|
|
|
orphans.push(child);
|
|
|
|
}
|
2009-09-24 03:13:25 +00:00
|
|
|
}
|
2009-10-09 02:14:47 +00:00
|
|
|
// if child has a bbox (i.e. not a <title> or <defs> element), then it is an orphan
|
|
|
|
else if(canvas.getBBox(child)) {
|
2009-09-24 03:13:25 +00:00
|
|
|
orphans.push(child);
|
2009-09-22 01:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-24 03:13:25 +00:00
|
|
|
// create a new layer and add all the orphans to it
|
|
|
|
if (orphans.length > 0) {
|
|
|
|
var i = 1;
|
|
|
|
while ($.inArray(("Layer " + i), layernames) != -1) { i++; }
|
|
|
|
var newname = "Layer " + i;
|
|
|
|
current_layer = svgdoc.createElementNS(svgns, "g");
|
|
|
|
var layer_title = svgdoc.createElementNS(svgns, "title");
|
|
|
|
layer_title.textContent = newname;
|
|
|
|
current_layer.appendChild(layer_title);
|
|
|
|
for (var j = 0; j < orphans.length; ++j) {
|
|
|
|
current_layer.appendChild(orphans[j]);
|
|
|
|
}
|
2009-10-21 15:12:32 +00:00
|
|
|
current_layer = svgcontent.appendChild(current_layer);
|
2009-09-24 03:13:25 +00:00
|
|
|
all_layers.push( [newname, current_layer] );
|
|
|
|
}
|
2009-10-11 04:32:21 +00:00
|
|
|
walkTree(current_layer, function(e){e.setAttribute("style","pointer-events:inherit");});
|
|
|
|
current_layer.setAttribute("style","pointer-events:all");
|
2009-09-22 01:00:24 +00:00
|
|
|
};
|
|
|
|
|
2009-10-09 19:07:52 +00:00
|
|
|
// Function: createLayer
|
2009-10-13 02:35:36 +00:00
|
|
|
// Creates a new top-level layer in the drawing with the given name, sets the current layer
|
|
|
|
// to it, and then clears the selection This function then calls the 'changed' handler.
|
|
|
|
// This is an undoable action.
|
2009-10-09 19:07:52 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// name - The given name
|
2009-09-22 01:00:24 +00:00
|
|
|
this.createLayer = function(name) {
|
|
|
|
var batchCmd = new BatchCommand("Create Layer");
|
2009-09-22 18:10:54 +00:00
|
|
|
var new_layer = svgdoc.createElementNS(svgns, "g");
|
2009-09-22 01:00:24 +00:00
|
|
|
var layer_title = svgdoc.createElementNS(svgns, "title");
|
2009-09-22 18:10:54 +00:00
|
|
|
layer_title.textContent = name;
|
|
|
|
new_layer.appendChild(layer_title);
|
2009-10-21 15:12:32 +00:00
|
|
|
new_layer = svgcontent.appendChild(new_layer);
|
2009-09-22 18:10:54 +00:00
|
|
|
batchCmd.addSubCommand(new InsertElementCommand(new_layer));
|
2009-09-22 01:00:24 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-09-22 02:42:31 +00:00
|
|
|
canvas.clearSelection();
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 18:10:54 +00:00
|
|
|
canvas.setCurrentLayer(name);
|
|
|
|
call("changed", [new_layer]);
|
2009-09-22 01:00:24 +00:00
|
|
|
};
|
|
|
|
|
2009-10-09 19:07:52 +00:00
|
|
|
// Function: deleteCurrentLayer
|
2009-10-13 02:35:36 +00:00
|
|
|
// Deletes the current layer from the drawing and then clears the selection. This function
|
|
|
|
// then calls the 'changed' handler. This is an undoable action.
|
2009-09-22 01:00:24 +00:00
|
|
|
this.deleteCurrentLayer = function() {
|
|
|
|
if (current_layer && all_layers.length > 1) {
|
|
|
|
var batchCmd = new BatchCommand("Delete Layer");
|
2009-09-22 18:10:54 +00:00
|
|
|
// actually delete from the DOM and store in our Undo History
|
|
|
|
var parent = current_layer.parentNode;
|
|
|
|
batchCmd.addSubCommand(new RemoveElementCommand(current_layer, parent));
|
|
|
|
parent.removeChild(current_layer);
|
2009-09-22 01:00:24 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-09-22 02:42:31 +00:00
|
|
|
canvas.clearSelection();
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 18:10:54 +00:00
|
|
|
canvas.setCurrentLayer(all_layers[all_layers.length-1][0]);
|
2009-10-21 15:12:32 +00:00
|
|
|
call("changed", [svgcontent]);
|
2009-09-22 01:00:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2009-10-09 19:07:52 +00:00
|
|
|
// Function: getNumLayers
|
2009-10-13 02:35:36 +00:00
|
|
|
// Returns the number of layers in the current drawing.
|
2009-10-09 19:07:52 +00:00
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// The number of layers in the current drawing.
|
2009-09-21 22:16:44 +00:00
|
|
|
this.getNumLayers = function() {
|
|
|
|
return all_layers.length;
|
|
|
|
};
|
2009-10-09 19:07:52 +00:00
|
|
|
|
2009-10-10 21:46:06 +00:00
|
|
|
// Function: getLayer
|
2009-10-13 02:35:36 +00:00
|
|
|
// Returns the name of the ith layer. If the index is out of range, an empty string is returned.
|
2009-10-10 21:46:06 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// i - the zero-based index of the layer you are querying.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// The name of the ith layer
|
2009-09-21 22:16:44 +00:00
|
|
|
this.getLayer = function(i) {
|
|
|
|
if (i >= 0 && i < canvas.getNumLayers()) {
|
|
|
|
return all_layers[i][0];
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
};
|
2009-09-22 04:44:56 +00:00
|
|
|
|
2009-10-10 21:46:06 +00:00
|
|
|
// Function: getCurrentLayer
|
2009-10-13 02:35:36 +00:00
|
|
|
// Returns the name of the currently selected layer. If an error occurs, an empty string
|
|
|
|
// is returned.
|
2009-10-10 21:46:06 +00:00
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// The name of the currently active layer.
|
2009-09-21 22:16:44 +00:00
|
|
|
this.getCurrentLayer = function() {
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][1] == current_layer) {
|
|
|
|
return all_layers[i][0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
};
|
2009-09-22 04:44:56 +00:00
|
|
|
|
2009-10-10 21:46:06 +00:00
|
|
|
// Function: setCurrentLayer
|
2009-10-13 02:35:36 +00:00
|
|
|
// Sets the current layer. If the name is not a valid layer name, then this function returns
|
|
|
|
// false. Otherwise it returns true. This is not an undo-able action.
|
2009-10-10 21:46:06 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// name - the name of the layer you want to switch to.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// true if the current layer was switched, otherwise false
|
2009-09-21 22:16:44 +00:00
|
|
|
this.setCurrentLayer = function(name) {
|
2009-10-05 08:16:41 +00:00
|
|
|
name = toXml(name);
|
2009-09-21 22:16:44 +00:00
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (name == all_layers[i][0]) {
|
|
|
|
if (current_layer != all_layers[i][1]) {
|
2009-09-22 01:00:24 +00:00
|
|
|
canvas.clearSelection();
|
2009-10-11 04:32:21 +00:00
|
|
|
current_layer.setAttribute("style", "pointer-events:none");
|
2009-09-21 22:16:44 +00:00
|
|
|
current_layer = all_layers[i][1];
|
2009-10-11 04:32:21 +00:00
|
|
|
current_layer.setAttribute("style", "pointer-events:all");
|
2009-09-21 22:16:44 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2009-09-22 04:44:56 +00:00
|
|
|
|
2009-10-10 21:46:06 +00:00
|
|
|
// Function: renameCurrentLayer
|
2009-10-13 02:35:36 +00:00
|
|
|
// Renames the current layer. If the layer name is not valid (i.e. unique), then this function
|
|
|
|
// does nothing and returns false, otherwise it returns true. This is an undo-able action.
|
2009-10-10 21:46:06 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// newname - the new name you want to give the current layer. This name must be unique
|
|
|
|
// among all layer names.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// true if the rename succeeded, false otherwise.
|
2009-09-23 03:05:27 +00:00
|
|
|
this.renameCurrentLayer = function(newname) {
|
|
|
|
if (current_layer) {
|
2009-09-21 22:16:44 +00:00
|
|
|
var oldLayer = current_layer;
|
|
|
|
// setCurrentLayer will return false if the name doesn't already exists
|
|
|
|
if (!canvas.setCurrentLayer(newname)) {
|
2009-09-22 02:42:31 +00:00
|
|
|
var batchCmd = new BatchCommand("Rename Layer");
|
2009-09-21 22:16:44 +00:00
|
|
|
// find the index of the layer
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][1] == oldLayer) break;
|
|
|
|
}
|
2009-09-23 03:05:27 +00:00
|
|
|
var oldname = all_layers[i][0];
|
2009-10-05 08:16:41 +00:00
|
|
|
all_layers[i][0] = toXml(newname);
|
2009-09-21 22:16:44 +00:00
|
|
|
|
|
|
|
// now change the underlying title element contents
|
|
|
|
var len = oldLayer.childNodes.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var child = oldLayer.childNodes.item(i);
|
|
|
|
// found the <title> element, now append all the
|
|
|
|
if (child && child.tagName == "title") {
|
|
|
|
// wipe out old name
|
|
|
|
while (child.firstChild) { child.removeChild(child.firstChild); }
|
2009-09-22 02:42:31 +00:00
|
|
|
child.textContent = newname;
|
|
|
|
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(child, {"#text":oldname}));
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
call("changed", [oldLayer]);
|
2009-09-21 22:16:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-23 03:05:27 +00:00
|
|
|
current_layer = oldLayer;
|
2009-09-21 22:16:44 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2009-09-22 04:44:56 +00:00
|
|
|
|
2009-10-10 21:46:06 +00:00
|
|
|
// Function: setCurrentLayerPosition
|
2009-10-13 02:35:36 +00:00
|
|
|
// Changes the position of the current layer to the new value. If the new index is not valid,
|
|
|
|
// this function does nothing and returns false, otherwise it returns true. This is an
|
|
|
|
// undo-able action.
|
2009-10-10 21:46:06 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// newpos - The zero-based index of the new position of the layer. This should be between
|
|
|
|
// 0 and (number of layers - 1)
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// true if the current layer position was changed, false otherwise.
|
2009-09-22 04:44:56 +00:00
|
|
|
this.setCurrentLayerPosition = function(newpos) {
|
|
|
|
if (current_layer && newpos >= 0 && newpos < all_layers.length) {
|
|
|
|
for (var oldpos = 0; oldpos < all_layers.length; ++oldpos) {
|
|
|
|
if (all_layers[oldpos][1] == current_layer) break;
|
|
|
|
}
|
|
|
|
// some unknown error condition (current_layer not in all_layers)
|
|
|
|
if (oldpos == all_layers.length) { return false; }
|
|
|
|
|
|
|
|
if (oldpos != newpos) {
|
|
|
|
// if our new position is below us, we need to insert before the node after newpos
|
|
|
|
var refLayer = null;
|
|
|
|
var oldNextSibling = current_layer.nextSibling;
|
|
|
|
if (newpos > oldpos ) {
|
|
|
|
if (newpos < all_layers.length-1) {
|
|
|
|
refLayer = all_layers[newpos+1][1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if our new position is above us, we need to insert before the node at newpos
|
|
|
|
else {
|
|
|
|
refLayer = all_layers[newpos][1];
|
|
|
|
}
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent.insertBefore(current_layer, refLayer);
|
|
|
|
addCommandToHistory(new MoveElementCommand(current_layer, oldNextSibling, svgcontent));
|
2009-09-22 04:44:56 +00:00
|
|
|
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 18:10:54 +00:00
|
|
|
canvas.setCurrentLayer(all_layers[newpos][0]);
|
2009-09-22 04:44:56 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2009-10-10 21:46:06 +00:00
|
|
|
// Function: getLayerVisibility
|
2009-10-13 02:35:36 +00:00
|
|
|
// Returns whether the layer is visible. If the layer name is not valid, then this function
|
|
|
|
// returns false.
|
2009-10-10 21:46:06 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// layername - the name of the layer which you want to query.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// The visibility state of the layer, or false if the layer name was invalid.
|
2009-09-24 04:48:40 +00:00
|
|
|
this.getLayerVisibility = function(layername) {
|
|
|
|
// find the layer
|
|
|
|
var layer = null;
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][0] == layername) {
|
|
|
|
layer = all_layers[i][1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!layer) return false;
|
|
|
|
return (layer.getAttribute("display") != "none");
|
|
|
|
};
|
|
|
|
|
2009-10-10 21:46:06 +00:00
|
|
|
// Function: setLayerVisibility
|
2009-10-13 02:35:36 +00:00
|
|
|
// Sets the visibility of the layer. If the layer name is not valid, this function return
|
|
|
|
// false, otherwise it returns true. This is an undo-able action.
|
2009-10-10 21:46:06 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// layername - the name of the layer to change the visibility
|
|
|
|
// bVisible - true/false, whether the layer should be visible
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// true if the layer's visibility was set, false otherwise
|
2009-09-24 04:48:40 +00:00
|
|
|
this.setLayerVisibility = function(layername, bVisible) {
|
|
|
|
// find the layer
|
|
|
|
var layer = null;
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][0] == layername) {
|
|
|
|
layer = all_layers[i][1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!layer) return false;
|
|
|
|
|
|
|
|
var oldDisplay = layer.getAttribute("display");
|
|
|
|
if (!oldDisplay) oldDisplay = "inline";
|
|
|
|
layer.setAttribute("display", bVisible ? "inline" : "none");
|
|
|
|
addCommandToHistory(new ChangeElementCommand(layer, {"display":oldDisplay}, "Layer Visibility"));
|
|
|
|
|
|
|
|
if (layer == current_layer) {
|
|
|
|
canvas.clearSelection();
|
|
|
|
}
|
|
|
|
// call("changed", [selected]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2009-10-10 21:46:06 +00:00
|
|
|
// Function: moveSelectedToLayer
|
2009-10-13 02:35:36 +00:00
|
|
|
// Moves the selected elements to layername. If the name is not a valid layer name, then false
|
|
|
|
// is returned. Otherwise it returns true. This is an undo-able action.
|
2009-10-10 21:46:06 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// layername - the name of the layer you want to which you want to move the selected elements
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// true if the selected elements were moved to the layer, false otherwise.
|
|
|
|
this.moveSelectedToLayer = function(layername) {
|
|
|
|
// find the layer
|
|
|
|
var layer = null;
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][0] == layername) {
|
|
|
|
layer = all_layers[i][1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!layer) return false;
|
|
|
|
|
|
|
|
var batchCmd = new BatchCommand("Move Elements to Layer");
|
|
|
|
|
|
|
|
// loop for each selected element and move it
|
|
|
|
var selElems = selectedElements;
|
|
|
|
var i = selElems.length;
|
|
|
|
while (i--) {
|
|
|
|
var elem = selElems[i];
|
|
|
|
if (!elem) continue;
|
|
|
|
var oldNextSibling = elem.nextSibling;
|
|
|
|
// TODO: this is pretty brittle!
|
|
|
|
var oldLayer = elem.parentNode;
|
|
|
|
layer.appendChild(elem);
|
|
|
|
batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldLayer));
|
|
|
|
}
|
|
|
|
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2009-10-14 05:11:51 +00:00
|
|
|
// Function: getLayerOpacity
|
|
|
|
// Returns the opacity of the given layer. If the input name is not a layer, null is returned.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// layername - name of the layer on which to get the opacity
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// The opacity value of the given layer. This will be a value between 0.0 and 1.0, or null
|
|
|
|
// if layername is not a valid layer
|
|
|
|
this.getLayerOpacity = function(layername) {
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][0] == layername) {
|
|
|
|
var g = all_layers[i][1];
|
|
|
|
var opacity = g.getAttribute("opacity");
|
|
|
|
if (!opacity) {
|
|
|
|
opacity = "1.0";
|
|
|
|
}
|
|
|
|
return parseFloat(opacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Function: setLayerOpacity
|
|
|
|
// Sets the opacity of the given layer. If the input name is not a layer, nothing happens.
|
|
|
|
// This is not an undo-able action. NOTE: this function exists solely to apply
|
|
|
|
// a highlighting/de-emphasis effect to a layer, when it is possible for a user to affect
|
|
|
|
// the opacity of a layer, we will need to allow this function to produce an undo-able action.
|
|
|
|
// If opacity is not a value between 0.0 and 1.0, then nothing happens.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// layername - name of the layer on which to set the opacity
|
|
|
|
// opacity - a float value in the range 0.0-1.0
|
|
|
|
this.setLayerOpacity = function(layername, opacity) {
|
|
|
|
if (opacity < 0.0 || opacity > 1.0) return;
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][0] == layername) {
|
|
|
|
var g = all_layers[i][1];
|
|
|
|
g.setAttribute("opacity", opacity);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2009-09-21 22:16:44 +00:00
|
|
|
|
2009-10-13 02:35:36 +00:00
|
|
|
// Function: clear
|
|
|
|
// Clears the current document. This is not an undoable action.
|
2009-06-01 21:24:30 +00:00
|
|
|
this.clear = function() {
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts = [];
|
2009-09-21 02:05:58 +00:00
|
|
|
|
2009-10-21 15:12:32 +00:00
|
|
|
// clear the svgcontent node
|
|
|
|
var nodes = svgcontent.childNodes;
|
|
|
|
var len = svgcontent.childNodes.length;
|
2009-06-01 21:24:30 +00:00
|
|
|
var i = 0;
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection();
|
2009-06-01 21:24:30 +00:00
|
|
|
for(var rep = 0; rep < len; rep++){
|
2009-06-02 16:40:33 +00:00
|
|
|
if (nodes[i].nodeType == 1) { // element node
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent.removeChild(nodes[i]);
|
2009-06-01 21:24:30 +00:00
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2009-09-21 02:05:58 +00:00
|
|
|
// create empty first layer
|
2009-09-21 22:16:44 +00:00
|
|
|
all_layers = [];
|
2009-09-22 01:00:24 +00:00
|
|
|
canvas.createLayer("Layer 1");
|
2009-09-21 02:05:58 +00:00
|
|
|
|
2009-07-02 03:54:11 +00:00
|
|
|
// clear the undo stack
|
|
|
|
resetUndoStack();
|
2009-09-04 06:36:37 +00:00
|
|
|
// reset the selector manager
|
|
|
|
selectorManager.initGroup();
|
2009-09-04 17:23:56 +00:00
|
|
|
// reset the rubber band box
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
|
|
|
|
2009-06-12 12:20:48 +00:00
|
|
|
call("cleared");
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-09-04 16:26:48 +00:00
|
|
|
|
2009-10-13 02:35:36 +00:00
|
|
|
// TODO: should this be an 'internal' function?
|
2009-10-18 20:46:32 +00:00
|
|
|
this.clearPath = function(remove) {
|
|
|
|
if(remove && current_mode == "path") {
|
2009-09-24 16:05:23 +00:00
|
|
|
var elem = svgdoc.getElementById(getId());
|
|
|
|
if(elem) elem.parentNode.removeChild(elem);
|
|
|
|
}
|
2009-10-18 20:46:32 +00:00
|
|
|
removeAllPointGripsFromPath();
|
|
|
|
current_path_pts = [];
|
|
|
|
current_path_pt = -1;
|
2009-09-04 16:26:48 +00:00
|
|
|
};
|
2009-10-05 13:50:44 +00:00
|
|
|
|
2009-10-13 02:35:36 +00:00
|
|
|
// TODO: should this be an 'internal' function?
|
2009-10-05 13:50:44 +00:00
|
|
|
this.getNodePoint = function() {
|
2009-10-18 20:46:32 +00:00
|
|
|
if(current_path_pt != -1) {
|
|
|
|
var pt = getPathPoint(current_path_pt, true);
|
|
|
|
var list = current_path.pathSegList;
|
2009-10-12 20:26:03 +00:00
|
|
|
var segtype;
|
2009-10-18 20:46:32 +00:00
|
|
|
if(list.numberOfItems > current_path_pt+1) {
|
|
|
|
segtype = list.getItem(current_path_pt+1).pathSegType;
|
2009-10-12 20:26:03 +00:00
|
|
|
} else {
|
|
|
|
segtype = false;
|
|
|
|
}
|
2009-10-09 15:29:40 +00:00
|
|
|
return {
|
|
|
|
x: pt[0],
|
|
|
|
y: pt[1],
|
|
|
|
type: segtype
|
|
|
|
}
|
2009-10-05 13:50:44 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-10-07 16:04:32 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
this.clonePathNode = function() {
|
2009-10-09 15:29:40 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var pt = current_path_pt, list = current_path.pathSegList;
|
2009-10-07 16:04:32 +00:00
|
|
|
|
|
|
|
var next_item = list.getItem(pt+1);
|
|
|
|
|
|
|
|
// Get point in between nodes
|
|
|
|
if(next_item.pathSegType % 2 == 0) { // even num, so abs
|
|
|
|
var cur_item = list.getItem(pt);
|
2009-10-09 19:47:32 +00:00
|
|
|
var new_x = (next_item.x + cur_item.x) / 2;
|
|
|
|
var new_y = (next_item.y + cur_item.y) / 2;
|
2009-10-07 16:04:32 +00:00
|
|
|
} else {
|
|
|
|
var new_x = next_item.x/2;
|
|
|
|
var new_y = next_item.y/2;
|
2009-10-05 19:09:39 +00:00
|
|
|
}
|
2009-10-07 16:04:32 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var seg = current_path.createSVGPathSegLinetoAbs(new_x, new_y);
|
2009-10-07 16:04:32 +00:00
|
|
|
list.insertItemBefore(seg, pt+1); // Webkit doesn't do this right.
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var abs_x = (getPathPoint(pt)[0] + new_x) * current_zoom;
|
|
|
|
var abs_y = (getPathPoint(pt)[1] + new_y) * current_zoom;
|
2009-10-07 16:04:32 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var last_num = current_path_pts.length/2;
|
2009-10-07 16:04:32 +00:00
|
|
|
|
|
|
|
// Add new grip
|
2009-10-18 20:46:32 +00:00
|
|
|
addPointGripToPath(abs_x, abs_y, last_num);
|
2009-10-09 15:29:40 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
// Update path_pts
|
|
|
|
current_path_pts.splice(pt*2 + 2, 0, abs_x, abs_y);
|
2009-10-07 16:04:32 +00:00
|
|
|
|
|
|
|
resetPointGrips();
|
|
|
|
this.addNodeToSelection(pt+1);
|
2009-10-09 19:47:32 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
// current_path.setAttribute("d", convertToD(current_path.pathSegList));
|
2009-10-07 16:04:32 +00:00
|
|
|
}
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
this.deletePathNode = function() {
|
|
|
|
var last_pt = current_path_pts.length/2 - 1;
|
|
|
|
var pt = current_path_pt, list = current_path.pathSegList;
|
2009-10-05 19:09:39 +00:00
|
|
|
var cur_item = list.getItem(pt);
|
|
|
|
var next_item = list.getItem(pt+1);
|
|
|
|
|
|
|
|
if(pt == 0) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var next_x = getPathPoint(1)[0];
|
|
|
|
var next_y = getPathPoint(1)[1];
|
2009-10-05 19:09:39 +00:00
|
|
|
// Make the next point be the "M" point
|
|
|
|
replacePathSeg(2, 1, [next_x, next_y]);
|
|
|
|
|
|
|
|
// Reposition last node
|
|
|
|
var last_item = list.getItem(last_pt);
|
2009-10-09 19:47:32 +00:00
|
|
|
replacePathSeg(4, last_pt, [next_x, next_y]);
|
2009-10-05 19:09:39 +00:00
|
|
|
removeControlPointGrips(last_pt - 1);
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts.splice(last_pt*2, 2, next_x, next_y);
|
|
|
|
current_path_pts.splice(0, 2);
|
2009-10-05 19:09:39 +00:00
|
|
|
} else {
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts.splice(pt*2, 2);
|
2009-10-05 19:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list.removeItem(pt);
|
|
|
|
|
2009-10-07 16:04:32 +00:00
|
|
|
resetPointGrips();
|
|
|
|
|
|
|
|
if(window.opera) { // Opera repaints incorrectly
|
2009-10-18 20:46:32 +00:00
|
|
|
var cp = $(current_path); cp.attr('d',cp.attr('d'));
|
2009-10-07 16:04:32 +00:00
|
|
|
}
|
2009-10-05 19:09:39 +00:00
|
|
|
|
|
|
|
this.addNodeToSelection(pt);
|
|
|
|
}
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
this.getResolution = function() {
|
2009-09-09 14:18:24 +00:00
|
|
|
// return [svgroot.getAttribute("width"), svgroot.getAttribute("height")];
|
2009-10-21 15:12:32 +00:00
|
|
|
var vb = svgcontent.getAttribute("viewBox").split(' ');
|
2009-09-09 14:18:24 +00:00
|
|
|
return {'w':vb[2], 'h':vb[3], 'zoom': current_zoom};
|
2009-07-10 05:04:06 +00:00
|
|
|
};
|
2009-09-22 17:22:28 +00:00
|
|
|
|
2009-10-19 19:06:07 +00:00
|
|
|
this.getImageTitle = function() {
|
2009-10-21 15:12:32 +00:00
|
|
|
var childs = svgcontent.childNodes;
|
2009-10-19 19:06:07 +00:00
|
|
|
for (var i=0; i<childs.length; i++) {
|
|
|
|
if(childs[i].nodeName == 'title') {
|
|
|
|
return childs[i].textContent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setImageTitle = function(newtitle) {
|
2009-10-21 15:12:32 +00:00
|
|
|
var childs = svgcontent.childNodes, doc_title = false, old_title = '';
|
2009-10-20 13:49:53 +00:00
|
|
|
|
|
|
|
var batchCmd = new BatchCommand("Change Image Title");
|
|
|
|
|
2009-10-19 19:06:07 +00:00
|
|
|
for (var i=0; i<childs.length; i++) {
|
|
|
|
if(childs[i].nodeName == 'title') {
|
2009-10-20 13:49:53 +00:00
|
|
|
doc_title = childs[i];
|
|
|
|
old_title = doc_title.textContent;
|
2009-10-19 19:06:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!doc_title) {
|
|
|
|
doc_title = svgdoc.createElementNS(svgns, "title");
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent.insertBefore(doc_title, svgcontent.firstChild);
|
2009-10-19 19:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(newtitle.length) {
|
|
|
|
doc_title.textContent = newtitle;
|
|
|
|
} else {
|
|
|
|
// No title given, so element is not necessary
|
|
|
|
doc_title.parentNode.removeChild(doc_title);
|
|
|
|
}
|
2009-10-20 13:49:53 +00:00
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(doc_title, {'#text': old_title}));
|
|
|
|
addCommandToHistory(batchCmd);
|
2009-10-19 19:06:07 +00:00
|
|
|
}
|
|
|
|
|
2009-06-25 08:22:30 +00:00
|
|
|
this.setResolution = function(x, y) {
|
2009-09-11 15:49:50 +00:00
|
|
|
var res = canvas.getResolution();
|
|
|
|
var w = res.w, h = res.h;
|
2009-10-23 18:03:48 +00:00
|
|
|
var batchCmd;
|
2009-07-30 22:45:59 +00:00
|
|
|
|
2009-09-22 17:22:28 +00:00
|
|
|
if(x == 'fit') {
|
2009-08-20 15:59:29 +00:00
|
|
|
canvas.clearSelection();
|
|
|
|
|
|
|
|
// Get bounding box
|
2009-09-22 14:35:05 +00:00
|
|
|
var bbox = canvas.getStrokedBBox();
|
2009-08-20 15:59:29 +00:00
|
|
|
|
|
|
|
if(bbox) {
|
2009-10-23 18:03:48 +00:00
|
|
|
batchCmd = new BatchCommand("Fit Canvas to Content");
|
2009-09-22 14:35:05 +00:00
|
|
|
canvas.addToSelection(canvas.getVisibleElements());
|
|
|
|
$.each(selectedElements, function(i, item) {
|
|
|
|
var sel_bb = item.getBBox();
|
2009-10-23 18:03:48 +00:00
|
|
|
var cmd = recalculateDimensions(item, {
|
2009-09-22 14:35:05 +00:00
|
|
|
x: sel_bb.x - bbox.x,
|
|
|
|
y: sel_bb.y - bbox.y,
|
|
|
|
width: sel_bb.width,
|
|
|
|
height: sel_bb.height
|
|
|
|
});
|
2009-10-23 18:03:48 +00:00
|
|
|
batchCmd.addSubCommand(cmd);
|
2009-09-22 14:35:05 +00:00
|
|
|
});
|
|
|
|
canvas.clearSelection();
|
2009-09-22 17:22:28 +00:00
|
|
|
x = Math.round(bbox.width);
|
|
|
|
y = Math.round(bbox.height);
|
2009-08-20 15:59:29 +00:00
|
|
|
} else {
|
2009-09-22 17:22:28 +00:00
|
|
|
return false;
|
2009-08-20 15:59:29 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-14 00:04:53 +00:00
|
|
|
x *= current_zoom;
|
|
|
|
y *= current_zoom;
|
|
|
|
if (x != w || y != h) {
|
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-10-23 18:03:48 +00:00
|
|
|
if(!batchCmd) {
|
|
|
|
batchCmd = new BatchCommand("Change Image Dimensions");
|
|
|
|
}
|
2009-09-14 00:04:53 +00:00
|
|
|
svgroot.setAttribute('width', x);
|
|
|
|
svgroot.setAttribute('height', y);
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(svgroot, {"width":w, "height":h}));
|
|
|
|
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent.setAttribute("viewBox", ["0 0", x/current_zoom, y/current_zoom].join(' '));
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(svgcontent, {"viewBox": ["0 0", w, h].join(' ')}));
|
2009-09-11 15:49:50 +00:00
|
|
|
|
2009-09-14 00:04:53 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
svgroot.unsuspendRedraw(handle);
|
2009-10-21 15:12:32 +00:00
|
|
|
call("changed", [svgcontent]);
|
2009-09-14 00:04:53 +00:00
|
|
|
}
|
2009-09-22 17:22:28 +00:00
|
|
|
return true;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-25 08:22:30 +00:00
|
|
|
|
2009-09-16 15:45:50 +00:00
|
|
|
this.setBBoxZoom = function(val, editor_w, editor_h) {
|
2009-09-16 14:12:51 +00:00
|
|
|
var spacer = .85;
|
2009-09-23 15:29:55 +00:00
|
|
|
var bb;
|
|
|
|
var calcZoom = function(bb) {
|
|
|
|
var w_zoom = Math.round((editor_w / bb.width)*100 * spacer)/100;
|
|
|
|
var h_zoom = Math.round((editor_h / bb.height)*100 * spacer)/100;
|
|
|
|
var zoomlevel = Math.min(w_zoom,h_zoom);
|
|
|
|
canvas.setZoom(zoomlevel);
|
|
|
|
return {'zoom': zoomlevel, 'bbox': bb};
|
|
|
|
}
|
2009-09-16 14:12:51 +00:00
|
|
|
|
2009-09-16 15:45:50 +00:00
|
|
|
if(typeof val == 'object') {
|
2009-09-23 15:29:55 +00:00
|
|
|
bb = val;
|
2009-09-16 15:45:50 +00:00
|
|
|
if(bb.width == 0 || bb.height == 0) {
|
2009-10-29 17:19:07 +00:00
|
|
|
var newzoom = bb.zoom?bb.zoom:current_zoom * bb.factor;
|
|
|
|
canvas.setZoom(newzoom);
|
2009-09-16 15:45:50 +00:00
|
|
|
return {'zoom': current_zoom, 'bbox': bb};
|
|
|
|
}
|
2009-09-23 15:29:55 +00:00
|
|
|
return calcZoom(bb);
|
2009-09-16 15:45:50 +00:00
|
|
|
}
|
2009-09-23 15:29:55 +00:00
|
|
|
|
2009-09-16 15:45:50 +00:00
|
|
|
switch (val) {
|
2009-09-16 14:12:51 +00:00
|
|
|
case 'selection':
|
|
|
|
if(!selectedElements[0]) return;
|
2009-09-22 14:35:05 +00:00
|
|
|
var sel_elems = $.map(selectedElements, function(n){ if(n) return n; });
|
2009-09-23 15:29:55 +00:00
|
|
|
bb = canvas.getStrokedBBox(sel_elems);
|
|
|
|
break;
|
2009-09-16 14:12:51 +00:00
|
|
|
case 'canvas':
|
|
|
|
var res = canvas.getResolution();
|
2009-09-23 15:29:55 +00:00
|
|
|
spacer = .95;
|
|
|
|
bb = {width:res.w, height:res.h ,x:0, y:0};
|
|
|
|
break;
|
2009-09-16 14:12:51 +00:00
|
|
|
case 'content':
|
2009-09-23 15:29:55 +00:00
|
|
|
bb = canvas.getStrokedBBox();
|
|
|
|
break;
|
|
|
|
case 'layer':
|
|
|
|
bb = canvas.getStrokedBBox(canvas.getVisibleElements(current_layer));
|
|
|
|
break;
|
2009-09-16 14:12:51 +00:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2009-09-23 15:29:55 +00:00
|
|
|
return calcZoom(bb);
|
2009-09-16 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
2009-09-09 14:18:24 +00:00
|
|
|
this.setZoom = function(zoomlevel) {
|
|
|
|
var res = canvas.getResolution();
|
|
|
|
svgroot.setAttribute("width", res.w * zoomlevel);
|
|
|
|
svgroot.setAttribute("height", res.h * zoomlevel);
|
|
|
|
current_zoom = zoomlevel;
|
2009-09-16 14:12:51 +00:00
|
|
|
$.each(selectedElements, function(i, elem) {
|
|
|
|
if(!elem) return;
|
|
|
|
selectorManager.requestSelector(elem).resize();
|
|
|
|
});
|
2009-10-17 20:29:56 +00:00
|
|
|
if(current_mode == "pathedit") {
|
2009-10-07 18:56:03 +00:00
|
|
|
resetPointGrips();
|
|
|
|
}
|
2009-09-09 14:18:24 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getMode = function() {
|
|
|
|
return current_mode;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
this.setMode = function(name) {
|
2009-10-18 20:46:32 +00:00
|
|
|
// toss out half-drawn path
|
|
|
|
if (current_mode == "path" && current_path_pts.length > 0) {
|
2009-08-14 15:50:11 +00:00
|
|
|
var elem = svgdoc.getElementById(getId());
|
|
|
|
elem.parentNode.removeChild(elem);
|
2009-10-18 20:46:32 +00:00
|
|
|
canvas.clearPath();
|
2009-08-14 15:50:11 +00:00
|
|
|
canvas.clearSelection();
|
|
|
|
started = false;
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
2009-10-17 20:29:56 +00:00
|
|
|
else if (current_mode == "pathedit") {
|
2009-10-18 20:46:32 +00:00
|
|
|
canvas.clearPath();
|
2009-08-17 03:57:43 +00:00
|
|
|
}
|
2009-08-31 14:54:59 +00:00
|
|
|
|
|
|
|
cur_properties = (selectedElements[0] && selectedElements[0].nodeName == 'text') ? cur_text : cur_shape;
|
2009-08-14 15:50:11 +00:00
|
|
|
current_mode = name;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeColor = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_properties.stroke;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-09-04 06:11:04 +00:00
|
|
|
this.setStrokeColor = function(val,preventUndo) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_shape.stroke = val;
|
2009-08-31 15:59:42 +00:00
|
|
|
cur_properties.stroke_paint = {type:"solidColor"};
|
2009-09-04 06:11:04 +00:00
|
|
|
if (!preventUndo)
|
|
|
|
this.changeSelectedAttribute("stroke", val);
|
2009-09-04 06:30:18 +00:00
|
|
|
else
|
|
|
|
this.changeSelectedAttributeNoUndo("stroke", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getFillColor = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_properties.fill;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-09-04 06:11:04 +00:00
|
|
|
this.setFillColor = function(val,preventUndo) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_properties.fill = val;
|
2009-08-31 15:59:42 +00:00
|
|
|
cur_properties.fill_paint = {type:"solidColor"};
|
2009-07-08 04:40:45 +00:00
|
|
|
// take out any path/line elements when setting fill
|
|
|
|
var elems = [];
|
|
|
|
var i = selectedElements.length;
|
|
|
|
while(i--) {
|
|
|
|
var elem = selectedElements[i];
|
2009-08-07 17:19:32 +00:00
|
|
|
if (elem && elem.tagName != "polyline" && elem.tagName != "line") {
|
2009-07-08 04:40:45 +00:00
|
|
|
elems.push(elem);
|
|
|
|
}
|
|
|
|
}
|
2009-09-04 06:30:18 +00:00
|
|
|
if (elems.length > 0) {
|
|
|
|
if (!preventUndo)
|
|
|
|
this.changeSelectedAttribute("fill", val, elems);
|
|
|
|
else
|
|
|
|
this.changeSelectedAttributeNoUndo("fill", val, elems);
|
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
var findDefs = function() {
|
2009-10-21 15:12:32 +00:00
|
|
|
var defs = svgcontent.getElementsByTagNameNS(svgns, "defs");
|
2009-07-30 22:45:59 +00:00
|
|
|
if (defs.length > 0) {
|
|
|
|
defs = defs[0];
|
|
|
|
}
|
|
|
|
else {
|
2009-08-28 14:39:53 +00:00
|
|
|
// first child is a comment, so call nextSibling
|
2009-10-21 15:12:32 +00:00
|
|
|
defs = svgcontent.insertBefore( svgdoc.createElementNS(svgns, "defs" ), svgcontent.firstChild.nextSibling);
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
|
|
|
return defs;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
var addGradient = function() {
|
|
|
|
$.each(['stroke','fill'],function(i,type) {
|
|
|
|
|
2009-08-31 14:54:59 +00:00
|
|
|
if(!cur_properties[type + '_paint'] || cur_properties[type + '_paint'].type == "solidColor") return;
|
2009-08-19 17:08:05 +00:00
|
|
|
var grad = canvas[type + 'Grad'];
|
|
|
|
// find out if there is a duplicate gradient already in the defs
|
|
|
|
var duplicate_grad = findDuplicateGradient(grad);
|
|
|
|
var defs = findDefs();
|
|
|
|
// no duplicate found, so import gradient into defs
|
|
|
|
if (!duplicate_grad) {
|
|
|
|
grad = defs.appendChild( svgdoc.importNode(grad, true) );
|
|
|
|
// get next id and set it on the grad
|
|
|
|
grad.id = getNextId();
|
|
|
|
}
|
|
|
|
else { // use existing gradient
|
|
|
|
grad = duplicate_grad;
|
|
|
|
}
|
|
|
|
var functype = type=='fill'?'Fill':'Stroke';
|
|
|
|
canvas['set'+ functype +'Color']("url(#" + grad.id + ")");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
var findDuplicateGradient = function(grad) {
|
|
|
|
var defs = findDefs();
|
|
|
|
var existing_grads = defs.getElementsByTagNameNS(svgns, "linearGradient");
|
|
|
|
var i = existing_grads.length;
|
|
|
|
while (i--) {
|
|
|
|
var og = existing_grads.item(i);
|
|
|
|
if (grad.getAttribute('x1') != og.getAttribute('x1') ||
|
|
|
|
grad.getAttribute('y1') != og.getAttribute('y1') ||
|
|
|
|
grad.getAttribute('x2') != og.getAttribute('x2') ||
|
|
|
|
grad.getAttribute('y2') != og.getAttribute('y2'))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
// else could be a duplicate, iterate through stops
|
|
|
|
var stops = grad.getElementsByTagNameNS(svgns, "stop");
|
|
|
|
var ostops = og.getElementsByTagNameNS(svgns, "stop");
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
if (stops.length != ostops.length) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
var j = stops.length;
|
|
|
|
while(j--) {
|
|
|
|
var stop = stops.item(j);
|
|
|
|
var ostop = ostops.item(j);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
if (stop.getAttribute('offset') != ostop.getAttribute('offset') ||
|
|
|
|
stop.getAttribute('stop-opacity') != ostop.getAttribute('stop-opacity') ||
|
|
|
|
stop.getAttribute('stop-color') != ostop.getAttribute('stop-color'))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
if (j == -1) {
|
|
|
|
return og;
|
|
|
|
}
|
|
|
|
} // for each gradient in defs
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
return null;
|
|
|
|
};
|
2009-10-13 03:16:02 +00:00
|
|
|
|
|
|
|
// Group: Fill and Stroke
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
this.setStrokePaint = function(p, addGrad) {
|
2009-09-04 06:11:04 +00:00
|
|
|
// make a copy
|
|
|
|
var p = new $.jGraduate.Paint(p);
|
2009-09-04 06:30:18 +00:00
|
|
|
this.setStrokeOpacity(p.alpha/100);
|
|
|
|
|
|
|
|
// now set the current paint object
|
|
|
|
cur_properties.stroke_paint = p;
|
2009-09-04 06:11:04 +00:00
|
|
|
if (p.type == "solidColor") {
|
|
|
|
this.setStrokeColor("#"+p.solidColor);
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
2009-09-04 06:11:04 +00:00
|
|
|
else if(p.type == "linearGradient") {
|
|
|
|
canvas.strokeGrad = p.linearGradient;
|
2009-08-19 17:08:05 +00:00
|
|
|
if(addGrad) addGradient();
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// console.log("none!");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
this.setFillPaint = function(p, addGrad) {
|
2009-09-04 06:11:04 +00:00
|
|
|
// make a copy
|
|
|
|
var p = new $.jGraduate.Paint(p);
|
2009-09-04 06:30:18 +00:00
|
|
|
this.setFillOpacity(p.alpha/100, true);
|
|
|
|
|
|
|
|
// now set the current paint object
|
|
|
|
cur_properties.fill_paint = p;
|
2009-09-04 06:11:04 +00:00
|
|
|
if (p.type == "solidColor") {
|
|
|
|
this.setFillColor("#"+p.solidColor);
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
2009-09-04 06:11:04 +00:00
|
|
|
else if(p.type == "linearGradient") {
|
|
|
|
canvas.fillGrad = p.linearGradient;
|
2009-08-19 17:08:05 +00:00
|
|
|
if(addGrad) addGradient();
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// console.log("none!");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeWidth = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_properties.stroke_width;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-10-22 19:36:04 +00:00
|
|
|
// When attempting to set a line's width to 0, change it to 1 instead
|
2009-06-01 22:01:07 +00:00
|
|
|
this.setStrokeWidth = function(val) {
|
2009-10-22 19:36:04 +00:00
|
|
|
if(val == 0 && $.inArray(current_mode, ['line', 'path']) != -1) {
|
2009-09-01 19:52:34 +00:00
|
|
|
canvas.setStrokeWidth(1);
|
2009-10-22 19:36:04 +00:00
|
|
|
return;
|
2009-09-01 19:52:34 +00:00
|
|
|
}
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_properties.stroke_width = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("stroke-width", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeStyle = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_shape.stroke_style;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-01 22:01:07 +00:00
|
|
|
this.setStrokeStyle = function(val) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_shape.stroke_style = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("stroke-dasharray", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 22:01:07 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getOpacity = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_shape.opacity;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-04 13:43:58 +00:00
|
|
|
this.setOpacity = function(val) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_shape.opacity = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("opacity", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-04 13:43:58 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getFillOpacity = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_shape.fill_opacity;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-09-04 06:11:04 +00:00
|
|
|
this.setFillOpacity = function(val, preventUndo) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_shape.fill_opacity = val;
|
2009-09-04 06:11:04 +00:00
|
|
|
if (!preventUndo)
|
|
|
|
this.changeSelectedAttribute("fill-opacity", val);
|
2009-09-04 06:30:18 +00:00
|
|
|
else
|
|
|
|
this.changeSelectedAttributeNoUndo("fill-opacity", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-04 13:43:58 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeOpacity = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_shape.stroke_opacity;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-09-04 06:11:04 +00:00
|
|
|
this.setStrokeOpacity = function(val, preventUndo) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_shape.stroke_opacity = val;
|
2009-09-04 06:11:04 +00:00
|
|
|
if (!preventUndo)
|
|
|
|
this.changeSelectedAttribute("stroke-opacity", val);
|
2009-09-04 06:30:18 +00:00
|
|
|
else
|
|
|
|
this.changeSelectedAttributeNoUndo("stroke-opacity", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-10-27 20:54:29 +00:00
|
|
|
// returns an object that behaves like a SVGTransformList
|
|
|
|
this.getTransformList = function(elem) {
|
|
|
|
if (isWebkit) {
|
|
|
|
var t = svgTransformLists[elem];
|
|
|
|
if (!t) {
|
|
|
|
t = svgTransformLists[elem] = new SVGEditTransformList(elem);
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
return elem.transform.baseVal;
|
|
|
|
};
|
|
|
|
|
2009-08-16 15:07:00 +00:00
|
|
|
this.getBBox = function(elem) {
|
|
|
|
var selected = elem || selectedElements[0];
|
2009-10-09 02:14:47 +00:00
|
|
|
if (elem.nodeType != 1) return null;
|
|
|
|
var ret = null;
|
2009-08-24 12:55:22 +00:00
|
|
|
if(elem.nodeName == 'text' && selected.textContent == '') {
|
2009-08-21 20:12:01 +00:00
|
|
|
selected.textContent = 'a'; // Some character needed for the selector to use.
|
2009-10-09 02:14:47 +00:00
|
|
|
ret = selected.getBBox();
|
2009-08-21 20:12:01 +00:00
|
|
|
selected.textContent = '';
|
|
|
|
} else {
|
2009-10-09 02:14:47 +00:00
|
|
|
try { ret = selected.getBBox(); }
|
|
|
|
catch(e) { ret = null; }
|
2009-08-21 20:12:01 +00:00
|
|
|
}
|
|
|
|
|
2009-08-19 04:49:50 +00:00
|
|
|
// get the bounding box from the DOM (which is in that element's coordinate system)
|
2009-08-21 20:12:01 +00:00
|
|
|
return ret;
|
2009-08-16 15:07:00 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-16 14:56:22 +00:00
|
|
|
this.getRotationAngle = function(elem) {
|
|
|
|
var selected = elem || selectedElements[0];
|
2009-09-11 15:49:50 +00:00
|
|
|
if(!elem.transform) return null;
|
2009-08-16 14:56:22 +00:00
|
|
|
// find the rotation transform (if any) and set it
|
|
|
|
var tlist = selected.transform.baseVal;
|
|
|
|
var t = tlist.numberOfItems;
|
|
|
|
var foundRot = false;
|
|
|
|
while (t--) {
|
|
|
|
var xform = tlist.getItem(t);
|
|
|
|
if (xform.type == 4) {
|
|
|
|
return xform.angle;
|
2009-10-02 23:19:11 +00:00
|
|
|
} else if (xform.type == 1) {
|
|
|
|
// Matrix transformation. Extract the rotation. (for Webkit)
|
|
|
|
var angle = Math.round( Math.acos(xform.matrix.a) * 180.0 / Math.PI );
|
|
|
|
if (xform.matrix.b < 0) angle = -angle;
|
|
|
|
return angle;
|
2009-08-16 14:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-16 19:18:22 +00:00
|
|
|
return 0;
|
2009-08-16 14:56:22 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-04 05:10:48 +00:00
|
|
|
this.setRotationAngle = function(val,preventUndo) {
|
2009-08-16 06:02:26 +00:00
|
|
|
var elem = selectedElements[0];
|
2009-08-20 05:33:34 +00:00
|
|
|
// we use the actual element's bbox (not the calculated one) since the
|
|
|
|
// calculated bbox's center can change depending on the angle
|
2009-09-04 05:10:48 +00:00
|
|
|
var bbox = elem.getBBox();
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(bbox.x+bbox.width/2), cy = round(bbox.y+bbox.height/2);
|
2009-09-04 05:10:48 +00:00
|
|
|
var rotate = "rotate(" + val + " " + cx + "," + cy + ")";
|
|
|
|
if (preventUndo) {
|
|
|
|
this.changeSelectedAttributeNoUndo("transform", rotate, selectedElements);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.changeSelectedAttribute("transform",rotate,selectedElements);
|
|
|
|
}
|
2009-10-18 20:46:32 +00:00
|
|
|
var pointGripContainer = document.getElementById("pathpointgrip_container");
|
2009-08-31 19:19:07 +00:00
|
|
|
if(elem.nodeName == "path" && pointGripContainer) {
|
2009-10-09 15:29:40 +00:00
|
|
|
setPointContainerTransform(rotate);
|
2009-08-18 19:05:42 +00:00
|
|
|
}
|
2009-08-26 13:59:25 +00:00
|
|
|
selectorManager.requestSelector(selectedElements[0]).updateGripCursors(val);
|
2009-08-16 02:48:00 +00:00
|
|
|
};
|
2009-06-04 13:43:58 +00:00
|
|
|
|
2009-06-09 22:13:13 +00:00
|
|
|
this.each = function(cb) {
|
|
|
|
$(svgroot).children().each(cb);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-09 22:13:13 +00:00
|
|
|
|
2009-06-06 18:34:42 +00:00
|
|
|
this.bind = function(event, f) {
|
2009-09-02 16:51:11 +00:00
|
|
|
var old = events[event];
|
2009-06-06 18:34:42 +00:00
|
|
|
events[event] = f;
|
2009-09-02 16:51:11 +00:00
|
|
|
return old;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 18:34:42 +00:00
|
|
|
|
2009-06-12 06:45:37 +00:00
|
|
|
this.setIdPrefix = function(p) {
|
|
|
|
idprefix = p;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-06 14:57:13 +00:00
|
|
|
this.getBold = function() {
|
|
|
|
// should only have one element selected
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null && selected.tagName == "text" &&
|
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
|
|
|
return (selected.getAttribute("font-weight") == "bold");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-06 14:57:13 +00:00
|
|
|
this.setBold = function(b) {
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null && selected.tagName == "text" &&
|
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
2009-07-06 16:21:12 +00:00
|
|
|
this.changeSelectedAttribute("font-weight", b ? "bold" : "normal");
|
2009-07-06 14:57:13 +00:00
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-06 14:57:13 +00:00
|
|
|
this.getItalic = function() {
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null && selected.tagName == "text" &&
|
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
|
|
|
return (selected.getAttribute("font-style") == "italic");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-06 14:57:13 +00:00
|
|
|
this.setItalic = function(i) {
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null && selected.tagName == "text" &&
|
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
2009-07-06 15:03:56 +00:00
|
|
|
this.changeSelectedAttribute("font-style", i ? "italic" : "normal");
|
2009-07-06 14:57:13 +00:00
|
|
|
}
|
|
|
|
};
|
2009-06-12 06:45:37 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.getFontFamily = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_text.font_family;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.setFontFamily = function(val) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_text.font_family = val;
|
2009-06-29 14:38:30 +00:00
|
|
|
this.changeSelectedAttribute("font-family", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-10 03:12:19 +00:00
|
|
|
|
|
|
|
this.getFontSize = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_text.font_size;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.setFontSize = function(val) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_text.font_size = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("font-size", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.getText = function() {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-10 03:12:19 +00:00
|
|
|
if (selected == null) { return ""; }
|
|
|
|
return selected.textContent;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.setTextContent = function(val) {
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("#text", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-09-04 22:22:40 +00:00
|
|
|
this.setImageURL = function(val) {
|
2009-10-16 13:17:23 +00:00
|
|
|
// Below is some code to fetch the data: URL representation
|
|
|
|
// of local image files. It is commented out until we figure out
|
|
|
|
// a way of introducing this as an option into the UI. Also, it
|
|
|
|
// does not work in Firefox at all :(
|
|
|
|
/*
|
|
|
|
// load in the image and once it's loaded, get the dimensions
|
|
|
|
var img = document.createElement("img");
|
|
|
|
var svgCanvas = this;
|
|
|
|
img.addEventListener("load", function(evt) {
|
|
|
|
if (this.width > 0 && this.height > 0) {
|
|
|
|
// create a canvas the same size as the raster image
|
|
|
|
var canvas = document.createElement("canvas");
|
|
|
|
canvas.width = this.width;
|
|
|
|
canvas.height = this.height;
|
|
|
|
// load the raster image into the canvas
|
|
|
|
canvas.getContext("2d").drawImage(img,0,0);
|
|
|
|
// retrieve the data: URL
|
|
|
|
try {
|
|
|
|
var dataurl = canvas.toDataURL();
|
|
|
|
svgCanvas.changeSelectedAttribute("#href", dataurl);
|
|
|
|
} catch(e) {
|
|
|
|
// if we get an exception, just set the href to the URL
|
|
|
|
svgCanvas.changeSelectedAttribute("#href", val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
img.setAttribute("src", val);
|
|
|
|
*/
|
|
|
|
|
|
|
|
svgCanvas.changeSelectedAttribute("#href", val);
|
2009-09-04 22:22:40 +00:00
|
|
|
};
|
|
|
|
|
2009-06-11 15:18:46 +00:00
|
|
|
this.setRectRadius = function(val) {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-11 15:18:46 +00:00
|
|
|
if (selected != null && selected.tagName == "rect") {
|
2009-06-25 02:54:07 +00:00
|
|
|
var r = selected.getAttribute("rx");
|
|
|
|
if (r != val) {
|
|
|
|
selected.setAttribute("rx", val);
|
|
|
|
selected.setAttribute("ry", val);
|
|
|
|
addCommandToHistory(new ChangeElementCommand(selected, {"rx":r, "ry":r}, "Radius"));
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed", [selected]);
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-06-11 15:18:46 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-28 12:37:52 +00:00
|
|
|
|
2009-10-09 15:29:40 +00:00
|
|
|
this.setSegType = function(new_type) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var grip = $('#pathpointgrip_' + current_path_pt);
|
|
|
|
var old_d = current_path.getAttribute('d');
|
2009-10-09 15:29:40 +00:00
|
|
|
|
|
|
|
var index = grip[0].id.split('_')[1] - 0;
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var last_index = current_path_pts.length/2 - 1;
|
|
|
|
var is_closed = current_path.getAttribute('d').toLowerCase().indexOf('z') != -1;
|
2009-10-12 20:26:03 +00:00
|
|
|
|
2009-10-09 15:29:40 +00:00
|
|
|
if(!is_closed && index == last_index) {
|
2009-10-18 20:46:32 +00:00
|
|
|
return; // Last point of unclosed path should do nothing
|
2009-10-09 15:29:40 +00:00
|
|
|
} else if(index >= last_index && is_closed) {
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
var next_index = index+1;
|
2009-10-18 20:46:32 +00:00
|
|
|
var cur_x = getPathPoint(index)[0];
|
|
|
|
var cur_y = getPathPoint(index)[1];
|
|
|
|
var next_x = getPathPoint(next_index)[0];
|
|
|
|
var next_y = getPathPoint(next_index)[1];
|
2009-10-09 15:29:40 +00:00
|
|
|
|
|
|
|
if(!new_type) { // double-click, so just toggle
|
2009-10-18 20:46:32 +00:00
|
|
|
var batchCmd = new BatchCommand("Toggle Path Segment Type");
|
2009-10-09 15:29:40 +00:00
|
|
|
|
|
|
|
// Toggle segment to curve/straight line
|
2009-10-18 20:46:32 +00:00
|
|
|
var old_type = current_path.pathSegList.getItem(index+1).pathSegType;
|
2009-10-09 15:29:40 +00:00
|
|
|
|
2009-10-09 19:47:32 +00:00
|
|
|
new_type = (old_type == 6) ? 4 : 6;
|
2009-10-09 15:29:40 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
new_type -= 0;
|
2009-10-18 20:46:32 +00:00
|
|
|
var batchCmd = new BatchCommand("Change Path Segment Type");
|
2009-10-09 15:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var points;
|
2009-10-09 19:47:32 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var bb = current_path.getBBox();
|
2009-10-09 15:29:40 +00:00
|
|
|
|
|
|
|
switch ( new_type ) {
|
2009-10-09 19:47:32 +00:00
|
|
|
case 6:
|
|
|
|
var diff_x = next_x - cur_x;
|
|
|
|
var diff_y = next_y - cur_y;
|
|
|
|
|
|
|
|
var ct1_x = cur_x + (diff_y/2);
|
|
|
|
var ct1_y = cur_y - (diff_x/2);
|
|
|
|
var ct2_x = next_x + (diff_y/2);
|
|
|
|
var ct2_y = next_y - (diff_x/2);
|
2009-10-09 15:29:40 +00:00
|
|
|
|
2009-10-09 19:47:32 +00:00
|
|
|
points = [next_x,next_y, ct1_x,ct1_y, ct2_x,ct2_y];
|
2009-10-09 15:29:40 +00:00
|
|
|
break;
|
2009-10-09 19:47:32 +00:00
|
|
|
case 4:
|
|
|
|
points = [next_x,next_y];
|
2009-10-09 15:29:40 +00:00
|
|
|
removeControlPointGrips(index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
replacePathSeg(new_type, next_index, points);
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
addAllPointGripsToPath();
|
|
|
|
recalculateDimensions(current_path, current_path.getBBox());
|
2009-10-13 14:52:51 +00:00
|
|
|
updateSegLine(true);
|
2009-10-12 18:33:27 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(current_path, {d: old_d}));
|
2009-10-09 15:29:40 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-10-18 20:46:32 +00:00
|
|
|
call("changed", [current_path]);
|
2009-10-09 15:29:40 +00:00
|
|
|
}
|
|
|
|
|
2009-08-28 12:37:52 +00:00
|
|
|
this.quickClone = function(elem) {
|
|
|
|
// Hack for Firefox bugs where text element features aren't updated
|
2009-09-03 17:03:07 +00:00
|
|
|
if(navigator.userAgent.indexOf('Gecko/') == -1) return elem;
|
2009-08-28 12:37:52 +00:00
|
|
|
var clone = elem.cloneNode(true)
|
|
|
|
elem.parentNode.insertBefore(clone, elem);
|
|
|
|
elem.parentNode.removeChild(elem);
|
2009-10-21 18:47:23 +00:00
|
|
|
selectorManager.releaseSelector(elem);
|
|
|
|
selectedElements[0] = clone;
|
|
|
|
selectorManager.requestSelector(clone).showGrips(true);
|
2009-08-28 12:37:52 +00:00
|
|
|
return clone;
|
|
|
|
}
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-09-04 05:10:48 +00:00
|
|
|
// New functions for refactoring of Undo/Redo
|
|
|
|
|
|
|
|
// this is the stack that stores the original values, the elements and
|
|
|
|
// the attribute name for begin/finish
|
|
|
|
var undoChangeStackPointer = -1;
|
|
|
|
var undoableChangeStack = [];
|
|
|
|
|
|
|
|
// This function tells the canvas to remember the old values of the
|
|
|
|
// attrName attribute for each element sent in. The elements and values
|
|
|
|
// are stored on a stack, so the next call to finishUndoableChange() will
|
|
|
|
// pop the elements and old values off the stack, gets the current values
|
|
|
|
// from the DOM and uses all of these to construct the undo-able command.
|
|
|
|
this.beginUndoableChange = function(attrName, elems) {
|
|
|
|
var p = ++undoChangeStackPointer;
|
2009-07-08 04:40:45 +00:00
|
|
|
var i = elems.length;
|
2009-09-04 05:10:48 +00:00
|
|
|
var oldValues = new Array(i), elements = new Array(i);
|
|
|
|
while (i--) {
|
|
|
|
var elem = elems[i];
|
|
|
|
if (elem == null) continue;
|
|
|
|
elements[i] = elem;
|
|
|
|
oldValues[i] = elem.getAttribute(attrName);
|
|
|
|
}
|
|
|
|
undoableChangeStack[p] = {'attrName': attrName,
|
|
|
|
'oldValues': oldValues,
|
|
|
|
'elements': elements};
|
|
|
|
};
|
|
|
|
|
|
|
|
// This function makes the changes to the elements and then
|
|
|
|
// fires the 'changed' event
|
|
|
|
this.changeSelectedAttributeNoUndo = function(attr, newValue, elems) {
|
2009-07-30 22:45:59 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-10-17 20:29:56 +00:00
|
|
|
if(current_mode == 'pathedit') {
|
2009-10-05 13:50:44 +00:00
|
|
|
// Editing node
|
|
|
|
var num = (attr == 'x')?0:1;
|
2009-10-18 20:46:32 +00:00
|
|
|
var old_path_pts = $.map(current_path_pts, function(n){return n/current_zoom;});
|
2009-10-05 13:50:44 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts[current_path_pt*2 + num] = newValue-0;
|
|
|
|
current_path_pt_drag = current_path_pt;
|
|
|
|
updatePath(current_path_pts[current_path_pt*2], current_path_pts[current_path_pt*2 + 1], old_path_pts);
|
2009-10-05 13:50:44 +00:00
|
|
|
}
|
2009-09-04 06:30:18 +00:00
|
|
|
var elems = elems || selectedElements;
|
2009-09-04 05:10:48 +00:00
|
|
|
var i = elems.length;
|
|
|
|
while (i--) {
|
2009-07-08 04:40:45 +00:00
|
|
|
var elem = elems[i];
|
|
|
|
if (elem == null) continue;
|
2009-10-08 02:16:36 +00:00
|
|
|
// only allow the transform/opacity attribute to change on <g> elements, slightly hacky
|
|
|
|
if (elem.tagName == "g" && (attr != "transform" && attr != "opacity")) continue;
|
2009-09-04 05:10:48 +00:00
|
|
|
var oldval = attr == "#text" ? elem.textContent : elem.getAttribute(attr);
|
|
|
|
if (oldval == null) oldval = "";
|
2009-10-26 14:28:48 +00:00
|
|
|
if (oldval !== newValue) {
|
2009-08-28 12:37:52 +00:00
|
|
|
if (attr == "#text") {
|
2009-10-22 19:36:04 +00:00
|
|
|
var old_w = elem.getBBox().width;
|
2009-09-04 05:10:48 +00:00
|
|
|
elem.textContent = newValue;
|
2009-08-28 12:37:52 +00:00
|
|
|
elem = canvas.quickClone(elem);
|
2009-10-22 19:36:04 +00:00
|
|
|
|
|
|
|
// Hoped to solve the issue of moving text with text-anchor="start",
|
|
|
|
// but this doesn't actually fix it. Hopefully on the right track, though. -Fyrd
|
|
|
|
|
|
|
|
// var box=canvas.getBBox(elem), left=box.x, top=box.y, width=box.width,
|
|
|
|
// height=box.height, dx = width - old_w, dy=0;
|
|
|
|
// var angle = canvas.getRotationAngle(elem);
|
|
|
|
// if (angle) {
|
|
|
|
// var r = Math.sqrt( dx*dx + dy*dy );
|
|
|
|
// var theta = Math.atan2(dy,dx) - angle * Math.PI / 180.0;
|
|
|
|
// dx = r * Math.cos(theta);
|
|
|
|
// dy = r * Math.sin(theta);
|
|
|
|
//
|
|
|
|
// elem.setAttribute('x', elem.getAttribute('x')-dx);
|
|
|
|
// elem.setAttribute('y', elem.getAttribute('y')-dy);
|
|
|
|
// }
|
|
|
|
|
2009-09-04 22:22:40 +00:00
|
|
|
} else if (attr == "#href") {
|
2009-09-05 11:19:42 +00:00
|
|
|
elem.setAttributeNS(xlinkns, "href", newValue);
|
|
|
|
}
|
2009-09-04 05:10:48 +00:00
|
|
|
else elem.setAttribute(attr, newValue);
|
2009-08-16 15:07:00 +00:00
|
|
|
selectedBBoxes[i] = this.getBBox(elem);
|
2009-09-03 17:03:07 +00:00
|
|
|
// Use the Firefox quickClone hack for text elements with gradients or
|
|
|
|
// where other text attributes are changed.
|
2009-08-28 13:19:47 +00:00
|
|
|
if(elem.nodeName == 'text') {
|
2009-09-04 13:42:44 +00:00
|
|
|
if((newValue+'').indexOf('url') == 0 || $.inArray(attr, ['font-size','font-family','x','y']) != -1) {
|
2009-08-28 13:19:47 +00:00
|
|
|
elem = canvas.quickClone(elem);
|
|
|
|
}
|
2009-08-27 18:42:46 +00:00
|
|
|
}
|
2009-08-13 15:42:41 +00:00
|
|
|
// Timeout needed for Opera & Firefox
|
|
|
|
setTimeout(function() {
|
2009-10-21 18:47:23 +00:00
|
|
|
selectorManager.requestSelector(elem).resize(elem.getBBox());
|
2009-08-13 15:42:41 +00:00
|
|
|
},0);
|
2009-08-23 03:18:59 +00:00
|
|
|
// if this element was rotated, and we changed the position of this element
|
2009-09-04 05:10:48 +00:00
|
|
|
// we need to update the rotational transform attribute
|
2009-08-23 03:18:59 +00:00
|
|
|
var angle = canvas.getRotationAngle(elem);
|
2009-09-02 22:17:04 +00:00
|
|
|
if (angle && attr != "transform") {
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(selectedBBoxes[i].x + selectedBBoxes[i].width/2),
|
|
|
|
cy = round(selectedBBoxes[i].y + selectedBBoxes[i].height/2);
|
2009-08-23 03:18:59 +00:00
|
|
|
var rotate = ["rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
|
|
|
if (rotate != elem.getAttribute("transform")) {
|
|
|
|
elem.setAttribute("transform", rotate);
|
|
|
|
}
|
|
|
|
}
|
2009-09-04 05:10:48 +00:00
|
|
|
} // if oldValue != newValue
|
|
|
|
} // for each elem
|
2009-10-21 15:12:32 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
2009-10-21 18:47:23 +00:00
|
|
|
|
2009-10-27 13:24:18 +00:00
|
|
|
// Only call "changed" if really necessary, as it updates the toolbar each time
|
|
|
|
if(current_mode == 'rotate') {
|
|
|
|
call("changed", elems);
|
|
|
|
}
|
2009-09-04 05:10:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// This function returns a BatchCommand object which summarizes the
|
|
|
|
// change since beginUndoableChange was called. The command can then
|
|
|
|
// be added to the command history
|
|
|
|
this.finishUndoableChange = function() {
|
|
|
|
var p = undoChangeStackPointer--;
|
|
|
|
var changeset = undoableChangeStack[p];
|
|
|
|
var i = changeset['elements'].length;
|
|
|
|
var attrName = changeset['attrName'];
|
|
|
|
var batchCmd = new BatchCommand("Change " + attrName);
|
|
|
|
while (i--) {
|
|
|
|
var elem = changeset['elements'][i];
|
|
|
|
if (elem == null) continue;
|
|
|
|
var changes = {};
|
|
|
|
changes[attrName] = changeset['oldValues'][i];
|
|
|
|
if (changes[attrName] != elem.getAttribute(attrName)) {
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(elem, changes, attrName));
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-06-24 04:33:09 +00:00
|
|
|
}
|
2009-09-04 05:10:48 +00:00
|
|
|
undoableChangeStack[p] = null;
|
|
|
|
return batchCmd;
|
|
|
|
};
|
|
|
|
|
|
|
|
// If you want to change all selectedElements, ignore the elems argument.
|
|
|
|
// If you want to change only a subset of selectedElements, then send the
|
|
|
|
// subset to this function in the elems argument.
|
|
|
|
this.changeSelectedAttribute = function(attr, val, elems) {
|
|
|
|
var elems = elems || selectedElements;
|
|
|
|
canvas.beginUndoableChange(attr, elems);
|
|
|
|
var i = elems.length;
|
|
|
|
|
|
|
|
canvas.changeSelectedAttributeNoUndo(attr, val, elems);
|
|
|
|
|
|
|
|
var batchCmd = canvas.finishUndoableChange();
|
2009-07-08 04:40:45 +00:00
|
|
|
if (!batchCmd.isEmpty()) {
|
2009-09-04 05:10:48 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-07-08 04:40:45 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-07 00:17:21 +00:00
|
|
|
$(container).mouseup(mouseUp);
|
|
|
|
$(container).mousedown(mouseDown);
|
|
|
|
$(container).mousemove(mouseMove);
|
2009-06-01 17:35:22 +00:00
|
|
|
|
2009-07-01 20:22:29 +00:00
|
|
|
this.deleteSelectedElements = function() {
|
2009-07-01 21:28:22 +00:00
|
|
|
var batchCmd = new BatchCommand("Delete Elements");
|
2009-07-03 02:49:42 +00:00
|
|
|
var len = selectedElements.length;
|
2009-09-05 22:09:42 +00:00
|
|
|
var selectedCopy = []; //selectedElements is being deleted
|
2009-07-03 02:49:42 +00:00
|
|
|
for (var i = 0; i < len; ++i) {
|
2009-07-01 20:22:29 +00:00
|
|
|
var selected = selectedElements[i];
|
|
|
|
if (selected == null) break;
|
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
var parent = selected.parentNode;
|
2009-06-10 04:44:59 +00:00
|
|
|
var t = selected;
|
2009-07-01 20:22:29 +00:00
|
|
|
// this will unselect the element and remove the selectedOutline
|
|
|
|
selectorManager.releaseSelector(t);
|
2009-06-25 02:54:07 +00:00
|
|
|
var elem = parent.removeChild(t);
|
2009-09-02 18:22:35 +00:00
|
|
|
selectedCopy.push(selected) //for the copy
|
2009-07-01 20:22:29 +00:00
|
|
|
selectedElements[i] = null;
|
2009-07-01 21:28:22 +00:00
|
|
|
batchCmd.addSubCommand(new RemoveElementCommand(elem, parent));
|
2009-06-10 04:44:59 +00:00
|
|
|
}
|
2009-07-01 21:28:22 +00:00
|
|
|
if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd);
|
2009-09-22 18:29:15 +00:00
|
|
|
call("changed", selectedCopy);
|
|
|
|
canvas.clearSelection();
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-09-05 22:09:42 +00:00
|
|
|
|
|
|
|
this.groupSelectedElements = function() {
|
|
|
|
var batchCmd = new BatchCommand("Group Elements");
|
|
|
|
|
|
|
|
// create and insert the group element
|
|
|
|
var g = addSvgElementFromJson({
|
|
|
|
"element": "g",
|
|
|
|
"attr": {
|
|
|
|
"id": getNextId()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
batchCmd.addSubCommand(new InsertElementCommand(g));
|
|
|
|
|
|
|
|
// now move all children into the group
|
|
|
|
var i = selectedElements.length;
|
|
|
|
while (i--) {
|
|
|
|
var elem = selectedElements[i];
|
2009-09-06 23:25:12 +00:00
|
|
|
if (elem == null) continue;
|
2009-09-05 22:09:42 +00:00
|
|
|
var oldNextSibling = elem.nextSibling;
|
|
|
|
var oldParent = elem.parentNode;
|
|
|
|
g.appendChild(elem);
|
|
|
|
batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldParent));
|
|
|
|
}
|
|
|
|
if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd);
|
|
|
|
|
|
|
|
// ensure selectors are at bottom and update selection
|
|
|
|
selectorManager.update();
|
|
|
|
canvas.clearSelection();
|
|
|
|
canvas.addToSelection([g]);
|
|
|
|
};
|
|
|
|
|
2009-09-06 13:00:05 +00:00
|
|
|
// TODO: when transferring group's rotational transform to the children, must deal
|
2009-09-23 16:18:04 +00:00
|
|
|
// with children who are already rotated within the group (Issue 204)
|
2009-09-05 22:09:42 +00:00
|
|
|
this.ungroupSelectedElement = function() {
|
|
|
|
var g = selectedElements[0];
|
|
|
|
if (g.tagName == "g") {
|
|
|
|
var batchCmd = new BatchCommand("Ungroup Elements");
|
|
|
|
var parent = g.parentNode;
|
|
|
|
var anchor = g.previousSibling;
|
|
|
|
var children = new Array(g.childNodes.length);
|
2009-09-06 03:51:16 +00:00
|
|
|
var xform = g.getAttribute("transform");
|
2009-09-05 22:09:42 +00:00
|
|
|
var i = 0;
|
2009-09-06 13:00:05 +00:00
|
|
|
var gbox = g.getBBox(),
|
|
|
|
gx = gbox.x + gbox.width/2,
|
|
|
|
gy = gbox.y + gbox.height/2;
|
2009-09-30 20:11:08 +00:00
|
|
|
var gangle = canvas.getRotationAngle(g) * Math.PI / 180.0;
|
2009-09-05 22:09:42 +00:00
|
|
|
while (g.firstChild) {
|
|
|
|
var elem = g.firstChild;
|
|
|
|
var oldNextSibling = elem.nextSibling;
|
|
|
|
var oldParent = elem.parentNode;
|
|
|
|
children[i++] = elem = parent.insertBefore(elem, anchor);
|
2009-09-06 03:51:16 +00:00
|
|
|
batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldParent));
|
|
|
|
if (xform) {
|
2009-09-06 13:00:05 +00:00
|
|
|
var childBox = elem.getBBox();
|
|
|
|
var cx = childBox.x + childBox.width/2,
|
|
|
|
cy = childBox.y + childBox.height/2,
|
|
|
|
dx = cx - gx,
|
|
|
|
dy = cy - gy,
|
|
|
|
r = Math.sqrt(dx*dx + dy*dy);
|
2009-09-30 20:11:08 +00:00
|
|
|
var tangle = gangle + Math.atan2(dy,dx);
|
|
|
|
var newcx = r * Math.cos(tangle) + gx,
|
|
|
|
newcy = r * Math.sin(tangle) + gy;
|
2009-09-06 13:00:05 +00:00
|
|
|
childBox.x += (newcx - cx);
|
|
|
|
childBox.y += (newcy - cy);
|
2009-09-30 20:11:08 +00:00
|
|
|
// now we add the angle that the element was rotated by
|
|
|
|
// if it's non-zero, we need to set the new transform
|
|
|
|
// otherwise, we clear it
|
|
|
|
var angle = gangle + canvas.getRotationAngle(elem) * Math.PI / 180.0;
|
|
|
|
var changes = {};
|
|
|
|
changes["transform"] = elem.getAttribute("transform");
|
|
|
|
if (angle != 0) {
|
|
|
|
elem.setAttribute("transform", "rotate(" + (angle*180.0)/Math.PI + " " + cx + "," + cy + ")");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
elem.setAttribute("transform", "");
|
|
|
|
}
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(elem, changes));
|
2009-09-06 13:00:05 +00:00
|
|
|
batchCmd.addSubCommand(recalculateDimensions(elem, childBox));
|
2009-09-06 03:51:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove transform and make it undo-able
|
|
|
|
if (xform) {
|
|
|
|
var changes = {};
|
2009-09-06 13:00:05 +00:00
|
|
|
changes["transform"] = xform;
|
|
|
|
g.setAttribute("transform", "");
|
|
|
|
g.removeAttribute("transform");
|
2009-09-06 03:51:16 +00:00
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(g, changes));
|
2009-09-05 22:09:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove the group from the selection
|
|
|
|
canvas.clearSelection();
|
|
|
|
|
|
|
|
// delete the group element (but make undo-able)
|
|
|
|
g = parent.removeChild(g);
|
|
|
|
batchCmd.addSubCommand(new RemoveElementCommand(g, parent));
|
|
|
|
|
|
|
|
if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd);
|
|
|
|
|
|
|
|
// ensure selectors are at bottom and update selection
|
|
|
|
selectorManager.update();
|
|
|
|
canvas.addToSelection(children);
|
|
|
|
}
|
|
|
|
};
|
2009-06-10 04:44:59 +00:00
|
|
|
|
2009-06-17 20:08:53 +00:00
|
|
|
this.moveToTopSelectedElement = function() {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-17 20:08:53 +00:00
|
|
|
if (selected != null) {
|
|
|
|
var t = selected;
|
2009-06-25 02:54:07 +00:00
|
|
|
var oldParent = t.parentNode;
|
|
|
|
var oldNextSibling = t.nextSibling;
|
2009-06-29 13:20:39 +00:00
|
|
|
if (oldNextSibling == selectorManager.selectorParentGroup) oldNextSibling = null;
|
2009-06-25 02:54:07 +00:00
|
|
|
t = t.parentNode.appendChild(t);
|
|
|
|
addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, "top"));
|
2009-06-17 20:08:53 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-17 20:08:53 +00:00
|
|
|
|
|
|
|
this.moveToBottomSelectedElement = function() {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-17 20:08:53 +00:00
|
|
|
if (selected != null) {
|
|
|
|
var t = selected;
|
2009-06-25 02:54:07 +00:00
|
|
|
var oldParent = t.parentNode;
|
|
|
|
var oldNextSibling = t.nextSibling;
|
2009-06-29 13:20:39 +00:00
|
|
|
if (oldNextSibling == selectorManager.selectorParentGroup) oldNextSibling = null;
|
2009-08-28 14:39:53 +00:00
|
|
|
// first child is a comment, so call nextSibling
|
|
|
|
var firstChild = t.parentNode.firstChild.nextSibling;
|
2009-07-30 22:45:59 +00:00
|
|
|
if (firstChild.tagName == 'defs') {
|
|
|
|
firstChild = firstChild.nextSibling;
|
|
|
|
}
|
|
|
|
t = t.parentNode.insertBefore(t, firstChild);
|
2009-06-25 02:54:07 +00:00
|
|
|
addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, "bottom"));
|
2009-06-17 20:08:53 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-07-13 13:51:00 +00:00
|
|
|
this.moveSelectedElements = function(dx,dy,undoable) {
|
|
|
|
// if undoable is not sent, default to true
|
2009-09-13 04:09:33 +00:00
|
|
|
// if single values, scale them to the zoom
|
|
|
|
if (dx.constructor != Array) {
|
2009-10-30 19:39:33 +00:00
|
|
|
dx /= current_zoom;
|
|
|
|
dy /= current_zoom;
|
2009-09-13 04:09:33 +00:00
|
|
|
}
|
2009-08-08 03:36:01 +00:00
|
|
|
var undoable = undoable || true;
|
2009-07-08 00:03:08 +00:00
|
|
|
var batchCmd = new BatchCommand("position");
|
|
|
|
var i = selectedElements.length;
|
|
|
|
while (i--) {
|
|
|
|
var selected = selectedElements[i];
|
|
|
|
if (selected != null) {
|
2009-08-16 15:07:00 +00:00
|
|
|
selectedBBoxes[i] = this.getBBox(selected);
|
2009-08-23 17:16:27 +00:00
|
|
|
// dx and dy could be arrays
|
|
|
|
if (dx.constructor == Array) {
|
2009-10-28 18:50:12 +00:00
|
|
|
selectedBBoxes[i].x += dx[i];
|
2009-08-23 17:16:27 +00:00
|
|
|
} else {
|
|
|
|
selectedBBoxes[i].x += dx;
|
|
|
|
}
|
|
|
|
if (dy.constructor == Array) {
|
2009-10-28 18:50:12 +00:00
|
|
|
selectedBBoxes[i].y += dy[i];
|
2009-08-23 17:16:27 +00:00
|
|
|
} else {
|
|
|
|
selectedBBoxes[i].y += dy;
|
|
|
|
}
|
2009-09-05 23:09:46 +00:00
|
|
|
var cmd = recalculateDimensions(selected,selectedBBoxes[i]);
|
2009-07-08 00:03:08 +00:00
|
|
|
if (cmd) {
|
|
|
|
batchCmd.addSubCommand(cmd);
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
selectorManager.requestSelector(selected).resize(selectedBBoxes[i]);
|
2009-07-08 00:03:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!batchCmd.isEmpty()) {
|
2009-07-13 13:51:00 +00:00
|
|
|
if (undoable)
|
|
|
|
addCommandToHistory(batchCmd);
|
2009-07-08 00:03:08 +00:00
|
|
|
call("changed", selectedElements);
|
2009-06-23 21:18:23 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-17 20:08:53 +00:00
|
|
|
|
2009-09-22 14:35:05 +00:00
|
|
|
this.getStrokedBBox = function(elems) {
|
|
|
|
// TODO: Get correct BBoxes for rotated elements
|
|
|
|
if(!elems) elems = canvas.getVisibleElements();
|
2009-09-22 17:22:28 +00:00
|
|
|
if(!elems.length) return false;
|
2009-09-22 14:35:05 +00:00
|
|
|
var full_bb = elems[0].getBBox();
|
|
|
|
var max_x = full_bb.x + full_bb.width;
|
|
|
|
var max_y = full_bb.y + full_bb.height;
|
|
|
|
var min_x = full_bb.x;
|
|
|
|
var min_y = full_bb.y;
|
|
|
|
var getOffset = function(elem) {
|
2009-10-28 19:20:56 +00:00
|
|
|
var sw = elem.getAttribute("stroke-width");
|
2009-09-22 14:35:05 +00:00
|
|
|
var offset = 0;
|
|
|
|
if (elem.getAttribute("stroke") != "none" && !isNaN(sw)) {
|
|
|
|
offset += sw/2;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.each(elems, function(i, elem) {
|
|
|
|
var cur_bb = elem.getBBox();
|
|
|
|
var offset = getOffset(elem);
|
|
|
|
min_x = Math.min(min_x, cur_bb.x - offset);
|
|
|
|
min_y = Math.min(min_y, cur_bb.y - offset);
|
|
|
|
});
|
|
|
|
|
|
|
|
full_bb.x = min_x;
|
|
|
|
full_bb.y = min_y;
|
|
|
|
|
|
|
|
$.each(elems, function(i, elem) {
|
|
|
|
var cur_bb = elem.getBBox();
|
|
|
|
var offset = getOffset(elem);
|
|
|
|
max_x = Math.max(max_x, cur_bb.x + cur_bb.width + offset);
|
|
|
|
max_y = Math.max(max_y, cur_bb.y + cur_bb.height + offset);
|
|
|
|
});
|
|
|
|
|
|
|
|
full_bb.width = max_x - min_x;
|
|
|
|
full_bb.height = max_y - min_y;
|
|
|
|
return full_bb;
|
|
|
|
}
|
|
|
|
|
2009-09-23 15:29:55 +00:00
|
|
|
this.getVisibleElements = function(parent, includeBBox) {
|
2009-10-21 15:12:32 +00:00
|
|
|
if(!parent) parent = svgcontent;
|
2009-08-28 19:20:54 +00:00
|
|
|
|
2009-10-28 19:20:56 +00:00
|
|
|
var contentElems = [];
|
|
|
|
$(parent).find('*').each(function(i, elem) {
|
|
|
|
if(elem.nodeName == 'g') return;
|
2009-08-28 19:20:54 +00:00
|
|
|
try {
|
|
|
|
var box = canvas.getBBox(elem);
|
2009-09-09 14:18:24 +00:00
|
|
|
if (box) {
|
2009-08-28 19:20:54 +00:00
|
|
|
var item = includeBBox?{'elem':elem, 'bbox':box}:elem;
|
|
|
|
contentElems.push(item);
|
|
|
|
}
|
|
|
|
} catch(e) {}
|
2009-10-28 19:20:56 +00:00
|
|
|
});
|
2009-08-28 19:20:54 +00:00
|
|
|
return contentElems;
|
|
|
|
}
|
|
|
|
|
2009-08-21 13:39:23 +00:00
|
|
|
this.cycleElement = function(next) {
|
|
|
|
var cur_elem = selectedElements[0];
|
|
|
|
var elem = false;
|
2009-09-29 03:23:52 +00:00
|
|
|
var all_elems = this.getVisibleElements(current_layer);
|
2009-08-21 13:39:23 +00:00
|
|
|
if (cur_elem == null) {
|
2009-08-28 19:20:54 +00:00
|
|
|
var num = next?all_elems.length-1:0;
|
|
|
|
elem = all_elems[num];
|
2009-08-21 13:39:23 +00:00
|
|
|
} else {
|
2009-08-28 19:20:54 +00:00
|
|
|
var i = all_elems.length;
|
|
|
|
while(i--) {
|
|
|
|
if(all_elems[i] == cur_elem) {
|
|
|
|
var num = next?i-1:i+1;
|
2009-08-31 17:03:33 +00:00
|
|
|
if(num >= all_elems.length) {
|
|
|
|
num = 0;
|
|
|
|
} else if(num < 0) {
|
|
|
|
num = all_elems.length-1;
|
|
|
|
}
|
|
|
|
elem = all_elems[num];
|
|
|
|
break;
|
2009-08-28 19:20:54 +00:00
|
|
|
}
|
2009-08-21 13:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas.clearSelection();
|
|
|
|
canvas.addToSelection([elem], true);
|
|
|
|
call("selected", selectedElements);
|
|
|
|
}
|
|
|
|
|
2009-07-02 03:54:11 +00:00
|
|
|
var resetUndoStack = function() {
|
|
|
|
undoStack = [];
|
|
|
|
undoStackPointer = 0;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
|
|
|
this.getUndoStackSize = function() { return undoStackPointer; };
|
2009-07-06 18:54:38 +00:00
|
|
|
this.getRedoStackSize = function() { return undoStack.length - undoStackPointer; };
|
|
|
|
|
|
|
|
this.getNextUndoCommandText = function() {
|
|
|
|
if (undoStackPointer > 0)
|
|
|
|
return undoStack[undoStackPointer-1].text;
|
|
|
|
return "";
|
|
|
|
};
|
|
|
|
this.getNextRedoCommandText = function() {
|
|
|
|
if (undoStackPointer < undoStack.length)
|
|
|
|
return undoStack[undoStackPointer].text;
|
|
|
|
return "";
|
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.undo = function() {
|
2009-06-29 14:38:30 +00:00
|
|
|
if (undoStackPointer > 0) {
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection();
|
2009-10-18 20:46:32 +00:00
|
|
|
removeAllPointGripsFromPath();
|
2009-06-25 15:35:28 +00:00
|
|
|
var cmd = undoStack[--undoStackPointer];
|
|
|
|
cmd.unapply();
|
2009-07-01 21:28:22 +00:00
|
|
|
call("changed", cmd.elements());
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-07-06 18:54:38 +00:00
|
|
|
};
|
2009-06-25 02:54:07 +00:00
|
|
|
this.redo = function() {
|
|
|
|
if (undoStackPointer < undoStack.length && undoStack.length > 0) {
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection();
|
2009-06-25 15:35:28 +00:00
|
|
|
var cmd = undoStack[undoStackPointer++];
|
|
|
|
cmd.apply();
|
2009-07-01 21:28:22 +00:00
|
|
|
call("changed", cmd.elements());
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-10-09 05:34:01 +00:00
|
|
|
// this function no longer uses cloneNode because we need to update the id
|
|
|
|
// of every copied element (even the descendants)
|
|
|
|
// we also do it manually because Opera/Win/non-EN puts , instead of .
|
|
|
|
var copyElem = function(el) {
|
|
|
|
// manually create a copy of the element
|
|
|
|
var new_el = document.createElementNS(svgns, el.nodeName);
|
|
|
|
$.each(el.attributes, function(i, attr) {
|
2009-10-13 05:41:32 +00:00
|
|
|
var ns = attr.nodeName == 'href' ? xlinkns :
|
|
|
|
attr.prefix == "xml" ? xmlns : null;
|
2009-10-09 05:34:01 +00:00
|
|
|
new_el.setAttributeNS(ns, attr.nodeName, attr.nodeValue);
|
|
|
|
});
|
|
|
|
// set the copied element's new id
|
|
|
|
new_el.removeAttribute("id");
|
|
|
|
new_el.id = getNextId();
|
|
|
|
// manually increment obj_num because our cloned elements are not in the DOM yet
|
|
|
|
obj_num++;
|
|
|
|
|
|
|
|
// now create copies of all children
|
|
|
|
$.each(el.childNodes, function(i, child) {
|
|
|
|
switch(child.nodeType) {
|
2009-09-23 13:43:13 +00:00
|
|
|
case 1: // element node
|
|
|
|
new_el.appendChild(copyElem(child));
|
|
|
|
break;
|
|
|
|
case 3: // text node
|
|
|
|
new_el.textContent = child.nodeValue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2009-10-09 05:34:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return new_el;
|
|
|
|
};
|
2009-09-23 13:43:13 +00:00
|
|
|
|
2009-10-09 05:34:01 +00:00
|
|
|
// this creates deep DOM copies (clones) of all selected elements
|
|
|
|
this.cloneSelectedElements = function() {
|
2009-08-06 11:43:19 +00:00
|
|
|
var batchCmd = new BatchCommand("Clone Elements");
|
2009-09-01 19:47:25 +00:00
|
|
|
// find all the elements selected (stop at first null)
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
2009-08-29 18:38:34 +00:00
|
|
|
var elem = selectedElements[i];
|
|
|
|
if (elem == null) break;
|
2009-07-13 13:51:00 +00:00
|
|
|
}
|
2009-09-01 19:47:25 +00:00
|
|
|
// use slice to quickly get the subset of elements we need
|
|
|
|
var copiedElements = selectedElements.slice(0,i);
|
2009-07-13 13:51:00 +00:00
|
|
|
this.clearSelection();
|
2009-09-01 19:47:25 +00:00
|
|
|
// note that we loop in the reverse way because of the way elements are added
|
|
|
|
// to the selectedElements array (top-first)
|
|
|
|
var i = copiedElements.length;
|
|
|
|
while (i--) {
|
|
|
|
// clone each element and replace it within copiedElements
|
2009-09-23 13:43:13 +00:00
|
|
|
var elem = copiedElements[i] = copyElem(copiedElements[i]);
|
2009-09-22 18:58:52 +00:00
|
|
|
current_layer.appendChild(elem);
|
2009-07-13 13:51:00 +00:00
|
|
|
batchCmd.addSubCommand(new InsertElementCommand(elem));
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-13 13:51:00 +00:00
|
|
|
if (!batchCmd.isEmpty()) {
|
|
|
|
this.addToSelection(copiedElements);
|
|
|
|
this.moveSelectedElements(20,20,false);
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
call("selected", selectedElements);
|
2009-08-17 06:56:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// aligns selected elements (type is a char - see switch below for explanation)
|
2009-08-25 07:04:19 +00:00
|
|
|
// relative_to can be "selected", "largest", "smallest", "page"
|
|
|
|
this.alignSelectedElements = function(type, relative_to) {
|
2009-08-24 15:30:11 +00:00
|
|
|
var bboxes = [], angles = [];
|
2009-08-23 17:16:27 +00:00
|
|
|
var minx = Number.MAX_VALUE, maxx = Number.MIN_VALUE, miny = Number.MAX_VALUE, maxy = Number.MIN_VALUE;
|
2009-08-25 07:04:19 +00:00
|
|
|
var curwidth = Number.MIN_VALUE, curheight = Number.MIN_VALUE;
|
2009-08-17 06:56:55 +00:00
|
|
|
var len = selectedElements.length;
|
|
|
|
if (!len) return;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) break;
|
|
|
|
var elem = selectedElements[i];
|
2009-08-24 15:30:11 +00:00
|
|
|
bboxes[i] = this.getBBox(elem);
|
|
|
|
// TODO: could make the following code block as part of getBBox() and add a parameter
|
|
|
|
// to that function
|
|
|
|
// if element is rotated, get angle and rotate the 4 corners of the bbox and get
|
|
|
|
// the new axis-aligned bbox
|
|
|
|
angles[i] = this.getRotationAngle(elem) * Math.PI / 180.0;
|
|
|
|
if (angles[i]) {
|
|
|
|
var rminx = Number.MAX_VALUE, rminy = Number.MAX_VALUE,
|
|
|
|
rmaxx = Number.MIN_VALUE, rmaxy = Number.MIN_VALUE;
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(bboxes[i].x + bboxes[i].width/2),
|
|
|
|
cy = round(bboxes[i].y + bboxes[i].height/2);
|
2009-08-24 15:30:11 +00:00
|
|
|
var pts = [ [bboxes[i].x - cx, bboxes[i].y - cy],
|
|
|
|
[bboxes[i].x + bboxes[i].width - cx, bboxes[i].y - cy],
|
2009-08-25 07:04:19 +00:00
|
|
|
[bboxes[i].x + bboxes[i].width - cx, bboxes[i].y + bboxes[i].height - cy],
|
2009-08-24 15:30:11 +00:00
|
|
|
[bboxes[i].x - cx, bboxes[i].y + bboxes[i].height - cy] ];
|
|
|
|
var j = 4;
|
|
|
|
while (j--) {
|
2009-08-25 07:04:19 +00:00
|
|
|
var x = pts[j][0],
|
2009-08-24 15:30:11 +00:00
|
|
|
y = pts[j][1],
|
|
|
|
r = Math.sqrt( x*x + y*y );
|
|
|
|
var theta = Math.atan2(y,x) + angles[i];
|
2009-09-17 14:47:54 +00:00
|
|
|
x = round(r * Math.cos(theta) + cx);
|
|
|
|
y = round(r * Math.sin(theta) + cy);
|
2009-08-24 15:30:11 +00:00
|
|
|
|
|
|
|
// now set the bbox for the shape after it's been rotated
|
|
|
|
if (x < rminx) rminx = x;
|
|
|
|
if (y < rminy) rminy = y;
|
|
|
|
if (x > rmaxx) rmaxx = x;
|
|
|
|
if (y > rmaxy) rmaxy = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
bboxes[i].x = rminx;
|
|
|
|
bboxes[i].y = rminy;
|
|
|
|
bboxes[i].width = rmaxx - rminx;
|
|
|
|
bboxes[i].height = rmaxy - rminy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now bbox is axis-aligned and handles rotation
|
2009-08-25 07:04:19 +00:00
|
|
|
switch (relative_to) {
|
|
|
|
case 'smallest':
|
|
|
|
if ( (type == 'l' || type == 'c' || type == 'r') && (curwidth == Number.MIN_VALUE || curwidth > bboxes[i].width) ||
|
|
|
|
(type == 't' || type == 'm' || type == 'b') && (curheight == Number.MIN_VALUE || curheight > bboxes[i].height) ) {
|
|
|
|
minx = bboxes[i].x;
|
|
|
|
miny = bboxes[i].y;
|
|
|
|
maxx = bboxes[i].x + bboxes[i].width;
|
|
|
|
maxy = bboxes[i].y + bboxes[i].height;
|
|
|
|
curwidth = bboxes[i].width;
|
|
|
|
curheight = bboxes[i].height;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'largest':
|
|
|
|
if ( (type == 'l' || type == 'c' || type == 'r') && (curwidth == Number.MIN_VALUE || curwidth < bboxes[i].width) ||
|
|
|
|
(type == 't' || type == 'm' || type == 'b') && (curheight == Number.MIN_VALUE || curheight < bboxes[i].height) ) {
|
|
|
|
minx = bboxes[i].x;
|
|
|
|
miny = bboxes[i].y;
|
|
|
|
maxx = bboxes[i].x + bboxes[i].width;
|
|
|
|
maxy = bboxes[i].y + bboxes[i].height;
|
|
|
|
curwidth = bboxes[i].width;
|
|
|
|
curheight = bboxes[i].height;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: // 'selected'
|
|
|
|
if (bboxes[i].x < minx) minx = bboxes[i].x;
|
|
|
|
if (bboxes[i].y < miny) miny = bboxes[i].y;
|
|
|
|
if (bboxes[i].x + bboxes[i].width > maxx) maxx = bboxes[i].x + bboxes[i].width;
|
|
|
|
if (bboxes[i].y + bboxes[i].height > maxy) maxy = bboxes[i].y + bboxes[i].height;
|
|
|
|
break;
|
|
|
|
}
|
2009-08-24 15:30:11 +00:00
|
|
|
} // loop for each element to find the bbox and adjust min/max
|
2009-08-25 07:04:19 +00:00
|
|
|
|
|
|
|
if (relative_to == 'page') {
|
|
|
|
minx = 0;
|
|
|
|
miny = 0;
|
|
|
|
maxx = svgroot.getAttribute('width');
|
|
|
|
maxy = svgroot.getAttribute('height');
|
|
|
|
}
|
|
|
|
|
2009-08-23 17:16:27 +00:00
|
|
|
var dx = new Array(len);
|
|
|
|
var dy = new Array(len);
|
2009-08-17 06:56:55 +00:00
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) break;
|
|
|
|
var elem = selectedElements[i];
|
2009-08-24 15:30:11 +00:00
|
|
|
var bbox = bboxes[i];
|
2009-08-23 17:16:27 +00:00
|
|
|
dx[i] = 0;
|
|
|
|
dy[i] = 0;
|
|
|
|
switch (type) {
|
|
|
|
case 'l': // left (horizontal)
|
|
|
|
dx[i] = minx - bbox.x;
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 'c': // center (horizontal)
|
|
|
|
dx[i] = (minx+maxx)/2 - (bbox.x + bbox.width/2);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 'r': // right (horizontal)
|
|
|
|
dx[i] = maxx - (bbox.x + bbox.width);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 't': // top (vertical)
|
|
|
|
dy[i] = miny - bbox.y;
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 'm': // middle (vertical)
|
|
|
|
dy[i] = (miny+maxy)/2 - (bbox.y + bbox.height/2);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 'b': // bottom (vertical)
|
|
|
|
dy[i] = maxy - (bbox.y + bbox.height);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-08-23 17:16:27 +00:00
|
|
|
this.moveSelectedElements(dx,dy);
|
2009-07-13 13:51:00 +00:00
|
|
|
};
|
2009-09-17 14:47:54 +00:00
|
|
|
this.getZoom = function(){return current_zoom;};
|
2009-09-21 02:05:58 +00:00
|
|
|
|
2009-10-14 16:57:31 +00:00
|
|
|
// Function: getVersion
|
|
|
|
// Returns a string which describes the revision number of SvgCanvas.
|
|
|
|
this.getVersion = function() {
|
|
|
|
return "svgcanvas.js ($Rev$)";
|
|
|
|
};
|
|
|
|
|
2009-09-21 02:05:58 +00:00
|
|
|
this.clear();
|
|
|
|
};
|
2009-06-08 08:09:15 +00:00
|
|
|
|
|
|
|
// Static class for various utility functions
|
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
var Utils = {
|
2009-06-08 08:09:15 +00:00
|
|
|
|
|
|
|
// This code was written by Tyler Akins and has been placed in the
|
|
|
|
// public domain. It would be nice if you left this header intact.
|
|
|
|
// Base64 code from Tyler Akins -- http://rumkin.com
|
|
|
|
|
2009-06-23 07:48:15 +00:00
|
|
|
// schiller: Removed string concatenation in favour of Array.join() optimization,
|
2009-06-17 03:20:42 +00:00
|
|
|
// also precalculate the size of the array needed.
|
2009-06-16 18:51:35 +00:00
|
|
|
|
2009-06-12 13:27:42 +00:00
|
|
|
"_keyStr" : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-12 13:27:42 +00:00
|
|
|
"encode64" : function(input) {
|
2009-06-17 03:20:42 +00:00
|
|
|
// base64 strings are 4/3 larger than the original string
|
2009-10-14 18:49:35 +00:00
|
|
|
// input = Utils.encodeUTF8(input); // convert non-ASCII characters
|
|
|
|
input = Utils.convertToXMLReferences(input);
|
2009-10-19 19:06:07 +00:00
|
|
|
if(window.btoa) return window.btoa(input); // Use native if available
|
2009-06-17 19:00:23 +00:00
|
|
|
var output = new Array( Math.floor( (input.length + 2) / 3 ) * 4 );
|
2009-06-08 09:08:45 +00:00
|
|
|
var chr1, chr2, chr3;
|
|
|
|
var enc1, enc2, enc3, enc4;
|
2009-06-17 03:20:42 +00:00
|
|
|
var i = 0, p = 0;
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
do {
|
|
|
|
chr1 = input.charCodeAt(i++);
|
|
|
|
chr2 = input.charCodeAt(i++);
|
|
|
|
chr3 = input.charCodeAt(i++);
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
enc1 = chr1 >> 2;
|
|
|
|
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
|
|
|
|
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
|
|
|
|
enc4 = chr3 & 63;
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
if (isNaN(chr2)) {
|
|
|
|
enc3 = enc4 = 64;
|
|
|
|
} else if (isNaN(chr3)) {
|
|
|
|
enc4 = 64;
|
|
|
|
}
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-17 03:20:42 +00:00
|
|
|
output[p++] = this._keyStr.charAt(enc1);
|
|
|
|
output[p++] = this._keyStr.charAt(enc2);
|
|
|
|
output[p++] = this._keyStr.charAt(enc3);
|
|
|
|
output[p++] = this._keyStr.charAt(enc4);
|
2009-06-08 09:08:45 +00:00
|
|
|
} while (i < input.length);
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-16 18:51:35 +00:00
|
|
|
return output.join('');
|
2009-07-02 04:50:24 +00:00
|
|
|
},
|
2009-10-14 18:49:35 +00:00
|
|
|
|
|
|
|
// based on http://phpjs.org/functions/utf8_encode:577
|
|
|
|
// codedread:does not seem to work with webkit-based browsers on OSX
|
|
|
|
"encodeUTF8": function(input) {
|
|
|
|
//return unescape(encodeURIComponent(input)); //may or may not work
|
|
|
|
var output = '';
|
|
|
|
for (var n = 0; n < input.length; n++){
|
|
|
|
var c = input.charCodeAt(n);
|
|
|
|
if (c < 128) {
|
|
|
|
output += input[n];
|
|
|
|
}
|
|
|
|
else if (c > 127) {
|
|
|
|
if (c < 2048){
|
|
|
|
output += String.fromCharCode((c >> 6) | 192);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
output += String.fromCharCode((c >> 12) | 224) + String.fromCharCode((c >> 6) & 63 | 128);
|
|
|
|
}
|
|
|
|
output += String.fromCharCode((c & 63) | 128);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
|
|
|
"convertToXMLReferences": function(input) {
|
|
|
|
var output = '';
|
|
|
|
for (var n = 0; n < input.length; n++){
|
|
|
|
var c = input.charCodeAt(n);
|
|
|
|
if (c < 128) {
|
|
|
|
output += input[n];
|
|
|
|
}
|
|
|
|
else if(c > 127) {
|
|
|
|
output += ("&#" + c + ";");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
},
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-02 04:50:24 +00:00
|
|
|
"rectsIntersect": function(r1, r2) {
|
|
|
|
return r2.x < (r1.x+r1.width) &&
|
|
|
|
(r2.x+r2.width) > r1.x &&
|
|
|
|
r2.y < (r1.y+r1.height) &&
|
|
|
|
(r2.y+r2.height) > r1.y;
|
2009-07-10 05:04:06 +00:00
|
|
|
},
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// found this function http://groups.google.com/group/jquery-dev/browse_thread/thread/c6d11387c580a77f
|
|
|
|
"text2xml": function(sXML) {
|
2009-08-17 06:56:55 +00:00
|
|
|
// NOTE: I'd like to use jQuery for this, but jQuery makes all tags uppercase
|
|
|
|
//return $(xml)[0];
|
|
|
|
var out;
|
|
|
|
try{
|
|
|
|
var dXML = ($.browser.msie)?new ActiveXObject("Microsoft.XMLDOM"):new DOMParser();
|
|
|
|
dXML.async = false;
|
|
|
|
} catch(e){
|
|
|
|
throw new Error("XML Parser could not be instantiated");
|
|
|
|
};
|
|
|
|
try{
|
|
|
|
if($.browser.msie) out = (dXML.loadXML(sXML))?dXML:false;
|
|
|
|
else out = dXML.parseFromString(sXML, "text/xml");
|
|
|
|
}
|
|
|
|
catch(e){ throw new Error("Error parsing XML string"); };
|
|
|
|
return out;
|
|
|
|
}
|
2009-07-02 04:50:24 +00:00
|
|
|
};
|