1
0
Fork 0

🎨 Moved round() to utils

This commit is contained in:
Joost De Cock 2018-08-31 09:44:12 +02:00
parent 85137b2334
commit 1faad2a01b
9 changed files with 34 additions and 23 deletions

View file

@ -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);

View file

@ -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() {

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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", () => {

View file

@ -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;
}

View file

@ -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();

View file

@ -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);
});