Hacker News new | ask | show | jobs
by pavlov 568 days ago
Canvas is one of the easiest browser APIs to implement because most of it maps 1:1 to PostScript-style 2D APIs.

It was created by Apple as a wrapper for CoreGraphics back around Mac OS X Tiger 10.4. (IIRC, Canvas even debuted in Apple's Dashboard widgets rather than Safari? I'm not 100% sure.)

I remember writing my own Canvas implementation around 2008-2009 using JavaScriptCore and CoreGraphics. It took maybe two days. I used it for a video application where JS plugins needed to render 2D graphics but I couldn't provide a browser engine because that would have been too slow and unpredictable for render functions that get called in the video render loop.

1 comments

I'd love to learn more about how your 2 day implementation did fillText (incl full unicode rendering, proper text shaping etc)
I'm not claiming I implemented everything from scratch. Since it was a Mac OS X Cocoa app, it would just call the APIs available on that platform, like Apple's original Canvas implementation in WebKit.

Also since this was a video application running custom scripts written specifically for that host app, there wasn't a big worry about exact compatibility with any other Canvas-using code.

Anyway, here's the old code that doesn't look like there's anything interesting going on:

https://github.com/pojala/lacqit/blob/main/Lacqit/LQJSBridge...

Looks like I misremembered — it's using Cairo rather than CoreGraphics. I don't remember why that was.