|
@@ -7,7 +7,7 @@
|
|
*/
|
|
*/
|
|
|
|
|
|
/*jslint browser: true, white: false, bitwise: false */
|
|
/*jslint browser: true, white: false, bitwise: false */
|
|
-/*global window, Util, Base64, changeCursor, getKeysym */
|
|
|
|
|
|
+/*global Util, Base64, changeCursor */
|
|
|
|
|
|
function Canvas(conf) {
|
|
function Canvas(conf) {
|
|
"use strict";
|
|
"use strict";
|
|
@@ -23,10 +23,6 @@ var that = {}, // Public API interface
|
|
|
|
|
|
c_prevStyle = "",
|
|
c_prevStyle = "",
|
|
|
|
|
|
- c_keyPress = null,
|
|
|
|
- c_mouseButton = null,
|
|
|
|
- c_mouseMove = null,
|
|
|
|
-
|
|
|
|
c_webkit_bug = false,
|
|
c_webkit_bug = false,
|
|
c_flush_timer = null;
|
|
c_flush_timer = null;
|
|
|
|
|
|
@@ -91,7 +87,7 @@ that.get_height = function() {
|
|
function constructor() {
|
|
function constructor() {
|
|
Util.Debug(">> Canvas.init");
|
|
Util.Debug(">> Canvas.init");
|
|
|
|
|
|
- var c, ctx, func, origfunc, imgTest, tval, i, curDat, curSave,
|
|
|
|
|
|
+ var c, ctx, func, imgTest, tval, i, curDat, curSave,
|
|
has_imageData = false, UE = Util.Engine;
|
|
has_imageData = false, UE = Util.Engine;
|
|
|
|
|
|
if (! conf.target) { throw("target must be set"); }
|
|
if (! conf.target) { throw("target must be set"); }
|
|
@@ -214,121 +210,6 @@ function constructor() {
|
|
return that ;
|
|
return that ;
|
|
}
|
|
}
|
|
|
|
|
|
-function onMouseButton(e, down) {
|
|
|
|
- var evt, pos, bmask;
|
|
|
|
- if (! conf.focused) {
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
- evt = (e ? e : window.event);
|
|
|
|
- pos = Util.getEventPosition(e, conf.target, conf.scale);
|
|
|
|
- if (evt.which) {
|
|
|
|
- /* everything except IE */
|
|
|
|
- bmask = 1 << evt.button;
|
|
|
|
- } else {
|
|
|
|
- /* IE including 9 */
|
|
|
|
- bmask = (evt.button & 0x1) + // Left
|
|
|
|
- (evt.button & 0x2) * 2 + // Right
|
|
|
|
- (evt.button & 0x4) / 2; // Middle
|
|
|
|
- }
|
|
|
|
- //Util.Debug("mouse " + pos.x + "," + pos.y + " down: " + down +
|
|
|
|
- // " bmask: " + bmask + "(evt.button: " + evt.button + ")");
|
|
|
|
- if (c_mouseButton) {
|
|
|
|
- c_mouseButton(pos.x, pos.y, down, bmask);
|
|
|
|
- }
|
|
|
|
- Util.stopEvent(e);
|
|
|
|
- return false;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-function onMouseDown(e) {
|
|
|
|
- onMouseButton(e, 1);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-function onMouseUp(e) {
|
|
|
|
- onMouseButton(e, 0);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-function onMouseWheel(e) {
|
|
|
|
- var evt, pos, bmask, wheelData;
|
|
|
|
- evt = (e ? e : window.event);
|
|
|
|
- pos = Util.getEventPosition(e, conf.target, conf.scale);
|
|
|
|
- wheelData = evt.detail ? evt.detail * -1 : evt.wheelDelta / 40;
|
|
|
|
- if (wheelData > 0) {
|
|
|
|
- bmask = 1 << 3;
|
|
|
|
- } else {
|
|
|
|
- bmask = 1 << 4;
|
|
|
|
- }
|
|
|
|
- //Util.Debug('mouse scroll by ' + wheelData + ':' + pos.x + "," + pos.y);
|
|
|
|
- if (c_mouseButton) {
|
|
|
|
- c_mouseButton(pos.x, pos.y, 1, bmask);
|
|
|
|
- c_mouseButton(pos.x, pos.y, 0, bmask);
|
|
|
|
- }
|
|
|
|
- Util.stopEvent(e);
|
|
|
|
- return false;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-function onMouseMove(e) {
|
|
|
|
- var evt, pos;
|
|
|
|
- evt = (e ? e : window.event);
|
|
|
|
- pos = Util.getEventPosition(e, conf.target, conf.scale);
|
|
|
|
- //Util.Debug('mouse ' + evt.which + '/' + evt.button + ' up:' + pos.x + "," + pos.y);
|
|
|
|
- if (c_mouseMove) {
|
|
|
|
- c_mouseMove(pos.x, pos.y);
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-function onKeyDown(e) {
|
|
|
|
- //Util.Debug("keydown: " + getKeysym(e));
|
|
|
|
- if (! conf.focused) {
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
- if (c_keyPress) {
|
|
|
|
- c_keyPress(getKeysym(e), 1, e.ctrlKey, e.shiftKey, e.altKey);
|
|
|
|
- }
|
|
|
|
- Util.stopEvent(e);
|
|
|
|
- return false;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-function onKeyUp(e) {
|
|
|
|
- //Util.Debug("keyup: " + getKeysym(e));
|
|
|
|
- if (! conf.focused) {
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
- if (c_keyPress) {
|
|
|
|
- c_keyPress(getKeysym(e), 0, e.ctrlKey, e.shiftKey, e.altKey);
|
|
|
|
- }
|
|
|
|
- Util.stopEvent(e);
|
|
|
|
- return false;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-function onKeyPress(e) {
|
|
|
|
- //Util.Debug("keypress: " + e.charCode);
|
|
|
|
- if (! conf.focused) {
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
- // Stop keypress events. Necessary for Opera because stopping
|
|
|
|
- // keydown and keyup events still results in a keypress event.
|
|
|
|
- Util.stopEvent(e);
|
|
|
|
- return false;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-function onMouseDisable(e) {
|
|
|
|
- var evt, pos;
|
|
|
|
- if (! conf.focused) {
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
- evt = (e ? e : window.event);
|
|
|
|
- pos = Util.getEventPosition(e, conf.target, conf.scale);
|
|
|
|
- /* Stop propagation if inside canvas area */
|
|
|
|
- if ((pos.x >= 0) && (pos.y >= 0) &&
|
|
|
|
- (pos.x < c_width) && (pos.y < c_height)) {
|
|
|
|
- //Util.Debug("mouse event disabled");
|
|
|
|
- Util.stopEvent(e);
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- //Util.Debug("mouse event not disabled");
|
|
|
|
- return true;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
//
|
|
//
|
|
// Public API interface functions
|
|
// Public API interface functions
|
|
//
|
|
//
|
|
@@ -337,52 +218,6 @@ that.getContext = function () {
|
|
return conf.ctx;
|
|
return conf.ctx;
|
|
};
|
|
};
|
|
|
|
|
|
-that.start = function(keyPressFunc, mouseButtonFunc, mouseMoveFunc) {
|
|
|
|
- var c;
|
|
|
|
- Util.Debug(">> Canvas.start");
|
|
|
|
-
|
|
|
|
- c = conf.target;
|
|
|
|
- c_keyPress = keyPressFunc || null;
|
|
|
|
- c_mouseButton = mouseButtonFunc || null;
|
|
|
|
- c_mouseMove = mouseMoveFunc || null;
|
|
|
|
-
|
|
|
|
- Util.addEvent(conf.focusContainer, 'keydown', onKeyDown);
|
|
|
|
- Util.addEvent(conf.focusContainer, 'keyup', onKeyUp);
|
|
|
|
- Util.addEvent(conf.focusContainer, 'keypress', onKeyPress);
|
|
|
|
- Util.addEvent(c, 'mousedown', onMouseDown);
|
|
|
|
- Util.addEvent(c, 'mouseup', onMouseUp);
|
|
|
|
- Util.addEvent(c, 'mousemove', onMouseMove);
|
|
|
|
- Util.addEvent(c, (Util.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel',
|
|
|
|
- onMouseWheel);
|
|
|
|
-
|
|
|
|
- /* Work around right and middle click browser behaviors */
|
|
|
|
- Util.addEvent(conf.focusContainer, 'click', onMouseDisable);
|
|
|
|
- Util.addEvent(conf.focusContainer.body, 'contextmenu', onMouseDisable);
|
|
|
|
-
|
|
|
|
- Util.Debug("<< Canvas.start");
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-that.stop = function() {
|
|
|
|
- var c = conf.target;
|
|
|
|
- Util.removeEvent(conf.focusContainer, 'keydown', onKeyDown);
|
|
|
|
- Util.removeEvent(conf.focusContainer, 'keyup', onKeyUp);
|
|
|
|
- Util.removeEvent(conf.focusContainer, 'keypress', onKeyPress);
|
|
|
|
- Util.removeEvent(c, 'mousedown', onMouseDown);
|
|
|
|
- Util.removeEvent(c, 'mouseup', onMouseUp);
|
|
|
|
- Util.removeEvent(c, 'mousemove', onMouseMove);
|
|
|
|
- Util.removeEvent(c, (Util.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel',
|
|
|
|
- onMouseWheel);
|
|
|
|
-
|
|
|
|
- /* Work around right and middle click browser behaviors */
|
|
|
|
- Util.removeEvent(conf.focusContainer, 'click', onMouseDisable);
|
|
|
|
- Util.removeEvent(conf.focusContainer.body, 'contextmenu', onMouseDisable);
|
|
|
|
-
|
|
|
|
- // Turn off cursor rendering
|
|
|
|
- if (conf.cursor_uri) {
|
|
|
|
- c.style.cursor = "default";
|
|
|
|
- }
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
that.rescale = function(factor) {
|
|
that.rescale = function(factor) {
|
|
var c, tp, x, y,
|
|
var c, tp, x, y,
|
|
properties = ['transform', 'WebkitTransform', 'MozTransform', null];
|
|
properties = ['transform', 'WebkitTransform', 'MozTransform', null];
|
|
@@ -480,7 +315,7 @@ that.copyImage = function(old_x, old_y, new_x, new_y, width, height) {
|
|
* gecko, Javascript array handling is much slower.
|
|
* gecko, Javascript array handling is much slower.
|
|
*/
|
|
*/
|
|
that.getTile = function(x, y, width, height, color) {
|
|
that.getTile = function(x, y, width, height, color) {
|
|
- var img, data = [], p, rgb, red, green, blue, j, i;
|
|
|
|
|
|
+ var img, data = [], rgb, red, green, blue, i;
|
|
img = {'x': x, 'y': y, 'width': width, 'height': height,
|
|
img = {'x': x, 'y': y, 'width': width, 'height': height,
|
|
'data': data};
|
|
'data': data};
|
|
if (conf.prefer_js) {
|
|
if (conf.prefer_js) {
|
|
@@ -534,9 +369,8 @@ that.setSubTile = function(img, x, y, w, h, color) {
|
|
that.putTile = function(img) {
|
|
that.putTile = function(img) {
|
|
if (conf.prefer_js) {
|
|
if (conf.prefer_js) {
|
|
that.rgbxImage(img.x, img.y, img.width, img.height, img.data, 0);
|
|
that.rgbxImage(img.x, img.y, img.width, img.height, img.data, 0);
|
|
- } else {
|
|
|
|
- // No-op, under gecko already done by setSubTile
|
|
|
|
}
|
|
}
|
|
|
|
+ // else: No-op, under gecko already done by setSubTile
|
|
};
|
|
};
|
|
|
|
|
|
that.imageDataGet = function(width, height) {
|
|
that.imageDataGet = function(width, height) {
|
|
@@ -628,6 +462,10 @@ that.changeCursor = function(pixels, mask, hotx, hoty, w, h) {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+that.defaultCursor = function() {
|
|
|
|
+ conf.target.style.cursor = "default";
|
|
|
|
+};
|
|
|
|
+
|
|
return constructor(); // Return the public API interface
|
|
return constructor(); // Return the public API interface
|
|
|
|
|
|
} // End of Canvas()
|
|
} // End of Canvas()
|
|
@@ -731,116 +569,3 @@ function changeCursor(target, pixels, mask, hotx, hoty, w, h, cmap) {
|
|
target.style.cursor = "url(" + url + ") " + hotx + " " + hoty + ", default";
|
|
target.style.cursor = "url(" + url + ") " + hotx + " " + hoty + ", default";
|
|
//Util.Debug("<< changeCursor, cur.length: " + cur.length);
|
|
//Util.Debug("<< changeCursor, cur.length: " + cur.length);
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-/* Translate DOM key down/up event to keysym value */
|
|
|
|
-function getKeysym(e) {
|
|
|
|
- var evt, keysym;
|
|
|
|
- evt = (e ? e : window.event);
|
|
|
|
-
|
|
|
|
- /* Remap modifier and special keys */
|
|
|
|
- switch ( evt.keyCode ) {
|
|
|
|
- case 8 : keysym = 0xFF08; break; // BACKSPACE
|
|
|
|
- case 9 : keysym = 0xFF09; break; // TAB
|
|
|
|
- case 13 : keysym = 0xFF0D; break; // ENTER
|
|
|
|
- case 27 : keysym = 0xFF1B; break; // ESCAPE
|
|
|
|
- case 45 : keysym = 0xFF63; break; // INSERT
|
|
|
|
- case 46 : keysym = 0xFFFF; break; // DELETE
|
|
|
|
- case 36 : keysym = 0xFF50; break; // HOME
|
|
|
|
- case 35 : keysym = 0xFF57; break; // END
|
|
|
|
- case 33 : keysym = 0xFF55; break; // PAGE_UP
|
|
|
|
- case 34 : keysym = 0xFF56; break; // PAGE_DOWN
|
|
|
|
- case 37 : keysym = 0xFF51; break; // LEFT
|
|
|
|
- case 38 : keysym = 0xFF52; break; // UP
|
|
|
|
- case 39 : keysym = 0xFF53; break; // RIGHT
|
|
|
|
- case 40 : keysym = 0xFF54; break; // DOWN
|
|
|
|
- case 112 : keysym = 0xFFBE; break; // F1
|
|
|
|
- case 113 : keysym = 0xFFBF; break; // F2
|
|
|
|
- case 114 : keysym = 0xFFC0; break; // F3
|
|
|
|
- case 115 : keysym = 0xFFC1; break; // F4
|
|
|
|
- case 116 : keysym = 0xFFC2; break; // F5
|
|
|
|
- case 117 : keysym = 0xFFC3; break; // F6
|
|
|
|
- case 118 : keysym = 0xFFC4; break; // F7
|
|
|
|
- case 119 : keysym = 0xFFC5; break; // F8
|
|
|
|
- case 120 : keysym = 0xFFC6; break; // F9
|
|
|
|
- case 121 : keysym = 0xFFC7; break; // F10
|
|
|
|
- case 122 : keysym = 0xFFC8; break; // F11
|
|
|
|
- case 123 : keysym = 0xFFC9; break; // F12
|
|
|
|
- case 16 : keysym = 0xFFE1; break; // SHIFT
|
|
|
|
- case 17 : keysym = 0xFFE3; break; // CONTROL
|
|
|
|
- //case 18 : keysym = 0xFFE7; break; // Left Meta (Mac Option)
|
|
|
|
- case 18 : keysym = 0xFFE9; break; // Left ALT (Mac Command)
|
|
|
|
- default : keysym = evt.keyCode; break;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /* Remap symbols */
|
|
|
|
- switch (keysym) {
|
|
|
|
- case 186 : keysym = 59; break; // ; (IE)
|
|
|
|
- case 187 : keysym = 61; break; // = (IE)
|
|
|
|
- case 188 : keysym = 44; break; // , (Mozilla, IE)
|
|
|
|
- case 109 : // - (Mozilla, Opera)
|
|
|
|
- if (Util.Engine.gecko || Util.Engine.presto) {
|
|
|
|
- keysym = 45; }
|
|
|
|
- break;
|
|
|
|
- case 189 : keysym = 45; break; // - (IE)
|
|
|
|
- case 190 : keysym = 46; break; // . (Mozilla, IE)
|
|
|
|
- case 191 : keysym = 47; break; // / (Mozilla, IE)
|
|
|
|
- case 192 : keysym = 96; break; // ` (Mozilla, IE)
|
|
|
|
- case 219 : keysym = 91; break; // [ (Mozilla, IE)
|
|
|
|
- case 220 : keysym = 92; break; // \ (Mozilla, IE)
|
|
|
|
- case 221 : keysym = 93; break; // ] (Mozilla, IE)
|
|
|
|
- case 222 : keysym = 39; break; // ' (Mozilla, IE)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /* Remap shifted and unshifted keys */
|
|
|
|
- if (!!evt.shiftKey) {
|
|
|
|
- switch (keysym) {
|
|
|
|
- case 48 : keysym = 41 ; break; // ) (shifted 0)
|
|
|
|
- case 49 : keysym = 33 ; break; // ! (shifted 1)
|
|
|
|
- case 50 : keysym = 64 ; break; // @ (shifted 2)
|
|
|
|
- case 51 : keysym = 35 ; break; // # (shifted 3)
|
|
|
|
- case 52 : keysym = 36 ; break; // $ (shifted 4)
|
|
|
|
- case 53 : keysym = 37 ; break; // % (shifted 5)
|
|
|
|
- case 54 : keysym = 94 ; break; // ^ (shifted 6)
|
|
|
|
- case 55 : keysym = 38 ; break; // & (shifted 7)
|
|
|
|
- case 56 : keysym = 42 ; break; // * (shifted 8)
|
|
|
|
- case 57 : keysym = 40 ; break; // ( (shifted 9)
|
|
|
|
-
|
|
|
|
- case 59 : keysym = 58 ; break; // : (shifted `)
|
|
|
|
- case 61 : keysym = 43 ; break; // + (shifted ;)
|
|
|
|
- case 44 : keysym = 60 ; break; // < (shifted ,)
|
|
|
|
- case 45 : keysym = 95 ; break; // _ (shifted -)
|
|
|
|
- case 46 : keysym = 62 ; break; // > (shifted .)
|
|
|
|
- case 47 : keysym = 63 ; break; // ? (shifted /)
|
|
|
|
- case 96 : keysym = 126; break; // ~ (shifted `)
|
|
|
|
- case 91 : keysym = 123; break; // { (shifted [)
|
|
|
|
- case 92 : keysym = 124; break; // | (shifted \)
|
|
|
|
- case 93 : keysym = 125; break; // } (shifted ])
|
|
|
|
- case 39 : keysym = 34 ; break; // " (shifted ')
|
|
|
|
- }
|
|
|
|
- } else if ((keysym >= 65) && (keysym <=90)) {
|
|
|
|
- /* Remap unshifted A-Z */
|
|
|
|
- keysym += 32;
|
|
|
|
- } else if (evt.keyLocation === 3) {
|
|
|
|
- // numpad keys
|
|
|
|
- switch (keysym) {
|
|
|
|
- case 96 : keysym = 48; break; // 0
|
|
|
|
- case 97 : keysym = 49; break; // 1
|
|
|
|
- case 98 : keysym = 50; break; // 2
|
|
|
|
- case 99 : keysym = 51; break; // 3
|
|
|
|
- case 100: keysym = 52; break; // 4
|
|
|
|
- case 101: keysym = 53; break; // 5
|
|
|
|
- case 102: keysym = 54; break; // 6
|
|
|
|
- case 103: keysym = 55; break; // 7
|
|
|
|
- case 104: keysym = 56; break; // 8
|
|
|
|
- case 105: keysym = 57; break; // 9
|
|
|
|
- case 109: keysym = 45; break; // -
|
|
|
|
- case 110: keysym = 46; break; // .
|
|
|
|
- case 111: keysym = 47; break; // /
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return keysym;
|
|
|
|
-}
|
|
|
|
-
|
|
|