From fb45cfa6c8493ff49816879e0cbbae0d965fd793 Mon Sep 17 00:00:00 2001 From: Yuan Chuan Date: Mon, 4 Sep 2023 10:13:34 +0800 Subject: [PATCH] Precision (#115) * Round the calculated fraction values to maximum 6 decimals * Fix test * Round compute * Revert "Round compute" This reverts commit 1bf9f258b6036423609a8c4266ffc03871cd2854. * Revert "Fix test" This reverts commit 322e391f9e8f106bb26719ba56960e9f7d36676a. * Revert rounding to calc * Do not round angle values * Fix precision count --- src/generator/shapes.js | 12 ++++++------ src/utils/index.js | 8 ++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/generator/shapes.js b/src/generator/shapes.js index e51149e..6820a91 100644 --- a/src/generator/shapes.js +++ b/src/generator/shapes.js @@ -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'; @@ -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)); } @@ -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)]; }); } diff --git a/src/utils/index.js b/src/utils/index.js index 66293eb..e6f8e62 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -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, @@ -216,4 +223,5 @@ export { lerp, unique_id, get_grid, + round, }