TweetFollow Us on Twitter

Fun With Quartz Composer

Volume Number: 21 (2005)
Issue Number: 5
Column Tag: Programming

Fun With Quartz Composer

by Scott Knaster

Have you seen the incredible RSS Visualizer screen saver in Mac OS X Tiger? If you haven't, go check it out now: you'll find it in the Desktop & Screen Saver panel in System Preferences. Figure 1 gives you the flavor of what it looks like, but you really have to see the words swooping and twisting around on the screen to get the full astonishing effect.


Figure 1. RSS Visualizer is a hypnotically cool screen saver.

One of the best things about RSS Visualizer is that it wasn't created in the old-fashioned screen saver way by writing a little Cocoa program. Instead RSS Visualizer was built with no code whatsoever using a remarkable new tool called Quartz Composer. In this article, we're going to take a look at Quartz Composer and get a feel for what it does and how it works.

Quartz Composer is an application included with Xcode Tools 2.0. Quartz Composer lets you play with and learn about the cool Quartz graphics features built into Tiger without having to study APIs or write code. Documents you create with Quartz Composer, called compositions, can be turned into QuickTime movies or OS X screen savers. In this article, we'll use Quartz Composer to create a nifty screen saver without very much effort. It will take a little while to follow all the steps, but I promise the results will be worthwhile.

Here's a friendly warning: Quartz Composer is supercool and highly addictive. Before you start using it, be sure you have several hours (or perhaps an entire night) to dive in and try things out, because you might find it hard to walk away.

And now, let's get started.

Compose Yourself

To begin, go to /Developer/Applications/Graphic Tools and run Quartz Composer. As the program starts up, you might get an alert complaining that your Mac's video hardware won't support all the program's greatest features, but that's OK. If you get the alert, read what it says and click Continue.

Next, you see the New Composition Assistant offering several types of templates to start our composition. For this example, we're going to start from scratch, so just click Cancel to make the Assistant go away.

Use File > New to create a new, empty composition. You see two windows: The one that looks like a checkerboard is called the viewer; and the other is the editor.

On the left side of the editor window, make sure Patch Library is selected. In Quartz Composer, a patch is an individual graphical element or process. Patches are the building blocks you use to make compositions. We'll start this one with a cube. Look down the Name column in the Patch Library until you find the patch named Cube--it's in the Renderer category. Double-click Cube to add a cube patch to the composition. Instantly, if not sooner, a box labeled Cube shows up in the editor. There's also a cube showing in the viewer--you can't see it, because it has no color or texture yet, but you can tell it's there because it's blotting out the grid background (Figure 2).


Figure 2. Our composition starts with a cube. We only know it's there because part of the background is obscured in the viewer window.

Let's be honest about this: our cube is totally boring right now. Why, it's barely a cube at all. But if you take a look at the cube patch in the editor, you see that it has a bunch of values we can use to fill it that should make it far more interesting. Let's start by putting something on the front of the cube. In the Patch Library, find Image Importer (it's in the Generator category) and double-click to add one to the composition.

Note: when you're adding a patch to your composition, there are various ways to find the patch you're looking for. By default, the Patch Library is sorted by category. You might find it more useful to alphabetize the patch names--click the Name column to do that. If you know any part of the name of the patch or category, you can type text in the Search In Libraries field to get instant results. Note that some patches have a bullet in front of their names. The bullet indicates that these patches work best with graphics hardware that's not present on the computer running Quartz Composer, so they will run slowly if used on this computer. (The alert you might have seen when you started up Quartz Composer was warning about this.)

Click the new Image Importer patch to select it. Click the Inspector button in the toolbar (you can also choose Editor > Show Inspector or press Command-I).

In the Inspector, click the pop-up menu and choose Settings.

Face Front

Now you need to find a nice picture to put on a cube face. Click Import From File and navigate to any image you want in almost any format, including JPEG, TIFF, PNG, TIFF, and PDF. For best results, pick an image that's square or nearly so. If you can't find any images you like, look in the current user's Pictures folder. When you locate the image you want to use, double-click it. If you prefer, there's a cool shortcut: Drag an image file and drop it into the Image Data area in the Inspector. The image you picked should appear in the Image Data area.

