30 lines
656 B
JavaScript
30 lines
656 B
JavaScript
import {canvas} from "./canvas.js";
|
|
|
|
const state = {
|
|
x: 0,
|
|
y: 0,
|
|
buttons: 0,
|
|
};
|
|
|
|
function update(e) {
|
|
const elem_root = e.target.getBoundingClientRect();
|
|
state.x = e.clientX - elem_root.left;
|
|
state.y = e.clientY - elem_root.top;
|
|
state.buttons = e.buttons;
|
|
}
|
|
canvas.addEventListener("mousedown", update);
|
|
canvas.addEventListener("mouseup", update);
|
|
canvas.addEventListener("mousemove", update);
|
|
canvas.addEventListener("contextmenu", (e) => {
|
|
e.preventDefault();
|
|
});
|
|
|
|
export function get_x() {
|
|
return state.x;
|
|
}
|
|
export function get_y() {
|
|
return state.y;
|
|
}
|
|
export function button_pressed(button) {
|
|
return ((state.buttons >>> button) & 1) !== 0;
|
|
}
|