🚧 Part cloning
This commit is contained in:
parent
9a60dc5756
commit
18cc30fe74
7 changed files with 90 additions and 3 deletions
|
@ -48,4 +48,12 @@ attributes.prototype.renderIfPrefixIs = function(prefix = "") {
|
|||
return svg;
|
||||
};
|
||||
|
||||
/** Returns a deep copy of this */
|
||||
attributes.prototype.clone = function() {
|
||||
let clone = new attributes();
|
||||
clone.list = JSON.parse(JSON.stringify(this.list));
|
||||
|
||||
return clone;
|
||||
};
|
||||
|
||||
export default attributes;
|
||||
|
|
39
src/part.js
39
src/part.js
|
@ -15,6 +15,8 @@ function part(id) {
|
|||
this.freeId = 0;
|
||||
this.topLeft = false;
|
||||
this.bottomRight = false;
|
||||
this.width = false;
|
||||
this.height = false;
|
||||
this.render = id.substr(0, 1) === "_" ? false : true;
|
||||
this.points.origin = new point(0, 0);
|
||||
for (let k in hooklib) this[k] = hooklib[k];
|
||||
|
@ -77,6 +79,8 @@ part.prototype.boundary = function() {
|
|||
// Add 10mm margin
|
||||
this.topLeft = new point(topLeft.x - 10, topLeft.y - 10);
|
||||
this.bottomRight = new point(bottomRight.x + 10, bottomRight.y + 10);
|
||||
this.width = this.bottomRight.x - this.topLeft.x;
|
||||
this.height = this.bottomRight.y - this.topLeft.y;
|
||||
|
||||
return this;
|
||||
};
|
||||
|
@ -100,4 +104,39 @@ part.prototype.attr = function(name, value) {
|
|||
return this;
|
||||
};
|
||||
|
||||
/** Returns a (deep) clone of this part object */
|
||||
part.prototype.clone = function() {};
|
||||
|
||||
/** Returns a deep copy of this */
|
||||
part.prototype.clone = function(id = false) {
|
||||
let clone = new part(id);
|
||||
clone.freeId = 0;
|
||||
clone.width = this.width;
|
||||
clone.height = this.height;
|
||||
clone.attributes = this.attributes.clone();
|
||||
clone.render = this.render;
|
||||
|
||||
if (!id) clone.id = this.id;
|
||||
if (this.topLeft) clone.topLeft = this.topLeft.clone();
|
||||
else clone.topLeft = false;
|
||||
if (this.bottomRight) clone.bottomRight = this.bottomRight.clone();
|
||||
else clone.bottomRight = false;
|
||||
|
||||
clone.points = {};
|
||||
clone.paths = {};
|
||||
clone.snippets = {};
|
||||
for (let i in this.points) clone.points[i] = this.points[i].clone();
|
||||
for (let i in this.paths) clone.paths[i] = this.paths[i].clone();
|
||||
for (let i in this.snippets) clone.snippets[i] = this.snippets[i].clone();
|
||||
for (let k in hooklib) clone[k] = hooklib[k];
|
||||
|
||||
clone.point = point;
|
||||
clone.path = path;
|
||||
clone.snippet = snippet;
|
||||
clone.round = round;
|
||||
|
||||
clone.context = this.context;
|
||||
|
||||
return clone;
|
||||
};
|
||||
export default part;
|
||||
|
|
26
src/path.js
26
src/path.js
|
@ -152,4 +152,30 @@ path.prototype.boundary = function() {
|
|||
|
||||
return this;
|
||||
};
|
||||
|
||||
/** Returns a deep copy of this */
|
||||
path.prototype.clone = function() {
|
||||
let clone = new path();
|
||||
clone.render = this.render = true;
|
||||
if (this.topLeft) clone.topLeft = this.topLeft.clone();
|
||||
else clone.topLeft = false;
|
||||
if (this.bottomRight) clone.bottomRight = this.bottomRight.clone();
|
||||
else clone.bottomRight = false;
|
||||
clone.attributes = this.attributes.clone();
|
||||
clone.ops = [];
|
||||
for (let i in this.ops) {
|
||||
let op = this.ops[i];
|
||||
clone.ops[i] = { type: op.type };
|
||||
if (op.type === "move" || op.type === "line") {
|
||||
clone.ops[i].to = op.to.clone();
|
||||
} else if (op.type === "curve") {
|
||||
clone.ops[i].to = op.to.clone();
|
||||
clone.ops[i].cp1 = op.cp1.clone();
|
||||
clone.ops[i].cp2 = op.cp2.clone();
|
||||
}
|
||||
}
|
||||
|
||||
return clone;
|
||||
};
|
||||
|
||||
export default path;
|
||||
|
|
|
@ -85,7 +85,7 @@ pattern.prototype.render = function() {
|
|||
this.hooks.attach("postRenderSvg", this.svg);
|
||||
//this.hooks.attach('insertText', this.svg);
|
||||
|
||||
return this.svg.render(this);
|
||||
return this.pack().svg.render(this);
|
||||
};
|
||||
|
||||
pattern.prototype.on = function(hook, method) {
|
||||
|
|
|
@ -108,4 +108,12 @@ point.prototype.shiftOutwards = function(that, distance) {
|
|||
return this.shiftTowards(that, this.dist(that) + distance);
|
||||
};
|
||||
|
||||
/** Returns a deep copy of this */
|
||||
point.prototype.clone = function() {
|
||||
let clone = new point(this.x, this.y);
|
||||
clone.attributes = this.attributes.clone();
|
||||
|
||||
return clone;
|
||||
};
|
||||
|
||||
export default point;
|
||||
|
|
|
@ -9,4 +9,12 @@ function snippet(def, anchor, description = "") {
|
|||
return this;
|
||||
}
|
||||
|
||||
/** Returns a deep copy of this */
|
||||
snippet.prototype.clone = function() {
|
||||
let clone = new snippet(this.def, this.anchor.clone(), this.description);
|
||||
clone.attributes = this.attributes.clone();
|
||||
|
||||
return clone;
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
|
|
@ -41,8 +41,6 @@ svg.prototype.insertText = function() {};
|
|||
/** Renders a draft object as SVG */
|
||||
svg.prototype.render = function(pattern) {
|
||||
this.preRenderSvg();
|
||||
// this needs to run after the preSvgRender hook as it might add stuff
|
||||
pattern.pack();
|
||||
this.attributes.add("width", pattern.width + "mm");
|
||||
this.attributes.add("height", pattern.height + "mm");
|
||||
this.attributes.add("viewBox", `0 0 ${pattern.width} ${pattern.height}`);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue