✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 463 — #1 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR9 SALE OR DISTRIBUTION © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOTGraphics FOR SALE OR DISTRIBUTION and AnimationNOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9.1 Fundamentals 9.4.8 Transformations © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC 9.1.1 Coordinate Spaces 9.4.9 9.1.2NOTColors FOR SALE OR DISTRIBUTION 9.4.10Canvas by ExampleNOT FOR SALE OR DISTRIBUTION 9.1.3 Pixels vs. Objects/Vectors 9.5 SVG 9.1.4 Animation 9.5.1 Seeing SVG in a 9.2 HTML and CSS 9.5.2 SVG Case Study: A Bezier © Jones &9.2.1 BartlettHTML Learning, Elements forLLC Graphics © JonesCurve & EditorBartlett Learning, LLC NOT FOR 9.2.2SALECSS OR DISTRIBUTION 9.5.3NOTObjects FOR SALE in the DrawingOR DISTRIBUTION 9.2.3 Visual Properties 9.5.4 Reading and Writing Attributes 9.2.4 Absolute Position 9.5.5 Interactivity (a.k.a. Event Handling 9.2.5 Case Study: Bar Chart Redux) 9.2.6 Case Study: Towers of Hanoi Display 9.5.6 Other SVG Features © Jones & Bartlett9.3 Learning,Animation LLC in HTML and CSS © Jones9.6 & Bartlett3D Graphics Learning, with WebGL LLC NOT FOR SALE OR DISTRIBUTION9.3.1 Constant Velocity NOT FOR SALE9.6.1 ORWebGL DISTRIBUTION is the 3D canvas 9.3.2 Fading In and Out 9.6.2 Case Study: The Sierpinski Gasket 9.3.3 Animating Other Properties 9.6.3 Defining the 3D Data 9.3.4 Ramped (or Eased) Animation 9.6.4 Shader Code 9.3.5 Declarative CSS Animation 9.6.5 Drawing the Scene 9.4 The© Canvas Jones Element & Bartlett Learning, LLC 9.6.6 Interactivity© and Jones Events & Bartlett Learning, LLC 9.4.1NOTInstantiating FOR SALE a Canvas OR DISTRIBUTION9.7 Other Client-Side GraphicsNOT FOR Technologies SALE OR DISTRIBUTION 9.4.2 The Rendering Context 9.7.1 Flash 9.4.3 Drawing Rectangles 9.7.2 Java 9.4.4 Drawing Lines and Polygons 9.7.3 VML 9.4.5 Drawing Arcs and Circles Chapter Summary © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC 9.4.6 Drawing Bezier and Quadratic Curves Exercises NOT FOR 9.4.7SALEWorking OR DISTRIBUTION with Images NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 464 — #2 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

464 CHAPTER 9 Graphics and Animation

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC IntroductionNOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Many JavaScript applications involve performing computations and showing re- sults by manipulating the DOM of a web page. Effective event handling, as seen in Chapter 6, based on both user activity and the passage of time, facilitates interac- © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC tivity, with JavaScript as the connecting technology. We have seen, so far, that our NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION options for displaying information to the user have been generally text or images that are stored (or generated) on a server. This range of elements is adequate for conventional documents and forms but falls short for applications such as games, simulations, visualization, and animation. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC This chapter aims to introduce you to the suite of visual, graphics, and anima- NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION tion technologies that are available in modern, standards-compliant web browsers. These options range from additional visual and graphical properties within the HTML DOM to full-fledged graphics technologies that allow you to draw and fill lines, curves, polygons, or other shapes, perform sophisticated image pro- © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC cessing and compositing operations, and even render hardware-accelerated three- NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION dimensional (3D) objects. All this, manipulated entirely within your browser, with no additional software.

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR9.1 SALE Fundamentals OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

Certain concepts and techniques are common to all graphics and animation subsys- tems. This section introduces the core set that underlies the specific technologies in this chapter. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 9.1.1 Coordinate Spaces

All computer graphics operations are generally performed within the context of a coordinate space—that is, a two-dimensional (2D) or 3D region within which colors, lines,© shapes, Jones or & other Bartlett entities Learning, are placed. LLC A specific location within© thatJones space & canBartlett Learning, LLC be representedNOT FOR with SALE sequences OR DISTRIBUTION of numbers called coordinates, withNOT one number FOR SALE for OR DISTRIBUTION each dimension of the space. Thus, 2D coordinates are represented by an ordered pair (x, y), while 3D coordinates are represented by an ordered triple (x,y,z). By convention, the x in 2D or 3D coordinates represents a horizontal location (“left © Jones to& Bartlett right”), while Learning,y is vertical LLC (“up to down”). In 3D,© Jonesz is typically & Bartlett referred Learning, to as LLC NOT FORdepth SALE, representing OR DISTRIBUTION the “front-to-back” dimension.NOT The FOR special SALE coordinates OR DISTRIBUTION (0, 0) and (0, 0, 0) are designated as the origin of the space.

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 465 — #3 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9.1 Fundamentals 465

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC The directionNOT FOR along SALE which OR a coordinate DISTRIBUTION grows also varies based onNOT the graph- FOR SALE OR DISTRIBUTION ics system. A typical convention is to have x-coordinates increase to the right. In 2D graphics systems, y-coordinates usually increase in the downward direc- tion. In some 3D graphics systems, the y-coordinate increases upward, with the © Jonesz-coordinate & Bartlett increasing Learning, as it movesLLC toward the viewer.© These Jones are & all Bartlett conventions, Learning, LLC NOThowever, FOR SALE and there OR DISTRIBUTION is no hard-and-fast rule on directionalityNOT and FOR coordinate SALE OR values, DISTRIBUTION it is thus another thing to note when learning about a new graphics technology. Coordinate spaces give us an unambiguous mechanism for designating positions and sizes—an understandably crucial part of being able to tell a computer where © Jones & Bartlettto Learning, draw something. LLC Figure 9.1 illustrates© typical Jones 2D & andBartlett 3D coordinate Learning, spaces. LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

y (0, 0) © Jones & Bartlettx Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

(0, 0) x © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC

NOT FORy SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

y z © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALEy OR DISTRIBUTION

(0, 0, 0) (0, 0, 0) x x © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION z

FIGURE 9.1 © Jones2D and & 3D Bartlett coordinate Learning, spaces, each LLC showing a rectangle with opposite© Jones corners & (2Bartlett, 1) (indicated Learning, LLC NOT byFOR the small SALE square) OR and DISTRIBUTION(4, 2) (indicated by the small triangle) inNOT 2D, or (2FOR, 1, 0) SALEand (4 ,OR2, 0) DISTRIBUTIONin 3D.

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 466 — #4 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

466 CHAPTER 9 Graphics and Animation

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC InNOT the physicalFOR SALE world, OR we DISTRIBUTION tend to attach units to coordinatesNOT and FOR distances. SALE OR DISTRIBUTION A location on the earth is typically given in degrees (latitude and longitude); lengths or distances may be measured in inches, meters, miles, or even light-years, with “square” or “cubic” versions of these units representing areas and volumes, © Jones respectively.& Bartlett Learning, Computer graphics LLC systems vary in their© Jones use of units.& Bartlett Keep anLearning, eye on LLC NOT FORthem SALE in theOR later DISTRIBUTION sections. NOT FOR SALE OR DISTRIBUTION

9.1.2 Colors

If coordinate spaces define where or how big a visual entity may be, colors form © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC the basis of what that entity looks like. Without going into a deeper discussion NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION of the physics of light and color, suffice it to say that in most computer graphics systems, a color is represented by the ordered triple (r, g, b), with r representing the amount of red in a color, g representing the amount of green, and b representing the amount of blue. The scale used varies depending on the graphics technology. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC One convention, for example, uses 0 to mean the complete absence of r, g,orb and NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 1 for any of these colors at “full intensity.” Fractional values represent all levels in between. Under this convention, the RGB color (0, 0, 0) is black, (1, 1, 1) is white, (1, 0, 0) is red, (1, 1, 0) is yellow, (0, 1, 0) is green, (0, 0, 1) is blue, (0.5, 0.5, 0.5) is a medium gray, (0.25, 0, 0.25) is a deep, dark violet, etc. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC The range of colors that can be formed by this system is typically shown as NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION a color cube, with red, green, and blue each representing one dimension in a 3D coordinate space (see Figure 9.2). To “quantify” a particular color, one would find that color within the cube; its RGB representation would then be the coordinates of that color. © Jones & Bartlett Learning,Some LLC graphics systems accept a© fourth Jones value, & Bartlett called the Learning,alpha channel LLC, as part NOT FOR SALE OR DISTRIBUTIONof a color definition. The alpha channelNOT represents FOR SALE transparency, OR DISTRIBUTION with the minimum value (typically 0) denoting a completely transparent color and the maximum value (typically 1) denoting complete opacity. Thus, the RGBA tuple (1, 0, 1, 0.75) represents a 75% opaque magenta. In other words, if this color is “painted” over another© Jones color, approximately& Bartlett Learning, one-fourth LLC of that preexisting color© willJones be visible& Bartlett Learning, LLC beneathNOT the FOR magenta. SALE1 OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION While itis convenienttothinkaboutcolors as levels from 0 to1, many web technologies use a different scale, from 0 to 255. Further complicating the issue, this

© Jones & 1BartlettThe specific Learning, computation LLC that takes place can actually© varyJones quite & aBartlett bit, but that’sLearning, for a LLC NOT FORcomputer SALE graphicsOR DISTRIBUTION book to handle, not an introduction to programming.NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 467 — #5 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9.1 Fundamentals 467

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

black (0, 0, 0) white (1, 1, 1) green (0, 1, 0) yellow (1, 1, 0) © Jones & BartlettFIGURE Learning, 9.2 LLC © Jones & Bartlett Learning, LLC NOT FOR SALE ORThe RGBDISTRIBUTION color cube from different perspectives, withNOT the FOR indicated SALE colors OR at the DISTRIBUTION protruding corners [Rok09].

range is sometimes represented in hexadecimal, from to (refer to Section 3.3.4 © Jones & Bartlett Learning, LLC 00 FF © Jones & Bartlett Learning, LLC for a refresher). When expressed in this manner, an RGB color starts with a NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION pound/number sign (#) then lists the two-hexadecimal-digit red, green, and blue values in that order. The example RGB colors in Figure 9.2, when written this way, would be #000000 for black, #FFFFFF for white, #00FF00 for green, and #FFFF00 for yellow. The expression #000033 would be a dark blue, #808080 a medium gray, © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC #FFE6E6 a lightpink, and so on. NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9.1.3 Pixels vs. Objects/Vectors

© Jones & BartlettThere Learning, are two LLC main approaches for describing© Jones a& computerBartlett Learning, graphics image LLC or NOT FOR SALE OR“scene”: DISTRIBUTION it can be represented as a discreteNOT grid FOR of coloredSALE squaresOR DISTRIBUTION or pixels (e.g., images from a digital camera or scanner) or as a set of geometric or other objects, each with different properties and characteristics (e.g., pictures created by drawing programs like Adobe Illustrator, Dia, OmniGraffle, or Microsoft Visio). With the© Jones pixel-based & Bartlett or image-space Learning,perspective LLC of a scene, all© operations Jones & Bartlett Learning, LLC end up changingNOT FOR the color(s)SALE OR of oneDISTRIBUTION or more pixels. With such a perspective,NOT FOR a SALE OR DISTRIBUTION drawCircle function would calculate a set of pixels that best approximates a circle and would color that set accordingly. The notion of a circle dissipates after that. Once the circle is colored, only the pixels remain. © JonesWith & Bartlett the object-space Learning,perspective LLC (also known as vector-based© Jones graphics& Bartlett), a com-Learning, LLC NOT puterFOR graphicsSALE OR scene DISTRIBUTION is viewed as a collection of entitiesNOT that FOR can be SALE manipulated OR DISTRIBUTION

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 468 — #6 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

468 CHAPTER 9 Graphics and Animation

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC individually.NOT FOR The SALE pixels thatOR DISTRIBUTION these entities occupy cannot be changedNOT directly; FOR SALE in- OR DISTRIBUTION stead, modifications to an object’s properties, such as its size, location, color, line style, and others, result in object-wide changes in its appearance. In this perspec- tive, a drawCircle function would retain the notion of a circle after it is called. A © Jones mechanism& Bartlett wouldLearning, exist through LLC which the drawn circle© Jones could & be Bartlett retrieved Learning, as a cir- LLC NOT FORcle, SALE and propertiesOR DISTRIBUTION such as radius, location, color,NOT etc., couldFOR beSALE changed OR directly.DISTRIBUTION Such property changes would then affect the circle’s and/or its scene’s appearance. There is no “best” approach here. The perspective to choose depends on the needs of the computer graphics application. A digital photo editor is better served © Jones & Bartlett Learning,by a pixel-based LLC perspective since© photographs Jones & Bartlett store only Learning, pixels, not LLC shapes or NOT FOR SALE OR DISTRIBUTIONobjects, while a diagramming programNOT is FOR better SALE served OR by objectsDISTRIBUTION since diagrams are manipulated in terms of individual shapes and lines. Some advanced programs combine both perspectives, but at any given moment, the user does retain one approach or the other. The© Jones general & tradeoffBartlett between Learning, pixels LLC and objects/vectors is© that Jones the & pixel- Bartlett Learning, LLC orientedNOT approach FOR SALE gives youOR absolutely DISTRIBUTION fine control over how a sceneNOT looks—literally FOR SALE OR DISTRIBUTION down to the dots that comprise the picture—while the object-based approach typ- ically uses fewer machine resources (there tend to be millions of pixels in a picture as opposed to a few hundred objects) and provides for resolution independence. © Jones Object-based& Bartlett Learning, graphics canLLC be scaled up or down© and, Jones since & theBartlett objects Learning, are re- LLC NOT FORdrawn SALE every OR time,DISTRIBUTION they can always be drawn for maximumNOT FOR detail SALE or smoothness.OR DISTRIBUTION Figure 9.3 illustrates this tradeoff. In the figure, the same circle, with a radius of 25 pixels, is drawn with a pixel-based technology, then with an object-based technology. The picture is then magnified a few times over. © Jones & Bartlett Learning,The web LLC page shown is in fact drawn© Jones using & two Bartlett technologies Learning, that weLLC will cover in this chapter: the pixel-based element and the object-based Scalable NOT FOR SALE OR DISTRIBUTION canvasNOT FOR SALE OR DISTRIBUTION Vector Graphics or SVG standard. In case you are wondering, the full code to the page is shown here: ✞ ¤ Bartlett Learning, LLC © Jones & Bartlett Learning, LLC © Jones & Bartlett © Jones & Bartlett Learning, LLC NOT FOR SALEPixel DISTRIBUTION vs. Object NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 469 — #7 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9.1 Fundamentals 469

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION FIGURE 9.3 Comparison of a shaded circle with pixel-based (left) vs. object-based (right) approaches.

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR< SALEscript >OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION window.onload = function () { varcanvas = document.getElementById("canvas"); var renderingContext = canvas.getContext("2d");

© Jones & Bartlett Learning,// Image-spaceLLC circle. © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTIONvar radialGradient = renderingContext.createRadialGradientNOT FOR SALE OR DISTRIBUTION (42, 42, 1, 50, 50, 25); radialGradient.addColorStop(0, "white"); radialGradient.addColorStop(1, "#880000"); © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC renderingContext.fillStyle = radialGradient; NOTrenderingContext.beginPath(); FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION renderingContext.arc(50, 50, 25, 0, Math.PI * 2, true); renderingContext.fill(); }; © Jones & Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 470 — #8 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

470 CHAPTER 9 Graphics and Animation

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✝ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE✆ OR DISTRIBUTION In the end, the final representation is based on the display technology used for viewing computer graphics. Today, these technologies are almost all pixel-based, © Jones and& Bartlett so even Learning, object- or vector-based LLC graphics get converted© Jones into& Bartlett pixels (a Learning, process LLC NOT FORcalled SALErasterization OR DISTRIBUTIONor scan conversion) as they reachNOT the FOR computer SALE display. OR DISTRIBUTION

9.1.4 Animation

© Jones & Bartlett Learning,At its core, LLC animation involves showing© Jones a sequence & Bartlett of images Learning, quickly enoughLLC that NOT FOR SALE OR DISTRIBUTIONthe viewer perceives the illusion ofNOT motion, FOR with SALE “quickly OR DISTRIBUTION enough” being 30 or more images, or frames, per second. Differences between successive images should be fairly small, allowing the brain to “fill in the blanks” from one frame to the other. When© Jones the images& Bartlett are pixel-based, Learning, theLLC approach to animation© isJones typically & Bartlett to Learning, LLC redrawNOT frames FOR in SALE their entirety,OR DISTRIBUTION since any pixel can change at anyNOT time. FOR Under SALE OR DISTRIBUTION certain circumstances, only the parts of the frame that change can be redrawn, if those parts are known. For object- or vector-based animation, object properties such as position, size, © Jones color,& Bartlett and others Learning, are changed LLC in small amounts at© eachJones “frame” & Bartlett or interval. Learning, The LLC NOT FORobject-based SALE OR graphicsDISTRIBUTION system then takes care of redisplayingNOT FOR theSALE objects. OR DISTRIBUTION

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 471 — #9 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9.2 HTML and CSS 471

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE ORReview DISTRIBUTION and Practice NOT FOR SALE OR DISTRIBUTION 1. Based on how colors are “quantified” as described in Section 9.1.2, state how the following color changes would be performed on some (r,g,b) value:

© Jones & BartlettMake Learning, the color brighter LLC © Jones & Bartlett Learning, LLC NOT FOR SALEMake OR theDISTRIBUTION color darker NOT FOR SALE OR DISTRIBUTION Make the color a gray level

2. What is the difference between pixel- and object-based graphics? What © Jones & Bartlett Learning,factors orLLC features would make you© Jones choose one& Bartlett over the Learning, other? LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC 9.2 HTMLNOT FOR and SALE CSS OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

Chapter 6 introduced how web pages are really trees or outlines of elements that can be created using either HTML or JavaScript. These elements comprise what © Jonesis called & Bartlett the Document Learning, Object LLC Model, or DOM. © Jones & Bartlett Learning, LLC NOT FORIn thatSALE chapter, OR DISTRIBUTION the DOM was viewed as a mechanismNOT forFOR creating SALE userOR in-DISTRIBUTION terfaces. In this chapter, we add a new dimension to the DOM: we view it as a visual medium. Using the terminology of the previous section, the DOM can be used as an object-based computer graphics system, with the spectrum of possible © Jones & Bartlettweb Learning, elements servingLLC as the objects to display,© Jones and Cascading& Bartlett Style Learning, Sheets, orLLC CSS, NOT FOR SALE ORserving DISTRIBUTION as the mechanism for determiningNOT how FOR these SALE objects OR look. DISTRIBUTION

9.2.1 HTML Elements for Graphics © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC Chapter 6,NOT and inFOR particular SALE SectionOR DISTRIBUTION 6.2, showed you how to build webNOT pages FOR con- SALE OR DISTRIBUTION sisting of elements such as paragraphs (p), push buttons (input with type="button"), text fields (input with type="input"), lists (select), and others. These elements are not typically what one would associate with computer graphics, however. In © Jonesthis section,& Bartlett we look Learning, at other LLC elements that are better© suited Jones for & general-purpose Bartlett Learning, LLC NOT visuals.FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 472 — #10 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

472 CHAPTER 9 Graphics and Animation

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC The NOTimg ElementFOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION The img element includes ready-made image files such as screenshots, digital pho- tos, or any other (web-compatible) visual content in a web page. This element’s most important attribute is src, which specifies the image file to include, either © Jones as& anBartlett absolute Learning, web address LLC or a path relative to the© Jones location & of Bartlett the web Learning, page. In LLC NOT FORHTML, SALEimg ORis DISTRIBUTION specified as follows: NOT FOR SALE OR DISTRIBUTION ✞ ¤ ✝ ✆ © Jones & Bartlett Learning,This causesLLC the image file located© at Joneshttp://javascript.cs.lmu.edu/images/ & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTIONbookcover.jpg to appear whereverNOT the tagFOR is locatedSALE inOR the DISTRIBUTION web page. As with all web page elements, you can also create an element in JavaScript and append it to the web page. The following code is meant to be run in our JavaScriptrunner page at http://javascript.cs.lmu.edu/runner; itcreates an img© elementJones & that Bartlett is identical Learning, to the previous LLC HTML example and© Jones adds it to& theBartlett Learning, LLC web elementwhoseNOT FOR SALEid is OR"footer" DISTRIBUTION(yes, this is a try-it-yourself example):NOT FOR SALE OR DISTRIBUTION ✞ ¤ varfooter= document.getElementById("footer"); varimage = document.createElement("img"); image.src = "http://javascript.cs.lmu.edu/images/bookcover.jpg"; © Jones &footer Bartlett.appendChild Learning,(image LLC); © Jones & Bartlett Learning, LLC NOT FOR✝ SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION✆

The div Element

The div elementserves as a sortof counterpointto img; instead of an element © Jones & Bartlett Learning,that displays LLC prepared content, div©is Jones more of & a Bartlett blank slate. Learning, It can be LLC viewed as a NOT FOR SALE OR DISTRIBUTION“graphics-from-scratch” building block.NOT FOR SALE OR DISTRIBUTION Specifying a div elementin HTML is trivial: ✞ ¤

✝ © Jones & Bartlett Learning, LLC © Jones & Bartlett✆ Learning, LLC Or,NOT for FOR use in SALEhttp://javascript.cs.lmu.edu/runner OR DISTRIBUTION (adjustaccordinglyNOT FOR SALE OR DISTRIBUTION when using thiscode fragmenton a differentweb page): ✞ ¤ varfooter= document.getElementById("footer"); vardiv = document.createElement("div"); © Jones &footer Bartlett.appendChild Learning,(div LLC); © Jones & Bartlett Learning, LLC NOT FOR✝ SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION✆

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 473 — #11 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9.2 HTML and CSS 473

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC In thisNOT minimal FOR form, SALE however, OR DISTRIBUTION the result is as trivial as the mechanism:NOT FOR you SALE OR DISTRIBUTION get nothing more than a box with zero height. Instead, the usefulness of div comes from using it as a generic container for other elements and the customization of its visual properties. The div element truly is the blank slate of the DOM. © JonesThe & Bartlett next few Learning, sections illustrate LLC just how this blank© slate Jones can be& Bartlett turned into Learning, a LLC NOTwide FOR variety SALE of OR object-based DISTRIBUTION graphics displays on a webNOT page. FOR SALE OR DISTRIBUTION

9.2.2 CSS

CSS, or Cascading Style Sheets, is the visual or presentation technology of the web. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC You have already used some CSS: it is effectively the DOM property named style. NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION By touching style, whether in JavaScript or HTML (as the style attribute within most tags), you are touching CSS. The C in CSS stands for cascading, which tries to express how the style property or attribute may be applied at many levels, ranging from a single, unique © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC element( this particular div; that exact img) to all elements of a particular type NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION (all p elements; all h1 elements) to all elements with the same class, which is a new way to group or categorize elements that we have not seen before. Specific style values may also be applicable only to elements at a certain point in the web page, such as only a elements that are inside p elements. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC To illustrate some ways by which style can be assigned, we will use an easily NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION discernible, easy-to-understand visual property: border-style. The border-style property represents how an element’s boundaries are rendered. Known border styles include dotted, dashed, solid, double, groove, ridge, inset, outset, and none (for no border atall). © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Individual Elements

Individual elements can be given specific visual properties in the following ways:

The ©style Jonesattribute & Bartlett for elements Learning, created LLC using HTML tags © Jones & Bartlett Learning, LLC The NOTstyle FORproperty SALE for OR elements DISTRIBUTION created or accessed through theNOT DOM FOR SALE OR DISTRIBUTION

A CSS rule that selects a single element by its id attribute/property

© JonesThese & waysBartlett are illustratedLearning, in LLC the following examples;© all Jones of them & pertainBartlett to Learning, the LLC NOT JavaScriptrunnerFOR SALE OR pageDISTRIBUTION at http://javascript.cs.lmu.edu/runnerNOT FOR SALE, and theyOR DISTRIBUTION all

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 474 — #12 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

474 CHAPTER 9 Graphics and Animation

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC haveNOT the identical FOR SALE result OR of settingDISTRIBUTION the border of the introductionNOT elementtoFOR SALE OR DISTRIBUTION solid. In its original form, this element is specified in HTML as follows: ✞ ¤ © Jones &

LLC © Jones & Bartlett Learning, LLC NOT FOR SALE NOT FOR SALE OR DISTRIBUTION

✝ ✆ Modifying the tag as shown below gives this element a solid border: © Jones & Bartlett Learning,✞ LLC © Jones & Bartlett Learning, LLC ¤ NOT FOR SALE OR DISTRIBUTION

OR DISTRIBUTION

✝ ✆ Alternatively, you can run the following code to set border-style through JavaScript.© Jones Note & howBartlett the hyphenatedLearning, LLC property name is replaced© byJones its “camel & Bartlett Learning, LLC case”NOT version; FOR that SALE is, hyphens OR DISTRIBUTION are replaced with a capitalized firstNOT letter FOR (can SALE you OR DISTRIBUTION explain why this change is necessary?): ✞ ¤ document.getElementById("introduction").style.borderStyle = "solid"; © Jones ✝& Bartlett Learning, LLC © Jones & Bartlett Learning,✆ LLC NOT FOR SALEThis codeOR DISTRIBUTION fragmentuses getElementById toNOT retrieve FOR the SALEintroduction OR DISTRIBUTIONele- ment, then assigns the string "solid" to the borderStyle subproperty of that element’s style property. Style assignment by CSS rule involves modifying the HTML again. For this © Jones & Bartlett Learning,approach, LLC the pound sign (#) is shorthand© Jones for & “the Bartlett element Learning, whose id isLLC... ,” and NOT FOR SALE OR DISTRIBUTIONthe curly braces ({}) enclose the CSSNOT properties FOR SALE to be OR applied: DISTRIBUTION ✞ ¤ © © Jones ✝& Bartlett Learning, LLC © Jones & Bartlett Learning,✆ LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 475 — #13 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9.2 HTML and CSS 475

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC For bothNOT the FOR HTML SALE attribute OR DISTRIBUTION and CSS rule approaches, multiple styleNOT prop-FOR SALE OR DISTRIBUTION erties can be set by separating them with semicolons (;): ✞ ¤

© Jones & © Jones & Bartlett Learning, LLC

NOT✝ FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION✆ Or: ✞ ¤ © Jones & Bartlett Learning, & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✝ ✆ Test yourself: see if you can figure out how to set multiple style properties in JavaScript. No worries if you can’t; you will learn how soon enough. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Elements of the Same Type

To assign the same visual properties to all elements of the same type, say all p elements, you may use either JavaScript or a CSS rule. Because this style © Jones & Bartlettassignment Learning, affects LLC multiple elements uniformly,© Jones the & Bartlett “in-tag” approachLearning, does LLC not NOT FOR SALE ORapply. DISTRIBUTION NOT FOR SALE OR DISTRIBUTION The document object’s getElementsByTagName function returns an array of every element with the same type/tag. You can then use a for statement to assign the style subproperties for every element in that array (you can try this yourself at http://javascript.cs.lmu.edu/runner© Jones & Bartlett Learning, LLC): © Jones & Bartlett Learning, LLC ✞ NOT FOR SALE OR DISTRIBUTION NOT FOR¤ SALE OR DISTRIBUTION varpElements = document.getElementsByTagName("p"); for (vari =0;i < pElements.length; i += 1) { pElements[i].style.borderStyle = "solid"; } © Jones✝ & Bartlett Learning, LLC © Jones & Bartlett Learning,✆ LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 476 — #14 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

476 CHAPTER 9 Graphics and Animation

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC AssignmentbyNOT FOR SALE CSS ruleOR usesDISTRIBUTION thesame style tag as in the previousNOT FOR section, SALE OR DISTRIBUTION only this time the selector preceding the {}block should be the name of the tag: ✞ ¤ © Jones & Bartlett © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning,✝ LLC © Jones & Bartlett Learning, LLC ✆ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Note a key difference between the JavaScript and CSS rule approaches: the JavaScript approach changes the style for the existing elements at the time the code is executed. If subsequentJavaScriptcode createsa new p elementand appends it 2 to the© document,Jones & thatBartlett element Learning,will not LLChave the assigned style subproperties.© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Other CSS Selectors

Beyond individual property setting and setting by element type, the remaining mechanisms for modifying CSS visual properties mainly involve the selector that © Jones begins& Bartlett CSS Learning, rules. Since LLC our focus here is computer© Jones graphics & andBartlett not webLearning, page LLC NOT FORauthoring, SALE OR we DISTRIBUTION only summarize them here: NOT FOR SALE OR DISTRIBUTION

A selector that begins with a period (.), such as .helpbox or .menuitem, applies to web elements that have been assigned a class attribute: © Jones & Bartlett Learning,✞ LLC © Jones & Bartlett Learning, LLC ¤ NOT FOR SALE OR DISTRIBUTION

A CSS ruleNOT that FOR begins SALE with OR .helpboxDISTRIBUTION will affect this p element.

✝ ✆

Multiple selectors separated by commas (,) allow you to apply the same set ©of Jones visual properties& Bartlett to Learning, distinct groups LLC of elements: © Jones & Bartlett Learning, LLC NOT✞ FOR SALE OR DISTRIBUTION NOT FOR SALE¤ OR DISTRIBUTION p,.helpbox,#mainInstruction { background-color: rgb(250, 250, 192); border-style: outset } © Jones & Bartlett✝ Learning, LLC © Jones & Bartlett Learning,✆ LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 2There is a JavaScript technique for creating or modifying CSS rules, but we have chosen to leave that as something for lookup.

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 477 — #15 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9.2 HTML and CSS 477

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC JustNOT to throwFOR SALE in some OR newDISTRIBUTION things, note the fairly self-explanatoryNOT FOR SALE OR DISTRIBUTION background-color property, and how it is set using an rgb expression (one of many RGB representations that CSS accepts; this version takes color com- ponentvalues as integersfrom 0 to 255).3 © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FORMultiple SALE OR selectors DISTRIBUTION separated by spaces instead of commasNOT FOR constitute SALEcontain- OR DISTRIBUTION ment instead of a list: a selector of pa, for example, affects only a elements that are inside p elements. To specify that div elements are contained within another div that are in turn contained in a top-level div, you would use the © Jones & Bartlett Learning,selector divLLC div div. © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION CSS is, on one level, simple in principle, yetsurprisingly deep, powerful, and com- plex on another. We have attempted to cover the basic mechanisms here; if you are interested in more, we suggest the official CSS home page for definitive, no-holds- barred coverage© Jones [W3C10]. & Bartlett A quick Learning, web search LLC will also reveal a large© number Jones of & Bartlett Learning, LLC learning, tutorial,NOT FOR and SALE reference OR sites. DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

jQuery’s $

© JonesWith & this Bartlett wide array Learning, of approaches LLC for assigning visual© properties,Jones & Bartlett we hope thatLearning, LLC NOTsome FOR of SALE the motivation OR DISTRIBUTION and accomplishment behind jQueryNOT (Section FOR SALE 7.5) becomes OR DISTRIBUTION clear: thanks to jQuery’s $ function, setting the properties of just the right collec- tion of elements becomes much simpler to do. This is what effective library design achieves—it can take an existing mechanism for getting things done and, with the © Jones & Bartlettright Learning, functions LLC and objects, make programming© Jones such & Bartlett activities Learning, easier, faster, LLC and NOT FOR SALE ORmore DISTRIBUTION powerful. NOT FOR SALE OR DISTRIBUTION

9.2.3 Visual Properties

At this point,© Jones we turn & Bartlett to CSS properties Learning, that LLC specifically facilitate visual© Jones effects & Bartlett Learning, LLC beyond documentNOT FOR layout SALE or user OR interface DISTRIBUTION appearance. In addition, we willNOT empha- FOR SALE OR DISTRIBUTION size CSS property manipulation from JavaScript as opposed to the HTML and CSS rule approaches seen previously. All examples are designed for the JavaScript runner page at http://javascript.cs.lmu.edu/runner. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR3In yetSALE another OR pesky DISTRIBUTION detail that we don’t have space to fullyNOT address, FOR not SALE all colors OR can DISTRIBUTION be represented by all web browsers. For the purposes of this chapter, however, we won’t worry about this issue.

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 478 — #16 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

478 CHAPTER 9 Graphics and Animation

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC SizeNOT and SpacingFOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

The width and height properties, as you might expect, can set the size of a web element’s content to something other than its default. Units must be provided along with the dimensions; we will stick to pixels (px) here and leave you to learn © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC about other units on your own. Borders also have a size property: border-width. NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION As with width and height, units are required. Related to size is the spacing around a web element. Two types of space are available: margin and padding. Margin refers to the space around an element that is outside the border, while padding refers to the space between an element’s border © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC and its content. Since web elements have four sides (top, left, bottom, and right), NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION there are four properties for each type of spacing: margin-top, margin-left, margin-bottom, and margin-right for margin, and (obviously enough) padding-top, padding-left, padding-bottom, and padding-right for padding. “Shortcut prop- erties” margin and padding are also assignable, to one, two, or four number-plus- © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC unitexpressions separatedby spaces: one value expressions setall four sides, two NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION value expressions set vertical and horizontal spacing, and four-value expressions can setall four sides todifferentvalues in a single assignment. The following http://javascript.cs.lmu.edu/runner example provides some code that manipulates these size and spacing properties for the div elementwhose © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC id is footer. Note how the overall area that is occupied by an element is ultimately NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION the combined space determined by width, height, border-width, padding, and margin. Type this code in and play around (recall, again, the “camel case” rule for how hyphenated CSS property names are modified when accessed via JavaScript):

© Jones & Bartlett Learning,✞ LLC © Jones & Bartlett Learning, LLC ¤ NOT FOR SALE OR DISTRIBUTIONvarfooter= document.getElementByIdNOT (FOR"footer" SALE); OR DISTRIBUTION footer.innerHTML = "Fun with sizes and spaces"; footer.style.width = "100px"; footer.style.height = "100px"; footer© Jones.style. &borderStyle Bartlett Learning,= "outset" LLC; © Jones & Bartlett Learning, LLC footerNOT.style FOR.borderWidth SALE OR =DISTRIBUTION"2px"; NOT FOR SALE OR DISTRIBUTION footer.style.marginLeft = "300px"; footer.style.marginTop = "100px"; footer.style.paddingRight = "50px"; footer.style.paddingBottom = "200px"; © Jones ✝& Bartlett Learning, LLC © Jones & Bartlett Learning,✆ LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 479 — #17 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9.2 HTML and CSS 479

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC Color, Images,NOT FOR Opacity, SALE and OR VisibilityDISTRIBUTION NOT FOR SALE OR DISTRIBUTION Color and images form another broad category of CSS properties. Color proper- tiescan be setusing expressions ranging from presetkeywords (red, blue, black, white, etc.) to, as you have seen, an rgb triplet. Web elements have three indepen- © Jonesdently & settable Bartlett colors: Learning, foreground, LLC background, and border.© Jones We will & letBartlett JavaScript Learning, LLC NOTrunner FOR SALE code do OR the DISTRIBUTION talking now: NOT FOR SALE OR DISTRIBUTION ✞ ¤ varfooter= document.getElementById("footer"); footer.innerHTML = "Fun with color"; © Jones & Bartlettfooter Learning,.style LLC.color = "yellow"; © Jones & Bartlett Learning, LLC NOT FOR SALE ORfooter DISTRIBUTION.style.backgroundColor = "rgb(0,NOT 0, FOR 200)" SALE; OR DISTRIBUTION footer.style.borderStyle = "inset"; footer.style.borderColor = "rgb(150,150, 150)"; ✝ ✆ In addition to solid colors, an element’s background can be set to a preloaded © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC image file if available. Image files are specified in CSS using a expression: the NOT FOR SALE OR DISTRIBUTION url NOT FOR SALE OR DISTRIBUTION keyword url followed by the image’s, well, URL enclosed in parentheses. Here’s some more code, using an image that we know exists somewhere on http:// javascript.cs.lmu.edu: ✞ ¤ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FORvarfooter SALE= ORdocument DISTRIBUTION.getElementById("footer"); NOT FOR SALE OR DISTRIBUTION footer.innerHTML = "Fun with color"; footer.style.height = "128px"; footer.style.color = "yellow"; footer.style.backgroundImage = "url(http://javascript.cs.lmu.edu/images/bookcover.jpg)"; © Jones & Bartlett✝ Learning, LLC © Jones & Bartlett Learning, LLC ✆ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Notice how background images repeat by default, and what can be seen de- pends, on the size of the element. The background-repeat property controls how (or whether) background image repeats within its web element’s area. Separate images can© alsoJones be assigned& Bartlett to bordersLearning, via theLLCborder-image property,© Jones using a& Bartlett Learning, LLC similar mechanism.NOT FOR We SALE will leave OR thatDISTRIBUTION for you to look up and discover.NOT FOR SALE OR DISTRIBUTION A final, related pair of properties is opacity and visibility. You can set the opacity of a web element via the opacity property. This is equivalent to the aforementioned alpha channel with colors. In CSS, this property can take values © Jonesfrom & 0 toBartlett 1, with 1Learning, indicating LLC total opacity and 0 indicating© Jones complete & Bartlett transparency Learning, LLC NOT (i.e.,FOR the SALE element OR is DISTRIBUTION effectively invisible, but still takes upNOT space). FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 480 — #18 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

480 CHAPTER 9 Graphics and Animation

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC TheNOT following FOR SALE example OR plays DISTRIBUTION with the title/header of the JavaScriptNOT FOR runner SALE OR DISTRIBUTION page as well as the overall visible portion of the page (i.e., its body element). Note how opacity is not a matter of “lightness” or “darkness”—it does interact with overlapping elements, such as the underlying color of the web page. Play with the © Jones code,& Bartlett trying Learning, out different LLC color and opacity values,© Jones to see how& Bartlett they affect Learning, each LLC other: NOT FOR✞ SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION¤ varheader= document.getElementById("header"); document.body.style.backgroundColor = "rgb(0, 80, 0)"; header.style.color = "cyan"; © Jones & Bartlett Learning,header. styleLLC .opacity = "0.5"; © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION✝ NOT FOR SALE OR DISTRIBUTION ✆ An opacity of 0 makes an element totally transparent, but it remains present on the web page; that is, it still takes up space. Compare this to the display property, which determines whether an element is even there (i.e., displayed or not).© A Jones value of &none Bartletttakes theLearning, element awayLLC from the page’s display© .Jones Other values& Bartlett Learning, LLC makeNOT itvisible, FOR themostcommon SALE OR DISTRIBUTION one being block: NOT FOR SALE OR DISTRIBUTION ✞ ¤ varheader= document.getElementById("header"); header.style.display = "none"; ✝ ✆ © Jones & BartlettNote the Learning, difference between LLC an opacity of 0 and© Jones a display & Bartlettof none .Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION More Advanced Visual Effects

Borders, solid colors, and images comprise the primary graphics properties of most © Jones & Bartlett Learning,web elements. LLC With CSS Level 3 (CSS3)© Jones or greater, & Bartlett additional Learning, properties LLC become NOT FOR SALE OR DISTRIBUTIONavailable that greatly expand the rangeNOT ofFOR possible SALE visuals OR DISTRIBUTION in “pure” HTML and CSS (i.e., visuals that a compatible web browser can generate by itself, without requiring a predrawn image file). CSS3 is sufficiently new that your web browser may not support the exact property© Jones names & given Bartlett here. Learning, If the code LLC examples do not initially© work,Jones try & ap-Bartlett Learning, LLC pendingNOT a prefix FOR toSALE the property OR DISTRIBUTION name: Moz for the Mozilla familyNOT of web FOR browsers SALE OR DISTRIBUTION (Firefox, Flock, etc.), and Webkit for the WebKit family (, Chrome, etc.). At the HTML attribute and CSS rule level, you will need hyphens before and after each prefix (e.g., -moz- or --). © Jones & BartlettDrop shadows Learning, are an LLC easy way to immediately© Jones add some & Bartlett dimensionality Learning, to LLC NOT FORa SALE web page. OR CSS3DISTRIBUTION drop shadows consist of fourNOT values: FOR the SALE shadow’s OR color, DISTRIBUTION its

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 481 — #19 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9.2 HTML and CSS 481

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC horizontalNOT offset ,itsFORvertical SALE offsetOR DISTRIBUTION, and itsblur radius. The offsets are theNOT distances FOR SALE OR DISTRIBUTION by which the shadow is, well, offset from the web element. The blur radius is effectively the “softness” of the shadow: the larger, the softer. The following example gives the footer elementof theJavaScriptrunner page © Jonesa relatively & Bartlett soft, grayishLearning, drop LLC shadow that falls to the right© Jones and below & Bartlett it (note Learning, the LLC expected units for the offsets and blur radius): NOT✞ FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION¤ varfooter= document.getElementById("footer"); footer.innerHTML = "Getting fancy"; footer.style.boxShadow = "rgb(128, 128, 128) 3px 5px 10px"; © Jones & Bartlett✝ Learning, LLC © Jones & Bartlett Learning, LLC ✆ NOT FOR SALE OR Again,DISTRIBUTION if this code does not workNOT as is, FOR remember SALE to OR assign DISTRIBUTION the value to MozBoxShadow for Firefox, Flock, and other Mozilla browsers and to WebkitBoxShadow for Safari, Chrome, and other WebKit browsers. The CSS3 border-radius facilitates rounded corners—the effect of which may be greater© than Jones you & might Bartlett expect Learning, in terms ofLLC eliminating the “boxy”© default Jones ap- & Bartlett Learning, LLC pearance ofNOT many FOR web SALE elements. OR In DISTRIBUTION its simplest form, border-radius takesNOT a singleFOR SALE OR DISTRIBUTION length value. This results in a web element’s corners being drawn as quarters of a circle whose radius is the given length: ✞ ¤ © Jonesvarfooter & Bartlett= document Learning,.getElementById LLC ("footer"); © Jones & Bartlett Learning, LLC footer.innerHTML = "Styling outside the box..."; NOT FORfooter SALE.style OR.borderStyle DISTRIBUTION= "outset"; NOT FOR SALE OR DISTRIBUTION footer.style.borderWidth = "2px"; footer.style.borderRadius = "10px"; ✝ ✆ © Jones & Bartlett Learning,There’s quite LLC a bit of flexibility here.© More Jones complex & Bartlett forms Learning, of border-radius LLC NOT FOR SALE ORallow DISTRIBUTION for distinct horizontal and verticalNOT radii FOR (thus SALE making OR the DISTRIBUTION corners appear as quarter ellipses instead of quarters of a circle) as well as different radii for each corner. Plus, border-radius and box-shadow play well together. See what happens when you combine the previous two examples to create a rounded, drop- shadowed ©div Joneselement. & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC We wrapNOT up FOR our quick SALE tour OR of DISTRIBUTION CSS3 with gradient backgrounds—perhapsNOT FOR the SALE OR DISTRIBUTION trickiest of the properties we are reviewing, but well worth the learning curve. CSS3 approaches gradientbackgrounds as browser-generated images; that is, they can be used for any property that also takes a url expression for an online image file. © JonesThus, & gradients Bartlett canLearning, be used LLC with background-image,©border-image Jones & Bartlett, and other Learning, LLC NOT propertiesFOR SALE that OR typically DISTRIBUTION take an image URL. NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 482 — #20 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

482 CHAPTER 9 Graphics and Animation

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC AsNOT with FORborder-radius SALE OR ,DISTRIBUTION the CSS3 expression for a gradientNOT can rangeFOR fromSALE OR DISTRIBUTION simple-but-standardized to complicated-but-customized. We will stay with simple here, leaving the full gamut of options to reading outside of this text: ✞ ¤ © Jones &varfooter Bartlett= Learning,document LLC.getElementById("footer"©); Jones & Bartlett Learning, LLC footer.innerHTML = "Look ma, no images!"; NOT FOR footerSALE. styleOR DISTRIBUTION.backgroundImage = NOT FOR SALE OR DISTRIBUTION "linear-gradient(white, rgb(200, 0, 0), rgb(128, 0, 0))"; document.body.style.backgroundImage = "linear-gradient(left, lightgray, white)"; ✝ ✆ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTIONIn this simplest form, a gradientNOT is a FOR comma-separated SALE OR DISTRIBUTION list of colors. The web element then transitions as smoothly as possible across these colors in a particu- lar direction (top to bottom) by default. Different directions can be specified by including a starting point as the first parameter, as seen in the gradient that is assigned© Jones to the & document’s Bartlett Learning,body element. LLC In this example, the© “starting Jones color”& Bartlett Learning, LLC beginsNOT at the FOR left SALE side of OR the DISTRIBUTION element and travels to the right. NOT FOR SALE OR DISTRIBUTION Older web browsers actually take differing expressions for gradients, among other values and properties that are available only in the latest standards. The Mozilla/Firefox family of web browsers expects the usual -moz- prefix, such as © Jones "-moz-linear-gradient(white,& Bartlett Learning, LLC rgb(200, 0, 0),© Jones rgb(128, & Bartlett 0, 0))" Learning,, while the LLC NOT FORWebKitfamily SALE OR DISTRIBUTION of web browsers uses the -webkit-NOTprefix FOR and SALE expects OR the DISTRIBUTION type of gradient( linear, radial) as a parameter. Dealing with these backward-compatibility browser variations is straightfor- ward though verbose: set them all. Web browsers know to ignore a setting or value © Jones & Bartlett Learning,that they LLC do not recognize. Thus, a© functional Jones & workaround Bartlett Learning, until all web LLC browsers NOT FOR SALE OR DISTRIBUTIONconverge upon the latest standardsNOT is to setFOR every SALE known OR property DISTRIBUTION variant (CSS3, -moz-, -webkit-, and others potentially) and, in the case of gradients, assign multiple gradient variations. Here is a code example: ✞ ¤ varfooter© Jones= &document Bartlett.getElementById Learning, LLC("footer"); © Jones & Bartlett Learning, LLC footer.innerHTML = "Look ma, no images!"; NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION // Mozilla version: WebKit ignores this. document.body.style.backgroundImage = "-moz-linear-gradient(left, lightgray, white)"; © Jones &footer Bartlett.style Learning,.backgroundImage LLC = © Jones & Bartlett Learning, LLC NOT FOR SALE"-moz-radial-gradient(25% OR DISTRIBUTION 50%, circle," +NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 483 — #21 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9.2 HTML and CSS 483

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC "whiteNOT 0%, FOR rgb(200, SALE 0, OR 0) DISTRIBUTION 50%, rgb(128, 0, 0) 100%)"; NOT FOR SALE OR DISTRIBUTION

// WebKit version: Mozilla ignores this. document.body.style.backgroundImage = "-webkit-gradient(linear, 0% 0%, 100% 0%," + © Jones &"color-stop(0, Bartlett Learning, lightgray), LLC color-stop(1, white))"© Jones; & Bartlett Learning, LLC NOT FORfooter SALE.style OR.backgroundImage DISTRIBUTION= NOT FOR SALE OR DISTRIBUTION "-webkit-gradient(radial, 25% 50%, 0, 50% 100%, 750," + "color-stop(0, white), color-stop(0.5, rgb(200, 0, 0))," + "color-stop(1, rgb(128, 0, 0)))"; © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC // CSS3 version: the latest browsers take this. NOT FOR SALE ORdocument DISTRIBUTION.body.style.backgroundImageNOT= FOR SALE OR DISTRIBUTION "linear-gradient(left, lightgray, white)"; footer.style.backgroundImage = "radial-gradient(25% 50%, circle," + "white 0%, rgb(200, 0, 0) 50%, rgb(128, 0, 0) 100%)"; ✝ © Jones & Bartlett Learning, LLC © Jones &✆ Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION It’s an intimidating number of settings (and we haven’t even covered all of them!), but the effort leads to a great deal of flexibility in coloring and/or displaying web elements—all without running a paint program or image editor. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC 9.2.4 Absolute Position NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION When web pages are designed as documents and user interfaces, they tend to follow default sizing, flow, and positioning rules. This is a good thing for those purposes, as consistency and device/window independence are paramount for such applica- © Jones & Bartletttions. Learning, In computer LLC graphics, however, we© wantJones greater & Bartlett flexibility Learning, and sometimes LLC NOT FOR SALE OReven DISTRIBUTION complete freedom from those constraintsNOT FOR and conventions. SALE OR DISTRIBUTION To make DOM elements support pixel-level positioning, do the following: 1. Determine the element that serves as the container for the overall graphics display. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC 2. SettheNOTposition FOR SALEstyle propertyOR DISTRIBUTION of the container element as relativeNOT. FOR This SALE OR DISTRIBUTION can be done directly within the HTML as follows: ✞ ¤

© Jones &
Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR✝ SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION✆

© Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. 3723 ©✐ Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ✐ NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ✐ ✐ ✐ ✐

“80609˙CH09˙Dionisio” — 2011/8/29 — 9:22 — page 484 — #22 ✐ ✐ © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

484 CHAPTER 9 Graphics and Animation

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOTAlternatively, FOR SALEposition OR DISTRIBUTIONcan be setwithCSS rules. The webNOT page atFORhttp: SALE OR DISTRIBUTION //javascript.cs.lmu.edu/basicanimation uses this approach, with the CSS rules placed in a different file instead of written “inline” within a style element. This is analogous to using the script tag with an src attribute. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC 3. Elements within the container element must, in turn, have their position NOT FOR SALEstyle OR property DISTRIBUTION set to absolute. NOT FOR SALE OR DISTRIBUTION

4. Initial position and size may also be set. The four style properties left, top, width, and height facilitate this—they mean exactly what their names © Jones & Bartlett Learning,say. LLC The positions should be© followed Jones by& Bartlett an appropriate Learning, unit LLC of measure, NOT FOR SALE OR DISTRIBUTIONtypically pixels (px). The propertiesNOT FORleft: SALE 0px and ORtop: DISTRIBUTION 0px correspond to the upper-left corner of the container element. Direct HTML setting of these properties looks like this: ✞ ¤ © SALE OR DISTRIBUTION