1
0
Fork 0
freesewing/src/path.js

73 lines
1.5 KiB
JavaScript
Raw Normal View History

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