Section 16.4. Multimedia in JavaScript

[Page 540]

16.4. Multimedia in JavaScript

It's possible to do multimedia programming in JavaScript, but not how we've been doing it here. There isn't really a way to manipulate samples or pixels from JavaScriptJavaScript deals with much higher levels of data than that. The most common way to deal with multimedia in JavaScript is through plugins. Plugins to the browsers, like Apple QuickTime, RealVideo, and Netscape LiveAudio, can be manipulated from JavaScript.

We can do simple animations in JavaScript. There is a setInterval() function that can make a JavaScript function to run at regular intervalssuch as when we'd like an animation to update. Divisions (set up with <div>...</div> tags) can be controlled with styles (notice the tag at the top) that come from cascading style sheets, and they have positions. Given these two pieces, it's possible to move the picture of Barb slowly over time.

<html> <head> <title>The Simplest Possible Web Page</title> <style> #barb { position: absolute; left:0; top: 0; } </style> <script> function drift() {    var object =;    object.pixelTop = object.pixelTop + 5;    object.pixelLeft = object.pixelLeft + 5; } </script> </head> <body onLoad="setInterval('drift()',100)"> <h1>A Simple Heading</h1> <p>This is a very simple web page.</p> <div > <p><image src="/books/1/79/1/html/2/mediasources/barbara.jpg"   /> </p> </div> <p>Here is some more text, just to make the document larger and make it obvious where the picture is drifting. </body> </html>

Okay, this is a pretty contorted example to show a pretty weak animation. JavaScript isn't really designed to do this level of programming. Rather, it's glue to make things work together that are already defined at a high level. It doesn't deal well with frames or pixels or samples, but it's great for telling QuickTime to play a movie or to increase the audio in RealAudio.

JavaScript is better than Java for this kind of high-level programming where you're combining things and controlling items on an HTML page. It's very powerful to be able to embed the programming inside the HTML page! But Java is a more full-featured programming language that allows you to do a wide range of things from programming servers to chromakey movies.

[Page 541]

Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Year: 2007
Pages: 191 © 2008-2017.
If you may any questions please contact us: