D
D
dpablo_escobarr2020-11-01 12:09:17
JavaScript
dpablo_escobarr, 2020-11-01 12:09:17

How to implement a diamond strategy map in Canvas, keeping selection of squares on hover?

I don’t understand how to make it so that after the canvas context is converted by functions rotate(); translate();
, the coordinates of each superimposed square drawImage()
are shifted by the picture through, and it’s no longer possible to catch the coordinates relative to the Canvas itself. It turns out that the cursor, when hovering over the Canvas, has some coordinates, while the shifted squares have others.

How can this idea be realized? There is an option through diamond-shaped png images, but after adding them from the function drawImage()

they still turn out to be square, and on the canvas, when you hover over such an image, the event does not work correctly.

I want to get from this (everything works correctly)

5f9e7ab95a9c8651803043.jpeg

this is
5f9e7af47a15a883077138.jpeg

applied here

ctx.rotate(45 * Math.PI / 180);
      ctx.translate(200, -250);

and, accordingly, the coordinates when hovering over the square of the tile, the wrong one is selected. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-11-01
@dpablo_escobarr

What you are looking for is called isometry. See examples: 1 , 2 . You can check which tile you are in using isPointInPath (and create the tiles themselves using Path2D ).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question