display.js 28 KB

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