How do I add extra parameters to a javascript event function? I want my current event functions to take in more parameters than just an event.
My current code is:
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: (evt.clientX - rect.left) / (rect.right - rect.left) * canvas.width,
y: (evt.clientY - rect.top) / (rect.bottom - rect.top) * canvas.height
};
}
function give_mouse_coord(e) {
var pos = getMousePos(canvas, e);
var pos_x = pos.x
var pos_y = pos.y
}
window.addEventListener('click', give_mouse_coord, false);
but I want to have the give_mouse_coord take in other parameters as well, for example:
function give_mouse_coord(e, scalar) {
var pos = getMousePos(canvas, e);
var pos_x = pos.x * scalar
var pos_y = pos.y * scalar
}
I want the window.addEventListener to run my functions but when I try adding more parameters the function won't run.
addEventListener. You can pass an anonymous function instead that does whatever, including calling other functions. What I'm saying is that you cannot change the fact that the JS engine only passes the event to the handler function but that doesn't mean you can't just do arbitrary stuff yourself, at any point.