Categories
JavaScript Answers

How to get distance between two points in canvas with JavaScript?

Spread the love

Sometimes, we want to get distance between two points in canvas with JavaScript.

In this article, we’ll look at how to get distance between two points in canvas with JavaScript.

How to get distance between two points in canvas with JavaScript?

To get distance between two points in canvas with JavaScript, we can use the Math.hypot method.

For instance, we write

const distance = Math.hypot(endX - startX, endY - startY);

to call Math.hypot with difference of the x and y coordinates between the start and end points.

We get the x-coordinate difference with endX - startX.

And we get the y-coordinate difference with endY - startY.

Conclusion

To get distance between two points in canvas with JavaScript, we can use the Math.hypot method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *