1
0
Fork 0

🚧 Fixing prototype issues

This commit is contained in:
Joost De Cock 2018-07-23 20:14:32 +02:00
parent 7758ba81a6
commit 9fbef27330
9 changed files with 1482 additions and 1467 deletions

View file

@ -3,71 +3,69 @@ import attributes from "./attributes";
function path() {
this.render = true;
this.attributes = new attributes();
/** Adds a move operation to Point to */
this.prototype.move = function(to) {
this.ops.push({ type: "move", to });
return this;
};
/** Adds a line operation to Point to */
this.prototype.line = function(to) {
this.ops.push({ type: "line", to });
return this;
};
/** Adds a line operation to Point to */
this.prototype.curve = function(cp1, cp2, to) {
this.ops.push({ type: "curve", cp1, cp2, to });
return this;
};
/** Adds a close operation */
this.prototype.close = function() {
this.ops.push({ type: "close" });
return this;
};
/** Adds an attribute. This is here to make this call chainable in assignment */
this.prototype.attr = function(name, value) {
this.attributes.add(name, value);
return this;
};
/** Returns SVG pathstring for this path */
this.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;
}
}
return d;
};
return this;
}
/** Adds a move operation to Point to */
path.prototype.move = function(to) {
this.ops.push({ type: "move", to });
return this;
};
/** Adds a line operation to Point to */
path.prototype.line = function(to) {
this.ops.push({ type: "line", to });
return this;
};
/** Adds a line operation to Point to */
path.prototype.curve = function(cp1, cp2, to) {
this.ops.push({ type: "curve", cp1, cp2, to });
return this;
};
/** Adds a close operation */
path.prototype.close = function() {
this.ops.push({ type: "close" });
return this;
};
/** Adds an attribute. This is here to make this call chainable in assignment */
path.prototype.attr = function(name, value) {
this.attributes.add(name, value);
return this;
};
/** 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;
}
}
return d;
};
export default path;