Home > Web Front-end > HTML Tutorial > How to use multiple click events on HTML5 canvas?

How to use multiple click events on HTML5 canvas?

WBOY
Release: 2023-08-28 13:13:12
forward
776 people have browsed it

How to use multiple click events on HTML5 canvas?

When a circle is drawn on the canvas and half of it is painted red and part of the circle is painted gray, when red is clicked, we call function 1.

When the gray part is clicked, function 2 is called, and we need to use a reusable path object to store the different parts we want to test. Click handlers can be used to share the canvas and do what we want. Path information can be stored using Path2D objects.

var path1 = new Path2D();
var path2 = new Path2D();

var newpaths = [path1,path 2];   // Array is needed to store paths

path1.arc(200, 85,650, -0.2 * Math.PI, 2.7 * Math.PI);  // Path for red part
path2.arc(200, 85, 60, 2.7 * Math.PI, -1.1 * Math.PI);  //Path for  grey part


// Two path objects are rendered  using a common context ctx1, but with different style
ctx1.lineWidth = 16;
ctx1.strokeStyle = "#d43030";
ctx1.stroke(path1);
ctx1.strokeStyle = "#b8b8b8";
ctx1.stroke(path2);
Copy after login

Then check for clicks on the common canvas using the x and y axes

Then iterate over the array of paths to test for clicks on each path.

<canvas id = "myCanvas1"></canvas> // Then it is attached with corresponding canvas.
Copy after login

The above is the detailed content of How to use multiple click events on HTML5 canvas?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template