Dynamic HTML in Action

The Structured Graphics control is an effective and versatile tool for adding lightweight graphics to a Web document. Structured graphics are vector-based (meaning that they are constructed out of a series of lines and curves, rather than pixel by pixel like a JPG or GIF image) and tend to have very small file sizes compared to conventional graphics.

The DirectAnimation controls, like other ActiveX controls, must be identified on the page. The basic syntax for using this control is as follows:

<OBJECT ID=ObjectID CLASSID="CLSID:369303C2-D7AC-11D0-89D5-00A0C90833E6" STYLE="height: xx; width: xx; [styleProperty: xx]"> <PARAM NAME="PropertyName" VALUE="PropertyValue"> </OBJECT>

Let's examine each part of the code. Notice first that everything is enclosed in a set of <OBJECT></OBJECT> tags, an HTML mechanism for identifying a non-HTML item. The ID attribute gives the object a name so that it can be referenced easily by script. Although an ID is not necessary, you will probably find that an object is of limited use without one since it becomes more difficult to address through script.

Every ActiveX control has a unique CLASSID—a required identifier that tells the browser which exact type of object is being used. Fortunately, you don't need to memorize those long hexadecimal number sequences. The easiest way to code the correct CLASSID is simply to copy it from another page that uses that control. To help you get started, we have included a text file named da_id.txt in the chap19 folder on the companion CD that contains all the CLASSIDs for the DirectAnimation controls, along with their friendly names. Many of the HTML editing programs, such as Microsoft FrontPage, Microsoft Visual Interdev, and others make adding objects (and their CLASSIDs) to a page as simple as selecting a name from a list.

The STYLE attribute is not completely necessary either, but it is very useful. It can be used to set a height and width for the overall structured graphic object (independent of any shapes created inside it). It is important that you size a structured graphic to be large enough to hold any items you create inside it. For example, if the object is only 100 pixels wide but it holds a shape 300 pixels wide, the shape inside will be clipped. Any shapes created inside the structured graphic object are normally drawn starting at its center. Without planning for this, you might find a shape being clipped by the right and bottom edges of the structured graphic object. You can avoid this situation by using the ExtentHeight and ExtentWidth parameters, discussed later in this chapter. The STYLE attribute also can be used to position an object on the page or to set its z-index to put the object behind or in front of other elements.

The PARAM tags allow you to set values for many of the properties that affect a control. <PARAM> tags can also be used to call methods of a particular control. The name portion of the PARAM element refers either to a property name, such as HighQuality, or to a line number. The value portion indicates either the value assigned to the property or a particular method that should be used. These properties and methods are different for every object. Although many objects might use properties or methods of the same name, the effects often differ for each object.

Code Listing 19-1 demonstrates a simple structured graphics object that creates a square. Figure 19-1 displays the results.

Code Listing 19-1.

<HTML> <HEAD> <TITLE>Code Listing 19-1</TITLE> </HEAD> <BODY> <OBJECT ID="square" CLASSID="CLSID:369303C2-D7AC-11D0-89D5-00A0C90833E6" STYLE="height: 102; width: 102"> <PARAM NAME="Line0001" VALUE="SetLineColor(0,0,255)"> <PARAM NAME="Line0002" VALUE="SetLineStyle(1,2)"> <PARAM NAME="Line0003" VALUE="SetFillStyle(10)"> <PARAM NAME="Line0004" VALUE="SetFillColor(0,0,255,255,196,196)"> <PARAM NAME="Line0005" VALUE="Rect(1,1,100,100,0)"> <PARAM NAME="ExtentTop" VALUE="0"> <PARAM NAME="ExtentLeft" VALUE="0"> </OBJECT> </BODY> </HTML>

Figure 19-1. A simple square with a fading fill.

NOTE


Many methods and properties require the use of line numbers. These settings will be executed in the order of their line numbers. Order of execution can be important in many situations. A real-world example would be:

  1. Turn so that you are facing South.
  2. Extend your West arm.
  3. Turn so that you are facing North.

In this case, you would finish with your right arm extended. If Steps 1 and 3 had been reversed, you would finish with your left arm extended.

