From de4ffc1d6934ec65f0a25801ed15fc79876c4fc6 Mon Sep 17 00:00:00 2001 From: Joost De Cock Date: Thu, 6 Sep 2018 15:32:43 +0200 Subject: [PATCH] :sparkles: Added support for anchored sampling --- src/path.js | 15 +++++++++++++++ src/pattern.js | 16 ++++++++++++++++ src/point.js | 8 ++++++++ 3 files changed, 39 insertions(+) diff --git a/src/path.js b/src/path.js index bb53dd83176..ebe5421b084 100644 --- a/src/path.js +++ b/src/path.js @@ -761,4 +761,19 @@ Path.prototype.trim = function() { return this; }; +/** Applies a path translate transform */ +Path.prototype.translate = function(x, y) { + for (let op of this.ops) { + if (op.type !== "close") { + op.to = op.to.translate(x, y); + } + if (op.type === "curve") { + op.cp1 = op.cp1.translate(x, y); + op.cp2 = op.cp2.translate(x, y); + } + } + + return this; +}; + export default Path; diff --git a/src/pattern.js b/src/pattern.js index cb5e84091bf..dabc071d89d 100644 --- a/src/pattern.js +++ b/src/pattern.js @@ -111,6 +111,7 @@ Pattern.prototype.sampleParts = function() { */ Pattern.prototype.sampleOption = function(optionName) { let step, val; + let anchors = {}; let parts = this.sampleParts(); let option = this.config.options[optionName]; if (typeof option.min === "undefined" || typeof option.max === "undefined") { @@ -126,10 +127,25 @@ Pattern.prototype.sampleOption = function(optionName) { ); this.draft(); for (let i in this.parts) { + let anchor = false; + let dx = 0; + let dy = 0; + if (this.parts[i].points.anchor) { + if (typeof anchors[i] === "undefined") + anchors[i] = this.parts[i].points.anchor; + else { + if (!anchors[i].sitsOn(this.parts[i].points.anchor)) { + dx = this.parts[i].points.anchor.dx(anchors[i]); + dy = this.parts[i].points.anchor.dy(anchors[i]); + } + } + } for (let j in this.parts[i].paths) { parts[i].paths[j + "_" + l] = this.parts[i].paths[j] .clone() .attr("class", "sample-" + l, true); + if (this.parts[i].points.anchor) + parts[i].paths[j + "_" + l].translate(dx, dy); } } val += step; diff --git a/src/point.js b/src/point.js index 14571f336d3..6f57775204b 100644 --- a/src/point.js +++ b/src/point.js @@ -130,4 +130,12 @@ Point.prototype.clone = function() { return clone; }; +/** Applies a translate transform */ +Point.prototype.translate = function(x, y) { + this.x += x; + this.y += y; + + return this; +}; + export default Point;