spacer

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

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

Applications Developer - Sharepoint
Professional Technical Resources
US-OR-Portland

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Some Ankle With Visual Studio
Gentoo Linux Cancels Distribution
It's Official: Windows 7 at PDC, WinHEC

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 Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
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
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
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
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Controllers: Programming Application Logic - Part 2 · How to Use JavaScript to Validate Form Data · Controllers: Programming Application Logic
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Sprint Launches Mobile WiMAX Network · Albatron Downsizes with the KI780G Mini-ITX Motherboard · Can't Find a Wi-Fi Network? Make Your Own.

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

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