canvas.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2010 Joel Martin
  4. * Licensed under LGPL-3 (see LICENSE.txt)
  5. *
  6. * See README.md for usage and integration instructions.
  7. */
  8. "use strict";
  9. /*jslint browser: true, white: false, bitwise: false */
  10. /*global window, Util, Base64 */
  11. function Canvas(conf) {
  12. conf = conf || {}; // Configuration
  13. var that = {}, // Public API interface
  14. // Pre-declare functions used before definitions (jslint)jslint
  15. setFillColor, fillRect,
  16. // Private Canvas namespace variables
  17. c_forceCanvas = false,
  18. c_width = 0,
  19. c_height = 0,
  20. c_prevStyle = "",
  21. c_keyPress = null,
  22. c_mouseButton = null,
  23. c_mouseMove = null;
  24. // Capability settings, default can be overridden
  25. Util.conf_default(conf, that, 'prefer_js', null);
  26. Util.conf_default(conf, that, 'cursor_uri', null);
  27. // Configuration settings
  28. Util.conf_default(conf, that, 'target', null);
  29. // Area that traps keyboard input
  30. Util.conf_default(conf, that, 'focusContainer', document);
  31. Util.conf_default(conf, that, 'true_color', true);
  32. Util.conf_default(conf, that, 'focused', true);
  33. Util.conf_default(conf, that, 'colourMap', []);
  34. Util.conf_default(conf, that, 'scale', 1);
  35. // Override some specific getters/setters
  36. that.set_prefer_js = function(val) {
  37. if (val && c_forceCanvas) {
  38. Util.Warn("Preferring Javascript to Canvas ops is not supported");
  39. return false;
  40. }
  41. conf.prefer_js = val;
  42. return true;
  43. };
  44. that.get_colourMap = function(idx) {
  45. if (typeof idx === 'undefined') {
  46. return conf.colourMap;
  47. } else {
  48. return conf.colourMap[idx];
  49. }
  50. };
  51. that.set_colourMap = function(val, idx) {
  52. if (typeof idx === 'undefined') {
  53. conf.colourMap = val;
  54. } else {
  55. conf.colourMap[idx] = val;
  56. }
  57. };
  58. // Add some other getters/setters
  59. that.get_width = function() {
  60. return c_width;
  61. };
  62. that.get_height = function() {
  63. return c_height;
  64. };
  65. //
  66. // Private functions
  67. //
  68. // Create the public API interface
  69. function constructor() {
  70. Util.Debug(">> Canvas.init");
  71. var c, ctx, imgTest, tval, i, curDat, curSave,
  72. has_imageData = false, UE = Util.Engine;
  73. if (! conf.target) { throw("target must be set"); }
  74. if (typeof conf.target === 'string') {
  75. conf.target = window.$(conf.target);
  76. }
  77. c = conf.target;
  78. if (! c.getContext) { throw("no getContext method"); }
  79. if (! conf.ctx) { conf.ctx = c.getContext('2d'); }
  80. ctx = conf.ctx;
  81. if (UE.gecko) { Util.Debug("Browser: gecko " + UE.gecko); }
  82. if (UE.webkit) { Util.Debug("Browser: webkit " + UE.webkit); }
  83. if (UE.trident) { Util.Debug("Browser: webkit " + UE.trident); }
  84. if (UE.presto) { Util.Debug("Browser: webkit " + UE.presto); }
  85. that.clear();
  86. /*
  87. * Determine browser Canvas feature support
  88. * and select fastest rendering methods
  89. */
  90. tval = 0;
  91. try {
  92. imgTest = ctx.getImageData(0, 0, 1,1);
  93. imgTest.data[0] = 123;
  94. imgTest.data[3] = 255;
  95. ctx.putImageData(imgTest, 0, 0);
  96. tval = ctx.getImageData(0, 0, 1, 1).data[0];
  97. if (tval === 123) {
  98. has_imageData = true;
  99. }
  100. } catch (exc1) {}
  101. if (has_imageData) {
  102. Util.Info("Canvas supports imageData");
  103. c_forceCanvas = false;
  104. if (ctx.createImageData) {
  105. // If it's there, it's faster
  106. Util.Info("Using Canvas createImageData");
  107. that.imageData = that.imageDataCreate;
  108. } else if (ctx.getImageData) {
  109. Util.Info("Using Canvas getImageData");
  110. that.imageData = that.imageDataGet;
  111. }
  112. Util.Info("Prefering javascript operations");
  113. if (conf.prefer_js === null) {
  114. conf.prefer_js = true;
  115. }
  116. that.rgbxImage = that.rgbxImageData;
  117. that.cmapImage = that.cmapImageData;
  118. } else {
  119. Util.Warn("Canvas lacks imageData, using fillRect (slow)");
  120. c_forceCanvas = true;
  121. conf.prefer_js = false;
  122. that.rgbxImage = that.rgbxImageFill;
  123. that.cmapImage = that.cmapImageFill;
  124. }
  125. /*
  126. * Determine browser support for setting the cursor via data URI
  127. * scheme
  128. */
  129. curDat = [];
  130. for (i=0; i < 8 * 8 * 4; i += 1) {
  131. curDat.push(255);
  132. }
  133. try {
  134. curSave = c.style.cursor;
  135. that.changeCursor(curDat, curDat, 2, 2, 8, 8);
  136. if (c.style.cursor) {
  137. if (conf.cursor_uri === null) {
  138. conf.cursor_uri = true;
  139. }
  140. Util.Info("Data URI scheme cursor supported");
  141. } else {
  142. if (conf.cursor_uri === null) {
  143. conf.cursor_uri = false;
  144. }
  145. Util.Warn("Data URI scheme cursor not supported");
  146. }
  147. c.style.cursor = curSave;
  148. } catch (exc2) {
  149. Util.Error("Data URI scheme cursor test exception: " + exc2);
  150. conf.cursor_uri = false;
  151. }
  152. conf.focused = true;
  153. Util.Debug("<< Canvas.init");
  154. return that ;
  155. }
  156. /* Translate DOM key event to keysym value */
  157. function getKeysym(e) {
  158. var evt, keysym;
  159. evt = (e ? e : window.event);
  160. /* Remap modifier and special keys */
  161. switch ( evt.keyCode ) {
  162. case 8 : keysym = 0xFF08; break; // BACKSPACE
  163. case 9 : keysym = 0xFF09; break; // TAB
  164. case 13 : keysym = 0xFF0D; break; // ENTER
  165. case 27 : keysym = 0xFF1B; break; // ESCAPE
  166. case 45 : keysym = 0xFF63; break; // INSERT
  167. case 46 : keysym = 0xFFFF; break; // DELETE
  168. case 36 : keysym = 0xFF50; break; // HOME
  169. case 35 : keysym = 0xFF57; break; // END
  170. case 33 : keysym = 0xFF55; break; // PAGE_UP
  171. case 34 : keysym = 0xFF56; break; // PAGE_DOWN
  172. case 37 : keysym = 0xFF51; break; // LEFT
  173. case 38 : keysym = 0xFF52; break; // UP
  174. case 39 : keysym = 0xFF53; break; // RIGHT
  175. case 40 : keysym = 0xFF54; break; // DOWN
  176. case 112 : keysym = 0xFFBE; break; // F1
  177. case 113 : keysym = 0xFFBF; break; // F2
  178. case 114 : keysym = 0xFFC0; break; // F3
  179. case 115 : keysym = 0xFFC1; break; // F4
  180. case 116 : keysym = 0xFFC2; break; // F5
  181. case 117 : keysym = 0xFFC3; break; // F6
  182. case 118 : keysym = 0xFFC4; break; // F7
  183. case 119 : keysym = 0xFFC5; break; // F8
  184. case 120 : keysym = 0xFFC6; break; // F9
  185. case 121 : keysym = 0xFFC7; break; // F10
  186. case 122 : keysym = 0xFFC8; break; // F11
  187. case 123 : keysym = 0xFFC9; break; // F12
  188. case 16 : keysym = 0xFFE1; break; // SHIFT
  189. case 17 : keysym = 0xFFE3; break; // CONTROL
  190. //case 18 : keysym = 0xFFE7; break; // Left Meta (Mac Option)
  191. case 18 : keysym = 0xFFE9; break; // Left ALT (Mac Command)
  192. default : keysym = evt.keyCode; break;
  193. }
  194. /* Remap symbols */
  195. switch (keysym) {
  196. case 186 : keysym = 59; break; // ; (IE)
  197. case 187 : keysym = 61; break; // = (IE)
  198. case 188 : keysym = 44; break; // , (Mozilla, IE)
  199. case 109 : // - (Mozilla)
  200. if (Util.Engine.gecko) {
  201. keysym = 45; }
  202. break;
  203. case 189 : keysym = 45; break; // - (IE)
  204. case 190 : keysym = 46; break; // . (Mozilla, IE)
  205. case 191 : keysym = 47; break; // / (Mozilla, IE)
  206. case 192 : keysym = 96; break; // ` (Mozilla, IE)
  207. case 219 : keysym = 91; break; // [ (Mozilla, IE)
  208. case 220 : keysym = 92; break; // \ (Mozilla, IE)
  209. case 221 : keysym = 93; break; // ] (Mozilla, IE)
  210. case 222 : keysym = 39; break; // ' (Mozilla, IE)
  211. }
  212. /* Remap shifted and unshifted keys */
  213. if (!!evt.shiftKey) {
  214. switch (keysym) {
  215. case 48 : keysym = 41 ; break; // ) (shifted 0)
  216. case 49 : keysym = 33 ; break; // ! (shifted 1)
  217. case 50 : keysym = 64 ; break; // @ (shifted 2)
  218. case 51 : keysym = 35 ; break; // # (shifted 3)
  219. case 52 : keysym = 36 ; break; // $ (shifted 4)
  220. case 53 : keysym = 37 ; break; // % (shifted 5)
  221. case 54 : keysym = 94 ; break; // ^ (shifted 6)
  222. case 55 : keysym = 38 ; break; // & (shifted 7)
  223. case 56 : keysym = 42 ; break; // * (shifted 8)
  224. case 57 : keysym = 40 ; break; // ( (shifted 9)
  225. case 59 : keysym = 58 ; break; // : (shifted `)
  226. case 61 : keysym = 43 ; break; // + (shifted ;)
  227. case 44 : keysym = 60 ; break; // < (shifted ,)
  228. case 45 : keysym = 95 ; break; // _ (shifted -)
  229. case 46 : keysym = 62 ; break; // > (shifted .)
  230. case 47 : keysym = 63 ; break; // ? (shifted /)
  231. case 96 : keysym = 126; break; // ~ (shifted `)
  232. case 91 : keysym = 123; break; // { (shifted [)
  233. case 92 : keysym = 124; break; // | (shifted \)
  234. case 93 : keysym = 125; break; // } (shifted ])
  235. case 39 : keysym = 34 ; break; // " (shifted ')
  236. }
  237. } else if ((keysym >= 65) && (keysym <=90)) {
  238. /* Remap unshifted A-Z */
  239. keysym += 32;
  240. }
  241. return keysym;
  242. }
  243. function onMouseButton(e, down) {
  244. var evt, pos, bmask;
  245. if (! conf.focused) {
  246. return true;
  247. }
  248. evt = (e ? e : window.event);
  249. pos = Util.getEventPosition(e, conf.target, conf.scale);
  250. bmask = 1 << evt.button;
  251. //Util.Debug('mouse ' + pos.x + "," + pos.y + " down: " + down + " bmask: " + bmask);
  252. if (c_mouseButton) {
  253. c_mouseButton(pos.x, pos.y, down, bmask);
  254. }
  255. Util.stopEvent(e);
  256. return false;
  257. }
  258. function onMouseDown(e) {
  259. onMouseButton(e, 1);
  260. }
  261. function onMouseUp(e) {
  262. onMouseButton(e, 0);
  263. }
  264. function onMouseWheel(e) {
  265. var evt, pos, bmask, wheelData;
  266. evt = (e ? e : window.event);
  267. pos = Util.getEventPosition(e, conf.target, conf.scale);
  268. wheelData = evt.detail ? evt.detail * -1 : evt.wheelDelta / 40;
  269. if (wheelData > 0) {
  270. bmask = 1 << 3;
  271. } else {
  272. bmask = 1 << 4;
  273. }
  274. //Util.Debug('mouse scroll by ' + wheelData + ':' + pos.x + "," + pos.y);
  275. if (c_mouseButton) {
  276. c_mouseButton(pos.x, pos.y, 1, bmask);
  277. c_mouseButton(pos.x, pos.y, 0, bmask);
  278. }
  279. Util.stopEvent(e);
  280. return false;
  281. }
  282. function onMouseMove(e) {
  283. var evt, pos;
  284. evt = (e ? e : window.event);
  285. pos = Util.getEventPosition(e, conf.target, conf.scale);
  286. //Util.Debug('mouse ' + evt.which + '/' + evt.button + ' up:' + pos.x + "," + pos.y);
  287. if (c_mouseMove) {
  288. c_mouseMove(pos.x, pos.y);
  289. }
  290. }
  291. function onKeyDown(e) {
  292. //Util.Debug("keydown: " + getKeysym(e));
  293. if (! conf.focused) {
  294. return true;
  295. }
  296. if (c_keyPress) {
  297. c_keyPress(getKeysym(e), 1);
  298. }
  299. Util.stopEvent(e);
  300. return false;
  301. }
  302. function onKeyUp(e) {
  303. //Util.Debug("keyup: " + getKeysym(e));
  304. if (! conf.focused) {
  305. return true;
  306. }
  307. if (c_keyPress) {
  308. c_keyPress(getKeysym(e), 0);
  309. }
  310. Util.stopEvent(e);
  311. return false;
  312. }
  313. function onMouseDisable(e) {
  314. var evt, pos;
  315. if (! conf.focused) {
  316. return true;
  317. }
  318. evt = (e ? e : window.event);
  319. pos = Util.getEventPosition(e, conf.target, conf.scale);
  320. /* Stop propagation if inside canvas area */
  321. if ((pos.x >= 0) && (pos.y >= 0) &&
  322. (pos.x < c_width) && (pos.y < c_height)) {
  323. //Util.Debug("mouse event disabled");
  324. Util.stopEvent(e);
  325. return false;
  326. }
  327. //Util.Debug("mouse event not disabled");
  328. return true;
  329. }
  330. //
  331. // Public API interface functions
  332. //
  333. that.getContext = function () {
  334. return conf.ctx;
  335. };
  336. that.start = function(keyPressFunc, mouseButtonFunc, mouseMoveFunc) {
  337. var c;
  338. Util.Debug(">> Canvas.start");
  339. c = conf.target;
  340. c_keyPress = keyPressFunc || null;
  341. c_mouseButton = mouseButtonFunc || null;
  342. c_mouseMove = mouseMoveFunc || null;
  343. Util.addEvent(conf.focusContainer, 'keydown', onKeyDown);
  344. Util.addEvent(conf.focusContainer, 'keyup', onKeyUp);
  345. Util.addEvent(c, 'mousedown', onMouseDown);
  346. Util.addEvent(c, 'mouseup', onMouseUp);
  347. Util.addEvent(c, 'mousemove', onMouseMove);
  348. Util.addEvent(c, (Util.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel',
  349. onMouseWheel);
  350. /* Work around right and middle click browser behaviors */
  351. Util.addEvent(conf.focusContainer, 'click', onMouseDisable);
  352. Util.addEvent(conf.focusContainer.body, 'contextmenu', onMouseDisable);
  353. Util.Debug("<< Canvas.start");
  354. };
  355. that.rescale = function(factor) {
  356. var c, tp, x, y,
  357. properties = ['transform', 'WebkitTransform', 'MozTransform', null];
  358. c = conf.target;
  359. tp = properties.shift();
  360. while (tp) {
  361. if (typeof c.style[tp] !== 'undefined') {
  362. break;
  363. }
  364. tp = properties.shift();
  365. }
  366. if (tp === null) {
  367. Util.Debug("No scaling support");
  368. return;
  369. }
  370. if (conf.scale === factor) {
  371. //Util.Debug("Canvas already scaled to '" + factor + "'");
  372. return;
  373. }
  374. conf.scale = factor;
  375. x = c.width - c.width * factor;
  376. y = c.height - c.height * factor;
  377. c.style[tp] = "scale(" + conf.scale + ") translate(-" + x + "px, -" + y + "px)";
  378. };
  379. that.resize = function(width, height, true_color) {
  380. var c = conf.target;
  381. if (typeof true_color !== "undefined") {
  382. conf.true_color = true_color;
  383. }
  384. c.width = width;
  385. c.height = height;
  386. c_width = c.offsetWidth;
  387. c_height = c.offsetHeight;
  388. that.rescale(conf.scale);
  389. };
  390. that.clear = function() {
  391. that.resize(640, 20);
  392. conf.ctx.clearRect(0, 0, c_width, c_height);
  393. };
  394. that.stop = function() {
  395. var c = conf.target;
  396. Util.removeEvent(conf.focusContainer, 'keydown', onKeyDown);
  397. Util.removeEvent(conf.focusContainer, 'keyup', onKeyUp);
  398. Util.removeEvent(c, 'mousedown', onMouseDown);
  399. Util.removeEvent(c, 'mouseup', onMouseUp);
  400. Util.removeEvent(c, 'mousemove', onMouseMove);
  401. Util.removeEvent(c, (Util.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel',
  402. onMouseWheel);
  403. /* Work around right and middle click browser behaviors */
  404. Util.removeEvent(conf.focusContainer, 'click', onMouseDisable);
  405. Util.removeEvent(conf.focusContainer.body, 'contextmenu', onMouseDisable);
  406. // Turn off cursor rendering
  407. if (conf.cursor_uri) {
  408. c.style.cursor = "default";
  409. }
  410. };
  411. setFillColor = function(color) {
  412. var rgb, newStyle;
  413. if (conf.true_color) {
  414. rgb = color;
  415. } else {
  416. rgb = conf.colourMap[color[0]];
  417. }
  418. if (newStyle !== c_prevStyle) {
  419. newStyle = "rgb(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")";
  420. conf.ctx.fillStyle = newStyle;
  421. c_prevStyle = newStyle;
  422. }
  423. };
  424. that.setFillColor = setFillColor;
  425. fillRect = function(x, y, width, height, color) {
  426. setFillColor(color);
  427. conf.ctx.fillRect(x, y, width, height);
  428. };
  429. that.fillRect = fillRect;
  430. that.copyImage = function(old_x, old_y, new_x, new_y, width, height) {
  431. conf.ctx.drawImage(conf.target, old_x, old_y, width, height,
  432. new_x, new_y, width, height);
  433. };
  434. /*
  435. * Tile rendering functions optimized for rendering engines.
  436. *
  437. * - In Chrome/webkit, Javascript image data array manipulations are
  438. * faster than direct Canvas fillStyle, fillRect rendering. In
  439. * gecko, Javascript array handling is much slower.
  440. */
  441. that.getTile = function(x, y, width, height, color) {
  442. var img, data, p, rgb, red, green, blue, j, i;
  443. img = {'x': x, 'y': y, 'width': width, 'height': height,
  444. 'data': []};
  445. if (conf.prefer_js) {
  446. data = img.data;
  447. if (conf.true_color) {
  448. rgb = color;
  449. } else {
  450. rgb = conf.colourMap[color[0]];
  451. }
  452. red = rgb[0];
  453. green = rgb[1];
  454. blue = rgb[2];
  455. for (j = 0; j < height; j += 1) {
  456. for (i = 0; i < width; i += 1) {
  457. p = (i + (j * width) ) * 4;
  458. data[p + 0] = red;
  459. data[p + 1] = green;
  460. data[p + 2] = blue;
  461. //data[p + 3] = 255; // Set Alpha
  462. }
  463. }
  464. } else {
  465. fillRect(x, y, width, height, color);
  466. }
  467. return img;
  468. };
  469. that.setSubTile = function(img, x, y, w, h, color) {
  470. var data, p, rgb, red, green, blue, width, j, i;
  471. if (conf.prefer_js) {
  472. data = img.data;
  473. width = img.width;
  474. if (conf.true_color) {
  475. rgb = color;
  476. } else {
  477. rgb = conf.colourMap[color[0]];
  478. }
  479. red = rgb[0];
  480. green = rgb[1];
  481. blue = rgb[2];
  482. for (j = 0; j < h; j += 1) {
  483. for (i = 0; i < w; i += 1) {
  484. p = (x + i + ((y + j) * width) ) * 4;
  485. data[p + 0] = red;
  486. data[p + 1] = green;
  487. data[p + 2] = blue;
  488. //img.data[p + 3] = 255; // Set Alpha
  489. }
  490. }
  491. } else {
  492. fillRect(img.x + x, img.y + y, w, h, color);
  493. }
  494. };
  495. that.putTile = function(img) {
  496. if (conf.prefer_js) {
  497. that.rgbxImage(img.x, img.y, img.width, img.height, img.data, 0);
  498. } else {
  499. // No-op, under gecko already done by setSubTile
  500. }
  501. };
  502. that.imageDataGet = function(width, height) {
  503. return conf.ctx.getImageData(0, 0, width, height);
  504. };
  505. that.imageDataCreate = function(width, height) {
  506. return conf.ctx.createImageData(width, height);
  507. };
  508. that.rgbxImageData = function(x, y, width, height, arr, offset) {
  509. var img, i, j, data;
  510. img = that.imageData(width, height);
  511. data = img.data;
  512. for (i=0, j=offset; i < (width * height * 4); i=i+4, j=j+4) {
  513. data[i + 0] = arr[j + 0];
  514. data[i + 1] = arr[j + 1];
  515. data[i + 2] = arr[j + 2];
  516. data[i + 3] = 255; // Set Alpha
  517. }
  518. conf.ctx.putImageData(img, x, y);
  519. };
  520. // really slow fallback if we don't have imageData
  521. that.rgbxImageFill = function(x, y, width, height, arr, offset) {
  522. var i, j, sx = 0, sy = 0;
  523. for (i=0, j=offset; i < (width * height); i+=1, j+=4) {
  524. fillRect(x+sx, y+sy, 1, 1, [arr[j+0], arr[j+1], arr[j+2]]);
  525. sx += 1;
  526. if ((sx % width) === 0) {
  527. sx = 0;
  528. sy += 1;
  529. }
  530. }
  531. };
  532. that.cmapImageData = function(x, y, width, height, arr, offset) {
  533. var img, i, j, data, rgb, cmap;
  534. img = that.imageData(width, height);
  535. data = img.data;
  536. cmap = conf.colourMap;
  537. for (i=0, j=offset; i < (width * height * 4); i+=4, j+=1) {
  538. rgb = cmap[arr[j]];
  539. data[i + 0] = rgb[0];
  540. data[i + 1] = rgb[1];
  541. data[i + 2] = rgb[2];
  542. data[i + 3] = 255; // Set Alpha
  543. }
  544. conf.ctx.putImageData(img, x, y);
  545. };
  546. that.cmapImageFill = function(x, y, width, height, arr, offset) {
  547. var i, j, sx = 0, sy = 0, cmap;
  548. cmap = conf.colourMap;
  549. for (i=0, j=offset; i < (width * height); i+=1, j+=1) {
  550. fillRect(x+sx, y+sy, 1, 1, [arr[j]]);
  551. sx += 1;
  552. if ((sx % width) === 0) {
  553. sx = 0;
  554. sy += 1;
  555. }
  556. }
  557. };
  558. that.blitImage = function(x, y, width, height, arr, offset) {
  559. if (conf.true_color) {
  560. that.rgbxImage(x, y, width, height, arr, offset);
  561. } else {
  562. that.cmapImage(x, y, width, height, arr, offset);
  563. }
  564. };
  565. that.blitStringImage = function(str, x, y) {
  566. var img = new Image();
  567. img.onload = function () { conf.ctx.drawImage(img, x, y); };
  568. img.src = str;
  569. };
  570. that.changeCursor = function(pixels, mask, hotx, hoty, w, h) {
  571. var cur = [], cmap, rgb, IHDRsz, ANDsz, XORsz, url, idx, alpha, x, y;
  572. //Util.Debug(">> changeCursor, x: " + hotx + ", y: " + hoty + ", w: " + w + ", h: " + h);
  573. if (conf.cursor_uri === false) {
  574. Util.Warn("changeCursor called but no cursor data URI support");
  575. return;
  576. }
  577. // Push multi-byte little-endian values
  578. cur.push16le = function (num) {
  579. this.push((num ) & 0xFF,
  580. (num >> 8) & 0xFF );
  581. };
  582. cur.push32le = function (num) {
  583. this.push((num ) & 0xFF,
  584. (num >> 8) & 0xFF,
  585. (num >> 16) & 0xFF,
  586. (num >> 24) & 0xFF );
  587. };
  588. cmap = conf.colourMap;
  589. IHDRsz = 40;
  590. ANDsz = w * h * 4;
  591. XORsz = Math.ceil( (w * h) / 8.0 );
  592. // Main header
  593. cur.push16le(0); // Reserved
  594. cur.push16le(2); // .CUR type
  595. cur.push16le(1); // Number of images, 1 for non-animated ico
  596. // Cursor #1 header
  597. cur.push(w); // width
  598. cur.push(h); // height
  599. cur.push(0); // colors, 0 -> true-color
  600. cur.push(0); // reserved
  601. cur.push16le(hotx); // hotspot x coordinate
  602. cur.push16le(hoty); // hotspot y coordinate
  603. cur.push32le(IHDRsz + XORsz + ANDsz); // cursor data byte size
  604. cur.push32le(22); // offset of cursor data in the file
  605. // Cursor #1 InfoHeader
  606. cur.push32le(IHDRsz); // Infoheader size
  607. cur.push32le(w); // Cursor width
  608. cur.push32le(h*2); // XOR+AND height
  609. cur.push16le(1); // number of planes
  610. cur.push16le(32); // bits per pixel
  611. cur.push32le(0); // Type of compression
  612. cur.push32le(XORsz + ANDsz); // Size of Image
  613. cur.push32le(0);
  614. cur.push32le(0);
  615. cur.push32le(0);
  616. cur.push32le(0);
  617. // XOR/color data
  618. for (y = h-1; y >= 0; y -= 1) {
  619. for (x = 0; x < w; x += 1) {
  620. idx = y * Math.ceil(w / 8) + Math.floor(x/8);
  621. alpha = (mask[idx] << (x % 8)) & 0x80 ? 255 : 0;
  622. if (conf.true_color) {
  623. idx = ((w * y) + x) * 4;
  624. cur.push(pixels[idx + 2]); // blue
  625. cur.push(pixels[idx + 1]); // green
  626. cur.push(pixels[idx + 0]); // red
  627. cur.push(alpha); // red
  628. } else {
  629. idx = (w * y) + x;
  630. rgb = cmap[pixels[idx]];
  631. cur.push(rgb[2]); // blue
  632. cur.push(rgb[1]); // green
  633. cur.push(rgb[0]); // red
  634. cur.push(alpha); // alpha
  635. }
  636. }
  637. }
  638. // AND/bitmask data (ignored, just needs to be right size)
  639. for (y = 0; y < h; y += 1) {
  640. for (x = 0; x < Math.ceil(w / 8); x += 1) {
  641. cur.push(0x00);
  642. }
  643. }
  644. url = "data:image/x-icon;base64," + Base64.encode(cur);
  645. conf.target.style.cursor = "url(" + url + ") " + hotx + " " + hoty + ", default";
  646. //Util.Debug("<< changeCursor, cur.length: " + cur.length);
  647. };
  648. return constructor(); // Return the public API interface
  649. } // End of Canvas()