Birthdays and Canvas P.J.s

The first checkin to the Disk-Clock Mercurial repository was on December 25th, 2007. The Quartz Composer file – my first attempt – is dated the 2nd. I gave up because tearing apart the time value was too painful in the graphical language. I found out later composer actually has a JavaScript element. Oops. Or maybe I decided that if I had to use JavaScript I might as well go all the way. (Oh, and my birthday is Monday, too.)

Last week or so I tried to take another fast/reflection. I rather botched the later. The problem with open reflection is that there isn’t any urgent thing and it’s easy to wander. I found the RubyConf Videos early on, and had them playing in the background for the rest of the weekend.

One, however covered the slightly tangential topic of Directed Attention Fatigue. It’s considered a sufficiently tangible phenomenon to be studied. Directed attention acts as a kind of resource (a neurotransmitter perhaps?) that can be depleted. Coincidentally, the conditions that tend to restore directed attention are optimized in natural settings.

I’ve been feeling a little harried lately, and I realized that my habit of exploring parks and trails faded out a long time ago. Saturday I went down to the waterfall; some of the big rains seem have carved out the splashpool quite bit. The immediate area beyond that is pretty full of rock, and beyond that is pretty clear. I wonder if they dredged it out when they rebuilt the bridge.

After that I went across town to East Avenue Park. The main path has been paved; they installed benches, with little signs describing the local wildlife. There are a few overlook decks as well. And I only got through half the park – it’s the half with a long path out the back of the park and into the wood beyond. It was a good walk though.

I was feeling fairly good afterwards, but it was hardly a scientific experiment. I also got going on a wicked cool project this weekend. Or maybe just wicked. Or even just stupid – if it was that useful someone would have done it by now, of course.

A proof-of-concept is up for Canvas P.J.s – a JavaScript library to capture canvas output to PostScript.

Apple’s OSX uses ‘Display PostScript’, which derives from the Adobe printer language PostScript. When it came time to design Dashboard, Apple added a special hook from the HTML based widgets to the Display PostScript system and called it the canvas tag. The JavaScript API and basic programming model of the canvas tag roughly correspond to PostScript. So, it becomes fairly easy to make a new canvas context type that captures it’s output to PostScript file.

This is probably about one day’s work, what with all the other things going on. That it works at all after that time is a wonder, so please don’t expect too much yet.

I’m currently testing the output by copy-pasting the results into a .eps file, and then running ps2pdf to create a PDF, which gets launched in the OSX Preview.app. Any other use of GhostScript or another PostScript parser should also work, in theory.

Challenges

The basics went pretty well. Fancy that with taking an API design backwards ;^) I had most of Disk Clock rendering on the first day. The problem is images – you’ve got to move the image data over somehow.

I Can’t Let You Do That, Dave

There is no way to get the data of an image. You can however get the data out of a regular canvas (so the spec says) and you can draw an image into a canvas.

I Can’t Let You Do That, Dave

Unfortunately, once you’ve drawn an image into a canvas, it’s tainted. Unless the image came from the same domain as the page. Your hard drive is the same domain right?

I Can’t Let You Do That, Dave

It’s my hard drive. I can elevate privileges to write files and open browser dialogs for crying out loud.

I Can’t Let You Do That, Dave

Hay, Google!

Nope, You Can’t Do That, Dave (unless you have a webserver)

How’d that screen-shot work? It raises privileges to write files, and then uses a related API (data uri) to get a png. Huh, I wonder if the data retrieval was benefiting from the privileges escalation?

Are You Sure, Dave?

Yes.

Okay.

That fixes FireFox, anyway. getImageData still isn’t a very common API, however. Incidentally, since this doesn’t seem to be widely known, that magic incantation is:

netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");

We Don’t Speak Alpha Here.

On the other side of the fence, when I finally got images into PostScript, the corners were solid. (I’m testing on Disk Clock, not square clock, if you recall.) So, how do you enable alpha in PostScript? You don’t. Google agrees – except for one guy who will sell you a difficult, awkward, hack. You can do masking to cut off the corners, but they’ll be jagged.

But They Do Over There

Of course, time marches on. PostScript isn’t very common these days, even if it is kind of cool. It’s a programming language if you didn’t know. However, it’s descendant, PDF, is quite popular. Given the relation, it might be possible to do a PDF backend sometime in the future. One of the Ruby Conf videos was (in part) on the Prawn PDF generator; there are some substantial differences, but hopefully enough could transfer over to make it reasonable.

Posted Sunday, December 14th, 2008 under Devlog.

3 comments