diff --git a/src/part.js b/src/part.js index bf7260f274a..5acc5277e9a 100644 --- a/src/part.js +++ b/src/part.js @@ -4,7 +4,6 @@ import Path from "./path"; import Snippet from "./snippet"; import Attributes from "./attributes"; import * as hooklib from "hooks"; -import { round } from "./round"; function Part() { this.attributes = new Attributes(); @@ -18,7 +17,6 @@ function Part() { this.height = false; this.render = true; this.utils = utils; - this.points.origin = new Point(0, 0); for (let k in hooklib) this[k] = hooklib[k]; // Keep track of attached hooks @@ -27,9 +25,6 @@ function Part() { this.Path = Path; this.Snippet = Snippet; - // Expose round method to plugins - this.round = round; - let self = this; this.hooks.attach("debug", self); diff --git a/src/path.js b/src/path.js index 09432795a8d..4f05eb88253 100644 --- a/src/path.js +++ b/src/path.js @@ -1,13 +1,13 @@ import Attributes from "./attributes"; import Point from "./point"; import Bezier from "bezier-js"; -import { round } from "./round"; import { linesIntersect, lineIntersectsCurve, curvesIntersect, pointOnLine, - pointOnCurve + pointOnCurve, + round } from "./utils"; function Path() { diff --git a/src/pattern.js b/src/pattern.js index 540e82e7701..d4c97413cd1 100644 --- a/src/pattern.js +++ b/src/pattern.js @@ -39,7 +39,11 @@ export default function Pattern(config = false) { for (let i in config.options) { let option = config.options[i]; if (typeof option === "object") { - this.options[i] = option.val / 100; + if (typeof option.val !== "undefined") this.options[i] = option.val / 100; + else if (typeof option.mm !== "undefined") this.options[i] = option.mm; + else if (typeof option.angle !== "undefined") + this.options[i] = option.angle; + else throw "Unknown option type"; } else if (typeof option === "number") { this.options[i] = option; } diff --git a/src/round.js b/src/round.js deleted file mode 100644 index 15b4cc57e5d..00000000000 --- a/src/round.js +++ /dev/null @@ -1,6 +0,0 @@ -// This is an a file on its own to avoid -// a circular dependency -/** Rounds a value to 2 decimals */ -export function round(value) { - return Math.round(value * 1e2) / 1e2; -} diff --git a/src/utils.js b/src/utils.js index b6934a1f6ea..40730c392df 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,6 +1,5 @@ import Point from "./point"; import Bezier from "bezier-js"; -import { round } from "./round"; /** Returns internal hook name for a macro */ export function macroName(name) { @@ -259,3 +258,22 @@ export function lineIntersectsCircle(c, r, p1, p2, sort = "x") { } } } + +/** + * Calculates scale factor based on stretch factor + * + * The way people measure stretch intuitively is + * different from the way we handle stretch in code. + * When people say '25% stretch' they mean that + * 10cm fabric should get stretched to 12.5cm fabric. + * In our code, that means we need to scale things by 80%. + * + * This method does that calculation. + */ +export function stretchToScale(stretch) { + return 1 / (1 + parseFloat(stretch)); +} + +export function round(value) { + return Math.round(value * 1e2) / 1e2; +} diff --git a/tests/part.test.js b/tests/part.test.js index adcadbb0eb3..f54f4a75296 100644 --- a/tests/part.test.js +++ b/tests/part.test.js @@ -12,8 +12,6 @@ it("Svg constructor should initialize object", () => { expect(part.width).to.equal(false); expect(part.height).to.equal(false); expect(part.render).to.equal(true); - expect(part.points.origin.x).to.equal(0); - expect(part.points.origin.y).to.equal(0); }); it("Should return a function from macroClosure", () => { @@ -97,12 +95,11 @@ it("Should set part attributes", () => { it("Should copy a part", () => { let pattern = new freesewing.Pattern(); let part = new pattern.Part(); - part.points.origin.x = 16; part.points.foo = new part.Point(12, 23); let test = new pattern.Part(); test.copy(part); - expect(test.points.origin.x).to.equal(0); expect(test.points.foo.x).to.equal(12); + expect(test.points.foo.y).to.equal(23); }); it("Should return shorthand", () => { diff --git a/tests/path.test.js b/tests/path.test.js index db18239c9b8..4ccafd4373b 100644 --- a/tests/path.test.js +++ b/tests/path.test.js @@ -1,5 +1,6 @@ let expect = require("chai").expect; let freesewing = require("./dist/index.js"); +let round = freesewing.utils.round; it("Should offset a line", () => { let pattern = new freesewing.Pattern(); @@ -617,7 +618,3 @@ it("Should split a path on a line", () => { expect(line.to.x).to.equal(29.81); expect(line.to.y).to.equal(46.98); }); - -function round(value) { - return Math.round(value * 1e2) / 1e2; -} diff --git a/tests/svg.test.js b/tests/svg.test.js index eb46359ad12..8a383310a4c 100644 --- a/tests/svg.test.js +++ b/tests/svg.test.js @@ -4,6 +4,7 @@ let expect = require("chai").expect; let chai = require("chai"); chai.use(require("chai-string")); let freesewing = require("./dist/index.js"); +var round = freesewing.utils.round; it("Svg constructor should initialize object", () => { let pattern = new freesewing.Pattern(); diff --git a/tests/utils.test.js b/tests/utils.test.js index 278264b681a..5afd815b201 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -452,3 +452,8 @@ it("Should not find intersections between identical circles", () => { ); expect(intersections).to.equal(false); }); + +it("Should return scale for a given amount of stretch", () => { + expect(freesewing.utils.stretchToScale(0)).to.equal(1); + expect(freesewing.utils.stretchToScale(0.15)).to.equal(0.9); +});