Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full rendering API #103

Open
3 tasks
edemaine opened this issue Nov 16, 2022 · 0 comments
Open
3 tasks

Full rendering API #103

edemaine opened this issue Nov 16, 2022 · 0 comments

Comments

@edemaine
Copy link
Owner

edemaine commented Nov 16, 2022

Suppose we want to use SVG Tiler without a CLI or CLI-inspired ideas (like svgtiler()), just raw API calls from a Node/Coffee script. In this case we should gain huge flexibility in terms of using/combining multiple drawings (#97), or generating lots of SVG files from various tweaks of a single drawing (as in animations #72), etc. But we need the API to make it easy to do the main render loop in multiple ways.

Here's a sketch:

render = new Render
drawing1 = svgtiler.require 'drawing1.asc'
drawing2 = svgtiler.require 'drawing2.asc'
# All in one:
render.render layout, drawing1, mapping1
render.render layout, drawing2, mapping2
# Or perhaps make it easy to share layouts (but still allow changing):
render.setLayout layout
render.render drawing1, mapping1
render.render drawing2, mapping2
# The `render` method would be shorthand for an explicit for loop over cells.
# Note that this style doesn't require any actual mappings;
# the code itself is acting as the mapping function.
render.setLayout layout
drawing1.forEach ({key, i, j}) -> # argument is Context
  render.addTile i, j, <symbol z-index="5">...</symbol>
drawing2.forEach ({key, i, j}) ->
  render.addTile i, j, ...svg content...
# And finally write current content to SVG:
render.writeSVG 'filename.svg'

This whole thing could be in a for loop over an svgtiler.glob array, to render multiple files in a similar way. It's like a Maketile and a mapping in one. (In fact this could be a Maketile, provided it's wrapped in an export make = ->.) And because you can call render or forEach many times, it's far more powerful.

To make it even more interesting, Yev suggests pattern matching alternatives to forEach. For example:

# Constant-size pattern
drawing.match [['o', 'x']
               ['x', 'o']], (context) -> ...
# Match horizontal patterns starting with a, b and ending with y, z
drawing.matchRow ['a', 'b'], ['y', 'z'], (context) ->
# Match vertical patterns starting with a, b and ending with y, z
drawing.matchColumn ['a', 'b'], ['y', 'z'], (context) ->
# Maybe also diagonal and antidiagonal patterns.
# Strings could also be regular expressions or functions detecting match.

Context would be given more information now, like a range of is and js for the match, total bounding box, etc.

For example, the chess graph could get rendered as follows. I'm not sure this is really cleaner for this application, but it's an interesting alternative to have available.

# rook attacks
drawing.matchRow ['r'], [/[A-Z]/]
drawing.matchRow ['R'], [/[a-z]/]
drawing.matchRow [/[A-Z]/], ['r']
drawing.matchRow [/[a-z]/], ['R']

Related to #34.

  • forEach
  • addTile
  • match
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant