diff --git a/d2renderers/d2sketch/sketch.go b/d2renderers/d2sketch/sketch.go index 4564c0f5b..8425b2fa3 100644 --- a/d2renderers/d2sketch/sketch.go +++ b/d2renderers/d2sketch/sketch.go @@ -3,6 +3,7 @@ package d2sketch import ( "encoding/json" "fmt" + "regexp" "strings" _ "embed" @@ -33,6 +34,8 @@ fillStyle: "solid", bowing: 2, seed: 1,` +var floatRE = regexp.MustCompile(`(\d+)\.(\d+)`) + func (r *Runner) run(js string) (goja.Value, error) { vm := (*goja.Runtime)(r) return vm.RunString(js) @@ -490,6 +493,17 @@ func extractRoughPaths(r *Runner) ([]roughPath, error) { return nil, err } + // we want to have a fixed precision to the decimals in the path data + for i := range roughPaths { + // truncate all floats in path to only use up to 6 decimal places + roughPaths[i].Attrs.D = floatRE.ReplaceAllStringFunc(roughPaths[i].Attrs.D, func(floatStr string) string { + i := strings.Index(floatStr, ".") + decimalLen := len(floatStr) - i - 1 + end := i + go2.Min(decimalLen, 6) + return floatStr[:end+1] + }) + } + return roughPaths, nil }