cleanup added elements in mouseUp

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@17 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Pavol Rusnak 2009-06-02 16:40:33 +00:00
parent 3f7f4165bf
commit 2255a31427
2 changed files with 231 additions and 257 deletions

View File

@ -45,11 +45,11 @@
<div>Stroke <div>Stroke
<div id="stroke_color"></div> <div id="stroke_color"></div>
<select id="stroke_width"> <select id="stroke_width">
<option selected="selected" value="1px">1px</option> <option selected="selected" value="1">1</option>
<option value="2px">2px</option> <option value="2">2</option>
<option value="3px">3px</option> <option value="3">3</option>
<option value="5px">5px</option> <option value="5">5</option>
<option value="7px">7px</option> <option value="7">7</option>
</select> </select>
<select id="stroke_style"> <select id="stroke_style">
<option selected="selected" value="none">-----</option> <option selected="selected" value="none">-----</option>

View File

@ -14,16 +14,17 @@ function SvgCanvas(doc)
var svgdoc = doc; var svgdoc = doc;
var svgroot = svgdoc.documentElement; var svgroot = svgdoc.documentElement;
var svgns = "http://www.w3.org/2000/svg"; var svgns = "http://www.w3.org/2000/svg";
var d_attr = ""; var d_attr = null;
var signature_started = 0; var started = false;
var obj_num = 1; var obj_num = 1;
var rect_x = null; var start_x = null;
var rect_y = null; var start_y = null;
var current_draw_element = "path"; var current_mode = "path";
var current_draw_element_fill = "none"; var current_fill = "none";
var current_draw_element_stroke = "black"; var current_stroke = "black";
var current_draw_element_stroke_width = "1px"; var current_stroke_width = 1;
var current_draw_element_stroke_style = "0"; var current_stroke_style = "none";
var current_opacity = 1;
var freehand_min_x = null; var freehand_min_x = null;
var freehand_max_x = null; var freehand_max_x = null;
var freehand_min_y = null; var freehand_min_y = null;
@ -37,9 +38,27 @@ function SvgCanvas(doc)
} }
} }
var createSvgElementFromJson = function(data) { // 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) {
var shape = svgdoc.createElementNS(svgns, data.element); var shape = svgdoc.createElementNS(svgns, data.element);
assignAttributes(shape, data.attr); assignAttributes(shape, data.attr);
cleanupElement(shape);
svgdoc.documentElement.appendChild(shape); svgdoc.documentElement.appendChild(shape);
} }
@ -68,12 +87,11 @@ function SvgCanvas(doc)
indent++; indent++;
for (i=0; i<childs.length; i++) for (i=0; i<childs.length; i++)
{ {
if (childs.item(i).nodeType == 1) // element node if (childs.item(i).nodeType == 1) { // element node
out = out + svgToString(childs.item(i), indent); out = out + svgToString(childs.item(i), indent);
else if (childs.item(i).nodeType == 3) // text node } else if (childs.item(i).nodeType == 3) { // text node
{ for (j=0; j<indent; j++) out += " ";
for (j=0; j<indent; j++) out += " "; out += childs.item(i).nodeValue + "\n";
out += childs.item(i).nodeValue + "\n";
} }
} }
indent--; indent--;
@ -92,13 +110,13 @@ function SvgCanvas(doc)
{ {
var x = evt.pageX; var x = evt.pageX;
var y = evt.pageY; var y = evt.pageY;
switch (current_draw_element) switch (current_mode)
{ {
case "select": case "select":
signature_started = 1; started = true;
rect_x = x; start_x = x;
rect_y = y; start_y = y;
createSvgElementFromJson({ addSvgElementFromJson({
"element": "rect", "element": "rect",
"attr": { "attr": {
"x": x, "x": x,
@ -108,73 +126,39 @@ function SvgCanvas(doc)
"id": "rect_" + obj_num, "id": "rect_" + obj_num,
"fill": '#DBEBF9', "fill": '#DBEBF9',
"stroke": '#CEE2F7', "stroke": '#CEE2F7',
"stroke-width": '1px', "stroke-width": 1,
"fill-opacity": 0.5 "fill-opacity": 0.5
} }
}); });
break; break;
case "fhellipse": case "fhellipse":
d_attr = "M" + x + " " + y + " ";
signature_started = 1;
createSvgElementFromJson({
"element": "path",
"attr": {
"d": d_attr,
"id": "path_" + obj_num,
"fill": "none",
"stroke": current_draw_element_stroke,
"stroke-width": current_draw_element_stroke_width,
"stroke-dasharray": current_draw_element_stroke_style,
"stroke-opacity": 0.5
}
});
freehand_min_x = x;
freehand_max_x = x;
freehand_min_y = y;
freehand_max_y = y;
break;
case "fhrect": case "fhrect":
case "path":
started = true;
d_attr = "M" + x + " " + y + " "; d_attr = "M" + x + " " + y + " ";
signature_started = 1; addSvgElementFromJson({
createSvgElementFromJson({
"element": "path", "element": "path",
"attr": { "attr": {
"d": d_attr, "d": d_attr,
"id": "path_" + obj_num, "id": "path_" + obj_num,
"fill": "none", "fill": "none",
"stroke": current_draw_element_stroke, "stroke": current_stroke,
"stroke-width": current_draw_element_stroke_width, "stroke-width": current_stroke_width,
"stroke-dasharray": current_draw_element_stroke_style, "stroke-dasharray": current_stroke_style,
"stroke-opacity": 0.5 "opacity": 0.5
} }
}); });
freehand_min_x = x; freehand_min_x = x;
freehand_max_x = x; freehand_max_x = x;
freehand_min_y = y; freehand_min_y = y;
freehand_max_y = y; freehand_max_y = y;
break; break;
case "path":
d_attr = "M" + x + " " + y + " ";
signature_started = 1;
createSvgElementFromJson({
"element": "path",
"attr": {
"d": d_attr,
"id": "path_" + obj_num,
"fill": "none",
"stroke": current_draw_element_stroke,
"stroke-width": current_draw_element_stroke_width,
"stroke-dasharray": current_draw_element_stroke_style,
"stroke-opacity": 0.5
}
});
break;
case "square": case "square":
case "rect": case "rect":
signature_started = 1; started = true;
rect_x = x; start_x = x;
rect_y = y; start_y = y;
createSvgElementFromJson({ addSvgElementFromJson({
"element": "rect", "element": "rect",
"attr": { "attr": {
"x": x, "x": x,
@ -182,18 +166,17 @@ function SvgCanvas(doc)
"width": "1px", "width": "1px",
"height": "1px", "height": "1px",
"id": "rect_" + obj_num, "id": "rect_" + obj_num,
"fill": current_draw_element_fill, "fill": current_fill,
"stroke": current_draw_element_stroke, "stroke": current_stroke,
"stroke-width": current_draw_element_stroke_width, "stroke-width": current_stroke_width,
"stroke-dasharray": current_draw_element_stroke_style, "stroke-dasharray": current_stroke_style,
"fill-opacity": 0.5, "opacity": 0.5
"stroke-opacity": 0.5
} }
}); });
break; break;
case "line": case "line":
signature_started = 1; started = true;
createSvgElementFromJson({ addSvgElementFromJson({
"element": "line", "element": "line",
"attr": { "attr": {
"x1": x, "x1": x,
@ -201,17 +184,17 @@ function SvgCanvas(doc)
"x2": x + 1 + "px", "x2": x + 1 + "px",
"y2": y + 1 + "px", "y2": y + 1 + "px",
"id": "line_" + obj_num, "id": "line_" + obj_num,
"stroke": current_draw_element_stroke, "stroke": current_stroke,
"stroke-width": current_draw_element_stroke_width, "stroke-width": current_stroke_width,
"stroke-dasharray": current_draw_element_stroke_style, "stroke-dasharray": current_stroke_style,
"stroke-opacity": 0.5 "opacity": 0.5
} }
}); });
break; break;
case "circle": case "circle":
case "ellipse": case "ellipse":
signature_started = 1; started = true;
createSvgElementFromJson({ addSvgElementFromJson({
"element": "ellipse", "element": "ellipse",
"attr": { "attr": {
"cx": x, "cx": x,
@ -219,18 +202,17 @@ function SvgCanvas(doc)
"rx": 1 + "px", "rx": 1 + "px",
"ry": 1 + "px", "ry": 1 + "px",
"id": "ellipse_" + obj_num, "id": "ellipse_" + obj_num,
"fill": current_draw_element_fill, "fill": current_fill,
"stroke": current_draw_element_stroke, "stroke": current_stroke,
"stroke-width": current_draw_element_stroke_width, "stroke-width": current_stroke_width,
"stroke-dasharray": current_draw_element_stroke_style, "stroke-dasharray": current_stroke_style,
"fill-opacity": 0.5, "opacity": 0.5
"stroke-opacity": 0.5
} }
}); });
break; break;
case "delete": case "delete":
var t = evt.target; var t = evt.target;
if(svgroot == evt.target) return; if (t == svgroot) return;
t.parentNode.removeChild(t); t.parentNode.removeChild(t);
break; break;
} }
@ -238,170 +220,162 @@ function SvgCanvas(doc)
this.mouseMove = function(evt) this.mouseMove = function(evt)
{ {
if (signature_started == 1) if (!started) return;
var x = evt.pageX;
var y = evt.pageY;
switch (current_mode)
{ {
var x = evt.pageX; case "line":
var y = evt.pageY; var shape = svgdoc.getElementById("line_" + obj_num);
switch (current_draw_element) shape.setAttributeNS(null, "x2", x);
{ shape.setAttributeNS(null, "y2", y);
case "path": break;
d_attr = d_attr + "L" + x + " " + y + " "; case "square":
var shape = svgdoc.getElementById("path_" + obj_num); var shape = svgdoc.getElementById("rect_" + obj_num);
shape.setAttributeNS(null, "d", d_attr); var size = Math.max( Math.abs(x - start_x), Math.abs(y - start_y) );
break; shape.setAttributeNS(null, "width", size);
case "line": shape.setAttributeNS(null, "height", size);
var shape = svgdoc.getElementById("line_" + obj_num); if(start_x < x) {
shape.setAttributeNS(null, "x2", x); shape.setAttributeNS(null, "x", start_x);
shape.setAttributeNS(null, "y2", y); } else {
break; shape.setAttributeNS(null, "x", start_x - size);
case "square": }
var shape = svgdoc.getElementById("rect_" + obj_num); if(start_y < y) {
var size = Math.max( Math.abs(x - rect_x), Math.abs(y - rect_y) ); shape.setAttributeNS(null, "y", start_y);
shape.setAttributeNS(null, "width", size); } else {
shape.setAttributeNS(null, "height", size); shape.setAttributeNS(null, "y", start_y - size);
if(rect_x < x) { }
shape.setAttributeNS(null, "x", rect_x); break;
} else { case "select":
shape.setAttributeNS(null, "x", rect_x - size); case "rect":
} var shape = svgdoc.getElementById("rect_" + obj_num);
if(rect_y < y) { if (start_x < x) {
shape.setAttributeNS(null, "y", rect_y); shape.setAttributeNS(null, "x", start_x);
} else { shape.setAttributeNS(null, "width", x - start_x);
shape.setAttributeNS(null, "y", rect_y - size); } else {
} shape.setAttributeNS(null, "x", x);
break; shape.setAttributeNS(null, "width", start_x - x);
case "select": }
case "rect": if (start_y < y) {
var shape = svgdoc.getElementById("rect_" + obj_num); shape.setAttributeNS(null, "y", start_y);
if (rect_x < x) { shape.setAttributeNS(null, "height", y - start_y);
shape.setAttributeNS(null, "x", rect_x); } else {
shape.setAttributeNS(null, "width", x - rect_x); shape.setAttributeNS(null, "y", y);
} else { shape.setAttributeNS(null, "height", start_y - y);
shape.setAttributeNS(null, "x", x); }
shape.setAttributeNS(null, "width", rect_x - x); break;
} case "circle":
if (rect_y < y) { var shape = svgdoc.getElementById("ellipse_" + obj_num);
shape.setAttributeNS(null, "y", rect_y); var cx = shape.getAttributeNS(null, "cx");
shape.setAttributeNS(null, "height", y - rect_y); var cy = shape.getAttributeNS(null, "cy");
} else { var rad = Math.sqrt( (x-cx)*(x-cx) + (y-cy)*(y-cy) );
shape.setAttributeNS(null, "y", y); shape.setAttributeNS(null, "rx", rad);
shape.setAttributeNS(null, "height", rect_y - y); shape.setAttributeNS(null, "ry", rad);
} break;
break; case "ellipse":
case "circle": var shape = svgdoc.getElementById("ellipse_" + obj_num);
var shape = svgdoc.getElementById("ellipse_" + obj_num); var cx = shape.getAttributeNS(null, "cx");
var cx = shape.getAttributeNS(null, "cx"); var cy = shape.getAttributeNS(null, "cy");
var cy = shape.getAttributeNS(null, "cy"); shape.setAttributeNS(null, "rx", Math.abs(x - cx) );
var rad = Math.sqrt( (x-cx)*(x-cx) + (y-cy)*(y-cy) ); shape.setAttributeNS(null, "ry", Math.abs(y - cy) );
shape.setAttributeNS(null, "rx", rad); break;
shape.setAttributeNS(null, "ry", rad); case "fhellipse":
break; case "fhrect":
case "ellipse": freehand_min_x = Math.min(x, freehand_min_x);
var shape = svgdoc.getElementById("ellipse_" + obj_num); freehand_max_x = Math.max(x, freehand_max_x);
var cx = shape.getAttributeNS(null, "cx"); freehand_min_y = Math.min(y, freehand_min_y);
var cy = shape.getAttributeNS(null, "cy"); freehand_max_y = Math.max(y, freehand_max_y);
shape.setAttributeNS(null, "rx", Math.abs(x - cx) ); // break missing on purpose
shape.setAttributeNS(null, "ry", Math.abs(y - cy) ); case "path":
break; d_attr += "L" + x + " " + y + " ";
case "fhellipse": var shape = svgdoc.getElementById("path_" + obj_num);
d_attr = d_attr + "L" + x + " " + y + " "; shape.setAttributeNS(null, "d", d_attr);
var shape = svgdoc.getElementById("path_" + obj_num); break;
shape.setAttributeNS(null, "d", d_attr);
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);
break;
case "fhrect":
d_attr = d_attr + "L" + x + " " + y + " ";
var shape = svgdoc.getElementById("path_" + obj_num);
shape.setAttributeNS(null, "d", d_attr);
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);
break;
}
} }
} }
this.mouseUp = function(evt) this.mouseUp = function(evt)
{ {
if (signature_started == 1) if (!started) return;
started = false;
var element = null;
switch (current_mode)
{ {
signature_started = 0; case "select":
switch (current_draw_element) element = svgdoc.getElementById("rect_" + obj_num);
{ element.parentNode.removeChild(element);
case "select": element = null;
var element = svgdoc.getElementById("rect_" + obj_num); break;
element.parentNode.removeChild(element); case "path":
break; d_attr = null;
case "path": element = svgdoc.getElementById("path_" + obj_num);
d_attr = 0; element.setAttribute("opacity", current_opacity);
var element = svgdoc.getElementById("path_" + obj_num); obj_num++;
element.setAttribute("stroke-opacity", 1.0); break;
obj_num++; case "line":
break; element = svgdoc.getElementById("line_" + obj_num);
case "line": element.setAttribute("opacity", current_opacity);
var element = svgdoc.getElementById("line_" + obj_num); obj_num++;
element.setAttribute("stroke-opacity", 1.0); break;
obj_num++; case "square":
break; case "rect":
case "square": element = svgdoc.getElementById("rect_" + obj_num);
case "rect": element.setAttribute("opacity", current_opacity);
var element = svgdoc.getElementById("rect_" + obj_num); obj_num++;
element.setAttribute("fill-opacity", 1.0); break;
element.setAttribute("stroke-opacity", 1.0); case "circle":
obj_num++; case "ellipse":
break; element = svgdoc.getElementById("ellipse_" + obj_num);
case "circle": element.setAttribute("opacity", current_opacity);
case "ellipse": obj_num++;
var element = svgdoc.getElementById("ellipse_" + obj_num); break;
element.setAttribute("fill-opacity", 1.0); case "fhellipse":
element.setAttribute("stroke-opacity", 1.0); d_attr = null;
obj_num++; element = svgdoc.getElementById("path_" + obj_num);
break; element.parentNode.removeChild(element);
case "fhellipse": addSvgElementFromJson({
d_attr = 0; "element": "ellipse",
var element = svgdoc.getElementById("path_" + obj_num); "attr": {
element.parentNode.removeChild(element); "cx": (freehand_min_x + freehand_max_x) / 2,
createSvgElementFromJson({ "cy": (freehand_min_y + freehand_max_y) / 2,
"element": "ellipse", "rx": (freehand_max_x - freehand_min_x) / 2 + "px",
"attr": { "ry": (freehand_max_y - freehand_min_y) / 2 + "px",
"cx": (freehand_min_x + freehand_max_x) / 2, "id": "ellipse_" + obj_num,
"cy": (freehand_min_y + freehand_max_y) / 2, "fill": current_fill,
"rx": (freehand_max_x - freehand_min_x) / 2 + "px", "stroke": current_stroke,
"ry": (freehand_max_y - freehand_min_y) / 2 + "px", "stroke-width": current_stroke_width,
"id": "ellipse_" + obj_num, "stroke-dasharray": current_stroke_style,
"fill": current_draw_element_fill, "opacity": current_opacity
"stroke": current_draw_element_stroke, }
"stroke-width": current_draw_element_stroke_width, });
"stroke-dasharray": current_draw_element_stroke_style obj_num++;
} break;
}); case "fhrect":
obj_num++; d_attr = null;
break; element = svgdoc.getElementById("path_" + obj_num);
case "fhrect": element.parentNode.removeChild(element);
d_attr = 0; addSvgElementFromJson({
var element = svgdoc.getElementById("path_" + obj_num); "element": "rect",
element.parentNode.removeChild(element); "attr": {
createSvgElementFromJson({ "x": freehand_min_x,
"element": "rect", "y": freehand_min_y,
"attr": { "width": (freehand_max_x - freehand_min_x) + "px",
"x": freehand_min_x, "height": (freehand_max_y - freehand_min_y) + "px",
"y": freehand_min_y, "id": "rect_" + obj_num,
"width": (freehand_max_x - freehand_min_x) + "px", "fill": current_fill,
"height": (freehand_max_y - freehand_min_y) + "px", "stroke": current_stroke,
"id": "rect_" + obj_num, "stroke-width": current_stroke_width,
"fill": current_draw_element_fill, "stroke-dasharray": current_stroke_style,
"stroke": current_draw_element_stroke, "opacity": current_opacity
"stroke-width": current_draw_element_stroke_width, }
"stroke-dasharray": current_draw_element_stroke_style });
} obj_num++;
}); break;
obj_num++; }
break; if (element != null) {
} cleanupElement(element);
} }
} }
@ -419,7 +393,7 @@ function SvgCanvas(doc)
var len = svgroot.childNodes.length; var len = svgroot.childNodes.length;
var i = 0; var i = 0;
for(var rep = 0; rep < len; rep++){ for(var rep = 0; rep < len; rep++){
if (nodes[i].nodeType == 1) { // element if (nodes[i].nodeType == 1) { // element node
nodes[i].parentNode.removeChild(nodes[i]); nodes[i].parentNode.removeChild(nodes[i]);
} else { } else {
i++; i++;
@ -428,23 +402,23 @@ function SvgCanvas(doc)
} }
this.setMode = function(name) { this.setMode = function(name) {
current_draw_element = name; current_mode = name;
} }
this.setStrokeColor = function(color) { this.setStrokeColor = function(color) {
current_draw_element_stroke = color; current_stroke = color;
} }
this.setFillColor = function(color) { this.setFillColor = function(color) {
current_draw_element_fill = color; current_fill = color;
} }
this.setStrokeWidth = function(val) { this.setStrokeWidth = function(val) {
current_draw_element_stroke_width = val; current_stroke_width = val;
} }
this.setStrokeStyle = function(val) { this.setStrokeStyle = function(val) {
current_draw_element_stroke_style = val; current_stroke_style = val;
} }
this.setup = function(evt) { this.setup = function(evt) {