spacer

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

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

Web Project Manager
Aquent
US-PA-Collegeville

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

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]

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

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

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

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