1. 程式人生 > >基於HTML5 Canvas和jQuery的畫圖工具的實現

基於HTML5 Canvas和jQuery的畫圖工具的實現

  1.          //undo redo
  2.                               var history =new Array();
  3.                               var cStep = -1;
  4.                                                                 /**
  5.                                * put current canvas to cache
  6.                                */
  7.                               function historyPush()
  8.                               {
  9.                                   cStep++;
  10.                                       if (cStep < history.length) 
  11.                                       { 
  12.                                                     history.length = cStep; 
  13.                                       }
  14.                                       history.push($("#myCanvas").get(0).toDataURL());
  15.                               }
  16.                               /**
  17.                                * function: undo 
  18.                                */
  19.                               function undo()
  20.                               {
  21.                                       if (cStep >= 0) 
  22.                                       {
  23.                                               cStep--;
  24.                                               var tempImage = new Image();
  25.                                               tempImage.src = history[cStep];
  26.                                               tempImage.onload = function () { ctx.drawImage(tempImage, 0, 0);};
  27.                                       }
  28.                               }
  29.                               /**
  30.                                * function:  redo
  31.                                */
  32.                                   function redo()
  33.                                   {
  34.                                       if (cStep <history.length-1) 
  35.                                       {
  36.                                               clearCanvas();
  37.                                               cStep++;
  38.                                               var tempImage = new Image();
  39.                                               tempImage.src = history[cStep];
  40.                                               tempImage.onload = function () { ctx.drawImage(tempImage, 0, 0); };
  41.                                       }
  42.                                   }