canvas.js 24 KB

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