To add click interactions to HTML canvas elements, I went ahead and started us off with a canvas that has a height and width of 200, and then gave us a display co-ords class div that is invisible, but it's down there, I promise.
I'm also coloring our fill style the context of pink and drawing it as a rectangle with the same width and height of our canvas.
To get started, let's call out our canvas and an event listener. That event listener will be on-click. I'm going to call the method getCo-ords. We now need to create that getCo-ords method.
We'll say const getCo-ords is equal to. We're going to pass it the click event. We're all on board? Awesome. Now, inside of our getCo-ords function, we want to go ahead and create two variables.
The first is going to be the x variable, and the second is going to be the y variable. With our click event, we should be able to say event.client, and then it has two things that you see here, x and y. We'll set constx=event.clientx, and consty=event.clienty.
Now, for the final piece, to start seeing something on the screen, we're going to grab that co-ords variable that we create above. We'll say co-ords.textContent, and set that equal to -- now follow along closely, back tick, back tick. Inside here, we'll do a little string interpolation.
We want to go ahead and pass it the x variable and the y variable that we just created. On screen, we should see number, comma, number. Save, refresh, click. Look at that gorgeousness.
If I click on the top left, we might expect to get 00However, I'm getting closer to something like 10,120 when I'm over here. This is because the event.clientx and event.clienty are not grabbing co-ordinates relative to the square here. They're grabbing it relative to the entire page.
At this point when I clicked, I was 12 pixels over from the left side of the screen, and I was 120 pixels down from the top side of the screen.
If we're wanting to build a color picker, or for some reason, we want to print out cool numbers that are relative to the square we're clicking on, this would not work. We need actual co-ordinates to the square.
The next step is going to be creating another variable in here. I like to call it container because that makes logical sense to me, but you can call it whatever you'd like. We'll say canvas.getBoundingclientrect. Boundingclientrect is going to give us access to some pretty cool stuff.
Inside that constx variable that we just created, I'm going to subtract the event.clientx from the container.left, left side, and the y, as you may or may not have guessed, I'm going to subtract from the top, container.top.
If we go back and refresh, click in the top right-ish area, see we're getting much closer to 00If I could actually click on 00you would see that. The bottom right, we are close to 200,200, which if you remember is the width and height of the canvas container, and also the fill rect right here, canvas width, height.
We're achieving now co-ordinates that are based only off of this square that we're clicking in, which is wonderful. That's great.
However, sometimes whenever you're building things like color pickers, you do need, whenever you click in the very center of this object, this element here, you need that to be 00and you need this to be 100,100, and this to be -100,-100 like an actual co-ordinate plane.
The next step in achieving something like that is a little bit more math. We'll surround these guys with parenthesis. We're going to use container again. We're going to say subtract that constx that we just created from the container's width divided by 2.
We're going to do the same thing for our y variable, but instead of width, we're going to do height. Go, refresh. Now, when I click in the center-ish area, we should get close to 00right, close to 100,100. Then over here, -100,-100.
Of course, as any good co-ordinate plane does, if you're in the top, it's a positive number followed by a negative number. If you are on the bottom left, it's a negative number followed by a positive number.
Voila. Don't you just love the math?