第一章 基础知识

第一十零节 离屏canvas

    程序清单1-14 以图像方式实现的时钟程序(JavaScript代码,节选)

// Some declarations and functions omitted for brevity.

// See Section 1.9 for a complete listing of

// the clock.

var canvas = document.getElementById('canvas'),

    context = canvas.getContext('2d'),

    ...

// Functions..........................................................

function updateClockImage() {

   snapshotImageElement.src = canvas.toDataURL();

}

function drawClock() {

   context.clearRect(0, 0, canvas.width,canvas.height);

   context.save();

   context.fillStyle = 'rgba(255,255,255,0.8)';

   context.fillRect(0, 0, canvas.width,canvas.height);

   drawCircle();

   drawCenter();

   drawHands();

   context.restore();

   drawNumerals();

   updateClockImage();

}

...