I've got an animation on my canvas where I have some images (drawn using drawImage()
). For the sake of simplicity, let's say there's only two images. These images are following a circular path in faux-3d space such that sometimes one image overlaps the other and other times the second image overlaps the first. These images are also scaled as they move "closer" or "further" from the viewer.
Each of these images is an object with the following (simplified) code:
function slide_object() {
this.x = 0.0; // x = position along path (in radians)
this.xpos = 0.0; // x position on canvas
this.ypos = 0.0; // y position on canvas
this.scale = 0.0;
this.name = ""; // a string to be displayed when slide is moused over
this.imgx = 0.0; // width of original image
this.imgy = 0.0; // height of original image
this.init = function (abspath, startx, name) { // startx = path offset (in radians)
this.name = name;
this.x = (startx % (Math.PI * 2));
var slide_image = new Image();
slide_image.src = abspath;
this.img = slide_image;
calcObjPositions(0, this); // calculate's the image's position, then draws it
};
this.getDims = function () { // called by calcObjPositions when animation is started
this.imgx = this.img.width / 2;
this.imgy = this.img.height / 2;
}
}
Each of these objects is stored in an array called slides
.
In order to overlap the images appropriately, the drawObjs
function first sorts the slides
array in order of slides[i].scale
from smallest to largest, then draws the images starting with slides[0]
.
On $(document).ready()
I run an init
function that, among other things, adds an event listener to the canvas:
canvas = document.getElementById(canvas_id);
canvas.addEventListener('mousemove', mouse_handler, false);
The purpose of this handler is to determine where the mouse is and whether the mouse is over one of the slides (which will modify a <div>
on the page via jQuery).
Here's my problem -- I'm trying to figure out how to determine which slide the mouse is over at any given time. Essentially, I need code to fill in the following logic (most likely in the mouse_handler()
function):
// if (mouse is over a slide) {
// slideName = get .name of moused-over slide;
// } else {
// slideName = "";
// }
// $("#slideName").html(slideName);
Any thoughts?