Safari Reference Library Apple Developer
Search

Safari on iPhone Graphics, Media, and Visual Effects Coding How-To's

Graphics

How do I use the canvas tag to draw vector graphics and animation?

You can use the HTML5 <canvas> tag to set up a 2D bitmap context and then draw to that context using JavaScript. Graphics that can be drawn to the context of the <canvas> tag include lines, arcs, bezier curves, quadratic curves, images, shadows, and gradients. Many of the graphics that you see in Dashboard widgets on Mac OS X - such as the graph in the Stocks widget, and the hands of the World Clock widget - are drawn using the <canvas> tag. Listing 1 shows how to draw a circle using the <canvas> tag.

Listing 1: Draw a circle using the canvas tag.

<html>
<head>
<script>
function drawCircle() {

      /* get the main canvas and its context */
      var myCanvas = document.getElementById("myCanvas");
      var context = myCanvas.getContext("2d");

      /* draw a circle */
      radius = 90; x = 150; y = 150;
      context.beginPath();
      context.arc(x, y, radius, 0, 2 * Math.PI, false);
      context.closePath();
      context.setFillColor("3300FF");
      context.fill();
}
</script>
</head>
<body onload="drawCircle()">
      <canvas id="myCanvas" height="300" width="300" style="border: 2px solid; margin: 30px;" />
</body>
</html>

For more information about how to use the <canvas> tag, see the "Using the Canvas" section of WebKit DOM Programming Topics. For a list of available methods that you can use to draw vector graphics using <canvas>, see the "CanvasRenderingContext2D" class entry of the WebKit DOM Reference.



Media

How do I specify a poster image for audio or video in a webpage?

Safari on iPhone plays audio and video in webpages in fullscreen playback mode only. Since video is not decoded until a user enters playback mode, a gray rectangle with the Quicktime logo is shown until the Play button is pressed. It is possible to replace this gray rectangle with a still image. In the <embed> tag for your media, specify an image for the src attribute and use the href attribute to link to your media source file. Listing 2 illustrates this technique.

Listing 2: Using a poster image.

<embed src="poster.jpg" href="movie.m4v" type="video/x-m4v" />

For more information on using poster images, see the "Embedding Movies in Webpages" section of the Safari Web Content Guide for iPhone OS.

How do I determine whether my server will support random access in media playback?

iPhone uses byte-range requests to perform random access in media playback. To determine whether the server hosting your media files supports this type of request, you can attempt to download part of a file from the server using the curl command in Listing 3. Replace "example.com" with the name of your server, and replace "test.m4v" with the name of a known media file on your server.

Listing 3: curl command to test for byte-range request support.

curl --range 100-149 http://example.com/test.m4v -o /dev/null -s -w "%{size_download}\n"

If this command reports 50—meaning that curl downloaded 50 bytes—then the server correctly handled the byte-range request. If the entire file was downloaded, the server does not handle byte-range requests.

For more information on configuring your server to support random access in media playback, see the "Configuring Your Server" section of the Safari Web Content Guide for iPhone OS.

How do I load different video files dynamically based on the current network connection speed?

If you are displaying video in a webpage and want to load different video files dynamically based on the current network connection speed, use a reference movie. Reference movies can be created using QuickTime Pro 7.2.1 or later, or by using the MakeRefMovie tool available at http://developer.apple.com/quicktime/quicktimeintro/tools.

For more information on reference movies, see the "Creating a Reference Movie" section of the Safari Web Content Guide for iPhone OS.



Visual Effects

How do I use CSS to position or transform page elements in 2D or 3D space?

CSS transform properties provide powerful formatting possibilities, without having to resort to plug-ins or static images to display the desired layout. HTML elements including text, images, media elements for audio and video, and more can be transformed via translation, scaling, rotation, skewing, or a transform matrix. Transforms are specified with the transform property, which supports a list of functions so that transforms can be chained together.

For example, to scale an image to half its size, flip it on the x-axis, and rotate it by 12 and a half degrees, you could use the CSS transform property in Listing 4.

Listing 4: CSS transform example.

<img src="safari.png" style="-webkit-transform: scale(0.5) scaleX(-1) rotate(12.5deg);"/>

This is just a glimpse of what is possible when using CSS transforms. More information about how to transform page elements using CSS can be found in the “Transforms” section of the Safari Visual Effects Guide.

How do I change the origin of a CSS transform?

When an element is translated, rotated, scaled, skewed or transformed in some other way using CSS, the origin of the transform defaults to the center of the element. You may find the need to transform an object with the origin positioned elsewhere on the element. You can specify the origin for a CSS transform using the transform-origin property. This property has the same syntax as background-position, and values for this property may be expressed using constants (top, bottom, left, right, center), as a CSS length unit, or as a percentage of the element size. Listing 5 illustrates how to set the transform origin to the top left corner when rotating a line of text.

Listing 5: Changing the origin of a CSS transform.

<div style="-webkit-transform:rotate(20deg); -webkit-transform-origin: top left;">
    Hello Safari Developers!
</div>

For more information on changing the origin of a transform, see the "Changing the Origin" section of the Safari Visual Effects Guide, and the Safari CSS Reference.

How do I obtain a realistic scene when transforming elements in 3D space?

To obtain a realistic scene when transforming elements in 3D space, you should 1) define perspective so that elements in the distance appear smaller, and 2) make sure that nested elements are not flattened onto a single plane. To define perspective, use the perspective CSS property; smaller values for this property will make the perspective more pronounced. To prevent nested elements from being flattened onto a single plane, set the value of the transform-style property to preserve-3d.

Listing 6 illustrates how to set the perspective and transform-style properties. Remember that the perspective property does not apply directly to the element, but rather to the children of that element.

Listing 6: Setting perspective and preserving 3D.

<html>
<head>
<style>
    .box {
            height: 100px;
            width: 100px;
            background-color: purple;
            position: absolute;
            top: 100px;
            left: 100px;
      }

    .translated {
            -webkit-transform: translateZ(50px);
            background-color: yellow;
      }

      #main {
            -webkit-perspective: 500;
            -webkit-transform-style: preserve-3d;
            -webkit-transform: rotateY(60deg);
      }
</style>
<meta name="viewport" content="width=device-width">
</head>
<body>
      <div id="main">
            <div class="box"></div>
            <div class="box translated"></div>
      </div>
</body>
</html>

For more information on transforming elements in 3D space, see the "Setting the Transform Style" section of the Safari Visual Effects Guide and the CardFlip sample code.

How do I use CSS transitions to add animation to my web content?

A transition can be thought of a type of simple animation. You specify which CSS properties will be transitioned, and the duration of the transition. When you use JavaScript or CSS to change the value of a CSS property, WebKit—the layout engine that powers Safari—takes care of smoothly animating from the old value to the new value.

For example, Listing 7 shows how to make a green box shrink to half its size and turn yellow when clicked, over a period of 1.5 seconds.

Listing 7: Using CSS transitions to add animation.

<html>
<head>
<style>
    #box {
        -webkit-transform: scale(1.0);
        -webkit-transition-property: -webkit-transform, background-color;
        -webkit-transition-duration: 1.5s;
        background-color: green;
        border: 2px solid;
        height: 200px;
        width: 200px;
        }
</style>
<script>
    function transitionBox(box) {
        box.style.webkitTransform = 'scale(0.5)';
        box.style.backgroundColor = 'yellow';
    }
</script>
</head>
<body>
    <div id="box" onclick="transitionBox(this);"></div>
</body>
</html>

Be sure to specify a duration for your transition using the transition-duration property. Otherwise, the duration of your transition will be zero, and you will not see an animation between the changes in state. For more information on CSS transitions, see the “How Transitions Work” section of the Safari Visual Effects Guide.

How do I use CSS animations to add animation to my web content?

CSS animations are a lot like CSS transitions, except that you have fine-grained control over the keyframes of the animation, and you can listen for DOM events to be notified of an animation's start, end, or iteration. CSS animations introduce the @keyframes rule to define animations. You can then use the name of the animation you've defined as the value of the animation-name property on other elements.

Listing 8 shows a simple example of CSS animation. For the first 50% of the animation, the box scales down to 30% of its original size. For the second half of the animation, the box scales back up to its full size. Note that this animation occurs over a duration of 4 seconds.

Listing 8: Using CSS animations to add animation.

<html>
<head>
<style>
    @-webkit-keyframes changeScale {
        0% {
            -webkit-transform: scale(1.0);
        }
        50% {
            -webkit-transform: scale(0.3);
        }
        100% {
            -webkit-transform: scale(1.0);
        }
    }

    #box {
        height: 300px;
        width: 300px;
        background-color: purple;
        -webkit-animation-name: changeScale;
        -webkit-animation-duration: 4s;
    }
</style>
<meta name="viewport" content="width=device-width">
</head>
<body>
    <div id="box"></div>
</body>
</html>

For more information about CSS animations, see the "How Animations Work" section of the Safari Visual Effects Guide.

How do I control the acceleration curve for a CSS transition or animation?

You can control the acceleration curve of a CSS transition or animation by specifying a timing function. By default, CSS transitions and animations use a timing function named ease, which is an acceleration curve that looks good in most situations. Other timing functions available are linear, ease-in, ease-out, ease-in-out. You can also specify control points for a cubic-bezier function for complete control of the transition or animation.

To specify an acceleration curve for a CSS animation, use the animation-timing-function property. For CSS transitions, use the transition-timing-function property. The code in Listing 9 specifies that the transition to make a box shrink to half its size and change color should be performed over an ease-in acceleration curve.

Listing 9: Using timing functions with CSS transitions.

<html>
<head>
<style>
    #box {
        -webkit-transform: scale(1.0);
        -webkit-transition-property: -webkit-transform, background-color;
        -webkit-transition-duration: 5s;
        -webkit-transition-timing-function: ease-in;
        background-color: green;
        border: 2px solid;
        height: 200px;
        width: 200px;
        }
</style>
<script>
    function transitionBox(box) {
        box.style.webkitTransform = 'scale(0.5)';
        box.style.backgroundColor = 'yellow';
    }
</script>
</head>
<body>
    <div id="box" onclick="transitionBox(this);"></div>
</body>
</html>

For more information on using timing functions for CSS transitions or animations, see the the “Using Timing Functions” sections of the Safari Visual Effects Guide and the Safari CSS Reference.

How do I detect when animations have started, iterated, or ended?

When animating elements on a webpage using CSS animations, you can detect through JavaScript when an animation has started, iterated, or ended using DOM events. The event names are webkitAnimationStart, webkitAnimationIteration, and webkitAnimationEnd. You can listen for these events just like you would any other event - either by adding a listener for the event to an element via addEventListener, or by adding an attribute directly to the element, such as onWebkitAnimationEnd. Listing 10 shows how to listen for all three events on a single animated element.

Listing 10: CSS animation event handlers.

<html>
<head>
<script>
    function addAnimationHandlers() {
        var box = document.getElementById('box');
        box.addEventListener('webkitAnimationStart', function (){ alert('Animation started.'); }, false);
        box.addEventListener('webkitAnimationIteration', function (){ alert('Animation iterated.'); }, false);
        box.addEventListener('webkitAnimationEnd', function (){ alert('Animation ended.'); }, false);
    }
</script>
<style>
    @-webkit-keyframes changeScale {
        0% {
            -webkit-transform: scale(1.0);
        }
        50% {
            -webkit-transform: scale(0.3);
        }
        100% {
            -webkit-transform: scale(1.0);
        }
    }

    #box {
        height: 300px;
        width: 300px;
        background-color: purple;
        -webkit-animation-name: changeScale;
        -webkit-animation-duration: 4s;
        -webkit-animation-iteration-count: 3;
    }
</style>
<meta name="viewport" content="width=device-width">
</head>
<body onload="addAnimationHandlers();">
    <div id="box"></div>
</body>
</html>

For more information on detecting when an animation has started, iterated, or ended, see the "Handling Animation Events" section of the Safari Visual Effects Guide.

How do I delay the start of a CSS transition or CSS animation?

Sometimes you may find it necessary to delay the start of a CSS transition or CSS animation—perhaps you have several elements with the same animation, but you
 want to stagger their start times. To add a delay to the beginning of your CSS animation, use the animation-delay property with a value specified in seconds. For CSS transitions use the transition-delay property, as illustrated in Listing 11.

Listing 11: Delaying the start of a CSS transition.

#box {
    -webkit-transform: scale(1.0);
    -webkit-transition-duration: 1.5s;
    -webkit-transition-property: -webkit-transform, background-color;
    -webkit-transition-delay: 2s;
    background-color: green;
    border: 2px solid;
    height: 200px;
    width: 200px;
}

For more information on delaying the start of a CSS transitions or animations, see the the “Setting Transition Properties” section of the Safari Visual Effects Guide and the Safari CSS Reference.

How do I repeat a CSS animation?

CSS transitions always execute only once, when the values of the properties being animated are changed via JavaScript or CSS. If you use CSS animations, you can specify that an animation iterate multiple times or even infinitely by using the animation-iteration-count property. The default value of this property is 1; integers or the keyword infinite are other possible values for this property. Listing 12 shows code to draw a box that shrinks to 30% of its size then grows back to its full size over a period of 4 seconds. Because the value of the animation-iteration-count property is infinite, this animation will repeat forever.

Listing 12: Repeating CSS animation.

<html>
<head>
<style>
    @-webkit-keyframes changeScale {
        0% {
            -webkit-transform: scale(1.0);
        }
        50% {
            -webkit-transform: scale(0.3);
        }
        100% {
            -webkit-transform: scale(1.0);
        }
    }

    #box {
        height: 300px;
        width: 300px;
        background-color: purple;
        -webkit-animation-name: changeScale;
        -webkit-animation-duration: 4s;
        -webkit-animation-iteration-count: infinite;
    }
</style>
<meta name="viewport" content="width=device-width">
</head>
<body>
    <div id="box"></div>
</body>
</html>

For more information on repeating CSS animations, see the "Repeating Animations" section of the Safari Visual Effects Guide and the Leaves sample code.



Document Revision History

Date Notes
2009-02-18 First Version
Back to Top



Did this document help you? Yes It's good, but... Not helpful...