Skip to content

Commit

Permalink
Precision (#115)
Browse files Browse the repository at this point in the history
* Round the calculated fraction values to maximum 6 decimals

* Fix test

* Round compute

* Revert "Round compute"

This reverts commit 1bf9f25.

* Revert "Fix test"

This reverts commit 322e391.

* Revert rounding to calc

* Do not round angle values

* Fix precision count
  • Loading branch information
yuanchuan committed Sep 4, 2023
1 parent 4c7d74c commit fb45cfa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/generator/shapes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { clamp, is_empty, make_tag_function } from '../utils/index.js';
import { clamp, is_empty, make_tag_function, round } from '../utils/index.js';
import parse_shape_commands from '../parser/parse-shape-commands.js';
import parse_value_group from '../parser/parse-value-group.js';
import parse_direction from '../parser/parse-direction.js';
Expand Down Expand Up @@ -191,12 +191,12 @@ function create_polygon_points(option, fn) {
let angle = calc_angle(x, y, dx1, dy2, direction);
if (unit !== undefined && unit !== '%') {
if (unit !== 'none') {
x += unit;
y += unit;
x = round(x) + unit;
y = round(y) + unit;
}
} else {
x = (x + 1) * 50 + '%';
y = (y + 1) * 50 + '%';
x = round((x + 1) * 50) + '%';
y = round((y + 1) * 50) + '%';
}
points.push(new Point(x, y, angle));
}
Expand Down Expand Up @@ -338,7 +338,7 @@ function create_shape_points(props, {min, max}) {
if (props.move) {
[x, y, dx, dy] = translate(x, y, props.move);
}
return [x, y, dx, dy];
return [round(x), round(y), round(dx), round(dy)];
});
}

Expand Down
8 changes: 8 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ function get_grid(input) {
return { x, y }
}

function round(value, precision = 6) {
const power = Math.pow(10, precision + 1)
let result = Math.round((value * power) + (Number.EPSILON * power)) / power;
if (Number.isNaN(result)) result = 0;
return result;
}

export {
clamp,
maybe,
Expand All @@ -216,4 +223,5 @@ export {
lerp,
unique_id,
get_grid,
round,
}

0 comments on commit fb45cfa

Please sign in to comment.