diff options
author | Aleksander Guryanov <caiiiycuk@gmail.com> | 2013-03-17 12:10:13 +0700 |
---|---|---|
committer | Aleksander Guryanov <caiiiycuk@gmail.com> | 2013-03-17 21:19:19 +0700 |
commit | 58bb22b33d27977fa7ca15d73db36e25299d1b65 (patch) | |
tree | 3fe0b089b8bcab5bab0fed3670c1fbb58a7ced5d | |
parent | 4197860af16ed859e6a7d63045f4d4bf5305bcdb (diff) |
Fix offsets for canvas
-rw-r--r-- | src/library_sdl.js | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js index 9fc979d2..73258aa3 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -443,6 +443,21 @@ var LibrarySDL = { return false; }, + offsets: function(element) { + var left = 0; + var top = 0; + + do { + left += element.offsetLeft; + top += element.offsetTop; + } while (element = element.offsetParent) + + return { + left: left, + top: top + } + }, + makeCEvent: function(event, ptr) { if (typeof event === 'number') { // This is a pointer to a native C event that was SDL_PushEvent'ed @@ -524,8 +539,9 @@ var LibrarySDL = { } else { // Otherwise, calculate the movement based on the changes // in the coordinates. - var x = event.pageX - Module["canvas"].offsetLeft; - var y = event.pageY - Module["canvas"].offsetTop; + var offsets = SDL.offsets(Module["canvas"]); + var x = event.pageX - offsets.left; + var y = event.pageY - offsets.top; var movementX = x - SDL.mouseX; var movementY = y - SDL.mouseY; } @@ -912,10 +928,11 @@ var LibrarySDL = { SDL_WarpMouse: function(x, y) { return; // TODO: implement this in a non-buggy way. Need to keep relative mouse movements correct after calling this + var offsets = SDL.offsets(Module["canvas"]); SDL.events.push({ type: 'mousemove', - pageX: x + Module['canvas'].offsetLeft, - pageY: y + Module['canvas'].offsetTop + pageX: x + offsets.left, + pageY: y + offsets.top }); }, |