Skip to content

Commit

Permalink
- recycle SVG after path is parsed.
Browse files Browse the repository at this point in the history
 - reduce point sample count for faster path parsing.
 - bump version
  • Loading branch information
zbryikt committed Sep 30, 2023
1 parent 9c7631e commit c3e251c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Logs

## v0.0.2

- recycle SVG after path is parsed.
- reduce point sample count for faster path parsing.


## v0.0.1

- initial release
10 changes: 5 additions & 5 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "@loadingio/parse-path",
"license": "MIT",
"description": "SVG Path Separation and Grouping",
"version": "0.0.1",
"version": "0.0.2",
"browser": "dist/index.min.js",
"main": "dist/index.min.js",
"files": [
Expand Down
11 changes: 7 additions & 4 deletions src/parse-path.ls
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ parse-path = (path, trianglify = false) ->
[shape,hole] = [[],[]]
svg = document.createElementNS \http://www.w3.org/2000/svg, \svg
svg.style <<< position: \absolute, opacity: 0, z-index: -1, top: 0
# appendChild is required to get correct result from complex shapes.
document.body.appendChild svg
ds = []
t1 = Date.now!
paths = path.replace(/Z/g,'z').split \\z
.filter -> it
.map ->
Expand All @@ -14,7 +16,9 @@ parse-path = (path, trianglify = false) ->
box = path.getBoundingClientRect!
len = path.getTotalLength!
pts = []
for i from 0 to (len + 1) by (if len => (len / 200) else 1) =>
# TODO 50 samples made here. we may want to use sth like bisect with a threshold distance
# so we can have a dynamic point sample count.
for i from 0 to (len + 1) by (if len => (len / 50) else 1) =>
p = path.getPointAtLength i
pts.push p.x * 0.005 - 0.8, 0.1 - p.y * 0.02
sum = 0
Expand Down Expand Up @@ -93,9 +97,8 @@ parse-path = (path, trianglify = false) ->
ret.pts ++= pts
for i from 0 til pts.length / 2 => ret.groups.push g
g++
svg.parentNode.removeChild svg
return ret
else
ret = []
for g in gs => ret.push g.members.map((i)->ds[i]).join('z')
return ret
svg.parentNode.removeChild svg
return ret

0 comments on commit c3e251c

Please sign in to comment.