display.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2012 Joel Martin
  4. * Licensed under MPL 2.0 (see LICENSE.txt)
  5. *
  6. * See README.md for usage and integration instructions.
  7. */
  8. /*jslint browser: true, white: false */
  9. /*global Util, Base64, changeCursor */
  10. var Display;
  11. (function () {
  12. "use strict";
  13. Display = function (defaults) {
  14. this._drawCtx = null;
  15. this._c_forceCanvas = false;
  16. this._renderQ = []; // queue drawing actions for in-oder rendering
  17. // the full frame buffer (logical canvas) size
  18. this._fb_width = 0;
  19. this._fb_height = 0;
  20. // the visible "physical canvas" viewport
  21. this._viewportLoc = { 'x': 0, 'y': 0, 'w': 0, 'h': 0 };
  22. this._cleanRect = { 'x1': 0, 'y1': 0, 'x2': -1, 'y2': -1 };
  23. this._prevDrawStyle = "";
  24. this._tile = null;
  25. this._tile16x16 = null;
  26. this._tile_x = 0;
  27. this._tile_y = 0;
  28. Util.set_defaults(this, defaults, {
  29. 'true_color': true,
  30. 'colourMap': [],
  31. 'scale': 1.0,
  32. 'viewport': false,
  33. 'render_mode': ''
  34. });
  35. Util.Debug(">> Display.constructor");
  36. if (!this._target) {
  37. throw new Error("Target must be set");
  38. }
  39. if (typeof this._target === 'string') {
  40. throw new Error('target must be a DOM element');
  41. }
  42. if (!this._target.getContext) {
  43. throw new Error("no getContext method");
  44. }
  45. if (!this._drawCtx) {
  46. this._drawCtx = this._target.getContext('2d');
  47. }
  48. Util.Debug("User Agent: " + navigator.userAgent);
  49. if (Util.Engine.gecko) { Util.Debug("Browser: gecko " + Util.Engine.gecko); }
  50. if (Util.Engine.webkit) { Util.Debug("Browser: webkit " + Util.Engine.webkit); }
  51. if (Util.Engine.trident) { Util.Debug("Browser: trident " + Util.Engine.trident); }
  52. if (Util.Engine.presto) { Util.Debug("Browser: presto " + Util.Engine.presto); }
  53. this.clear();
  54. // Check canvas features
  55. if ('createImageData' in this._drawCtx) {
  56. this._render_mode = 'canvas rendering';
  57. } else {
  58. throw new Error("Canvas does not support createImageData");
  59. }
  60. if (this._prefer_js === null) {
  61. Util.Info("Prefering javascript operations");
  62. this._prefer_js = true;
  63. }
  64. // Determine browser support for setting the cursor via data URI scheme
  65. var curDat = [];
  66. for (var i = 0; i < 8 * 8 * 4; i++) {
  67. curDat.push(255);
  68. }
  69. try {
  70. var curSave = this._target.style.cursor;
  71. Display.changeCursor(this._target, curDat, curDat, 2, 2, 8, 8);
  72. if (this._target.style.cursor) {
  73. if (this._cursor_uri === null || this._cursor_uri === undefined) {
  74. this._cursor_uri = true;
  75. }
  76. Util.Info("Data URI scheme cursor supported");
  77. this._target.style.cursor = curSave;
  78. } else {
  79. if (this._cursor_uri === null || this._cursor_uri === undefined) {
  80. this._cursor_uri = false;
  81. }
  82. Util.Warn("Data URI scheme cursor not supported");
  83. this._target.style.cursor = "none";
  84. }
  85. } catch (exc) {
  86. Util.Error("Data URI scheme cursor test exception: " + exc);
  87. this._cursor_uri = false;
  88. }
  89. Util.Debug("<< Display.constructor");
  90. };
  91. Display.prototype = {
  92. // Public methods
  93. viewportChange: function (deltaX, deltaY, width, height) {
  94. var vp = this._viewportLoc;
  95. var cr = this._cleanRect;
  96. var canvas = this._target;
  97. if (!this._viewport) {
  98. Util.Debug("Setting viewport to full display region");
  99. deltaX = -vp.w; // clamped later of out of bounds
  100. deltaY = -vp.h;
  101. width = this._fb_width;
  102. height = this._fb_height;
  103. }
  104. if (typeof(deltaX) === "undefined") { deltaX = 0; }
  105. if (typeof(deltaY) === "undefined") { deltaY = 0; }
  106. if (typeof(width) === "undefined") { width = vp.w; }
  107. if (typeof(height) === "undefined") { height = vp.h; }
  108. // Size change
  109. if (width > this._fb_width) { width = this._fb_width; }
  110. if (height > this._fb_height) { height = this._fb_height; }
  111. if (vp.w !== width || vp.h !== height) {
  112. // Change width
  113. if (width < vp.w && cr.x2 > vp.x + width - 1) {
  114. cr.x2 = vp.x + width - 1;
  115. }
  116. vp.w = width;
  117. // Change height
  118. if (height < vp.h && cr.y2 > vp.y + height - 1) {
  119. cr.y2 = vp.y + height - 1;
  120. }
  121. vp.h = height;
  122. var saveImg = null;
  123. if (vp.w > 0 && vp.h > 0 && canvas.width > 0 && canvas.height > 0) {
  124. var img_width = canvas.width < vp.w ? canvas.width : vp.w;
  125. var img_height = canvas.height < vp.h ? canvas.height : vp.h;
  126. saveImg = this._drawCtx.getImageData(0, 0, img_width, img_height);
  127. }
  128. canvas.width = vp.w;
  129. canvas.height = vp.h;
  130. if (saveImg) {
  131. this._drawCtx.putImageData(saveImg, 0, 0);
  132. }
  133. }
  134. var vx2 = vp.x + vp.w - 1;
  135. var vy2 = vp.y + vp.h - 1;
  136. // Position change
  137. if (deltaX < 0 && vp.x + deltaX < 0) {
  138. deltaX = -vp.x;
  139. }
  140. if (vx2 + deltaX >= this._fb_width) {
  141. deltaX -= vx2 + deltaX - this._fb_width + 1;
  142. }
  143. if (vp.y + deltaY < 0) {
  144. deltaY = -vp.y;
  145. }
  146. if (vy2 + deltaY >= this._fb_height) {
  147. deltaY -= (vy2 + deltaY - this._fb_height + 1);
  148. }
  149. if (deltaX === 0 && deltaY === 0) {
  150. return;
  151. }
  152. Util.Debug("viewportChange deltaX: " + deltaX + ", deltaY: " + deltaY);
  153. vp.x += deltaX;
  154. vx2 += deltaX;
  155. vp.y += deltaY;
  156. vy2 += deltaY;
  157. // Update the clean rectangle
  158. if (vp.x > cr.x1) {
  159. cr.x1 = vp.x;
  160. }
  161. if (vx2 < cr.x2) {
  162. cr.x2 = vx2;
  163. }
  164. if (vp.y > cr.y1) {
  165. cr.y1 = vp.y;
  166. }
  167. if (vy2 < cr.y2) {
  168. cr.y2 = vy2;
  169. }
  170. var x1, w;
  171. if (deltaX < 0) {
  172. // Shift viewport left, redraw left section
  173. x1 = 0;
  174. w = -deltaX;
  175. } else {
  176. // Shift viewport right, redraw right section
  177. x1 = vp.w - deltaX;
  178. w = deltaX;
  179. }
  180. var y1, h;
  181. if (deltaY < 0) {
  182. // Shift viewport up, redraw top section
  183. y1 = 0;
  184. h = -deltaY;
  185. } else {
  186. // Shift viewport down, redraw bottom section
  187. y1 = vp.h - deltaY;
  188. h = deltaY;
  189. }
  190. // Copy the valid part of the viewport to the shifted location
  191. var saveStyle = this._drawCtx.fillStyle;
  192. this._drawCtx.fillStyle = "rgb(255,255,255)";
  193. if (deltaX !== 0) {
  194. this._drawCtx.drawImage(canvas, 0, 0, vp.w, vp.h, -deltaX, 0, vp.w, vp.h);
  195. this._drawCtx.fillRect(x1, 0, w, vp.h);
  196. }
  197. if (deltaY !== 0) {
  198. this._drawCtx.drawImage(canvas, 0, 0, vp.w, vp.h, 0, -deltaY, vp.w, vp.h);
  199. this._drawCtx.fillRect(0, y1, vp.w, h);
  200. }
  201. this._drawCtx.fillStyle = saveStyle;
  202. },
  203. // Return a map of clean and dirty areas of the viewport and reset the
  204. // tracking of clean and dirty areas
  205. //
  206. // Returns: { 'cleanBox': { 'x': x, 'y': y, 'w': w, 'h': h},
  207. // 'dirtyBoxes': [{ 'x': x, 'y': y, 'w': w, 'h': h }, ...] }
  208. getCleanDirtyReset: function () {
  209. var vp = this._viewportLoc;
  210. var cr = this._cleanRect;
  211. var cleanBox = { 'x': cr.x1, 'y': cr.y1,
  212. 'w': cr.x2 - cr.x1 + 1, 'h': cr.y2 - cr.y1 + 1 };
  213. var dirtyBoxes = [];
  214. if (cr.x1 >= cr.x2 || cr.y1 >= cr.y2) {
  215. // Whole viewport is dirty
  216. dirtyBoxes.push({ 'x': vp.x, 'y': vp.y, 'w': vp.w, 'h': vp.h });
  217. } else {
  218. // Redraw dirty regions
  219. var vx2 = vp.x + vp.w - 1;
  220. var vy2 = vp.y + vp.h - 1;
  221. if (vp.x < cr.x1) {
  222. // left side dirty region
  223. dirtyBoxes.push({'x': vp.x, 'y': vp.y,
  224. 'w': cr.x1 - vp.x + 1, 'h': vp.h});
  225. }
  226. if (vx2 > cr.x2) {
  227. // right side dirty region
  228. dirtyBoxes.push({'x': cr.x2 + 1, 'y': vp.y,
  229. 'w': vx2 - cr.x2, 'h': vp.h});
  230. }
  231. if(vp.y < cr.y1) {
  232. // top/middle dirty region
  233. dirtyBoxes.push({'x': cr.x1, 'y': vp.y,
  234. 'w': cr.x2 - cr.x1 + 1, 'h': cr.y1 - vp.y});
  235. }
  236. if (vy2 > cr.y2) {
  237. // bottom/middle dirty region
  238. dirtyBoxes.push({'x': cr.x1, 'y': cr.y2 + 1,
  239. 'w': cr.x2 - cr.x1 + 1, 'h': vy2 - cr.y2});
  240. }
  241. }
  242. this._cleanRect = {'x1': vp.x, 'y1': vp.y,
  243. 'x2': vp.x + vp.w - 1, 'y2': vp.y + vp.h - 1};
  244. return {'cleanBox': cleanBox, 'dirtyBoxes': dirtyBoxes};
  245. },
  246. absX: function (x) {
  247. return x + this._viewportLoc.x;
  248. },
  249. absY: function (y) {
  250. return y + this._viewportLoc.y;
  251. },
  252. resize: function (width, height) {
  253. this._prevDrawStyle = "";
  254. this._fb_width = width;
  255. this._fb_height = height;
  256. this._rescale(this._scale);
  257. this.viewportChange();
  258. },
  259. clear: function () {
  260. if (this._logo) {
  261. this.resize(this._logo.width, this._logo.height);
  262. this.blitStringImage(this._logo.data, 0, 0);
  263. } else {
  264. if (Util.Engine.trident === 6) {
  265. // NB(directxman12): there's a bug in IE10 where we can fail to actually
  266. // clear the canvas here because of the resize.
  267. // Clearing the current viewport first fixes the issue
  268. this._drawCtx.clearRect(0, 0, this._viewportLoc.w, this._viewportLoc.h);
  269. }
  270. this.resize(240, 20);
  271. this._drawCtx.clearRect(0, 0, this._viewportLoc.w, this._viewportLoc.h);
  272. }
  273. this._renderQ = [];
  274. },
  275. fillRect: function (x, y, width, height, color) {
  276. this._setFillColor(color);
  277. this._drawCtx.fillRect(x - this._viewportLoc.x, y - this._viewportLoc.y, width, height);
  278. },
  279. copyImage: function (old_x, old_y, new_x, new_y, w, h) {
  280. var x1 = old_x - this._viewportLoc.x;
  281. var y1 = old_y - this._viewportLoc.y;
  282. var x2 = new_x - this._viewportLoc.x;
  283. var y2 = new_y - this._viewportLoc.y;
  284. this._drawCtx.drawImage(this._target, x1, y1, w, h, x2, y2, w, h);
  285. },
  286. // start updating a tile
  287. startTile: function (x, y, width, height, color) {
  288. this._tile_x = x;
  289. this._tile_y = y;
  290. if (width === 16 && height === 16) {
  291. this._tile = this._tile16x16;
  292. } else {
  293. this._tile = this._drawCtx.createImageData(width, height);
  294. }
  295. if (this._prefer_js) {
  296. var bgr;
  297. if (this._true_color) {
  298. bgr = color;
  299. } else {
  300. bgr = this._colourMap[color[0]];
  301. }
  302. var red = bgr[2];
  303. var green = bgr[1];
  304. var blue = bgr[0];
  305. var data = this._tile.data;
  306. for (var i = 0; i < width * height * 4; i += 4) {
  307. data[i] = red;
  308. data[i + 1] = green;
  309. data[i + 2] = blue;
  310. data[i + 3] = 255;
  311. }
  312. } else {
  313. this.fillRect(x, y, width, height, color);
  314. }
  315. },
  316. // update sub-rectangle of the current tile
  317. subTile: function (x, y, w, h, color) {
  318. if (this._prefer_js) {
  319. var bgr;
  320. if (this._true_color) {
  321. bgr = color;
  322. } else {
  323. bgr = this._colourMap[color[0]];
  324. }
  325. var red = bgr[2];
  326. var green = bgr[1];
  327. var blue = bgr[0];
  328. var xend = x + w;
  329. var yend = y + h;
  330. var data = this._tile.data;
  331. var width = this._tile.width;
  332. for (var j = y; j < yend; j++) {
  333. for (var i = x; i < xend; i++) {
  334. var p = (i + (j * width)) * 4;
  335. data[p] = red;
  336. data[p + 1] = green;
  337. data[p + 2] = blue;
  338. data[p + 3] = 255;
  339. }
  340. }
  341. } else {
  342. this.fillRect(this._tile_x + x, this._tile_y + y, w, h, color);
  343. }
  344. },
  345. // draw the current tile to the screen
  346. finishTile: function () {
  347. if (this._prefer_js) {
  348. this._drawCtx.putImageData(this._tile, this._tile_x - this._viewportLoc.x,
  349. this._tile_y - this._viewportLoc.y);
  350. }
  351. // else: No-op -- already done by setSubTile
  352. },
  353. blitImage: function (x, y, width, height, arr, offset) {
  354. if (this._true_color) {
  355. this._bgrxImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
  356. } else {
  357. this._cmapImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
  358. }
  359. },
  360. blitRgbImage: function (x, y , width, height, arr, offset) {
  361. if (this._true_color) {
  362. this._rgbImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
  363. } else {
  364. // probably wrong?
  365. this._cmapImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
  366. }
  367. },
  368. blitStringImage: function (str, x, y) {
  369. var img = new Image();
  370. img.onload = function () {
  371. this._drawCtx.drawImage(img, x - this._viewportLoc.x, y - this._viewportLoc.y);
  372. }.bind(this);
  373. img.src = str;
  374. return img; // for debugging purposes
  375. },
  376. // wrap ctx.drawImage but relative to viewport
  377. drawImage: function (img, x, y) {
  378. this._drawCtx.drawImage(img, x - this._viewportLoc.x, y - this._viewportLoc.y);
  379. },
  380. renderQ_push: function (action) {
  381. this._renderQ.push(action);
  382. if (this._renderQ.length === 1) {
  383. // If this can be rendered immediately it will be, otherwise
  384. // the scanner will start polling the queue (every
  385. // requestAnimationFrame interval)
  386. this._scan_renderQ();
  387. }
  388. },
  389. changeCursor: function (pixels, mask, hotx, hoty, w, h) {
  390. if (this._cursor_uri === false) {
  391. Util.Warn("changeCursor called but no cursor data URI support");
  392. return;
  393. }
  394. if (this._true_color) {
  395. Display.changeCursor(this._target, pixels, mask, hotx, hoty, w, h);
  396. } else {
  397. Display.changeCursor(this._target, pixels, mask, hotx, hoty, w, h, this._colourMap);
  398. }
  399. },
  400. defaultCursor: function () {
  401. this._target.style.cursor = "default";
  402. },
  403. disableLocalCursor: function () {
  404. this._target.style.cursor = "none";
  405. },
  406. // Overridden getters/setters
  407. get_context: function () {
  408. return this._drawCtx;
  409. },
  410. set_scale: function (scale) {
  411. this._rescale(scale);
  412. },
  413. set_width: function (w) {
  414. this.resize(w, this._fb_height);
  415. },
  416. get_width: function () {
  417. return this._fb_width;
  418. },
  419. set_height: function (h) {
  420. this.resize(this._fb_width, h);
  421. },
  422. get_height: function () {
  423. return this._fb_height;
  424. },
  425. // Private Methods
  426. _rescale: function (factor) {
  427. var canvas = this._target;
  428. var properties = ['transform', 'WebkitTransform', 'MozTransform'];
  429. var transform_prop;
  430. while ((transform_prop = properties.shift())) {
  431. if (typeof canvas.style[transform_prop] !== 'undefined') {
  432. break;
  433. }
  434. }
  435. if (transform_prop === null) {
  436. Util.Debug("No scaling support");
  437. return;
  438. }
  439. if (typeof(factor) === "undefined") {
  440. factor = this._scale;
  441. } else if (factor > 1.0) {
  442. factor = 1.0;
  443. } else if (factor < 0.1) {
  444. factor = 0.1;
  445. }
  446. if (this._scale === factor) {
  447. return;
  448. }
  449. this._scale = factor;
  450. var x = canvas.width - (canvas.width * factor);
  451. var y = canvas.height - (canvas.height * factor);
  452. canvas.style[transform_prop] = 'scale(' + this._scale + ') translate(-' + x + 'px, -' + y + 'px)';
  453. },
  454. _setFillColor: function (color) {
  455. var bgr;
  456. if (this._true_color) {
  457. bgr = color;
  458. } else {
  459. bgr = this._colourMap[color[0]];
  460. }
  461. var newStyle = 'rgb(' + bgr[2] + ',' + bgr[1] + ',' + bgr[0] + ')';
  462. if (newStyle !== this._prevDrawStyle) {
  463. this._drawCtx.fillStyle = newStyle;
  464. this._prevDrawStyle = newStyle;
  465. }
  466. },
  467. _rgbImageData: function (x, y, vx, vy, width, height, arr, offset) {
  468. var img = this._drawCtx.createImageData(width, height);
  469. var data = img.data;
  470. for (var i = 0, j = offset; i < width * height * 4; i += 4, j += 3) {
  471. data[i] = arr[j];
  472. data[i + 1] = arr[j + 1];
  473. data[i + 2] = arr[j + 2];
  474. data[i + 3] = 255; // Alpha
  475. }
  476. this._drawCtx.putImageData(img, x - vx, y - vy);
  477. },
  478. _bgrxImageData: function (x, y, vx, vy, width, height, arr, offset) {
  479. var img = this._drawCtx.createImageData(width, height);
  480. var data = img.data;
  481. for (var i = 0, j = offset; i < width * height * 4; i += 4, j += 4) {
  482. data[i] = arr[j + 2];
  483. data[i + 1] = arr[j + 1];
  484. data[i + 2] = arr[j];
  485. data[i + 3] = 255; // Alpha
  486. }
  487. this._drawCtx.putImageData(img, x - vx, y - vy);
  488. },
  489. _cmapImageData: function (x, y, vx, vy, width, height, arr, offset) {
  490. var img = this._drawCtx.createImageData(width, height);
  491. var data = img.data;
  492. var cmap = this._colourMap;
  493. for (var i = 0, j = offset; i < width * height * 4; i += 4, j++) {
  494. var bgr = cmap[arr[j]];
  495. data[i] = bgr[2];
  496. data[i + 1] = bgr[1];
  497. data[i + 2] = bgr[0];
  498. data[i + 3] = 255; // Alpha
  499. }
  500. this._drawCtx.putImageData(img, x - vx, y - vy);
  501. },
  502. _scan_renderQ: function () {
  503. var ready = true;
  504. while (ready && this._renderQ.length > 0) {
  505. var a = this._renderQ[0];
  506. switch (a.type) {
  507. case 'copy':
  508. this.copyImage(a.old_x, a.old_y, a.x, a.y, a.width, a.height);
  509. break;
  510. case 'fill':
  511. this.fillRect(a.x, a.y, a.width, a.height, a.color);
  512. break;
  513. case 'blit':
  514. this.blitImage(a.x, a.y, a.width, a.height, a.data, 0);
  515. break;
  516. case 'blitRgb':
  517. this.blitRgbImage(a.x, a.y, a.width, a.height, a.data, 0);
  518. break;
  519. case 'img':
  520. if (a.img.complete) {
  521. this.drawImage(a.img, a.x, a.y);
  522. } else {
  523. // We need to wait for this image to 'load'
  524. // to keep things in-order
  525. ready = false;
  526. }
  527. break;
  528. }
  529. if (ready) {
  530. this._renderQ.shift();
  531. }
  532. }
  533. if (this._renderQ.length > 0) {
  534. requestAnimFrame(this._scan_renderQ.bind(this));
  535. }
  536. },
  537. };
  538. Util.make_properties(Display, [
  539. ['target', 'wo', 'dom'], // Canvas element for rendering
  540. ['context', 'ro', 'raw'], // Canvas 2D context for rendering (read-only)
  541. ['logo', 'rw', 'raw'], // Logo to display when cleared: {"width": w, "height": h, "data": data}
  542. ['true_color', 'rw', 'bool'], // Use true-color pixel data
  543. ['colourMap', 'rw', 'arr'], // Colour map array (when not true-color)
  544. ['scale', 'rw', 'float'], // Display area scale factor 0.0 - 1.0
  545. ['viewport', 'rw', 'bool'], // Use a viewport set with viewportChange()
  546. ['width', 'rw', 'int'], // Display area width
  547. ['height', 'rw', 'int'], // Display area height
  548. ['render_mode', 'ro', 'str'], // Canvas rendering mode (read-only)
  549. ['prefer_js', 'rw', 'str'], // Prefer Javascript over canvas methods
  550. ['cursor_uri', 'rw', 'raw'] // Can we render cursor using data URI
  551. ]);
  552. // Class Methods
  553. Display.changeCursor = function (target, pixels, mask, hotx, hoty, w0, h0, cmap) {
  554. var w = w0;
  555. var h = h0;
  556. if (h < w) {
  557. h = w; // increase h to make it square
  558. } else {
  559. w = h; // increase w to make it square
  560. }
  561. var cur = [];
  562. // Push multi-byte little-endian values
  563. cur.push16le = function (num) {
  564. this.push(num & 0xFF, (num >> 8) & 0xFF);
  565. };
  566. cur.push32le = function (num) {
  567. this.push(num & 0xFF,
  568. (num >> 8) & 0xFF,
  569. (num >> 16) & 0xFF,
  570. (num >> 24) & 0xFF);
  571. };
  572. var IHDRsz = 40;
  573. var RGBsz = w * h * 4;
  574. var XORsz = Math.ceil((w * h) / 8.0);
  575. var ANDsz = Math.ceil((w * h) / 8.0);
  576. cur.push16le(0); // 0: Reserved
  577. cur.push16le(2); // 2: .CUR type
  578. cur.push16le(1); // 4: Number of images, 1 for non-animated ico
  579. // Cursor #1 header (ICONDIRENTRY)
  580. cur.push(w); // 6: width
  581. cur.push(h); // 7: height
  582. cur.push(0); // 8: colors, 0 -> true-color
  583. cur.push(0); // 9: reserved
  584. cur.push16le(hotx); // 10: hotspot x coordinate
  585. cur.push16le(hoty); // 12: hotspot y coordinate
  586. cur.push32le(IHDRsz + RGBsz + XORsz + ANDsz);
  587. // 14: cursor data byte size
  588. cur.push32le(22); // 18: offset of cursor data in the file
  589. // Cursor #1 InfoHeader (ICONIMAGE/BITMAPINFO)
  590. cur.push32le(IHDRsz); // 22: InfoHeader size
  591. cur.push32le(w); // 26: Cursor width
  592. cur.push32le(h * 2); // 30: XOR+AND height
  593. cur.push16le(1); // 34: number of planes
  594. cur.push16le(32); // 36: bits per pixel
  595. cur.push32le(0); // 38: Type of compression
  596. cur.push32le(XORsz + ANDsz);
  597. // 42: Size of Image
  598. cur.push32le(0); // 46: reserved
  599. cur.push32le(0); // 50: reserved
  600. cur.push32le(0); // 54: reserved
  601. cur.push32le(0); // 58: reserved
  602. // 62: color data (RGBQUAD icColors[])
  603. var y, x;
  604. for (y = h - 1; y >= 0; y--) {
  605. for (x = 0; x < w; x++) {
  606. if (x >= w0 || y >= h0) {
  607. cur.push(0); // blue
  608. cur.push(0); // green
  609. cur.push(0); // red
  610. cur.push(0); // alpha
  611. } else {
  612. var idx = y * Math.ceil(w0 / 8) + Math.floor(x / 8);
  613. var alpha = (mask[idx] << (x % 8)) & 0x80 ? 255 : 0;
  614. if (cmap) {
  615. idx = (w0 * y) + x;
  616. var rgb = cmap[pixels[idx]];
  617. cur.push(rgb[2]); // blue
  618. cur.push(rgb[1]); // green
  619. cur.push(rgb[0]); // red
  620. cur.push(alpha); // alpha
  621. } else {
  622. idx = ((w0 * y) + x) * 4;
  623. cur.push(pixels[idx + 2]); // blue
  624. cur.push(pixels[idx + 1]); // green
  625. cur.push(pixels[idx]); // red
  626. cur.push(alpha); // alpha
  627. }
  628. }
  629. }
  630. }
  631. // XOR/bitmask data (BYTE icXOR[])
  632. // (ignored, just needs to be the right size)
  633. for (y = 0; y < h; y++) {
  634. for (x = 0; x < Math.ceil(w / 8); x++) {
  635. cur.push(0);
  636. }
  637. }
  638. // AND/bitmask data (BYTE icAND[])
  639. // (ignored, just needs to be the right size)
  640. for (y = 0; y < h; y++) {
  641. for (x = 0; x < Math.ceil(w / 8); x++) {
  642. cur.push(0);
  643. }
  644. }
  645. var url = 'data:image/x-icon;base64,' + Base64.encode(cur);
  646. target.style.cursor = 'url(' + url + ')' + hotx + ' ' + hoty + ', default';
  647. };
  648. })();