For instance, if the user pushes the mouse down on one clickable region, and drags it over to ANOTHER clickable region, the "click" event will still fire and (therefore) fire the second region's click event.
So what kinds of things do we actually need to figure this stuff out?
Well, for starters, let's demystify the mouse event positions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//let's cache the event | |
var priorEvt; | |
function getMousePos(evt) { | |
//set and store.. | |
priorEvt = evt = evt || priorEvt; | |
//get the bounding rectangle | |
var rect = canvas.getBoundingClientRect(); | |
//lastly, return the x and y coordinates | |
if (evt) | |
return { | |
x: evt.clientX - rect.left, | |
y: evt.clientY - rect.top | |
}; | |
return { | |
x: 0, | |
y: 0 | |
}; | |
} |
This will give you a way to get the mouse location of any click event. So let's build our clickable region first.
Cache the canvas and draw an image...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var canvas = yourCanvasHere; | |
function CanvasButton(img, x, y, width, height) { | |
//this function takes an "event" object passed by the canvas click events | |
function onButton(pos) { | |
//it determines if the position is greater than x, less than the width | |
//and greater than y, less than the height | |
return (x <= pos.x && pos.x <= x + width) && | |
(y <= pos.y && pos.y <= y + height); | |
} | |
//go ahead and replace this section with whatever you need to draw the image | |
if (Object.prototype.toString.call(img) === '[object String]') | |
img = document.getElementById(img); | |
var ctx = canvas.getContext("2d"); | |
ctx.drawImage(img, x, y, width, height); | |
} |
..and the only things that are missing are the click events...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var down = false; | |
canvas.addEventListener("mousedown", function (evt) { //event listener | |
down = onButton(getMousePos(evt)); | |
}); | |
//handle the document mouseup to reset the "down" variable every time. | |
document.addEventListener("mouseup", function (evt) { | |
if (onButton(getMousePos(evt)) && down) { | |
//we got a click! | |
} | |
}); |
and put it all together!
The final product looks a bit like this, because you totally took the code and made it work for your project.. right? Get coding people!
In functional health,
Josh
No comments:
Post a Comment