Archive for the ‘CoffeeScript’ Category
All that’s great about CoffeeScript in 40 characters
Today, I needed a JavaScript equivalent of Ruby’s Array#compact
(which returns the array stripped of any nil
values).
A standard JavaScript implementation looks like this:
Array.prototype.compact = function() { var x, _i, _len, _results; _results = []; for (_i = 0, _len = this.length; _i < _len; _i++) { x = this[_i]; if (x != null) { _results.push(x); } } return _results; };
But since I’m using CoffeeScript, I get this monstrosity of line-noise and distraction for free when I type this:
Array::compact = -> x for x in @ when x?
This is some of the best of CoffeeScript: array comprehensions, implicit return
, the existential operator ?
, and more. Taking 11 lines of JavaScript busywork and turning it into 40 pithy, elegant characters.
Spindlytext: write in the sky with the Google Earth API
I’ve just released Spindlytext on Github. It’s the library that powers the live data display in Pigeon Sim, by creating KML linestrings in the shape of letters.
A depthcam? A webkinect? Introducing a new kind of webcam
Update, 10 February: Sorry for some serious reliability issues over the last few days. The streaming server is now hosted in-house at CASA, which should be a lot more robust.
At CASA we’ve been looking at using a Kinect or three in our forthcoming ANALOGIES (Analogues of Cities) conference + exhibition.
We’ve been inspired in part by Ruairi Glynn‘s amazing work here at UCL, and Martin has been happily experimenting with the OpenKinect bindings for Processing.
Meanwhile, I recently got to grips with the excellent Three.js, which makes WebGL — aka 3D graphics in modern browsers — as easy as falling off a log. I’m also a big fan of making things accessible over the web. And so I began to investigate prospects for working with Kinect data in HTML5.
There’s DepthJS, an extension for Chrome and Safari, but this requires a locally-connected Kinect and isn’t very clear on Windows support. There’s also Intrael, which serves the depth data as JPEG files and provides some simple scene recognition output as JSON. But it’s closed-source and not terribly flexible.
The depthcam
So I decided to roll my own. I give you: the depthcam!
Overlapping markers on your Google Map? Meet OverlappingMarkerSpiderfier
Ever noticed how, in Google Earth, marker pins that overlap each other spring apart gracefully when you click them, so you can pick the one you meant?
And ever noticed how, when using the Google Maps API, the exact same thing doesn’t happen?
This code makes Google Maps API version 3 map markers behave in that Google Earth way. Small numbers of markers (up to 8, configurable) spiderfy into a circle. Larger numbers fan out into a (more space-efficient) spiral.