|
Abstract This project is a fun way to try your hand at programming. You'll learn how to create some simple animations, and you'll perform tests and make measurements to help you create more realistic-looking animations. All you need to get started is a Web browser and a text editor (like Notepad).Objective The goal of this project is to investigate the effects of timing interval and step size on perceived animation smoothness. You will write a simple JavaScript program that will move an object around on a Web page. This project will help you create more advanced Web pages that could have any type of animation. Introduction This is an example of a first-time programming project. You will be writing a basic animation program that will move an object around on your computer screen, on a Web page in your browser window. The project uses JavaScript, an interpreted programming language supported by most Web browsers. You will learn how to write an HTML file containing your program, and how to load and run the file in your browser. You'll also learn how to control the motion of the object, and you will experiment with different settings to see the effect on apparent smoothness of the motion. Preliminaries These are things you need to know before you get started.
Getting Started with JavaScript Now that you've succeeded with writing an HTML file and opening it with your browser, you're ready to delve into JavaScript. The following link has a step-by-step tutorial that will give you a great introduction to JavaScript programming: http://www.webteacher.com/javascript/index.html Writing a JavaScript Animation The rest of the Introduction will cover the pieces that you need to write an animation program in JavaScript:
A JavaScript Function As you learned in the tutorial, JavaScript uses functions. Functions can do nearly anything you want them to do. Your function will contain code that will run your animation. A function in JavaScript looks like this: Here is how to put the JavaScript function into an HTML file. The function is included in the section of the HTML file, like this:<HTML> <HEAD><TITLE>My HTML</TITLE> <!-- saved from url=(0030)http://www.sciencebuddies.org/ --> <!-- When this code is saved as a local file, the preceding line tells Internet Explorer to treat this file according to the security rules for the Internet zone (plus any security rules specific for the Science Buddies website). --> </HEAD> <BODY> <SCRIPT LANGUAGE="JavaScript1.2"> <!-- // This is an example of a single-line JavaScript comment. // Below is an example of a multi-line JavaScript comment. /* This function doesn't do anything useful yet, but we will add code later to make it work. function myFunction() { /* Your animation code will go here */ } // --> </SCRIPT> My Page </BODY> </HTML> An HTML Image Object Your function will need an object to animate. For this example, we will keep it simple. We will use a ball as an object. To get your object, right click on the red ball (below), and do a "Save As..." Save the ball image as ball1.gif, in the same directory as your HTML file. (Note: if you choose a different filename, you will also have to change the name in the HTML <IMG> tag, below.) Here is the ball image for you to save:
Now we will add the ball to your HTML file by inserting the following text just after <BODY>: Save your HTML file and open it in the Web browser. You should see the little red ball on your Web page. The ball should be 10 pixels wide. It should be located 200 pixels from the top of the Web page and 200 pixels from the left edge of the page. You can try changing the way your ball looks by changing the value of left, top, width, and height. For example, if you want a bigger ball, you can change the width and height from 10 to 50. Prove to yourself that it works. Try changing some of these values, saving the HTML file, and opening the file in a browser. An Object in JavaScript The object from step 2 is an HTML image object. We need to be able to use this object in JavaScript, so we have to create a JavaScript variable to represent the ball. Add this variable on the line before myFunction. The statement above creates the variable, next we have to assign a value to the variable. That is, we have to tell JavaScript that myBall should represent the HTML object "ball." To do this, we use a built-in JavaScript object called document. We can use document to get an HTML object by its ID. To do this, replace the last line you added with: The Time Animation requires that we change something on an interval. For example, we may want to change the position of the ball every five seconds, or change the color every ten seconds. We will tell JavaScript to call our function, on an interval so we can change something. To accomplish this add the following call to the built-in JavaScript method, "setInterval" (add this line before the code for myFunction()): Writing a Simple Animation Function in JavaScript For the first animation example, we will have the ball keep moving down the screen. First we need another variable to keep track of our current location. Add this variable after the myBall variable: Add this code between the braces of myFunction: In this case we are changing one of the properties of the ball. The property is style.top. We can change this property to make the ball move up or down. Now save your HTML file, and open it in your Web browser. You should see your ball keep moving down the screen. You have just created an animated Web page! If your page is not working, here is the entire HTML file: Improving the Animation Function: A Bouncing Ball Just having a ball move down of you Web page may not be very exciting. We can make the ball move up and down by using a variable to determine the direction the ball is moving. Once it reaches an upper limit or a lower limit, we will make it turn around. Add the variable direction to the line above myFunction: We will use this variable called direction so we know which way the ball is moving. 0 means the ball is moving down, and 1 means the ball is moving up. Try replacing myFunction with this new myFunction (to keep your previous work, save the HTML file with a new name): Going Further The goal of this project is to determine the best settings for the timer interval and ball step size in order to produce the smoothest animation. You can make the project even better by going further with your investigation of animation. The Variations section, below, has some suggestions to get you thinking.
Terms, Concepts, and Questions to Start Background Research To write a simple program in JavaScript, you should do research that enables you to understand the following terms and concepts:
Questions:
Bibliography
Materials and Equipment To do this experiment you will need the following materials and equipment:
Experimental Procedure Adding Variables The goal of this project is to determine the best settings for the timer interval and ball step size in order to produce the smoothest animation. The script you created in the Introduction section has hard-coded numbers for these quantities. It will be much more convenient to change these values if you add variables for the timer interval and the step size for the motion of the ball. This way, you will only need to change the value in one place, when the variable is assigned a value, rather than searching for a hard-coded value in multiple places throughout the script.
Calculating Time The next thing to do is to verify that your timer interval is working as you expect. For instance, there may be some timer settings that are too fast for your browser program to keep up with. For each of the timing intervals you wish to use in your experiment, you need to run a test like the one described below.
Measuring Smoothness
Collecting and Analyzing Your Data
Variations
Credits
By Nate Brogan, Symantec
|
If you like this project, you might enjoy exploring related careers.
![]() |
Computer Programmer Computers are essential tools in the modern world, handling everything from traffic control, car welding, movie animation, shipping, aircraft design, and social networking to book publishing, business management, music mixing, health care, agriculture, and online shopping. Computer programmers are the people who write the instructions that tell computers what to do. |
![]() |
Computer Software Engineer Are you interested in developing cool video game software for computers? Would you like to learn how to make software run faster and more reliably on different kinds of computers and operating systems? Do you like to apply your computer science skills to solve problems? If so, then you might be interested in the career of a computer software engineer. | |
![]() |
Software Quality Assurance Engineer & Tester Software quality assurance engineers and testers oversee the quality of a piece of software's development over its entire life cycle. Their goal is to see to it that the final product meets the customer's requirements and expectations in both performance and value. During the software life cycle, they verify (officially state) that it is possible for the software to accomplish certain tasks. They detect problems that exist in the process of developing the software, or in the product itself. They try and make things not work (try to "break" the software) by creating errors or combinations of errors that a user might make. For example, if a user enters a period or a pound sign for a password, will that break the software? They seek to anticipate potential issues with the software before they become visible. At the end of the life cycle, they reflect upon how problems or bugs arose, and figure out ways to make the software development process better in the future. |
![]() |
Multimedia Artist or Animator If you've ever watched a cartoon, played a video game, or seen an animated movie, you've seen the work of multimedia artists and animators. People in these careers use computers to create the series of pictures that form the animated images or special effects seen in movies, television programs, and computer games. | |
|
Join Science Buddies
Become a Science Buddies member! It's free! As a member you will be the first to receive our new and innovative project ideas, news about upcoming science competitions, science fair tips, and information on other science related initiatives. |