Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help Troubleshooting Some Image Animation Code 1

Status
Not open for further replies.

flasher40

Technical User
Apr 13, 2007
80
US
Hello,

Chip Chapin has a JavaScript "Eiffel Tower" animation example at:


I know just enough about JavaScript (and programming in general) to get myself into trouble. I wanted to create an animation similar to the Eiffel Tower one at his website. So, I copied and pasted the code from the Eiffel Tower page into my editor and deleted all the non-relevant text, etc. Then, I took five .jpg images to test the process, renamed them anim0.jpg, anim1.jpg, etc., created the image exampleA01-anim0.jpg, and placed the images in the same folder as the .html page. Then, I changed all references from .gif to .jpg in the code.

Below is the resulting code. When I test the animation, the exampleA01-anim0.jpg appears, but the animation doesn't work. Any ideas on what I need to do differently to make it work? Thanks!

Bill

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>
JavaScript Example A-1 -- Image Animation w/Speed Control
</title>

<script language="JavaScript">


function SymError()
{
return true;
}

window.onerror = SymError;

var SymRealWinOpen = window.open;

function SymWinOpen(url, name, attributes)
{
return (new Object());
}

window.open = SymWinOpen;


</script>

<script type="text/javascript" language="JavaScript1.2">
anims = new Array(5);
var frame = 0;
var timeout_state = null;

function imageLoad() {
for(var i = 0; i<5; i++) {
anims = new Image();
anims.src = "exampleA01-anim" + i + ".jpg";
}
}

function animator() {
document.animImage.src = anims[frame].src;
frame = (frame + 1);
if(frame >= 5) {
frame = 0;
}
timeout_state = setTimeout("animator()", document.animForm.animPace.value);
}

function buttonCheck() {
if(document.animForm.animButton.value == "Start") {
document.animForm.animButton.value = "Stop";
animator();
} else {
document.animForm.animButton.value = "Start";
clearTimeout(timeout_state);
timeout_state = null;
}
}


</script>
</head>
<p>
<img src="exampleA01-anim0.jpg" name="animImage"
</p>
<form name="animForm">
<input type="button" value="Start" name="animButton"
onclick="buttonCheck()"><input type="text" value="250"
name="animPace">milliseconds
</form>
<br>
<br>
</body>
</html>


<script language="JavaScript">
<!--
var SymRealOnLoad;
var SymRealOnUnload;

function SymOnUnload()
{
window.open = SymWinOpen;
if(SymRealOnUnload != null)
SymRealOnUnload();
}

function SymOnLoad()
{
if(SymRealOnLoad != null)
SymRealOnLoad();
window.open = SymRealWinOpen;
SymRealOnUnload = window.onunload;
window.onunload = SymOnUnload;
}

SymRealOnLoad = window.onload;
window.onload = SymOnLoad;

//-->
</script>
 
I suggest you contact Chip Chapin and ask him - it's his code. Once you've removed Norton Security from your system... which is injecting hostile code into every page you view in an attempt to thwart popup code (and in effect damaging normal code).

Cheers with that,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Thanks, Jeff

I have sent Chip an email but haven't heard from him.

If anyone else can see what's wrong with the code I've included in my original post, I'm still looking for suggestions.

That's the first I've heard about the problem you mentioned with Norton Security. Thanks for the tip.

Bill
 
Based on Chip's comment above about Norton, I stripped out what seemed to be that code. What's left is shown below.

Now, when I click on the START button, I get the error message:

Line 21, Char 9
'anims[...].src' is null or not an object

Any help with figuring out what's going on would be appreciated.

Thanks,
Bill

REVISED CODE:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>
JavaScript Example A-1 -- Image Animation w/Speed Control
</title>

<script type="text/javascript" language="JavaScript1.2">
anims = new Array(5);
var frame = 0;
var timeout_state = null;

function imageLoad() {
for(var i = 0; i<5; i++) {
anims = new Image();
anims.src = "exampleA01-anim" + i + ".jpg";
}
}

function animator() {
document.animImage.src = anims[frame].src;
frame = (frame + 1);
if(frame >= 5) {
frame = 0;
}
timeout_state = setTimeout("animator()", document.animForm.animPace.value);
}

function buttonCheck() {
if(document.animForm.animButton.value == "Start") {
document.animForm.animButton.value = "Stop";
animator();
} else {
document.animForm.animButton.value = "Start";
clearTimeout(timeout_state);
timeout_state = null;
}
}


</script>
</head>
<body>
<p>
<img src="exampleA01-anim0.jpg" name="animImage"
</p>
<form name="animForm">
<input type="button" value="Start" name="animButton"
onclick="buttonCheck()"><input type="text" value="250"
name="animPace">milliseconds
</form>
<br>
<br>
</body>
</html>
 
Your problem is you're not ever calling the function imageLoad to load your images into the array anim.


I tested this so I know that is the problem.

[monkey][snake] <.
 
Could someone show me the specific code changes needed to solve the problem?

Bill
 
I won't show specific code changes cause this should be completely elementary.

I will say put a call to the function imageLoad on the onload event of the <body> tag.

You should be able to figure it out based on that.

[monkey][snake] <.
 
After reviewing Chip Chapin’s (see start of my opening post) explanation, I made the following changes to my code:

1. From: <body>
To: <body onLoad="imageLoad()">

2. (2 lines below <body …>)
From: <img src="exampleA01-anim0.jpg" name="animImage"
To: <img src="anim0.jpg" name="animImage">

In short, the final section of code is now:

<body onLoad="imageLoad()">
<p>
<img src="anim0.jpg" name="animImage">
<form name="animForm">
<input type="button" value="Start" name="animButton"
onclick="buttonCheck()"><input type="text" value="250"
name="animPace">&nbsp;&nbsp;milliseconds
</form>
<br>
<br>
</body>
</html>

Now, the initial image keeps flashing on and off, but no animation occurs.

I’ve created a web page using the code:


(Aside: If you try viewing the source code on that web page and see code beginning with ” function SymError(),” that means your Norton Security program has inserted crippling code.)

Bill
 
I'm sorry, in the example I tested I did a little more changes than just what I posted.

Here are the imageLoad and animator functions after I changed them:

Code:
function imageLoad() {
     for(var i = 0; i<5; i++) {
         anims[i] = new Image();
         anims[i] = "exc-error-icon.gif";
     }
}                       

function animator() {
        document.animImage.src = anims[frame];
        frame = (frame + 1);
        if(frame >= 5) {
                frame = 0;
        }
        timeout_state = setTimeout("animator()", document.animForm.animPace.value);
}

Note the changes in these 2 statements

anims = "exc-error-icon.gif";
document.animImage.src = anims[frame];

In this example I just kept the same image, but made sure the anims array iterated.


[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top