Now we'll tell the cube to display the image you selected. In the editor, click the little circle next to the word Image in the Image Importer and drag to the circle next to Front Image in the Cube patch. You should see a spaghetti-like strand of yellow wire strung between the two circles or ports (Figure 3). Take a look in the viewer window--the cube now has the selected image on its face. It's not so lame any more.


Figure 3. All my tubes and wires, careful notes, and antiquated notions.

One of the most powerful features of Quartz Composer is its ability to create compositions that don't just sit there--they live, breathe, and move. To make your cube get active, you must change some of its settings repeatedly over time. In particular, if you change the rotation settings, the cube changes its orientation, and you see different parts of it. By changing the rotation values over and over, the cube spins. Quartz Composer includes a patch called an Interpolation that supplies an ever-changing stream of values. Find Interpolation in the patch library and double-click to add one to the composition.

Click the interpolation patch to select it, and then click Inspector to see its values. We want the cube to rotate by varying its orientation between 0 and 360 degrees, so we ask the interpolation to supply values that go from 0 to 360. In the Inspector, click the pop-up menu and choose Input Parameters. Leave Start Value at 0 and change End Value to 360. The Duration value determines how many seconds it takes to complete the interpolation. Set that value to 20. Leave the other values as they are.

Now that you have a source of values from 0 to 360, you need to supply those values to the cube for its rotation. Drag from the Result port of the Interpolation to the X Rotation port of the cube. Take a look at the viewer window. The cube is in motion! You should see the cube tumbling endlessly forward, all faces white except for the front, which still has an image on it. That's pretty neat, but we're just getting started.

Getting To Spin Alley

Because the cube is only changing its x-rotation, we never see its left and right sides. To fix that, we can spin the cube on its y-axis at the same time, and offset the z-rotation a bit (but not change it continuously). In the editor, drag another connection from the Results port, this time hooking it to the cube's Y Rotation. Click the Cube patch, and then click the Inspector button. Go to the Input Parameters panel. In the Z Rotation field, type 90, and then close the Inspector. Now the cube is tumbling, and all six faces eventually come into view.

Quartz Composer is drawing the cube over and over again, but something is missing: You see only one frame of the cube animation at a time. What's happening to the previous frames? As a development and debugging aid, Quartz Composer automatically erases the viewer before every redraw. This automatic erasing won't happen when you use the composition outside Quartz Composer. To see what that will look like, choose Viewer > Disable Background Erasing and take a look at the viewer. You soon get a big smear of cubes everywhere it's been drawn (Figure 4). That's not what you want. To prevent this from happening, you need a patch that erases each old frame of the cube before drawing a new one. Find the Clear patch in the Patch Library and double-click to create one.


Figure 4. The cube draws over itself repeatedly and eventually creates a big ugly mess.

You've added the Clear patch, and now the viewer window is...completely black! Nice job, Picasso! What's gone wrong? Take a look at the yellow numbers in the upper-right corner of the Cube and Clear patches. The number for Cube is 1, and Clear is 2. These numbers represent the rendering layer for each patch. Because the Clear patch was just added, it got rendering layer 2, the last layer in this composition. Quartz Composer draws compositions into an offscreen buffer, then moves the result onto the screen. Compositions are processed in order of their rendering layers. So each time through the composition, the cube is drawn (rendering layer 1), then cleared to black (rendering layer 2), then drawn on the screen! That's why you're seeing only a black screen in the viewer.

To fix this problem, we need to reverse the order: clear the screen first, and then draw the cube. To make this change, Control-click the title of the Clear patch and choose Rendering Layer. Now pick Layer 1. Note that the Rotating Cube's layer automatically changes to 2, and in the viewer window, your cube is back with the background now cleared to black, just as nature intended.

Picture This

Sure, you've got a lovely rotating cube, but five of the faces are blank. That's still some boring cube! We can make it better by putting things on the other faces, using various patches to show how cool Quartz Composer is. Start by adding an image from a Web page. Find the Image Downloader patch and double-click to add one. Open the Inspector, go to the Input Parameters panel, and type http://images.apple.com/home/2005/images/ipodfamilyphoto20050223.jpg into the Image URL field.

