display.js 27 KB

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