diff --git a/package.json b/package.json index 9800352f391..94d6403a095 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "browserbuild": "rollup -c rollup.js -o dist/browser.js -f iife -n freesewing", "nodebuild": "rollup -c rollup.js -o dist/index.js -f cjs", "modulebuild": "rollup -c rollup.js -o dist/index.mjs -f es", - "build": "npm run clean && npm run browserbuild && npm run nodebuild && npm run modulebuild" + "build": "npm run clean && npm run browserbuild && npm run nodebuild && npm run modulebuild", + "watch": "rollup -c rollup.js -o dist/index.js -f cjs --watch" }, "husky": { "hooks": { diff --git a/src/pattern.js b/src/pattern.js index b831a125b38..d3780d849da 100644 --- a/src/pattern.js +++ b/src/pattern.js @@ -20,7 +20,9 @@ export default function Pattern(config = false) { Svg.prototype.hooks = this.hooks; // Data containers - this.settings = {}; + this.settings = { + mode: "draft" + }; this.options = {}; this.store = new Store(); this.parts = {}; @@ -34,10 +36,9 @@ export default function Pattern(config = false) { this.config = { ...defaults, ...config }; for (let i in config.options) { let option = config.options[i]; - if (option.type === "%") this.options[i] = option.val / 100; + if (typeof option.type === "undefined") this.options[i] = option.val / 100; else this.options[i] = option.val; } - console.log(this.options); // Constructors this.Part = Part; @@ -67,6 +68,56 @@ Pattern.prototype.draft = function() { ); }; +/** + * Handles pattern sampling + */ +Pattern.prototype.sample = function() { + this.settings.mode = "sample"; + if (this.settings.sample.type === "option") { + return this.sampleOption(this.settings.sample.option); + } + this.debug("sampling", this.settings); + this.debug(this.settings); + this.draft(); +}; + +/** + * Handles option sampling + */ +Pattern.prototype.sampleOption = function(option) { + let factor = 1; + if (typeof this.config.options[option].type === "undefined") factor = 100; + let min = this.config.options[option].min / factor; + let max = this.config.options[option].max / factor; + let step = (max - min) / 10; + let val = min; + let count = 1; + // First run + this.draft(); + let parts = {}; + this.options[option] = val; + for (let i in this.parts) { + parts[i] = new Part(); + parts[i].render = this.parts[i].render; + for (let j in this.parts[i].paths) { + parts[i].paths[j + "_1"] = this.parts[i].paths[j].clone(); + } + } + for (let l = 2; l < 12; l++) { + val += step; + this.options[option] = val; + this.debug(`Sampling option ${option} with value ${val}`); + this.draft(); + for (let i in this.parts) { + for (let j in this.parts[i].paths) { + parts[i].paths[j + "_" + l] = this.parts[i].paths[j].clone(); + } + } + } + console.log(parts); + this.parts = parts; +}; + /** Debug method, exposes debug hook */ Pattern.prototype.debug = function(data) { this.dbg.debug(data);