peterc / trtl

Tk-powered Ruby turtle graphics
MIT License
64 stars 4 forks source link

Create fallback to using HTML5 canvas if TK isn't available #2

Open TheNotary opened 9 years ago

TheNotary commented 9 years ago

So ever since Peterc first introduced this gem to me, I thought it was really neat, but I could never get it to work on my platfrom due to the status of the tk package on windows and linux. It must work much nicer on macs? I would like to humbly propose we implement the canvas in HTML5 in the case that the TK package isn't accessible.

How about this:

Now you're probably wondering at this point, "How is old Notary Sojac going to draw to an HTML5 canvas in a ruby library." And that is where it gets tricky, but not too tricky.

Rendering to an HTML Canvas instead of a TK Canvas

Architecture

What do you think? This proposal is minimally invasive (requires only the addition of lines of code: an extra .rb file tucked away, Gemfile, Gemspec, require conditionality, etc). If you're really excited about the idea, we can re-write the Tk* function calls to Canvas* calls and upon initialization, define what Canvas* translates to (either the actual TK library commands or the ruby commands that delegate that same functionality to javascript).

P.S. Peter, I tried to get a hold of you over email, but looks like you're enjoying the summer holiday. I hope you're having a good time in Sweden! Maybe we can touch base when you get back? I'll gently spam you another email when you're back to your routine. Also is it possible for folks to arrange for a seat at your App Academy lectures?

peterc commented 9 years ago

I was on holiday, yeah, but unfortunately I don't recall getting an email from you. Feel free to resend to peter@peterc.org :) Things to my GitHub email account are more likely to go to spam considering how many people spam these addresses nowadays :( Regarding App Academy, no, I'm not currently doing anything with them.

On the idea presented here, it sounds like a good one! I wonder if there's already some sort of Canvas abstraction for Ruby out there? If so, using it would be a good idea. But otherwise it's not particularly complex and I'm keen on your overall idea for increasing the accessibility of the code (although I must admit I'd generally just jump ship to JavaScript for something like this if browser support were considered a must).

TheNotary commented 9 years ago

Btw, I sent you that email from wastyx@gmail.com (an old gamer handle). I'll follow up there.

"Some sort of canvas abstraction for ruby" I think this will be the first effort in bridging ruby CLI and web canvases.

So with your comments, I'll outline my plan for adding HTML support to this gem:

RubyTube's API might look something like

rt = RubyTube.new

# Point RubyTube to where the html public directory is
rt.www_root = "gui/"

# Possibly some routes setup for Sinatra
# rt... 

# start sinatra, faye/websockets and prints "browse to 127.0.0.1:4567"
rt.run!

Finally, RubyTube is included into the trtl and we can see it in action. I might need to do something to 'cache' commands, those 10.times {up} commands might give the websockets some trouble, but this is unknown to me. Once we can see it in action, if you think it's progress, I can put together a follow up commit to pull out the TK code alltogether (instead of writing code to support both).

TheNotary commented 9 years ago

I've got a working prototype on github now. Checkout the html-canvas branch: html-canvas

So to use it now:

$  irb
require 'trtl'

>  fd
>  left 90
>  fd

After you make a new turtle (or issue some commands to InteractiveTurtle), it will spin up the web/websocket servers (I haven't cleaned up the garbage output that spams the screen yet).

Now you should be able to navigate to 127.0.0.1:4567 and see a mostly blank canvas (there should be a trtl arrow). In the irb shell, you can control the trtl, and immediately, your updates should be reflected in the browser view thanks to the websocket server on port 4555.

What do you think of the overall approach?

'''Edit:''' I fixed InteractiveTurtle btw and edited out debugging comments =)

peterc commented 9 years ago

Unfortunately I haven't got time to check it out properly yet but from the way you describe it sounds like a reasonable approach, so yes!

TheNotary commented 9 years ago

I threw together a quick demo of it in action. The one problem is that the tree example is kind of hard on the rendering system. Sometimes it goes through fine, and sometimes it crashes fire fox.

Btw, reduce your volume level before starting the demo, youtube seems to have hidden the volume controls somewhere new in the video manager.

https://www.youtube.com/watch?v=P3ySQWH47Ks

peterc commented 9 years ago

That is really cool!!

On Mon, Aug 17, 2015 at 4:56 PM, TheNotary notifications@github.com wrote:

I threw together a quick demo of it in action. The one problem is that the tree example is kind of hard on the rendering system. Sometimes it goes through fine, and sometimes it crashes fire fox.

Btw, reduce your volume level before starting the demo, youtube seems to have hidden the volume controls somewhere new in the video manager.

https://www.youtube.com/watch?v=P3ySQWH47Ks

— Reply to this email directly or view it on GitHub https://github.com/peterc/trtl/issues/2#issuecomment-131871920.

Peter Cooper http://peterc.org/ http://twitter.com/peterc

TheNotary commented 9 years ago

Ok, I've made all the changes to Magic Mirror that I think I'm going to be making in the near future, and also made awesome optimizations to the javascript rendering system within trtl.

https://github.com/TheNotary/trtl/commit/0582b2f35dc148b2187b77fbfffa43df60e02e06

I think now we wait till you have a moment to look things over, check that you think this way is so good that it can replace the TK dependency, and add any spec changes you'd like to see. After that I'll either completely remove the old references to TK in trtl, or hook up a fallback solution if preferred. Finally I'll issue a final pull request and we call it a feature =)

peterc commented 9 years ago

OK, I've finally given it a go. Some observations:

TheNotary commented 9 years ago

It's great to hear back from you, Peter. In order:

I want this feature to be perfectly inline with your vision for the project, so thank you for getting the feedback to me. I'll get back to you with a new commit within the next week or so based on this input.

TheNotary commented 9 years ago

Hi Peter,

Btw, what is the aproximate rendering time of the tree example with TK support?

Edit: I did my best to setup an env and do gem install trtl on windows w/ ruby 1.9.3 (and 2.x) and active tlc, but got an error =( http://pastebin.com/H67XK9RS If you have tips on getting this functional on a mac (I'll try an apple store after a relocation I'm in the midst of) let me know and I can both try it out and also drop a paragraph touching on TK setup in the readme. Sorry I can't do a better job testing it, it's ironic that my impedes to add the feature was due to the fact that I could never get an operational install going on my end, heh. Anyway, I'd also like to add no rush, we're both a bit on the busy side I think.

Thanks again for that letter, btw!!! =)