1
0
Fork 0
freesewing/src/path.js

77 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-07-23 11:12:06 +00:00
import attributes from './attributes'
2018-07-14 16:04:39 +00:00
2018-07-23 11:12:06 +00:00
function path ()
{
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:12:06 +00:00
this.prototype.move = function (to)
{
2018-07-14 16:04:39 +00:00
this.ops.push({type: "move", to});
return this;
}
/** Adds a line operation to Point to */
2018-07-23 11:12:06 +00:00
this.prototype.line = function (to)
{
2018-07-14 16:04:39 +00:00
this.ops.push({type: "line", to});
return this;
}
/** Adds a line operation to Point to */
2018-07-23 11:12:06 +00:00
this.prototype.curve = function (cp1, cp2, to)
{
2018-07-14 16:04:39 +00:00
this.ops.push({type: "curve", cp1, cp2, to});
return this;
}
/** Adds a close operation */
2018-07-23 11:12:06 +00:00
this.prototype.close = function ()
{
2018-07-14 16:04:39 +00:00
this.ops.push({type: "close"});
return this;
}
/** Adds an attribute. This is here to make this call chainable in assignment */
2018-07-23 11:12:06 +00:00
this.prototype.attr = function (name, value)
{
this.attributes.add(name, value);
return this;
}
2018-07-14 16:04:39 +00:00
/** Returns SVG pathstring for this path */
2018-07-23 11:12:06 +00:00
this.prototype.asPathstring = function ()
{
2018-07-14 16:04:39 +00:00
let d = '';
for(let op of this.ops) {
switch (op.type) {
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;
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;
case 'curve':
2018-07-23 11:12:06 +00:00
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;
case 'close':
d += ' z';
break;
default:
throw `${op.type} is not a valid path command`;
break;
}
}
return d;
}
}
2018-07-23 11:12:06 +00:00
export default path;