-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclicky.js
51 lines (42 loc) · 1.33 KB
/
clicky.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import config from './config.js'
// Can't import canvas from index, error - can't access variable before intialization?
let canvas = document.querySelector('canvas')
let tileHeight = config.board.sprite.frameH
let tileWidth = config.board.sprite.frameW
// TODO: Wrap into a singleton for cleaner code and eliminating global var clickys
let clickys = []
/**
* Responds to click on canvas and calls appropriate handler for tile/region clicked
* @param {object} event event object
*/
function clickHandler(event) {
let clickX = Math.floor((event.pageX - canvas.offsetLeft) / tileWidth)
let clickY = Math.floor((event.pageY - canvas.offsetTop) / tileHeight)
clickys.forEach((tile) => {
let {x, y} = tile.pos
if(clickX === x && clickY === y) {
tile.callback({x, y}) // pass click position relative to the element
}
})
}
canvas.addEventListener('click', clickHandler)
/**
*
* @param {object} pos - position of tile (in tile indexes) to attach click event to
* @param {*} callback - function to call on click
* @param {number} pos.x - x coordinate of position
* @param {number} pos.y - y coordinate of position
*/
function onClick(pos, callback) {
clickys.push({
pos,
callback
})
}
function removeListener (pos) {
clickys = clickys.filter(el => el.pos.x !== pos.x && el.pos.y !== pos.y)
}
export {
onClick,
removeListener
}