---
title: "Discussion on \"JavaScript mouse drawing on the canvas 👨‍🎨\" | Hashnode"
description: "Discussion on \"JavaScript mouse drawing on the canvas 👨‍🎨\". Today I wanted to continue canvas explorations by checking out how to draw on the canvas with our mouse.\nIt turns out it's actually fairly simple and easy to implement!\nWe'll be building this cool drawing app. (Have a play around!)\nhttps://codepen.io..."
url: "https://hashnode.com/posts/javascript-mouse-drawing-on-the-canvas/5f7d5154eba2f86adc0b6465"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [JavaScript mouse drawing on the canvas 👨‍🎨](https://h.daily-dev-tips.com/javascript-mouse-drawing-on-the-canvas?utm_source=hashnode&utm_medium=feed)

Today I wanted to continue canvas explorations by checking out how to draw on the canvas with our mouse. It turns out it's actually fairly simple and easy to implement! We'll be building this cool drawing app. (Have a play around!) https://codepen.io...

Oct 7, 2020 · 3 min read · h.daily-dev-tips.com  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#html-canvas](https://hashnode.com/tag/html-canvas)
-   [#programming](https://hashnode.com/tag/programming)
-   [#2articles1week](https://hashnode.com/tag/2articles1week)
-   [#canvas](https://hashnode.com/tag/canvas)

## Written by

[Chris Bongers](https://hashnode.com/@dailydevtips)

@dailydevtips · Cape Town, SA

  
[View profile](https://hashnode.com/@dailydevtips)

Looking to get into development? As a full-stack developer I guide you on this journey and give you bite sized tips every single day 👊

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.

## Responses 3

Markdown supported · ⌘↵ to post

[Tapas Adhikary](https://hashnode.com/@atapas)

[Oct 7, 2020](https://hashnode.com/posts/javascript-mouse-drawing-on-the-canvas/5f7d5154eba2f86adc0b6465/comment/5f7dc39c94f39f6ab9fc0089)

Great article, [Chris Bongers](https://hashnode.com/@dailydevtips)!

[Chris Bongers](https://hashnode.com/@dailydevtips)

[Oct 7, 2020](https://hashnode.com/posts/javascript-mouse-drawing-on-the-canvas/5f7d5154eba2f86adc0b6465/comment/5f7dc39c94f39f6ab9fc0089#reply-5f7dc75feba2f86adc0b6a80)

Thanks Tapas! 🤟

[Abhishek Verma](https://hashnode.com/@vaultdmn)

[Oct 7, 2020](https://hashnode.com/posts/javascript-mouse-drawing-on-the-canvas/5f7d5154eba2f86adc0b6465/comment/5f7db7c7eba2f86adc0b69ad)

What about eraser?

[Chris Bongers](https://hashnode.com/@dailydevtips)

[Oct 7, 2020](https://hashnode.com/posts/javascript-mouse-drawing-on-the-canvas/5f7d5154eba2f86adc0b6465/comment/5f7db7c7eba2f86adc0b69ad#reply-5f7db883eba2f86adc0b69bb)

Good question, to actually remove the canvas lines is a bit harder a dirty solution would be to make an eraser button, which will draw a white line 👀

[Frendhi](https://hashnode.com/@fsdnr)

[Oct 7, 2020](https://hashnode.com/posts/javascript-mouse-drawing-on-the-canvas/5f7d5154eba2f86adc0b6465/comment/5f7d8e2494f39f6ab9fbfdf3)

Great article! Thanks! I've made the "fixed-width" version, by setting the canvas size to copy it's parent wrapper's size.

```
  ctx.canvas.width = canvas.offsetWidth;
  ctx.canvas.height = canvas.offsetHeight;
```

[codepen.io/frendhisaido\_1470415284/pen/dyXypKB](https://codepen.io/frendhisaido_1470415284/pen/dyXypKB)

But I'm still not sure why if the canvas is inside any parent that has a relative position, the mouse draw precision will be off by a few pixels.

[Chris Bongers](https://hashnode.com/@dailydevtips)

[Oct 7, 2020](https://hashnode.com/posts/javascript-mouse-drawing-on-the-canvas/5f7d5154eba2f86adc0b6465/comment/5f7d8e2494f39f6ab9fbfdf3#reply-5f7da6e2eba2f86adc0b68ea)

Awesome, yes that's the way to go, you mean the line is 1px ahead of your cursor? I think it's because the line is thick, so it sets the pointer inside the line.
