canvas.js 23 KB

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