Connect the Image port of the Image Downloader patch to the cube's Left Image port. The Web image should instantly appear on the right face of the tumbling cube. (If it doesn't, double-check to make sure you typed the URL correctly, or type the URL of another image on the web.)

The next face is practical: it's going to show the time of day. To make it, add three patches: System Time, Date Formatter, and Image With String. Run a wire from System Time to Date Formatter via their Time ports. Make a connection from Date Formatter to Image With String using their String ports. Finally, connect the Image output from Image With String to the Right Image port on the cube. Now you have the time of day on the right face of the cube.

The time display is kind of messy: it's scaled and hard to read, certainly not up to our fancy-pants graphics standards. Let's fix that. Click Image With String, then click Inspector, and go to the Settings panel. Change both the Horizontal and Vertical Alignment to Center. Pick a different font if you like. Click Fixed Dimensions (in pixels). Fill in an Image Size of 1024 by 1024, and a font size of 200. Ah, the time now looks a lot better.

Showtime

Wouldn't it be cool if one of the cube faces continuously showed a QuickTime movie clip? Let's do it. Add an Image With Movie patch to the composition and open the Inspector. Go to the Input Parameters panel and type the path to a QuickTime movie (for example, /Users/yourName/Documents/myMovie.mov, or download a movie trailer from www.apple.com/quicktime and then type its local path). Connect the Image With Movie patch's Image output to the Cube's Top Image input. Take a look at the viewer--you should see the movie playing on the cube's top face as it rotates into and out of view. Awesome! Figure 5 shows the composition at this point.


Figure 5. The composition now has images on four faces (this picture shows three).

There are still two blank faces on the cube, but I'll leave those to you and your creativity now that you have a feel for how it works. Here are some ideas you can use to dress up those faces:

Use a Host Info patch to display various bits of interesting and obscure information, such as the current user name, computer name, OS version, or CPU load.

Set the background color of the cube faces by using the Inspector to change the Front Color, Left Color, and so on.

Hook an Interpolation patch to an RGB Color patch to continually change the colors you put on the faces.

When you're all done with the cube, it's easy to make it into a screen saver. Just use File > Save As and navigate to the current user's Library/Screen Savers folder. Save the composition into that folder. When you go to System Preferences and click Desktop & Screen Saver, you see your composition at the bottom of the list, available as an official screen saver. Click, select, and enjoy.

Note that when you install Tiger, it doesn't create the user's Library/Screen Savers folder by default. If you navigate there and find no such folder, you can click the New Folder button in the Save dialog to create it.

Like Disneyland, a Quartz Composer document is never truly finished as long as there is imagination left in the world. If you want to play more with Quartz Composer, here are a just a few of the many interesting features you might want to investigate:

Use a Lighting patch to get the nifty effect of shining a light on objects in your composition.

Check out the Clip Library in the editor window to add a pulsating Apple logo or shimmering background.

Add a Mouse controller to use the mouse to slide your objects around on the screen. This won't work in a screen saver because moving the mouse deactivates the screen saver.

Grab a Random patch to feed random inputs to an object, such as the location values for a cube, to make it warp around the screen.

Tiger's RSS Visualizer screen saver, built with Quartz Composer, uses patches that read RSS feeds and display them in a composition, among many other cool features. You can open RSS Visualizer in Quartz Composer and spend hours or days studying it. If you do, you'll get a strong education in the features of Quartz Composer.

For more ideas about what you can do with Quartz Composer, check out the folder at /Developer/Examples/Quartz Composer. Quartz Composer is an amazing tool for creating beautiful images without any programming. Enjoy exploring it - but don't forget to go back to your regular life from time to time.


Scott Knaster lives a glamorous life that consists mostly of sitting in a chair and typing on a keyboard. Scott recently completed his latest book, Hacking Mac OS X Tiger, to be published in July by Wiley. This article is based on material in that book. Scott is a recovering Quartz Composer addict.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.