JavaScript mouse drawing on the canvas π¨βπ¨

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Thanks Tapas! π€
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 π
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;
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.
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.
In this series we will get started with the canvas element and use JavaScript to make some amazing art.
My confession: I've never used canvas before this article. I just have a cool idea in my head, which needs Canvas, so why not document my explorations using the HTML canvas element. <canvas> is an HTML element which can be used to draw graphics via J...
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

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!)
The HTML could not be simpler, all we need is one big canvas.
<canvas id="canvas"></canvas>
As for our styling, all we need to do is remove our default margin, create a cool emoji cursor, and set the width/height to be the same size as the viewport.
* {
margin: 0;
padding: 0;
cursor:url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='40' height='48' viewport='0 0 100 100' style='fill:black;font-size:24px;'><text y='50%'>βοΈ</text></svg>") 16 0,auto;
}
canvas {
width: 100vw;
height: 100vh;
}
Now on to the fun part, the JavaScript to connect our mouse movements to the canvas.
Let's start by defining our variables.
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
let coord = { x: 0, y: 0 };
We need the canvas, and retrieve it based on it's ID. Then we get the context (where we actually draw on)
Then we define our base coordinates.
Now let's attach listeners for:
document.addEventListener("mousedown", start);
document.addEventListener("mouseup", stop);
window.addEventListener("resize", resize);
Let's start with the resize function, this function will resize the canvas based on our viewport. It will make the canvas 100% or the width and height.
We also call this function right away.
function resize() {
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
}
resize();
Let's define our mousedown (start) function.
function start(event) {
document.addEventListener("mousemove", draw);
reposition(event);
}
This function will invoke the listener for mousemove, so we don't have to keep listening to it. Then we call our reposition function, which will register our mouse position.
The reposition function will look like this:
function reposition(event) {
coord.x = event.clientX - canvas.offsetLeft;
coord.y = event.clientY - canvas.offsetTop;
}
On to the stop function.
function stop() {
document.removeEventListener("mousemove", draw);
}
We only need to stop listening to our register mousemove function.
The last function we will make is the draw. This actually will create lines on the canvas.
function draw(event) {
ctx.beginPath();
ctx.lineWidth = 5;
ctx.lineCap = "round";
ctx.strokeStyle = "#ACD3ED";
ctx.moveTo(coord.x, coord.y);
reposition(event);
ctx.lineTo(coord.x, coord.y);
ctx.stroke();
}
In order:
That's it. We can now draw lines on the canvas.
If you want to read more about canvas, check out these articles.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter