cursor.html 4.4 KB

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