- Author:
- Simon Jefford
- Twitter:
- @
- GitHub:
- Facebook:
- Google+:
- +
- Reddit:
- /r/
- Pouet:
- Website:
- Compo:
- classic
- Demo link:
- https://js1k.com/2010-first/demo/356
- Shortlink:
- https://js1k.com/356
- Blog post:
- please update here!
- Bytes:
- 1006
- Chars:
- 1006
- Submission
var k,j,g,i,p,c,f,m,n,q,h,r=document,o="setAttribute",e="setAttributeNS",d="createElementNS",l="appendChild",b="pageX",a="pageY";g=function(s,t){r.addEventListener(s,t,false)};r.body.innerHTML="";k="http://www.w3.org/2000/svg";j=r[d](k,"svg");j[o]("width",500);j[o]("height",500);j[o]("style","border: 1px solid #666;");r.body[l](j);r.body.style.cursor="crosshair";h=function(t,v){var s,u;u=r[d](k,"line");u[e](null,"stroke","#000");u[e](null,"stroke-width",1);u[e](null,"stroke-linecap","butt");j[l](u);s=function(w,x){u[e](null,"x1",t);u[e](null,"y1",v);u[e](null,"x2",w);u[e](null,"y2",x)};s(t,v);return s};m=function(s,t){p=false;c=false;i=false;f=false};n=function(t,v,s,u){var w=t-s,x=v-u;return(w>=-10&&w<=10&&x>=-10&&x<=10)};g("mousedown",function(t){var s=t[b],u=t[a];if(i){if(n(s,u,p,c)){q(p,c);m();return}}q=h(s,u);p=p||s;c=c||u;f=true});g("mousemove",function(s){if(f){q(s[b],s[a])}});g("mouseup",function(t){var s=t[b],u=t[a];if(!i&&(p===s&&c===u)){i=true;return}if(f){q(s,u)}if(!i){m(s,u)}});
- Description
- Update to 353:
Removed pesky canvas tag that was pushing out the SVG pager.
SVG drawing app.
Line: mouse down -> mouse move -> mouse up
Polygon: click -> mouse move -> click -> mouse move -> click etc. To close the polygon just click within 10 px of the starting point as snap is on...
One up for the SVG Massive
- Base64 encoded
dmFyIGssaixnLGkscCxjLGYsbSxuLHEsaCxyPWRvY3VtZW50LG89InNldEF0dHJpYnV0ZSIsZT0ic2V0QXR0cmlidXRlTlMiLGQ9ImNyZWF0ZUVsZW1lbnROUyIsbD0iYXBwZW5k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sZnVuY3Rpb24odCl7dmFyIHM9dFtiXSx1PXRbYV07aWYoIWkmJihwPT09cyYmYz09PXUpKXtpPXRydWU7cmV0dXJufWlmKGYpe3Eocyx1KX1pZighaSl7bShzLHUpfX0pOw==
- Original source
var svgns,
paper,
addListener,
isPolygon,
x0,
y0,
draw,
clean,
snapToPoint,
currentLine,
line,
doc = document,
setAttribute = "setAttribute",
setAttributeNS = "setAttributeNS",
createElementNS = "createElementNS",
appendChild = "appendChild",
pageX = "pageX",
pageY = "pageY";
addListener = function(type, func) {
doc.addEventListener(type, func, false);
};
doc.body.innerHTML = "";
svgns = "http://www.w3.org/2000/svg";
paper = doc[createElementNS](svgns, "svg");
paper[setAttribute]("width", 500);
paper[setAttribute]("height", 500);
paper[setAttribute]("style", "border: 1px solid #666;");
doc.body[appendChild](paper);
doc.body.style.cursor = "crosshair";
line = function(x, y) {
var draw, line;
line = doc[createElementNS](svgns, "line");
line[setAttributeNS](null, "stroke", "#000");
line[setAttributeNS](null, "stroke-width", 1);
line[setAttributeNS](null, "stroke-linecap", "butt");
paper[appendChild](line);
draw = function(x2, y2) {
line[setAttributeNS](null, "x1", x);
line[setAttributeNS](null, "y1", y);
line[setAttributeNS](null, "x2", x2);
line[setAttributeNS](null, "y2", y2);
}
draw(x, y);
return draw;
}
clean = function(x, y) {
x0 = false;
y0 = false;
isPolygon = false;
draw = false;
};
// tests if one point is within 5 pixels of another point
snapToPoint = function(x1, y1, x2, y2) {
var xd = x1 - x2,
yd = y1 - y2;
return (xd >= -10 && xd <= 10 && yd >= -10 && yd <= 10);
};
// start drawing a new line
addListener("mousedown", function(e) {
var x = e[pageX], y = e[pageY];
// are we already drawing a polygon
if(isPolygon) {
// if the polygon is closed - finish
if(snapToPoint(x, y, x0, y0)) {
currentLine(x0, y0);
clean();
return;
}
}
// create a new line
currentLine = line(x, y)
x0 = x0 || x;
y0 = y0 || y;
draw = true;
});
// redraw the line as the mouse moves
addListener("mousemove", function(e) {
if(draw) {
currentLine(e[pageX], e[pageY]);
}
});
// stops drawing a line or switches into polygon mode
addListener("mouseup", function(e) {
var x = e[pageX], y = e[pageY];
// are we in line mode - and the user has not moved the mouse. go into polygon mode
if(!isPolygon && (x0 === x && y0 === y)) {
isPolygon = true;
return;
}
// draw the line
if(draw) {
currentLine(x, y);
}
// clean up
if(!isPolygon) {
clean(x, y);
}
});