Created
November 6, 2015 19:44
-
-
Save maxattack/f342af2d5ea1a77c630e to your computer and use it in GitHub Desktop.
I had some SVG data to unpack, which I used as an excuse to flex my functional muscles...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
checkStateChange = (token) -> switch(token) | |
when 'm' then state_moveTo | |
when 'c' then state_curveTo0 | |
when 'l' then state_lineTo | |
when 'z' then null | |
else null | |
checkPoint = (token) -> | |
numbers = token.split ',' | |
if numbers.length != 2 | |
return null | |
else | |
return [Number(numbers[0]), Number(numbers[1])] | |
state_moveTo = (token) -> | |
newState = checkStateChange(token) | |
if newState != null | |
return newState | |
p = checkPoint(token) | |
if p != null | |
[x, y] = p | |
doMoveTo x, y | |
return state_moveTo | |
return null | |
state_curveTo0 = (token) -> | |
newState = checkStateChange(token) | |
if newState != null | |
return newState | |
p = checkPoint(token) | |
if p != null | |
[x, y] = p | |
return (token) -> state_curveTo1(token, x, y) | |
return null | |
state_curveTo1 = (token, x0, y0) -> | |
p = checkPoint(token) | |
if p != null | |
[x, y] = p | |
return (token) -> state_curveTo2(token, x0, y0, x, y) | |
return null | |
state_curveTo2 = (token, x0, y0, x1, y1) -> | |
p = checkPoint(token) | |
if p != null | |
[x, y] = p | |
doCurveTo x0, y0, x1, y1, x, y | |
return state_curveTo0 | |
return null | |
state_lineTo = (token) -> | |
newState = checkStateChange(token) | |
if newState != null | |
return newState | |
p = checkPoint(token) | |
if p != null | |
[x, y] = p | |
doLineTo x, y | |
return state_lineTo | |
return null | |
doMoveTo = (x,y) -> console.log "moveTo (#{x}, #{y})" | |
doCurveTo = (x0, y0, x1, y1, x2, y2) -> console.log "curveTo (#{x0}, #{y0}) (#{x1}, #{y1}) (#{x2}, #{y2})" | |
doLineTo = (x,y) -> console.log "lineTo (#{x}, #{y})" | |
svgdata = "m 385.87798,90.696981 c 10.55149,15.865289 24.30523,49.980889 37.31095,109.706159 l -12.46103,1.76405 c 15.13742,71.39087 12.84246,100.86263 14.76188,197.59949 l -7.99055,0.3494 c 0,0 0.70872,5.8131 0.61941,11.61667 5.17594,2.79215 6.14519,10.31226 6.26218,18.88086 0.1085,7.94692 -0.30651,15.82536 -6.24269,19.86751 -3.60054,22.86022 -13.35696,35.6257 -32.26015,35.6257 0,0 0.0876,-357.70721 0,-395.409839 z" | |
state = checkStateChange | |
for token in svgdata.split ' ' | |
state = state(token) | |
if state == null | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment