Skip to content

Commit cb59b8b

Browse files
committed
Mouse Bug Fixes
Added PIXI.input.mouseContainer to properly scale mouse input
1 parent e52ae54 commit cb59b8b

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

dev/mouse-tracking/app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const app = new PIXI.Application({ width: 480, height: 270, backgroundColor: 0xffffff, antialias: false});
22
app.view.id = "PIXI";
33
document.body.appendChild(app.view);
4+
PIXI.input.mouseContainer = app.view;
45

56
const sleepTex = PIXI.Texture.from('assets/sleepy.png');
67
const rageTex = PIXI.Texture.from('assets/rage.png');
@@ -14,6 +15,8 @@ cursor.y = 0;
1415
cursor.anchor = {x: 0.5, y: 0.5};
1516
scene.addChild(cursor);
1617

18+
console.log(document.getElementsByTagName("html")[0].getBoundingClientRect());
19+
1720
app.ticker.add((deltaTime) => {
1821
cursor.x = (PIXI.input.getMouseX() * app.view.width + 0.5) ^ 0;
1922
cursor.y = (PIXI.input.getMouseY() * app.view.height + 0.5) ^ 0;

dev/notes/documentation.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,30 @@
22

33
## PIXI.input
44

5-
PIXI.input.getKeyFired(keyStr): Returns boolean
5+
PIXI.input.getKeyFired(keyString): Returns boolean
66
- Returns true if this is the first time it has been called since the key has been pressed.
77

8-
PIXI.input.getKeyDown(keyStr): Returns boolean
8+
PIXI.input.getKeyDown(keyString): Returns boolean
99
- Returns true if the key is actively pressed down.
1010

11+
PIXI.input.mouseContainer: [DOM Element]
12+
- Is used to scale the mouse's coordinates to the desired element.
13+
- USAGE: PIXI.input.mouseContainer = [DOM Element];
14+
15+
PIXI.getMouseFired(mouseButton): Returns boolean
16+
- Returns true if this is the first time it has been called since the button has been pressed.
17+
- NOTE: Mouse buttons are integers 0 (primary, typically left) 1 (middle), and 2 (secondary, typically right).
18+
19+
PIXI.getMouseDown(mouseButton): Returns boolean
20+
- Returns true if the button is actively pressed down.
21+
- NOTE: Mouse buttons are integers 0 (primary, typically left) 1 (middle), and 2 (secondary, typically right).
22+
23+
PIXI.getMouseX(): Returns number
24+
- Returns the last seen mouse X coordinate, where 0.0-1.0 is within the scene horizontally from left to right.
25+
26+
PIXI.getMouseY(): Returns number
27+
- Returns the last seen mouse Y coordinate, where 0.0-1.0 is within the scene vertically from top to bottom.
28+
1129
## PIXI.collision
1230

1331
PIXI.collision.aabb(a, b): Returns boolean

dev/zepyhr.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict"
22
// ZEPHYR.js directly builds on top of PixiJS for ease of use
3-
PIXI.zephyr = "ZephyrJS version 22.5.23";
3+
PIXI.zephyr = "ZephyrJS version 22.5.25";
44

55
PIXI.input = {
66
keyMap: new Map(),
@@ -15,14 +15,15 @@ PIXI.input = {
1515
return PIXI.input.keyMap.has(keyStr);
1616
},
1717

18+
mouseContainer: document.getElementsByTagName("html")[0],
1819
mouseMap: new Map(),
19-
getMouseFired: (btnStr) => {
20-
let r = PIXI.input.mouseMap.get(btnStr);
21-
PIXI.input.keyMap.set(btnStr, false);
20+
getMouseFired: (btn) => {
21+
let r = PIXI.input.mouseMap.get(btn);
22+
PIXI.input.keyMap.set(btn, false);
2223
return r;
2324
},
24-
getMouseDown: (btnStr) => {
25-
return PIXI.input.mouseMap.has(btnStr);
25+
getMouseDown: (btn) => {
26+
return PIXI.input.mouseMap.has(btn);
2627
},
2728
getMouseX: () => {
2829
return PIXI.input.mouseMap.get('x');
@@ -63,8 +64,9 @@ window.addEventListener('keyup', (e) => {
6364

6465
// Mouse
6566
window.addEventListener('mousemove', (e) => {
66-
PIXI.input.mouseMap.set('x', e.x / window.innerWidth);
67-
PIXI.input.mouseMap.set('y', e.y / window.innerHeight);
67+
let bounds = PIXI.input.mouseContainer.getBoundingClientRect();
68+
PIXI.input.mouseMap.set('x', (e.x - bounds.left) / bounds.width);
69+
PIXI.input.mouseMap.set('y', (e.y - bounds.top) / bounds.height);
6870
});
6971
window.addEventListener('mousedown', (e) => {
7072
PIXI.input.mouseMap.set(e.button, true);

0 commit comments

Comments
 (0)