cursor.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // Push multi-byte little-endian values
  43. arr.push16le = function (num) {
  44. this.push((num ) & 0xFF,
  45. (num >> 8) & 0xFF );
  46. };
  47. arr.push32le = function (num) {
  48. this.push((num ) & 0xFF,
  49. (num >> 8) & 0xFF,
  50. (num >> 16) & 0xFF,
  51. (num >> 24) & 0xFF );
  52. };
  53. // Main header
  54. arr.push16le(0); // Reserved
  55. arr.push16le(2); // .CUR type
  56. arr.push16le(1); // Number of images, 1 for non-animated arr
  57. // Cursor #1
  58. arr.push(w); // width
  59. arr.push(h); // height
  60. arr.push(0); // colors, 0 -> true-color
  61. arr.push(0); // reserved
  62. arr.push16le(hx); // hotspot x coordinate
  63. arr.push16le(hy); // hotspot y coordinate
  64. arr.push32le(IHDRsz + XORsz + ANDsz); // cursor data byte size
  65. arr.push32le(22); // offset of cursor data in the file
  66. // Infoheader for Cursor #1
  67. arr.push32le(IHDRsz); // Infoheader size
  68. arr.push32le(w); // Cursor width
  69. arr.push32le(h*2); // XOR+AND height
  70. arr.push16le(1); // number of planes
  71. arr.push16le(32); // bits per pixel
  72. arr.push32le(0); // type of compression
  73. arr.push32le(XORsz + ANDsz); // Size of Image
  74. arr.push32le(0);
  75. arr.push32le(0);
  76. arr.push32le(0);
  77. arr.push32le(0);
  78. // XOR/color data
  79. for (y = h-1; y >= 0; y--) {
  80. for (x = 0; x < w; x++) {
  81. //if ((x === hx) || (y === (h-hy-1))) {
  82. if ((x === hx) || (y === hy)) {
  83. arr.push(0xe0); // blue
  84. arr.push(0x00); // green
  85. arr.push(0x00); // red
  86. arr.push(0xff); // alpha
  87. } else {
  88. arr.push(0x05); // blue
  89. arr.push(0xe6); // green
  90. arr.push(0x00); // red
  91. arr.push(0x80); // alpha
  92. }
  93. }
  94. }
  95. // AND/bitmask data (seems to be ignored)
  96. for (y = 0; y < h; y++) {
  97. for (x = 0; x < Math.ceil(w / 8); x++) {
  98. arr.push(0x00);
  99. }
  100. }
  101. debug("cursor generated");
  102. return arr;
  103. }
  104. window.onload = function() {
  105. debug("onload");
  106. var canvas, cross, cursor, cursor64;
  107. canvas = new Canvas({'target' : "testcanvas"});
  108. debug("canvas indicates Data URI cursor support is: " + canvas.get_cursor_uri());
  109. $('button1').style.cursor="url(face.png), default";
  110. cursor = makeCursor();
  111. cursor64 = Base64.encode(cursor);
  112. //debug("cursor: " + cursor.slice(0,100) + " (" + cursor.length + ")");
  113. //debug("cursor64: " + cursor64.slice(0,100) + " (" + cursor64.length + ")");
  114. $('button2').style.cursor="url(data:image/x-icon;base64," + cursor64 + "), default";
  115. debug("onload complete");
  116. }
  117. </script>