The Clock - Javascript


// JavaScript Scripting Example 04
//
// "The Clock"
//


// Don't cache the image

Response.Cachecontrol = "No-cache";
Response.Expires = -1440;
Response.AddHeader("pragma", "no-cache");

var errormsg = new String("");
errormsg = DrawClock();

// Check if errors occured
if (errormsg != "")
DisplayError(errormsg);

// Draw the clock
function DrawClock()
{
 try
 {
  var xCenter;
  var yCenter;
  var path;

  // Fix path
  path = Server.MapPath("..\\..\\images") + "\\";

  // Since the clock consists of four images, we will create
  // four image objects.

  var imageobj = Server.CreateObject("W3Image.Image");
  var imageobj2 = Server.CreateObject("W3Image.Image");
  var imageobj3 = Server.CreateObject("W3Image.Image");
  var imageobj4 = Server.CreateObject("W3Image.Image");

  // Set the backgroundcolours to black.
  // We will use the black colour as transparent when blitting.

  imageobj.BkColor = 0x000000;
  imageobj2.BkColor = 0x000000;
  imageobj3.BkColor = 0x000000;
  imageobj4.BkColor = 0x000000;

  // Load the four bitmaps.
  if (imageobj.LoadImage(path + "background.bmp") == false)
  {
   // Do your error handling here...
   return "Error when loading image 'background.bmp'.";
  }
  
  if (imageobj2.LoadImage(path + "hour.bmp") == false)
  {
   // Do your error handling here...
   return "Error when loading image 'hour.bmp'.";
  }
  
  if (imageobj3.LoadImage(path + "minute.bmp") == false)
  {
   // Do your error handling here...
   return "Error when loading image 'minute.bmp'.";
  }
  
  if (imageobj4.LoadImage(path + "second.bmp") == false)
  {
   // Do your error handling here...
   return "Error when loading image 'second.bmp'.";
  }
  
  // Get the current time.
  var date = new Date();
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var seconds = date.getSeconds();
  
  
  // Add the seconds
  xCenter = imageobj4.Width / 2;
  yCenter = imageobj4.Height / 2;
  
  seconds = seconds * 6;
  
  imageobj4.Rotate(seconds, xCenter, yCenter);
  imageobj4.StretchBltExt(imageobj, 0, 0, imageobj.Width, imageobj.Height, 0, 0, imageobj.Width, imageobj.Height, "transparent");
  
  
  // Add the minutes
  xCenter = imageobj3.Width / 2;
  yCenter = imageobj3.Height / 2;
  
  minutes = minutes * 6;
  
  imageobj3.Rotate(minutes, xCenter, yCenter);
  imageobj3.StretchBltExt(imageobj, 0, 0, imageobj.Width, imageobj.Height, 0, 0, imageobj.Width, imageobj.Height, "transparent");
  
  
  // Add the hours
  xCenter = imageobj2.Width / 2;
  yCenter = imageobj2.Height / 2;
  
  if(hours >= 12)
  hours -= 12;
  
  hours = hours * 30;
  hours += (minutes / 12);
  
  imageobj2.Rotate(hours, xCenter, yCenter);
  imageobj2.StretchBltExt(imageobj, 0, 0, imageobj.Width, imageobj.Height, 0, 0, imageobj.Width, imageobj.Height, "transparent");
  
  
  // Stream imageobj as a JPG image.
  if (imageobj.StreamImage(Response, "JPG", 24) == false)
  {
   // Do your error handling here...
   return "Error when streaming image!";
  }
  
  // Sucess
  return "";
 }
 catch(err)
 {
  // Do your error handling here...
  return "Error: " + (err.number & 0xFFFF) + " - " + err.description + ".";
 }
}


// Example of a error handler - Displaying the error as a image
function DisplayError(msgcode)
{
 // Create an error image
 var errorimage = Server.CreateObject("W3Image.Image");
 errorimage.CreateEmptySurface(1,1);
 
 // Create and select the font
 var fontobj = errorimage.CreateFont("Tahoma",24,0,"normal",0,0x000000,false,false,true);
 errorimage.SetFont(fontobj);
 
 // Get size of the text
 var width = errorimage.GetTextWidth(msgcode);
 var height = errorimage.GetTextHeight(msgcode);
 
 // Create a surface as large as the error message
 errorimage.CreateEmptySurface(width,height);
 
 // Select the font again (font is deselected when creating a new surface)
 errorimage.SetFont(fontobj);
 
 // Write out error message
 errorimage.DrawText(msgcode,0,0);
 
 // Stream the image as a JPG image
 errorimage.StreamImage(Response, "JPG", 24);
}