This code sample shows some of the basic techniques for creating your own structured graphics. (Methods for automatically generating structured graphics shapes from existing graphic files will be covered later in this chapter.) The first two <PARAM> tags are used to define the line that creates the square. The first <PARAM> tag defines the color of the line in red, green, and blue components, in that order. We have set the red and green to 0 (thus no red or green) and the blue to 255 (the maximum), so our surrounding line is blue. The second <PARAM> tag sets the style of the line. The value of 1 sets it to a solid line, while 0 would have meant no line and 2 a dotted line. The second number in the SetLineStyle method sets the width of the line. The next two <PARAM> tags set the type and color of fill used for the shape. The SetFillStyle method supports a number of different types of fills, shown in the table below. We used fill type 11 to give a radial gradient from pure blue to a light red.

Style Number Equivalent
No Fill 0
Solid 1
Horizontal Hatch 3
Vertical Hatch 4
Forward Diagonal Hatch 5
Backward Diagonal Hatch 6
Cross Hatch 7
Diagonal Cross Hatch 8
Horizontal Gradient 9
Vertical Gradient 10
Radial Gradient 11
Line Gradient 12
Rectangular Gradient 13
Shaped Gradient 14

The next <PARAM> tag uses the Rect method to draw a rectangle. A rectangle is considered one of the basic shapes used to create structured graphics. These basic shapes are also known as primitives and are the building blocks for creating more complex images. The standard methods for creating primitives are listed in the following table.

Method Description
Arc Draws a circular arc or an elliptical arc.
Oval Draws an ellipse.
Pie Draws a wedge shape.
Polygon Draws a closed polygon.
Polyline Draws a segmented line.
Polyspline Draws a smooth curve through a set of points.
Rect Draws a rectangle.
RoundRect Draws a rectangle with rounded corners.
Text Draws text strings as structured graphics.

Each of these methods is used in a different way, due to the differences among shapes. You can find full descriptions of these methods at microsoft.com/directx/dxm/help/da/ref_cntrl/controls_oview.htm. You can also go to the MSDN Online Resource page at msdn.microsoft.com/resources/schurmandhtml.htm and choose Structured Graphics Overview.

The final two <PARAM> tags set the ExtentTop and ExtentLeft properties. These are similar to the CSS properties top and left. Setting both of these to 0 causes the shape to be drawn starting from the top left of the structured graphics object. Similar properties include ExtentHeight and ExtentWidth, which set the height and width of the shape within the overall structured graphic object. They can be used only if ExtentTop and ExtentLeft have been set. Some of the other useful properties of the Structured Graphics control that you can set with <PARAM> tags and through script are included in this table.

Property Description
CoordinateSystem Controls whethery values start with 0 at the top of the page (default, value 0) or at the bottom of the page (value 1).
HighQuality Turns antialiasing on (value 1) or off (default, value 0).
MouseEventsEnabled Controls whether mouse events are processed on the shapes (value 1) or not (default, value 0).
PreserveAspectRatio Controls whether a shape's ratio of height to width is maintained as it is scaled (value 1) or not (default, value 0).
SourceURL Points the Structured Graphics control to use an external file for its parameters. Similar to pointing a script tag to a JS file.

Several additional properties of the control can be set only through script. These properties deal with interfacing the Structured Graphics control with its more capable relative, the DirectAnimation control, which will not be covered in this book due to its highly technical nature.

Creating Structured Graphics Easily

From the previous information, you can imagine that creating a complex structured graphic could entail using hundreds of <PARAM> tags in the correct sequence. Needless to say, this could be difficult. As of this writing, structured graphics are most easily created by converting Windows Metafiles, which use the WMF filename extension. Many commercial applications can create WMF graphics, including CorelDraw!, Adobe Illustrator, Microsoft PowerPoint, Harvard Graphics, and many more. Microsoft Office also includes a number of clip art images in the WMF format.

To use WMF files in your Web pages, you'll need to convert them to Structured Graphics. You can find the Windows Metafile Converter utility (Wmfcnv.exe) in the Tools folder on the companion CD. This tool converts Windows Metafiles to text files of <PARAM> tags used to define a structured graphic. To use it, simply open the WMF file you want to convert, set the options you prefer, and save the file. You will then have a text file that you can cut and paste into your Web documents or refer to with the SourceURL <PARAM> tag.

Advantages of Vector-Based Graphics

The two most popular forms of computer graphics are raster graphics and vector graphics. Raster graphics, the form used by most of the popular image formats such as BMP and GIF, are built pixel by pixel, with information about each pixel saved as part of the graphic. Vector graphics are built using a series of lines and curves. The information for these images is saved for each line or curve rather than for each pixel.

Unlike raster images, vector images can be resized, rotated, stretched, and manipulated in many ways while keeping the integrity of the original image. If you zoom in too far in a normal bitmap graphic, aliasing will occur and the edges of a shape will no longer appear smooth. To demonstrate the advantages of vector graphics, we'll need a more complex structured graphic than the simple rectangle used in the last example. The necessary code is shown in Code Listing 19-2; you can see the output in Figure 19-2.

Code Listing 19-2.

<HTML> <HEAD> <TITLE>Code Listing 19-2</TITLE> </HEAD> <BODY> <INPUT TYPE=button VALUE="Bigger" onclick="Idea.Scale(1.2,1.2,0)"> <INPUT TYPE=button VALUE="Smaller" onclick="Idea.Scale(.8,.8,0)"> <BR> <OBJECT ID="Idea" STYLE="width: 400; height: 400" CLASSID="CLSID:369303C2-D7AC-11D0-89D5-00A0C90833E6"> <PARAM NAME="Line0001" VALUE="SetLineColor(0,0,0)"> <PARAM NAME="Line0002" VALUE="SetFillColor(255,255,0)"> <PARAM NAME="Line0003" VALUE="SetLineStyle(6,1)"> <PARAM NAME="Line0004" VALUE="Polygon(20,-6,-67,-6,-71,-7,-74,-8,-76, -10,-79,-10,-82,-9,-84,-6,-86,-4,-87,-2,-86,-1,-86,1,-84,2,-82,2,-79, 0,-77,-2,-73,-2,-71,-2,-69,-3,-67,-4,-65)"> <PARAM NAME="Line0005" VALUE="SetLineColor(0,0,0)"> <PARAM NAME="Line0006" VALUE="SetFillColor(0,0,0)"> <PARAM NAME="Line0007" VALUE="SetLineStyle(0,1)"> <PARAM NAME="Line0008" VALUE="Polygon(5,-14,-84,-22,-85,-23,-87,-22, -88,-21,-88)"> <PARAM NAME="Line0009" VALUE="Polygon(5,-10,-92,-14,-96,-14,-98,-12, -99,-11,-97)"> <PARAM NAME="Line0010" VALUE="Polygon(5,-1,-92,-2,-98,0,-100,2,-100, 1,-98)"> <PARAM NAME="Line0011" VALUE="Polygon(5,5,-89,11,-94,13,-93,13,-92, 12,-91)"> <PARAM NAME="Line0012" VALUE="Polygon(21,1,-48,-3,-54,-7,-58,-12,-58, -17,-55,-20,-52,-21,-47,-20,-40,-17,-33,-11,-28,-6,-26,-2,-25,2,-26, 4,-28,5,-33,5,-39,3,-44,12,-48,12,-50,12,-51,3,-46)"> <PARAM NAME="Line0013" VALUE="Polygon(38,11,-27,6,-23,4,-22,3,-19,5, -16,6,-15,11,-17,19,-23,25,-30,32,-38,32,-41,32,-50,30,-64,32,-72, 32,-75,31,-77,28,-78,26,-80,28,-87,27,-89,25,-88,24,-79,24,-76,23, -75,20,-76,17,-76,17,-74,19,-73,22,-73,24,-71,26,-69,27,-64,28,-55, 28,-47,28,-40,26,-38,20,-33,14,-30)"> <PARAM NAME="Line0014" VALUE="Polygon(35,-6,-20,-9,-21,-15,-21,-20,-17, -28,-8,-32,-1,-32,1,-30,6,-26,8,-20,10,-16,12,-14,14,-15,16,-18,20, -22,20,-25,19,-27,20,-26,22,-23,23,-18,23,-14,22,-11,20,-10,17,-9,14, -11,11,-16,9,-22,8,-26,5,-28,2,-27,-2,-23,-8,-19,-11,-12,-14,-6,-15, -6,-18)"> <PARAM NAME="Line0015" VALUE="Polygon(20,11,-6,8,-16,5,-21,-1,-23,-7, -22,-10,-17,-9,-10,-8,0,-8,10,-10,18,-11,22,-10,26,-7,28,-3,30,0,31, 5,31,10,27,14,18,14,11,11,2)"> <PARAM NAME="Line0016" VALUE="Polygon(28,0,22,-5,21,-8,22,-9,26,-8,49, -8,54,-10,64,-10,75,-9,81,-10,84,-16,89,-18,95,-18,97,-13,100,-12,99, -12,95,-10,90,-8,87,-6,86,-4,83,-3,82,-5,80,-6,79,-7,74,-6,63,-3,52, 0,42,1,31)"> <PARAM NAME="Line0017" VALUE="Polygon(31,12,31,12,24,8,21,3,21,2,24,3, 30,5,40,8,47,10,56,11,64,11,71,10,76,8,77,8,79,10,81,13,82,17,82,26, 84,28,87,32,86,33,81,32,80,25,79,17,79,14,79,13,76,14,72,14,64,13,55, 12,44,12,34)"> </OBJECT> </BODY> </HTML>

Figure 19-2. This entire page, including the graphics code, is only 2.67 KB.

Despite the length of Code Listing 19-2, the file size of the entire page, including the structured graphic, is only 2.67 KB. The image shown in Figure 19-2 is also completely scalable. By clicking the buttons in the upper left corner of the page, you can change the size of the image—and you can do it in real time, after the page has loaded. There is no need to load another image or even to refresh the page. This is accomplished by giving the Structured Graphics control's Scale method the factors by which we want to scale the graphic in the x, y, and z axes. In Code Listing 19-2, we scaled the image using the same value for the x and y coordinates to resize the image while maintaining its aspect ratio. You can experiment by using different scale values for the x, y, and z coordinates, which creates the effect of stretching the image. In addition to scale, structured graphics support many other methods, including Clear, Rotate, Translate (moves the graphic), SetIdentity (resets any changes), and Transform4x4 (scales, rotates, and translates all at once).

Code Listing 19-3 uses the structured graphic from Code Listing 19-2, but it adds buttons that allow rotation, stopping of rotation, and resetting to the initial position, as shown in Figure 19-3.

Code Listing 19-3.

<HTML> <HEAD> <TITLE>Code Listing 19-3</TITLE> <SCRIPT LANGUAGE="JavaScript"> var Xint,Yint,Zint function SpinIt(degX,degY,degZ){ Idea.Rotate(degX,degY,degZ) } </SCRIPT> </HEAD> <BODY> <INPUT TYPE=button VALUE="RotateX" onclick="clearInterval(Xint);Xint=setInterval(`SpinIt(20,0,0)',0100)"> <INPUT TYPE=button VALUE="RotateY" onclick="clearInterval(Yint);Yint=setInterval(`SpinIt(0,20,0)',0100)"> <INPUT TYPE=button VALUE="RotateZ" onclick="clearInterval(Zint);Zint=setInterval(`SpinIt(0,0,20)',0100)"> <BR> <INPUT TYPE=button VALUE=" Stop X " onclick="clearInterval(Xint)"> <INPUT TYPE=button VALUE=" Stop Y " onclick="clearInterval(Yint)"> <INPUT TYPE=button VALUE=" Stop Z " onclick="clearInterval(Zint)"> <INPUT TYPE=button VALUE="Initial Position" onclick="Idea.SetIdentity()"> <BR> <OBJECT ID="Idea" STYLE="width: 250; height: 250" CLASSID="CLSID:369303C2-D7AC-11D0-89D5-00A0C90833E6"> <PARAM NAME="Line0001" VALUE="SetLineColor(0,0,0)"> <PARAM NAME="Line0002" VALUE="SetFillColor(255,255,0)"> <PARAM NAME="Line0003" VALUE="SetLineStyle(6,1)"> <PARAM NAME="Line0004" VALUE="Polygon(20,-6,-67,-6,-71,-7,-74,-8,-76, -10,-79,-10,-82,-9,-84,-6,-86,-4,-87,-2,-86,-1,-86,1,-84,2,-82,2,-79, 0,-77,-2,-73,-2,-71,-2,-69,-3,-67,-4,-65)"> <PARAM NAME="Line0005" VALUE="SetLineColor(0,0,0)"> <PARAM NAME="Line0006" VALUE="SetFillColor(0,0,0)"> <PARAM NAME="Line0007" VALUE="SetLineStyle(0,1)"> <PARAM NAME="Line0008" VALUE="Polygon(5,-14,-84,-22,-85,-23,-87,-22, -88,-21,-88)"> <PARAM NAME="Line0009" VALUE="Polygon(5,-10,-92,-14,-96,-14,-98,-12, -99,-11,-97)"> <PARAM NAME="Line0010" VALUE="Polygon(5,-1,-92,-2,-98,0,-100,2,-100, 1,-98)"> <PARAM NAME="Line0011" VALUE="Polygon(5,5,-89,11,-94,13,-93,13,-92, 12,-91)"> <PARAM NAME="Line0012" VALUE="Polygon(21,1,-48,-3,-54,-7,-58,-12,-58, -17,-55,-20,-52,-21,-47,-20,-40,-17,-33,-11,-28,-6,-26,-2,-25,2,-26, 4,-28,5,-33,5,-39,3,-44,12,-48,12,-50,12,-51,3,-46)"> <PARAM NAME="Line0013" VALUE="Polygon(38,11,-27,6,-23,4,-22,3,-19,5, -16,6,-15,11,-17,19,-23,25,-30,32,-38,32,-41,32,-50,30,-64,32,-72, 32,-75,31,-77,28,-78,26,-80,28,-87,27,-89,25,-88,24,-79,24,-76,23, -75,20,-76,17,-76,17,-74,19,-73,22,-73,24,-71,26,-69,27,-64,28,-55, 28,-47,28,-40,26,-38,20,-33,14,-30)"> <PARAM NAME="Line0014" VALUE="Polygon(35,-6,-20,-9,-21,-15,-21,-20,-17, -28,-8,-32,-1,-32,1,-30,6,-26,8,-20,10,-16,12,-14,14,-15,16,-18,20, -22,20,-25,19,-27,20,-26,22,-23,23,-18,23,-14,22,-11,20,-10,17,-9,14, -11,11,-16,9,-22,8,-26,5,-28,2,-27,-2,-23,-8,-19,-11,-12,-14,-6,-15, -6,-18)"> <PARAM NAME="Line0015" VALUE="Polygon(20,11,-6,8,-16,5,-21,-1,-23,-7, -22,-10,-17,-9,-10,-8,0,-8,10,-10,18,-11,22,-10,26,-7,28,-3,30,0,31, 5,31,10,27,14,18,14,11,11,2)"> <PARAM NAME="Line0016" VALUE="Polygon(28,0,22,-5,21,-8,22,-9,26,-8,49, -8,54,-10,64,-10,75,-9,81,-10,84,-16,89,-18,95,-18,97,-13,100,-12,99, -12,95,-10,90,-8,87,-6,86,-4,83,-3,82,-5,80,-6,79,-7,74,-6,63,-3,52, 0,42,1,31)"> <PARAM NAME="Line0017" VALUE="Polygon(31,12,31,12,24,8,21,3,21,2,24,3, 30,5,40,8,47,10,56,11,64,11,71,10,76,8,77,8,79,10,81,13,82,17,82,26, 84,28,87,32,86,33,81,32,80,25,79,17,79,14,79,13,76,14,72,14,64,13,55, 12,44,12,34)"> </OBJECT> </BODY> </HTML>

Figure 19-3. The graphic can be rotated around any axis.

The code listing above uses the Rotate and SetIdentity methods of the Structured Graphics control. The Rotate method simply takes the graphic and rotates it a specified number of degrees around the x, y, and z axes. The RotateX, RotateY, and RotateZ buttons each contain an onclick event handler that looks something like the following:

onclick="clearInterval(Xint);Xint=setInterval(`SpinIt(20,0,0)',0100)"

This event handler takes advantage of the setInterval and clearInterval methods. The setInterval method is a method of the window object in both Internet Explorer and Netscape Navigator that allows for an action to be repeated whenever a particular period of time has elapsed. In this case, we specify that the SpinIt function should be called every 100 milliseconds. The SpinIt function rotates the graphic as specified above. Because setInterval repeats the rotate operation, the graphic appears to spin. The Xint variable is used to keep track of the particular timer object used by this setInterval method; it is used by clearInterval to remove any previous spinning occurring around that axis. The Initial Position button uses the SetIdentity method to reset the structured graphic to its initial orientation.

The Structured Graphics control supports a number of useful events, including onclick, ondblclick, onmousedown, onmousemove, onmouseout, onmouseover, and onmouseup.

Using Script Blocks to Capture Events

The best way of capturing an event on a DirectAnimation control is usually with a dedicated script block. For example, you could capture an onmouseout event on the above mentioned Idea structured graphic as follows:

<SCRIPT LANGUAGE="JavaScript" FOR="Idea" EVENT="onmouseout"> [Event handling script here] </SCRIPT>

This section has supplied only the basics on how to get started with the Structured Graphics control. You can use this control in many other ways and with many additional methods and properties. For more information about structured graphics, check out the Structured Graphics control reference by going to microsoft.com/directx/dxm and then choosing DirectAnimation followed by Multimedia Controls.

Категории