2009-06-02 11:09:49 +00:00
|
|
|
var svgcanvas = null;
|
2009-06-01 17:35:22 +00:00
|
|
|
|
2009-06-07 00:17:21 +00:00
|
|
|
function SvgCanvas(c)
|
2009-06-01 17:35:22 +00:00
|
|
|
{
|
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
// private members
|
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-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-06-07 00:17:21 +00:00
|
|
|
container.appendChild(svgroot);
|
|
|
|
|
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;
|
|
|
|
var current_mode = "path";
|
|
|
|
var current_fill = "none";
|
|
|
|
var current_stroke = "black";
|
|
|
|
var current_stroke_width = 1;
|
|
|
|
var current_stroke_style = "none";
|
|
|
|
var current_opacity = 1;
|
2009-06-04 13:43:58 +00:00
|
|
|
var current_stroke_opacity = 1;
|
|
|
|
var current_fill_opacity = 1;
|
2009-06-10 03:12:19 +00:00
|
|
|
var current_font_size = "12pt";
|
|
|
|
var current_font_family = "serif";
|
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-06-03 08:41:25 +00:00
|
|
|
var selected = null;
|
2009-06-07 05:07:23 +00:00
|
|
|
var selectedOutline = null;
|
2009-06-06 18:34:42 +00:00
|
|
|
var events = {};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
|
|
|
// private functions
|
2009-06-05 12:33:02 +00:00
|
|
|
var getId = function() {
|
|
|
|
return "svg_"+obj_num;
|
|
|
|
}
|
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-01 21:24:30 +00:00
|
|
|
var assignAttributes = function(node, attrs) {
|
|
|
|
for (i in attrs) {
|
|
|
|
node.setAttributeNS(null, i, attrs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-02 16:40:33 +00:00
|
|
|
// remove unneeded attributes
|
|
|
|
// makes resulting SVG smaller
|
|
|
|
var cleanupElement = function(element) {
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
var addSvgElementFromJson = function(data) {
|
2009-06-09 22:13:13 +00:00
|
|
|
return canvas.updateElementFromJson(data)
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var svgToString = function(elem, indent) {
|
|
|
|
var out = "";
|
|
|
|
if (elem) {
|
|
|
|
var attrs = elem.attributes;
|
|
|
|
var attr;
|
|
|
|
var i;
|
|
|
|
var childs = elem.childNodes;
|
|
|
|
for (i=0; i<indent; i++) out += " ";
|
|
|
|
out += "<" + elem.nodeName;
|
|
|
|
for (i=attrs.length-1; i>=0; i--) {
|
|
|
|
attr = attrs.item(i);
|
|
|
|
out += " " + attr.nodeName + "=\"" + attr.nodeValue+ "\"";
|
|
|
|
}
|
|
|
|
if (elem.hasChildNodes()) {
|
2009-06-08 07:24:58 +00:00
|
|
|
out += ">";
|
2009-06-01 21:24:30 +00:00
|
|
|
indent++;
|
|
|
|
for (i=0; i<childs.length; i++)
|
|
|
|
{
|
2009-06-02 16:40:33 +00:00
|
|
|
if (childs.item(i).nodeType == 1) { // element node
|
|
|
|
out = out + svgToString(childs.item(i), indent);
|
|
|
|
} else if (childs.item(i).nodeType == 3) { // text node
|
|
|
|
for (j=0; j<indent; j++) out += " ";
|
2009-06-08 07:24:58 +00:00
|
|
|
out += childs.item(i).nodeValue + "";
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
indent--;
|
|
|
|
for (i=0; i<indent; i++) out += " ";
|
2009-06-08 07:24:58 +00:00
|
|
|
out += "</" + elem.nodeName + ">";
|
2009-06-01 21:24:30 +00:00
|
|
|
} else {
|
2009-06-08 07:24:58 +00:00
|
|
|
out += " />";
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
2009-06-07 05:07:23 +00:00
|
|
|
} // end svgToString()
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-02 12:52:44 +00:00
|
|
|
// public events
|
2009-06-07 05:07:23 +00:00
|
|
|
// call this function to set the selected element
|
|
|
|
// call this function with null to clear the selected element
|
|
|
|
var selectElement = function(newSelected)
|
|
|
|
{
|
2009-06-08 12:46:04 +00:00
|
|
|
if (selected == newSelected) return;
|
|
|
|
|
2009-06-07 05:07:23 +00:00
|
|
|
// remove selected outline from previously selected element
|
|
|
|
if (selected != null && selectedOutline != null) {
|
2009-06-10 03:12:19 +00:00
|
|
|
// remove from DOM and store reference in JS
|
|
|
|
selectedOutline = svgroot.removeChild(selectedOutline);
|
2009-06-07 05:07:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
selected = newSelected;
|
|
|
|
|
|
|
|
if (selected != null) {
|
2009-06-10 03:12:19 +00:00
|
|
|
// we create this element for the first time here
|
|
|
|
if (selectedOutline == null) {
|
|
|
|
selectedOutline = addSvgElementFromJson({
|
2009-06-07 05:07:23 +00:00
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"id": "selectedBox",
|
|
|
|
"fill": "none",
|
|
|
|
"stroke": "blue",
|
|
|
|
"stroke-width": "1",
|
|
|
|
"stroke-dasharray": "5,5",
|
2009-06-10 03:12:19 +00:00
|
|
|
"width": 1,
|
|
|
|
"height": 1,
|
2009-06-08 12:46:04 +00:00
|
|
|
// need to specify this style so that the selectedOutline is not selectable
|
|
|
|
"style": "pointer-events:none",
|
2009-06-07 05:07:23 +00:00
|
|
|
}
|
2009-06-10 03:12:19 +00:00
|
|
|
});
|
|
|
|
// TODO: add SMIL animate child on stroke-dashoffset here
|
|
|
|
}
|
|
|
|
// recalculate size and then re-append to bottom of document
|
|
|
|
recalculateSelectedOutline();
|
|
|
|
svgroot.appendChild(selectedOutline);
|
2009-06-07 05:07:23 +00:00
|
|
|
|
|
|
|
// set all our current styles to the selected styles
|
|
|
|
current_fill = selected.getAttribute("fill");
|
|
|
|
current_fill_opacity = selected.getAttribute("fill-opacity");
|
|
|
|
current_stroke = selected.getAttribute("stroke");
|
|
|
|
current_stroke_opacity = selected.getAttribute("stroke-opacity");
|
|
|
|
current_stroke_width = selected.getAttribute("stroke-width");
|
|
|
|
current_stroke_style = selected.getAttribute("stroke-dasharray");
|
2009-06-10 03:12:19 +00:00
|
|
|
if (selected.tagName == "text") {
|
|
|
|
current_font_size = selected.getAttribute("font-size");
|
|
|
|
current_font_family = selected.getAttribute("font-family");
|
|
|
|
}
|
2009-06-07 05:07:23 +00:00
|
|
|
}
|
|
|
|
|
2009-06-07 15:43:51 +00:00
|
|
|
call("selected", selected);
|
2009-06-07 05:07:23 +00:00
|
|
|
}
|
2009-06-10 03:12:19 +00:00
|
|
|
|
|
|
|
var recalculateSelectedOutline = function() {
|
|
|
|
if (selected != null && selectedOutline != null) {
|
|
|
|
var bbox = selected.getBBox();
|
|
|
|
var sw = selected.getAttribute("stroke-width");
|
|
|
|
var offset = 1;
|
|
|
|
if (sw != null && sw != "") {
|
|
|
|
offset += parseInt(sw)/2;
|
|
|
|
}
|
|
|
|
selectedOutline.setAttribute("x", bbox.x-offset);
|
|
|
|
selectedOutline.setAttribute("y", bbox.y-offset);
|
|
|
|
selectedOutline.setAttribute("width", bbox.width+(offset<<1));
|
|
|
|
selectedOutline.setAttribute("height", bbox.height+(offset<<1));
|
|
|
|
}
|
|
|
|
}
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-07 00:17:21 +00:00
|
|
|
var mouseDown = function(evt)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-06-07 00:17:21 +00:00
|
|
|
var x = evt.pageX - container.offsetLeft;
|
|
|
|
var y = evt.pageY - container.offsetTop;
|
2009-06-03 08:41:25 +00:00
|
|
|
switch (current_mode) {
|
|
|
|
case "select":
|
2009-06-08 02:17:07 +00:00
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-06-07 05:07:23 +00:00
|
|
|
var t = evt.target;
|
|
|
|
if (t != svgroot) {
|
|
|
|
selectElement(t);
|
|
|
|
}
|
2009-06-03 08:41:25 +00:00
|
|
|
break;
|
|
|
|
case "fhellipse":
|
|
|
|
case "fhrect":
|
|
|
|
case "path":
|
|
|
|
started = true;
|
2009-06-08 02:17:07 +00:00
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
|
|
|
d_attr = "M" + x + "," + y + " ";
|
2009-06-03 08:41:25 +00:00
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "path",
|
|
|
|
"attr": {
|
|
|
|
"d": d_attr,
|
2009-06-05 12:33:02 +00:00
|
|
|
"id": getId(),
|
2009-06-03 08:41:25 +00:00
|
|
|
"fill": "none",
|
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"stroke-dasharray": current_stroke_style,
|
2009-06-04 13:43:58 +00:00
|
|
|
"stroke-opacity": current_stroke_opacity,
|
|
|
|
"opacity": current_opacity / 2
|
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;
|
|
|
|
case "square":
|
|
|
|
case "rect":
|
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"x": x,
|
|
|
|
"y": y,
|
|
|
|
"width": 0,
|
|
|
|
"height": 0,
|
2009-06-06 13:21:45 +00:00
|
|
|
"id": getId(),
|
2009-06-03 08:41:25 +00:00
|
|
|
"fill": current_fill,
|
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"stroke-dasharray": current_stroke_style,
|
2009-06-04 13:43:58 +00:00
|
|
|
"stroke-opacity": current_stroke_opacity,
|
|
|
|
"fill-opacity": current_fill_opacity,
|
|
|
|
"opacity": current_opacity / 2
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "line":
|
|
|
|
started = true;
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "line",
|
|
|
|
"attr": {
|
|
|
|
"x1": x,
|
|
|
|
"y1": y,
|
|
|
|
"x2": x,
|
|
|
|
"y2": y,
|
2009-06-05 12:33:02 +00:00
|
|
|
"id": getId(),
|
2009-06-03 08:41:25 +00:00
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"stroke-dasharray": current_stroke_style,
|
2009-06-04 13:43:58 +00:00
|
|
|
"stroke-opacity": current_stroke_opacity,
|
|
|
|
"opacity": current_opacity / 2
|
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-06-05 12:33:02 +00:00
|
|
|
"id": getId(),
|
2009-06-03 15:00:48 +00:00
|
|
|
"fill": current_fill,
|
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"stroke-dasharray": current_stroke_style,
|
2009-06-04 13:43:58 +00:00
|
|
|
"stroke-opacity": current_stroke_opacity,
|
|
|
|
"fill-opacity": current_fill_opacity,
|
|
|
|
"opacity": current_opacity / 2
|
2009-06-03 15:00:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2009-06-03 08:41:25 +00:00
|
|
|
case "ellipse":
|
|
|
|
started = true;
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "ellipse",
|
|
|
|
"attr": {
|
|
|
|
"cx": x,
|
|
|
|
"cy": y,
|
|
|
|
"rx": 0,
|
|
|
|
"ry": 0,
|
2009-06-05 13:20:04 +00:00
|
|
|
"id": getId(),
|
2009-06-03 08:41:25 +00:00
|
|
|
"fill": current_fill,
|
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"stroke-dasharray": current_stroke_style,
|
2009-06-04 13:43:58 +00:00
|
|
|
"stroke-opacity": current_stroke_opacity,
|
|
|
|
"fill-opacity": current_fill_opacity,
|
|
|
|
"opacity": current_opacity / 2
|
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,
|
|
|
|
"id": getId(),
|
|
|
|
"fill": current_fill,
|
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"stroke-dasharray": current_stroke_style,
|
|
|
|
"stroke-opacity": current_stroke_opacity,
|
|
|
|
"fill-opacity": current_fill_opacity,
|
|
|
|
"opacity": current_opacity / 2,
|
|
|
|
"font-size": current_font_size,
|
|
|
|
"font-family": current_font_family,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
newText.textContent = "text";
|
|
|
|
break;
|
2009-06-03 08:41:25 +00:00
|
|
|
case "delete":
|
|
|
|
var t = evt.target;
|
|
|
|
if (t == svgroot) return;
|
|
|
|
t.parentNode.removeChild(t);
|
2009-06-06 18:35:41 +00:00
|
|
|
call("deleted",t);
|
2009-06-03 08:41:25 +00:00
|
|
|
break;
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07 00:17:21 +00:00
|
|
|
var x = evt.pageX - container.offsetLeft;
|
|
|
|
var y = evt.pageY - container.offsetTop;
|
2009-06-05 12:33:02 +00:00
|
|
|
var shape = svgdoc.getElementById(getId());
|
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":
|
|
|
|
// we temporarily use a translate on the element being dragged
|
|
|
|
// this transform is removed upon mouseUp and the element is relocated to the
|
|
|
|
// new location
|
|
|
|
if (selected != null && selectedOutline != null) {
|
|
|
|
var dx = x - start_x;
|
|
|
|
var dy = y - start_y;
|
|
|
|
selected.setAttributeNS(null, "transform", "translate(" + dx + "," + dy + ")");
|
|
|
|
selectedOutline.setAttributeNS(null, "transform", "translate(" + dx + "," + dy + ")");
|
|
|
|
}
|
2009-06-10 03:12:19 +00:00
|
|
|
break;
|
|
|
|
case "text":
|
|
|
|
shape.setAttribute("x", x);
|
|
|
|
shape.setAttribute("y", y);
|
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "line":
|
|
|
|
shape.setAttributeNS(null, "x2", x);
|
|
|
|
shape.setAttributeNS(null, "y2", y);
|
|
|
|
break;
|
|
|
|
case "square":
|
|
|
|
var size = Math.max( Math.abs(x - start_x), Math.abs(y - start_y) );
|
|
|
|
shape.setAttributeNS(null, "width", size);
|
|
|
|
shape.setAttributeNS(null, "height", size);
|
2009-06-06 17:06:44 +00:00
|
|
|
shape.setAttributeNS(null, "x", start_x < x ? start_x : start_x - size);
|
|
|
|
shape.setAttributeNS(null, "y", start_y < y ? start_y : start_y - size);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-06-07 10:27:55 +00:00
|
|
|
// case "select":
|
2009-06-02 16:40:33 +00:00
|
|
|
case "rect":
|
2009-06-06 17:06:44 +00:00
|
|
|
shape.setAttributeNS(null, "x", Math.min(start_x,x));
|
|
|
|
shape.setAttributeNS(null, "y", Math.min(start_y,y));
|
|
|
|
shape.setAttributeNS(null, "width", Math.abs(x-start_x));
|
|
|
|
shape.setAttributeNS(null, "height", Math.abs(y-start_y));
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
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");
|
|
|
|
shape.setAttributeNS(null, "rx", Math.abs(x - cx) );
|
|
|
|
shape.setAttributeNS(null, "ry", Math.abs(y - cy) );
|
|
|
|
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-06-02 16:40:33 +00:00
|
|
|
case "path":
|
2009-06-08 02:17:07 +00:00
|
|
|
var dx = x - start_x;
|
|
|
|
var dy = y - start_y;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
|
|
|
d_attr += "l" + dx + "," + dy + " ";
|
2009-06-02 16:40:33 +00:00
|
|
|
shape.setAttributeNS(null, "d", d_attr);
|
|
|
|
break;
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-08 02:17:07 +00:00
|
|
|
var mouseUp = function(evt)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-06-02 16:40:33 +00:00
|
|
|
if (!started) return;
|
|
|
|
|
|
|
|
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-06-02 16:40:33 +00:00
|
|
|
case "select":
|
2009-06-08 02:17:07 +00:00
|
|
|
if (selected != null) {
|
|
|
|
var dx = evt.pageX - container.offsetLeft - start_x;
|
|
|
|
var dy = evt.pageY - container.offsetTop - start_y;
|
|
|
|
selected.removeAttribute("transform");
|
|
|
|
selectedOutline.removeAttribute("transform");
|
|
|
|
switch (selected.tagName)
|
|
|
|
{
|
|
|
|
case "path":
|
|
|
|
// extract the x,y from the path, adjust it and write back the new path
|
|
|
|
var M = selected.pathSegList.getItem(0);
|
|
|
|
var newd = "M" + (M.x+dx) + "," + (M.y+dy);
|
|
|
|
for (var i = 1; i < selected.pathSegList.numberOfItems; ++i) {
|
|
|
|
var l = selected.pathSegList.getItem(i);
|
|
|
|
newd += " l" + l.x + "," + l.y;
|
|
|
|
}
|
|
|
|
selected.setAttributeNS(null, "d", newd);
|
|
|
|
break;
|
|
|
|
case "line":
|
|
|
|
var x1 = parseInt(selected.getAttributeNS(null, "x1"));
|
|
|
|
var y1 = parseInt(selected.getAttributeNS(null, "y1"));
|
|
|
|
var x2 = parseInt(selected.getAttributeNS(null, "x2"));
|
|
|
|
var y2 = parseInt(selected.getAttributeNS(null, "y2"));
|
|
|
|
selected.setAttributeNS(null, "x1", x1+dx);
|
|
|
|
selected.setAttributeNS(null, "y1", y1+dy);
|
|
|
|
selected.setAttributeNS(null, "x2", x2+dx);
|
|
|
|
selected.setAttributeNS(null, "y2", y2+dy);
|
|
|
|
break;
|
|
|
|
case "circle":
|
|
|
|
case "ellipse":
|
|
|
|
var cx = parseInt(selected.getAttributeNS(null, "cx"));
|
|
|
|
var cy = parseInt(selected.getAttributeNS(null, "cy"));
|
|
|
|
selected.setAttributeNS(null, "cx", cx+dx);
|
|
|
|
selected.setAttributeNS(null, "cy", cy+dy);
|
|
|
|
break;
|
|
|
|
default: // rect
|
|
|
|
var x = parseInt(selected.getAttributeNS(null, "x"));
|
|
|
|
var y = parseInt(selected.getAttributeNS(null, "y"));
|
|
|
|
selected.setAttributeNS(null, "x", x+dx);
|
|
|
|
selected.setAttributeNS(null, "y", y+dy);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
var sx = parseInt(selectedOutline.getAttributeNS(null, "x"));
|
|
|
|
var sy = parseInt(selectedOutline.getAttributeNS(null, "y"));
|
|
|
|
selectedOutline.setAttributeNS(null, "x", sx+dx);
|
|
|
|
selectedOutline.setAttributeNS(null, "y", sy+dy);
|
|
|
|
// we return immediately from select so that the obj_num is not incremented
|
|
|
|
return;
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "path":
|
2009-06-06 17:53:38 +00:00
|
|
|
keep = true;
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "line":
|
2009-06-06 17:53:38 +00:00
|
|
|
keep = (element.getAttribute('x1') != element.getAttribute('x2') ||
|
2009-06-07 10:27:55 +00:00
|
|
|
element.getAttribute('y1') == element.getAttribute('y2'));
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "square":
|
|
|
|
case "rect":
|
2009-06-06 17:53:38 +00:00
|
|
|
keep = (element.getAttribute('width') != 0 ||
|
2009-06-07 10:27:55 +00:00
|
|
|
element.getAttribute('height') != 0);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "circle":
|
2009-06-06 17:53:38 +00:00
|
|
|
keep = (element.getAttribute('r') != 0);
|
2009-06-03 15:00:48 +00:00
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "ellipse":
|
2009-06-06 17:53:38 +00:00
|
|
|
keep = (element.getAttribute('rx') != 0 ||
|
2009-06-07 10:27:55 +00:00
|
|
|
element.getAttribute('ry') != 0);
|
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-06 18:37:43 +00:00
|
|
|
call("changed",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-06-03 08:41:25 +00:00
|
|
|
"fill": current_fill,
|
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"stroke-dasharray": current_stroke_style,
|
2009-06-04 13:43:58 +00:00
|
|
|
"opacity": current_opacity,
|
|
|
|
"stroke-opacity": current_stroke_opacity,
|
|
|
|
"fill-opacity": current_fill_opacity
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-06 18:37:43 +00:00
|
|
|
}));
|
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-06 18:37:43 +00:00
|
|
|
call("changed",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-06-03 08:41:25 +00:00
|
|
|
"fill": current_fill,
|
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"stroke-dasharray": current_stroke_style,
|
2009-06-04 13:43:58 +00:00
|
|
|
"opacity": current_opacity,
|
|
|
|
"stroke-opacity": current_stroke_opacity,
|
|
|
|
"fill-opacity": current_fill_opacity
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-06 18:37:43 +00:00
|
|
|
}));
|
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;
|
|
|
|
selectElement(element);
|
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
}
|
2009-06-06 17:53:38 +00:00
|
|
|
d_attr = null;
|
|
|
|
obj_num++;
|
2009-06-10 03:12:19 +00:00
|
|
|
if (!keep) {
|
|
|
|
element.parentNode.removeChild(element);
|
|
|
|
element = null;
|
|
|
|
} else if (element != null) {
|
|
|
|
element.setAttribute("opacity", current_opacity);
|
|
|
|
cleanupElement(element);
|
|
|
|
call("changed",element);
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// public functions
|
|
|
|
|
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-06-07 10:27:55 +00:00
|
|
|
this.selectNone();
|
2009-06-08 07:24:58 +00:00
|
|
|
var str = "<?xml version=\"1.0\" standalone=\"no\"?>"
|
|
|
|
str += "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">";
|
2009-06-01 21:24:30 +00:00
|
|
|
str += svgToString(svgroot, 0);
|
2009-06-06 23:54:53 +00:00
|
|
|
this.saveHandler(str);
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.clear = function() {
|
|
|
|
var nodes = svgroot.childNodes;
|
|
|
|
var len = svgroot.childNodes.length;
|
|
|
|
var i = 0;
|
2009-06-07 10:27:55 +00:00
|
|
|
this.selectNone();
|
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-06-01 21:24:30 +00:00
|
|
|
nodes[i].parentNode.removeChild(nodes[i]);
|
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getMode = function() {
|
|
|
|
return current_mode;
|
|
|
|
}
|
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
this.setMode = function(name) {
|
2009-06-02 16:40:33 +00:00
|
|
|
current_mode = name;
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeColor = function() {
|
|
|
|
return current_stroke;
|
|
|
|
}
|
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
this.setStrokeColor = function(color) {
|
2009-06-02 16:40:33 +00:00
|
|
|
current_stroke = color;
|
2009-06-07 05:07:23 +00:00
|
|
|
if (selected != null) {
|
|
|
|
selected.setAttribute("stroke", color);
|
|
|
|
call("changed", selected);
|
|
|
|
}
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getFillColor = function() {
|
|
|
|
return current_fill;
|
|
|
|
}
|
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
this.setFillColor = function(color) {
|
2009-06-02 16:40:33 +00:00
|
|
|
current_fill = color;
|
2009-06-07 05:07:23 +00:00
|
|
|
if (selected != null) {
|
|
|
|
selected.setAttribute("fill", color);
|
|
|
|
call("changed", selected);
|
|
|
|
}
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeWidth = function() {
|
|
|
|
return current_stroke_width;
|
|
|
|
}
|
|
|
|
|
2009-06-01 22:01:07 +00:00
|
|
|
this.setStrokeWidth = function(val) {
|
2009-06-02 16:40:33 +00:00
|
|
|
current_stroke_width = val;
|
2009-06-07 05:07:23 +00:00
|
|
|
if (selected != null) {
|
|
|
|
selected.setAttribute("stroke-width", val);
|
2009-06-10 03:12:19 +00:00
|
|
|
recalculateSelectedOutline();
|
2009-06-07 05:07:23 +00:00
|
|
|
call("changed", selected);
|
|
|
|
}
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeStyle = function() {
|
|
|
|
return current_stroke_style;
|
|
|
|
}
|
|
|
|
|
2009-06-01 22:01:07 +00:00
|
|
|
this.setStrokeStyle = function(val) {
|
2009-06-02 16:40:33 +00:00
|
|
|
current_stroke_style = val;
|
2009-06-07 05:07:23 +00:00
|
|
|
if (selected != null) {
|
|
|
|
selected.setAttribute("stroke-dasharray", val);
|
|
|
|
call("changed", selected);
|
|
|
|
}
|
2009-06-01 22:01:07 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getOpacity = function() {
|
|
|
|
return current_opacity;
|
|
|
|
}
|
|
|
|
|
2009-06-04 13:43:58 +00:00
|
|
|
this.setOpacity = function(val) {
|
|
|
|
current_opacity = val;
|
2009-06-07 10:27:55 +00:00
|
|
|
if (selected != null) {
|
|
|
|
selected.setAttribute("opacity", val);
|
|
|
|
call("changed", selected);
|
|
|
|
}
|
2009-06-04 13:43:58 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getFillOpacity = function() {
|
|
|
|
return current_fill_opacity;
|
|
|
|
}
|
|
|
|
|
2009-06-04 13:43:58 +00:00
|
|
|
this.setFillOpacity = function(val) {
|
|
|
|
current_fill_opacity = val;
|
2009-06-07 05:07:23 +00:00
|
|
|
if (selected != null) {
|
|
|
|
selected.setAttribute("fill-opacity", val);
|
|
|
|
call("changed", selected);
|
|
|
|
}
|
2009-06-04 13:43:58 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeOpacity = function() {
|
|
|
|
return current_stroke_opacity;
|
|
|
|
}
|
|
|
|
|
2009-06-04 13:43:58 +00:00
|
|
|
this.setStrokeOpacity = function(val) {
|
|
|
|
current_stroke_opacity = val;
|
2009-06-07 05:07:23 +00:00
|
|
|
if (selected != null) {
|
|
|
|
selected.setAttribute("stroke-opacity", val);
|
|
|
|
call("changed", selected);
|
|
|
|
}
|
2009-06-04 13:43:58 +00:00
|
|
|
}
|
|
|
|
|
2009-06-09 22:13:13 +00:00
|
|
|
this.updateElementFromJson = function(data) {
|
|
|
|
var shape = svgdoc.getElementById(data.id);
|
|
|
|
var newshape = !shape;
|
|
|
|
if (newshape) shape = svgdoc.createElementNS(svgns, data.element);
|
|
|
|
assignAttributes(shape, data.attr);
|
|
|
|
cleanupElement(shape);
|
|
|
|
if (newshape) svgroot.appendChild(shape);
|
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.each = function(cb) {
|
|
|
|
$(svgroot).children().each(cb);
|
|
|
|
}
|
|
|
|
|
2009-06-06 18:34:42 +00:00
|
|
|
this.bind = function(event, f) {
|
|
|
|
events[event] = f;
|
|
|
|
}
|
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.getFontFamily = function() {
|
|
|
|
return current_font_family;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setFontFamily = function(val) {
|
|
|
|
current_font_family = val;
|
|
|
|
if (selected != null) {
|
|
|
|
selected.setAttribute("font-family", val);
|
|
|
|
recalculateSelectedOutline();
|
|
|
|
call("changed", selected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getFontSize = function() {
|
|
|
|
return current_font_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setFontSize = function(val) {
|
|
|
|
current_font_size = val;
|
|
|
|
if (selected != null) {
|
|
|
|
selected.setAttribute("font-size", val);
|
|
|
|
recalculateSelectedOutline();
|
|
|
|
call("changed", selected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getText = function() {
|
|
|
|
if (selected == null) { return ""; }
|
|
|
|
return selected.textContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setTextContent = function(val) {
|
|
|
|
if (selected != null) {
|
|
|
|
selected.textContent = val;
|
|
|
|
recalculateSelectedOutline();
|
|
|
|
call("changed", selected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-06 23:54:53 +00:00
|
|
|
this.saveHandler = function(svg) {
|
2009-06-08 07:24:58 +00:00
|
|
|
//alert(svg);
|
2009-06-08 08:09:15 +00:00
|
|
|
window.open("data:image/svg+xml;base64," + Utils.encode64(svg));
|
2009-06-06 23:54:53 +00:00
|
|
|
}
|
2009-06-07 10:27:55 +00:00
|
|
|
|
2009-06-07 05:07:23 +00:00
|
|
|
this.selectNone = function() {
|
|
|
|
selectElement(null);
|
|
|
|
}
|
2009-06-06 23:54:53 +00:00
|
|
|
|
2009-06-01 17:35:22 +00:00
|
|
|
}
|
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-08 09:08:45 +00:00
|
|
|
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
encode64 : function(input) {
|
|
|
|
var output = "";
|
|
|
|
var chr1, chr2, chr3;
|
|
|
|
var enc1, enc2, enc3, enc4;
|
|
|
|
var i = 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-08 09:08:45 +00:00
|
|
|
output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
|
|
|
|
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
|
|
|
|
} while (i < input.length);
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
return output;
|
|
|
|
},
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
decode64 : function(input) {
|
|
|
|
var output = "";
|
|
|
|
var chr1, chr2, chr3;
|
|
|
|
var enc1, enc2, enc3, enc4;
|
|
|
|
var i = 0;
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
|
|
|
|
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
do {
|
|
|
|
enc1 = this._keyStr.indexOf(input.charAt(i++));
|
|
|
|
enc2 = this._keyStr.indexOf(input.charAt(i++));
|
|
|
|
enc3 = this._keyStr.indexOf(input.charAt(i++));
|
|
|
|
enc4 = this._keyStr.indexOf(input.charAt(i++));
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
chr1 = (enc1 << 2) | (enc2 >> 4);
|
|
|
|
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
|
|
|
|
chr3 = ((enc3 & 3) << 6) | enc4;
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
output = output + String.fromCharCode(chr1);
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
if (enc3 != 64) {
|
|
|
|
output = output + String.fromCharCode(chr2);
|
|
|
|
}
|
|
|
|
if (enc4 != 64) {
|
|
|
|
output = output + String.fromCharCode(chr3);
|
|
|
|
}
|
|
|
|
} while (i < input.length);
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
return output;
|
|
|
|
}
|
2009-06-08 08:09:15 +00:00
|
|
|
}
|