2018-07-23 11:44:34 +00:00
|
|
|
import attributes from "./attributes";
|
2018-07-14 16:04:39 +00:00
|
|
|
|
2018-07-23 11:44:34 +00:00
|
|
|
function path() {
|
2018-07-23 11:12:06 +00:00
|
|
|
this.render = true;
|
|
|
|
this.attributes = new attributes();
|
2018-07-14 16:04:39 +00:00
|
|
|
|
|
|
|
/** Adds a move operation to Point to */
|
2018-07-23 11:44:34 +00:00
|
|
|
this.prototype.move = function(to) {
|
|
|
|
this.ops.push({ type: "move", to });
|
2018-07-14 16:04:39 +00:00
|
|
|
|
|
|
|
return this;
|
2018-07-23 11:44:34 +00:00
|
|
|
};
|
2018-07-14 16:04:39 +00:00
|
|
|
|
|
|
|
/** Adds a line operation to Point to */
|
2018-07-23 11:44:34 +00:00
|
|
|
this.prototype.line = function(to) {
|
|
|
|
this.ops.push({ type: "line", to });
|
2018-07-14 16:04:39 +00:00
|
|
|
|
|
|
|
return this;
|
2018-07-23 11:44:34 +00:00
|
|
|
};
|
2018-07-14 16:04:39 +00:00
|
|
|
|
|
|
|
/** Adds a line operation to Point to */
|
2018-07-23 11:44:34 +00:00
|
|
|
this.prototype.curve = function(cp1, cp2, to) {
|
|
|
|
this.ops.push({ type: "curve", cp1, cp2, to });
|
2018-07-14 16:04:39 +00:00
|
|
|
|
|
|
|
return this;
|
2018-07-23 11:44:34 +00:00
|
|
|
};
|
2018-07-14 16:04:39 +00:00
|
|
|
|
|
|
|
/** Adds a close operation */
|
2018-07-23 11:44:34 +00:00
|
|
|
this.prototype.close = function() {
|
|
|
|
this.ops.push({ type: "close" });
|
2018-07-14 16:04:39 +00:00
|
|
|
|
|
|
|
return this;
|
2018-07-23 11:44:34 +00:00
|
|
|
};
|
2018-07-14 16:04:39 +00:00
|
|
|
|
2018-07-21 12:54:29 +02:00
|
|
|
/** Adds an attribute. This is here to make this call chainable in assignment */
|
2018-07-23 11:44:34 +00:00
|
|
|
this.prototype.attr = function(name, value) {
|
2018-07-21 12:54:29 +02:00
|
|
|
this.attributes.add(name, value);
|
|
|
|
|
|
|
|
return this;
|
2018-07-23 11:44:34 +00:00
|
|
|
};
|
2018-07-21 12:54:29 +02:00
|
|
|
|
2018-07-14 16:04:39 +00:00
|
|
|
/** Returns SVG pathstring for this path */
|
2018-07-23 11:44:34 +00:00
|
|
|
this.prototype.asPathstring = function() {
|
|
|
|
let d = "";
|
|
|
|
for (let op of this.ops) {
|
2018-07-14 16:04:39 +00:00
|
|
|
switch (op.type) {
|
2018-07-23 11:44:34 +00:00
|
|
|
case "move":
|
2018-07-23 11:12:06 +00:00
|
|
|
d += `M ${op.to.x},${op.to.y}`;
|
2018-07-14 16:04:39 +00:00
|
|
|
break;
|
2018-07-23 11:44:34 +00:00
|
|
|
case "line":
|
2018-07-23 11:12:06 +00:00
|
|
|
d += ` L ${op.to.x},${op.to.y}`;
|
2018-07-14 16:04:39 +00:00
|
|
|
break;
|
2018-07-23 11:44:34 +00:00
|
|
|
case "curve":
|
|
|
|
d += ` C ${op.cp1.x},${op.cp1.y} ${op.cp2.x},${op.cp2.y} ${op.to.x},${
|
|
|
|
op.to.y
|
|
|
|
}`;
|
2018-07-14 16:04:39 +00:00
|
|
|
break;
|
2018-07-23 11:44:34 +00:00
|
|
|
case "close":
|
|
|
|
d += " z";
|
2018-07-14 16:04:39 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw `${op.type} is not a valid path command`;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return d;
|
2018-07-23 11:44:34 +00:00
|
|
|
};
|
2018-07-23 17:35:06 +00:00
|
|
|
|
|
|
|
return this;
|
2018-07-14 16:04:39 +00:00
|
|
|
}
|
2018-07-23 11:12:06 +00:00
|
|
|
|
|
|
|
export default path;
|