fixed precision floats from rough
This commit is contained in:
parent
88c72c96f4
commit
ebdc3b029c
1 changed files with 14 additions and 0 deletions
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue