Académique Documents
Professionnel Documents
Culture Documents
Lecture 41
Images & Animation (Web Development Lecture 14)
1
Notationaly, these functions are accessed by referring to various methods of the String object Moreover, this object also contains the length property
3
String Methods
FORMAT string.methodName( ) EXAMPLE: name = Bhola ; document.write( name.toUpperCase( ) ) ; document.write( name.bold( ) ) ; BHOLABhola
4
link( URL )
split( delimiter )
7
The + Operator
When + is used with numeric operands, it adds them When it is used with string operands, it concatenates them When one operand is a string, and the other is not, the non-string will first be converted to a string and then the two strings will be concatenated 9
document.write( "Yes" ^ 43 ) ;
NaN
10
12
13
Images in HTML
It is quite straight forward to include gif and jpg images in an html Web page using the <IMG> tag
15
<H1>Image Demo</H1>
Here is an image <IMG src="die5.gif"> <IMG src="die5.gif" height="63" width="126"> <P>
Images in JavaScript
Images in JavaScript can be manipulated in many ways using the built-in object Image Properties: name, border, complete, height, width, hspace, vspace, lowsrc, src Methods: None Event handlers: onAbort, onError, onLoad, etc.
17
Image Preloading
The primary use for an Image object is to download an image into the cache before it is actually needed for display
This technique can be used to create smooth animations or to display one of several images based on the requirement
18
20
****
21
22
die1.gif
die2.gif
die3.gif
die4.gif
die5.gif
die6.gif
23
<HTML> <HEAD> <TITLE>Roll the Die</TITLE> <SCRIPT> JavaScript Code </SCRIPT> </HEAD>
24
25
Another Example
Develop a Web page that displays six thumbnail images and a main image The main image should change to a larger version of the thumbnail as soon as the mouse moves over on a thumbnail image
27
28
<HTML> <HEAD> <TITLE>Image Selector</TITLE> <SCRIPT> JavaScript Code </SCRIPT> </HEAD> <BODY > HTML Code </BODY> </HTML>
29
30
<IMG name="big" src="die6.gif" width="252" height="252"><P> <IMG src="die1.gif" width="63" height="63" onMouseOver= "document.big.src=dieImg[ 1 ].src"> <IMG src="die6.gif" width="63" height="63" onMouseOver= 31 "document.big.src=dieImg[ 6 ].src">
32
Animation Example 1
Take 16 images and cycle through them to create an animation effect
33
10
11
12
13
14
15
16
34
35
<HTML> <HEAD> <TITLE>Animation 1</TITLE> <SCRIPT> JavaScript Code </SCRIPT> </HEAD> <BODY > HTML Code </BODY> </HTML>
36
setTimeout( ) executes circulate( ) once after a delay of gap milliseconds <CENTER> <IMG name="circle" src="circle1.gif" onLoad="setTimeout( 'circulate( )', gap )"> </CENTER>
37
function circulate( ) {
document.circle.src = circImg[ imageN ].src ; imageN = imageN + 1 ; if( imageN > 16 ) imageN = 1 ; }
39
40
Animated Gifs
We could have saved the 16 gif images of the previous example in a single file in the form of an animated gif, and then used it in a regular <IMG> tag to display a moving image However, JavaScript provides better control over the sequencing and the gap between the individual images
41
Animation Example 2
Take 16 images and cycle through them to create an animation effect Provide buttons to slow down or speed up the animation
42
43
<HTML> <HEAD> <TITLE>Animation 2</TITLE> <SCRIPT> JavaScript Code </SCRIPT> </HEAD> <BODY > HTML Code </BODY> </HTML>
44
<CENTER> <IMG name="circle" src="circle1.gif" onLoad="setTimeout( 'circulate( )', gap )"> </CENTER> <FORM> <INPUT type="button" value="Slow Down" onClick="slowDown( )"> <INPUT type="button" value="Speed Up" onClick="speedUp( )"> </FORM> 45
No change
for( k = 1; k < 17; k = k + 1 ) { // Preload images circImg[ k ] = new Image( ) ; circImg[ k ].src = "circle" + k + ".gif" ; }
46
function circulate( ) {
document.circle.src = circImg[ imageN ].src ; imageN = imageN + 1 ; if( imageN > 16 ) imageN = 1 ; }
No change
47
function slowDown( ) {
gap = gap + 20 ;
48
49
Flash Animation
Designed for 2-D animations, but can be used for storing static vector-images as well A special program (called a plug-in) is required to view Flash files in a Web browser Can be used to design complete, animated Web sites with hardly any HTML in it Binary-file storage
50
51
52
Our
th 15
(Programming Methodology)
To understand effective programming practices that result in the development of correct programs with minimum effort
To become familiar with simple debugging techniques
53