display.js 29 KB

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