A Three-Pack of Core Animation Samples

Lucas Newman has posted a collection of three Core Animation sample projects. These examples stand out not only because they actually do interesting things, but the code is well-written and well commented.

The three samples are:

1. Wallball — a 3D pong-like game where you bounce the the ball against a backboard which stretches and distorts as the ball makes contact. Tons of great examples here. NSTrackingArea, Core Image filters, CATransactions, basic Core Animation geometry. Also fun to play.

Wallball


2. Scatter — Insects "scatter" as you hold down the mouse button to turn on a flashlight. They actively move around to avoid the light. Delegate-based CALayer drawing and animation, NSTimer, and Key-Value Observing.

Scatter


3. Jumpy — Grabs a snapshot of your desktop and "bounces" it off into the background. Examples of custom windows, the new Leopard-based CGWindow API, and implementing reflections.

Download Samples (1.4MB)
Design Element
A Three-Pack of Core Animation Samples
Posted Dec 16, 2007 — 13 comments below




 

Chuck — Dec 16, 07 5208

The Scatter example exposes a limitation of CoreAnimation: It can't handle layers that are too big to represent as (I think) a texture in your GPU. The limit on my GPU is 2048x2048. Since Scatter sets the layer's width to 702*3 (2106), I just get a big white flash. It's not too hard to work around in this case or many others, but it is something worth keeping in mind.

(I don't intend this as a knock against Lucas — it's some great sample code, and I'm sure lots of people will get good use out of it. I don't think anybody would expect him to test sample code on every hardware config Apple's shipped in the last five years, and a workaround would be less clear anyway.)

Wes — Dec 16, 07 5210

"It's not too hard to work around in this case..."

How would you do that?

Alexander Rauchfuss — Dec 17, 07 5211

@Wes
The easiest way would be to use CATiledLayer and the drawLayer:inContext: delegate method.

ssp — Dec 17, 07 5214

Neat examples, thanks to Lucas for that.

Jumpy didn't work for me at first. Just a white rectangle was bounced around pretty much as in what Chuck commented on. Disconnecting the second screen resolved that problem, but if we run into this problem so easily it may really be worth further discussion to see how to deal with it gracefully in an application.

George Dick — Dec 17, 07 5215

I had the same problem with needing to unplug my cinema display from my laptop... the image CGImageRef image was too large... This fixes it:
CGImageRef image = CGAutorelease(CGWindowListCreateImage( *(CGRect *)&displayBounds, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault));

George Dick — Dec 17, 07 5216

For Jumpy that is!

Lucas Newman — Dec 17, 07 5218

@Chuck
You are totally right about the flashlight layer size being too large — the layer size limitation is very inconvenient.

Switching to a CATiledLayer for the flashlight layer would definitely fix it, though.

jimi — Dec 17, 07 5221

I know nothing of this code malarky but please release Jumpy as a screensaver.

Its superfly.

Alexander Rauchfuss — Dec 18, 07 5222

I got scatter to work on my poor wimpy MacBook by separating the shadow and spotlight into two separate layers.

Altered Scatter

Blain — Dec 19, 07 5232

@jimi: Have you tried Fenętres Volantes?

Mark Stultz — Dec 24, 07 5286

It should also be noted that Apple has added (12/19/2007) another Core Animation sample code at:

http://developer.apple.com/samplecode/GeekGameBoard/index.html

Looks fun.

Merry Christmas.

julian — Mar 09, 09 6620

the file is down. anyone knows a mirror?

Micmoo — Apr 17, 09 6688

404'd




 

Comments Temporarily Disabled

I had to temporarily disable comments due to spam. I'll re-enable them soon.





Copyright © Scott Stevenson 2004-2015