cursor.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Cursor Change test</title>
  5. <meta charset="UTF-8">
  6. <!--
  7. <script type='text/javascript'
  8. src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
  9. -->
  10. <script src="include/util.js"></script>
  11. <script src="include/base64.js"></script>
  12. <script src="include/canvas.js"></script>
  13. </head>
  14. <body>
  15. <h1>Roll over the buttons to test cursors</h1>
  16. <br>
  17. <input id=button1 type="button" value="Cursor from file (smiley face)">
  18. <input id=button2 type="button" value="Data URI cursor (crosshair)">
  19. <br>
  20. <br>
  21. <br>
  22. Debug:<br>
  23. <textarea id="debug" style="font-size: 9px;" cols=80 rows=25></textarea>
  24. <br>
  25. <br>
  26. <canvas id="testcanvas" width="100px" height="20px">
  27. Canvas not supported.
  28. </canvas>
  29. </body>
  30. <script>
  31. function debug(str) {
  32. console.log(str);
  33. cell = $('debug');
  34. cell.innerHTML += str + "\n";
  35. cell.scrollTop = cell.scrollHeight;
  36. }
  37. function makeCursor() {
  38. var arr = [], x, y, w = 32, h = 32, hx = 16, hy = 16;
  39. var IHDRsz = 40;
  40. var ANDsz = w * h * 4;
  41. var XORsz = Math.ceil( (w * h) / 8.0 );
  42. // Main header
  43. arr.push16le(0); // Reserved
  44. arr.push16le(2); // .CUR type
  45. arr.push16le(1); // Number of images, 1 for non-animated arr
  46. // Cursor #1
  47. arr.push(w); // width
  48. arr.push(h); // height
  49. arr.push(0); // colors, 0 -> true-color
  50. arr.push(0); // reserved
  51. arr.push16le(hx); // hotspot x coordinate
  52. arr.push16le(hy); // hotspot y coordinate
  53. arr.push32le(IHDRsz + XORsz + ANDsz); // cursor data byte size
  54. arr.push32le(22); // offset of cursor data in the file
  55. // Infoheader for Cursor #1
  56. arr.push32le(IHDRsz); // Infoheader size
  57. arr.push32le(w); // Cursor width
  58. arr.push32le(h*2); // XOR+AND height
  59. arr.push16le(1); // number of planes
  60. arr.push16le(32); // bits per pixel
  61. arr.push32le(0); // type of compression
  62. arr.push32le(XORsz + ANDsz); // Size of Image
  63. arr.push32le(0);
  64. arr.push32le(0);
  65. arr.push32le(0);
  66. arr.push32le(0);
  67. // XOR/color data
  68. for (y = h-1; y >= 0; y--) {
  69. for (x = 0; x < w; x++) {
  70. //if ((x === hx) || (y === (h-hy-1))) {
  71. if ((x === hx) || (y === hy)) {
  72. arr.push(0xe0); // blue
  73. arr.push(0x00); // green
  74. arr.push(0x00); // red
  75. arr.push(0xff); // alpha
  76. } else {
  77. arr.push(0x05); // blue
  78. arr.push(0xe6); // green
  79. arr.push(0x00); // red
  80. arr.push(0x80); // alpha
  81. }
  82. }
  83. }
  84. // AND/bitmask data (seems to be ignored)
  85. for (y = 0; y < h; y++) {
  86. for (x = 0; x < Math.ceil(w / 8); x++) {
  87. arr.push(0x00);
  88. }
  89. }
  90. debug("cursor generated");
  91. return arr;
  92. }
  93. window.onload = function() {
  94. debug("onload");
  95. var cross, cursor, cursor64;
  96. Canvas.init("testcanvas");
  97. debug("Canvas.init() indicates Data URI cursor support is: " + Canvas.isCursor());
  98. $('button1').style.cursor="url(face.png), default";
  99. cursor = makeCursor();
  100. cursor64 = Base64.encode(cursor);
  101. //debug("cursor: " + cursor.slice(0,100) + " (" + cursor.length + ")");
  102. //debug("cursor64: " + cursor64.slice(0,100) + " (" + cursor64.length + ")");
  103. $('button2').style.cursor="url(data:image/x-icon;base64," + cursor64 + "), default";
  104. debug("onload complete");
  105. }
  106. </script>