spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / gr / column7 / 1 current pageTo page 2To page 3
[next]

Lead Software Engineer
Professional Technical Resources
US-OR-Portland

Justtechjobs.com Post A Job | Post A Resume
Developer News
Sir Tim Talks Up Linked Open Data Movement
From L.A. to Vegas With 100GbE
Salesforce Rolls Out Big Summer '08 Update

How to Create a JavaScript Web Page Screen Saver

Screensavers are ubiquitous on computers these days. Practically all computers, PDA's and mobile phones have a ready selection of screen savers to amuse their users. The original purpose of a screen saver was to literally to save the screen; long-time computer users may remember the screens of old that had text etched onto the screen which could be read even when the computer was off. Since then, the hardware has become more robust and the nature of computer programs has changed, so this problem is reduced. Today, screen savers are used for data protection, adding to one's environment and amusement. It's the last of these three aims that has inspired this article.

In this article, I present a simple framework to implement a 'screen saver' on a web page. It will do all of the essential things that good screen savers do; wait patiently in the background while the user is busy interacting with the page, activate after a timeout, hide the current contents of the page, display something pretty to look at and finally, restore the original page content when the user moves the mouse or presses a key on the keyboard.

The framework begins with a class called ScreenSaver, whose constructor function is shown here:

function ScreenSaver(settings)
{
  this.settings = settings;

this.nTimeout = this.settings.timeout;

// link in to body events
document.body.screenSaver = this;
document.body.onmousemove = ScreenSaver.prototype.onevent;
document.body.onmousedown = ScreenSaver.prototype.onevent;
document.body.onkeydown = ScreenSaver.prototype.onevent;

var pThis = this;
var f = function(){pThis.timeout();}
this.timerID = window.setTimeout(f, this.nTimeout);

}

The settings argument is a simple JavaScript Object that contains information needed by the screen saver, such as the timeout period and the flavor of screen saver to show. Thanks to the type-less nature of JavaScript, the settings object will contain parameters for the saver implementation - whatever those happen to be.

Next, a selection of event handlers is attached to the document.body object. We need one for mouse-move, mouse-down and key-down. These will be used to stop the saver (if running) and reset the timer.

Finally, window.setTimeout() is called to notify the ScreenSaver object after the timeout period (by calling the timeout() method, more on this later.) For those not familiar with the use of function variables, it works like this: A variable called pThis is declared and assigned to the 'this' pointer to provide access to the ScreenSaver object. The variable pThis is necessary because the keyword 'this' has different meanings in different contexts. Next, a function is declared that uses the pThis pointer to call the timeout() method on the screen saver. When this function is passed into window.setTimeout(), it can still use the assigned value of pThis because nested functions run under the context in which they are defined.

The onevent function attached to the document.body calls the signal() method on the ScreenSaver object, which will do three things: stop the screen saver (if running), clear any previous timeout using the timerID saved from the call to window.setTimeout() and start a new timeout.

ScreenSaver.prototype.signal = function()
{
  if ( this.saver )
{
this.saver.stop();
}

window.clearTimeout(this.timerID);

var pThis = this;
var f = function(){pThis.timeout();}
this.timerID = window.setTimeout(f, this.nTimeout);

}

ScreenSaver.prototype.onevent = function(e)
{

  this.screenSaver.signal();
}

After the timeout period has passed with no mouse or keyboard events generated by the user, the timeout() method is called on the ScreenSaver object:

ScreenSaver.prototype.timeout = function()
{
  if ( !this.saver )
{
this.saver = this.settings.create();
}
this.saver.start();
}

This function does little more than create a saver object and tell it to start. Creating the saver uses a create method in the settings object, which allows for any implementation of screen saver to be used so long as it supports the following interface:

start() Start the screen saver action.
stop() Stop the screen saver. The screen saver must ensure that any of its effects are not visible although they may be stored for use next time the saver is invoked.

To demonstrate how the saver object functions, I've created an old classic, the starfield simulation:

function StarFieldSaver(settings)
{
  // default values
this.speed = 50;
this.nStars = 50;
this.zIndex = 1000;

// override with settings values
if ( settings.speed ) this.speed = settings.speed;
if ( settings.nStars ) this.nStars = settings.nStars;
if ( settings.zIndex ) this.zIndex = settings.zIndex;
}

The constructor of the StarFieldSaver does little more than set up the initial values: speed to determine how fast the stars will travel (lower numbers means faster), nStars to specify how many stars to plot and zIndex to use for the zIndex style for the black background.


home / programming / javascript / gr / column7 / 1 current pageTo page 2To page 3
[next]



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Book Review: Head First JavaScript · Web Hosting Control Panels · Use Your Blog for Fast Search Engine Rankings
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
NetApp's Virtual Storage Strategy Crystallizes · F/MC Watch: A Cisco-Centric Approach · Olympic Time Trials Use Wi-Fi Mesh

Created: March 27, 2003
Revised: June 8, 2004

URL: http://webreference.com/programming/javascript/gr/column7/1