Skip to content

Instantly share code, notes, and snippets.

@Abdur-rahmaanJ
Forked from kosamari/transformer.js
Created August 31, 2017 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Abdur-rahmaanJ/9984f438686c890f19f4910d95239d4f to your computer and use it in GitHub Desktop.
Save Abdur-rahmaanJ/9984f438686c890f19f4910d95239d4f to your computer and use it in GitHub Desktop.
transform function for sbn compiler
function transformer (ast) {
var svg_ast = {
tag : 'svg',
attr: {
width: 100, height: 100, viewBox: '0 0 100 100',
xmlns: 'http://www.w3.org/2000/svg', version: '1.1'
},
body:[]
}
var pen_color = 100 // default pen color is black
// Extract a call expression at a time as `node`. Loop until we are out of expressions in body.
while (ast.body.length > 0) {
var node = ast.body.shift()
switch (node.name) {
case 'Paper' :
var paper_color = 100 - node.arguments[0].value
svg_ast.body.push({ // add rect element information to svg_ast's body
tag : 'rect',
attr : {
x: 0, y: 0,
width: 100, height:100,
fill: 'rgb(' + paper_color + '%,' + paper_color + '%,' + paper_color + '%)'
}
})
break
case 'Pen':
pen_color = 100 - node.arguments[0].value // keep current pen color in `pen_color` variable
break
case 'Line':
...
}
}
return svg_ast
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment