display.js 28 KB

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