<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/make-a-circle-follow-the-mouse-cursor-in-javascript-gsdugfcgn" -->

---
title: Make a Circle Follow the Mouse Cursor in JavaScript
description: To make a circle follow the mouse on an HTML canvas, store the pointer position in a move handler and redraw the circle in a requestAnimationFrame loop....
canonical: https://daily.dev/posts/make-a-circle-follow-the-mouse-cursor-in-javascript-gsdugfcgn
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Make a Circle Follow the Mouse Cursor in JavaScript | daily.dev
og:description: To make a circle follow the mouse on an HTML canvas, store the pointer position in a move handler and redraw the circle in a requestAnimationFrame loop....
og:url: https://daily.dev/posts/make-a-circle-follow-the-mouse-cursor-in-javascript-gsdugfcgn
og:image: https://api.daily.dev/og/posts/gsDUgFCGn.png
og:image:alt: Make a Circle Follow the Mouse Cursor in JavaScript
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Make a Circle Follow the Mouse Cursor in JavaScript

**[Kirupa](https://daily.dev/sources/kirupa)** · 17 min read · 0 upvotes · 0 comments

## Summary

To make a circle follow the mouse on an HTML canvas, store the pointer position in a move handler and redraw the circle in a requestAnimationFrame loop. Subtract the canvas position from clientX and clientY. Calling getBoundingClientRect inside the handler keeps that position correct after scrolling or resizing. Call context.clearRect at the top of each frame, or old circles pile up in a finger painting effect. Listening for pointermove and setting touch-action: none makes it work with fingers and pens too.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.kirupa.com/canvas/follow_mouse_cursor.htm>

## Questions this post answers

### Why is my mouse position wrong on an HTML canvas in JavaScript?

clientX and clientY are measured from the browser window's top-left corner, not the canvas. Subtract the canvas position from getBoundingClientRect, plus the border widths from clientLeft and clientTop. If CSS stretches the canvas, also multiply by canvas.width divided by canvas.clientWidth to convert screen pixels back to canvas pixels.

_Developers building canvas interactions follow this kind of JavaScript detail on daily.dev._

### Why does my canvas mouse position break after I scroll the page?

A position computed once at page load, such as with an offsetLeft-walking helper, goes stale when the canvas moves. Calling getBoundingClientRect inside the event handler returns the element's current position relative to the window every time, so scrolling and resizing no longer break the calculation.

_Developers debugging browser quirks like this keep up on daily.dev._

### Should I draw on the canvas inside the mousemove handler or use requestAnimationFrame?

Use requestAnimationFrame. The mousemove event does not know when the browser is ready to paint, so drawing inside it can force unnecessary paint work. Store the mouse position in the handler and draw in a requestAnimationFrame loop, which uses the latest position whenever the browser is ready to paint.

_Developers weighing rendering approaches follow canvas and web performance topics on daily.dev._

## Similar posts on daily.dev

- [Medium](https://daily.dev/posts/medium-b7ne44sxm) · Medium · 0 upvotes · 0 comments
- [Get the Mouse Click Position in JavaScript](https://daily.dev/posts/get-the-mouse-click-position-in-javascript-5oi8i3cwv) · Kirupa · 1 upvotes · 0 comments
- [Two Circles, One Arrow, and Anchor Positioning – Frontend Masters Blog](https://daily.dev/posts/two-circles-one-arrow-and-anchor-positioning-frontend-masters-blog-jstnigzbr) · Frontend Masters · 3 upvotes · 0 comments
- [Connecting Circles With Anchor Positioning](https://daily.dev/posts/connecting-circles-with-anchor-positioning-cpegf2ws0) · CSS Tip · 9 upvotes · 2 comments
- [Bouncing Square Animation With Random Colors in JavaScript](https://daily.dev/posts/bouncing-square-animation-with-random-colors-in-javascript-tmiq6kkev) · Kirupa · 3 upvotes · 0 comments

---

Tags: [#webdev](https://daily.dev/tags/webdev), [#javascript](https://daily.dev/tags/javascript), [#html](https://daily.dev/tags/html)

[View this post on daily.dev](https://daily.dev/posts/make-a-circle-follow-the-mouse-cursor-in-javascript-gsdugfcgn)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Make a Circle Follow the Mouse Cursor in JavaScript","url":"https://daily.dev/posts/make-a-circle-follow-the-mouse-cursor-in-javascript-gsdugfcgn","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/make-a-circle-follow-the-mouse-cursor-in-javascript-gsdugfcgn"},"datePublished":"2026-11-28T06:30:38.265Z","dateModified":"2026-11-28T06:31:02.992Z","description":"To make a circle follow the mouse on an HTML canvas, store the pointer position in a move handler and redraw the circle in a requestAnimationFrame loop....","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/caf87bee1a0cd64afc2f0cf47655bdd3?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/caf87bee1a0cd64afc2f0cf47655bdd3?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Kirupa","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Kirupa","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/ff2fdb4927d34b81bd07cd18d6049a98","url":"https://daily.dev/sources/kirupa"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/make-a-circle-follow-the-mouse-cursor-in-javascript-gsdugfcgn","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"webdev,javascript,html","timeRequired":"PT17M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Kirupa","item":"https://daily.dev/sources/kirupa"},{"@type":"ListItem","position":3,"name":"Make a Circle Follow the Mouse Cursor in JavaScript"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/make-a-circle-follow-the-mouse-cursor-in-javascript-gsdugfcgn#faq","mainEntity":[{"@type":"Question","name":"Why is my mouse position wrong on an HTML canvas in JavaScript?","acceptedAnswer":{"@type":"Answer","text":"clientX and clientY are measured from the browser window's top-left corner, not the canvas. Subtract the canvas position from getBoundingClientRect, plus the border widths from clientLeft and clientTop. If CSS stretches the canvas, also multiply by canvas.width divided by canvas.clientWidth to convert screen pixels back to canvas pixels. Developers building canvas interactions follow this kind of JavaScript detail on daily.dev."}},{"@type":"Question","name":"Why does my canvas mouse position break after I scroll the page?","acceptedAnswer":{"@type":"Answer","text":"A position computed once at page load, such as with an offsetLeft-walking helper, goes stale when the canvas moves. Calling getBoundingClientRect inside the event handler returns the element's current position relative to the window every time, so scrolling and resizing no longer break the calculation. Developers debugging browser quirks like this keep up on daily.dev."}},{"@type":"Question","name":"Should I draw on the canvas inside the mousemove handler or use requestAnimationFrame?","acceptedAnswer":{"@type":"Answer","text":"Use requestAnimationFrame. The mousemove event does not know when the browser is ready to paint, so drawing inside it can force unnecessary paint work. Store the mouse position in the handler and draw in a requestAnimationFrame loop, which uses the latest position whenever the browser is ready to paint. Developers weighing rendering approaches follow canvas and web performance topics on daily.dev."}}]}
```

