Introduction
Feeling artistic or need to draw a masterpiece? With Blazor, you can now create your own digital canvas right there in your browser. In this article, we'll walk through the steps of building a signature/ paint app.

What you just witnessed in the demo above is made possible through Canvas. But what exactly is Canvas? It's an <HTML> element that allows dynamic graphics and brings drawing capabilities directly within the browser. So, let your inner artist out and get started.
Articles sections
Let's break down this article into four sections
- Canvas
- Color Picker
- Save Button
- Reset Button
1. Canvas
Alright, let's start by dropping a <canvas> element into our Blazor component. We'll also need to create a reference for it to play around with in JavaScript. You can hook up a reference for any HTML element using the @ref attribute.
<canvas @ref="canvas"></canvas>
@code {
private ElementReference canvas;
}
Code snippet 1. Canvas element
Next, we'll override the OnAfterRenderAsync method to ensure that the canvas is fully rendered on the browser before we proceed with any JavaScript operations. Here, we're calling a JavaScript funtion initializeCanvas. This function initializes our canvas for drawing.
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JSRuntime.InvokeVoidAsync("initializeCanvas", canvas, selectedColor);
}
}
Code snippet 2. OnAfterRenderAsync
Now, let's set up a JavaScript file to host this function. So, go ahead and drop a fresh JS file into the mix, naming it whatever suits your vibe, like "Signature.js". Just make sure to stash it under the wwwroot folder. Then, go over to Index.razor and add a <script> tag under the <head> tag, like this.
<script src="signature.js" type="text/javascript"></script>
Code snippet 3. JavaScript script tag
InitializeCanvas JS function
Now, let's add the "initializeCanvas" function to JavaScript.
Its purpose? Well, it's all about enabling users to create signatures or objects on the canvas. When the user presses down the mouse and drags, a line is drawn. Then, once the user releases the mouse, the line drawing is complete.

initializeCanvas = (canvas) => {
const ctx = canvas.getContext('2d');
canvas.addEventListener('mousedown', (e) => {
ctx.beginPath();
isMouseDown = true;
});
canvas.addEventListener('mousemove', (e) => {
if (isMouseDown) {
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
ctx.lineTo(x, y);
ctx.stroke();
}
});
canvas.addEventListener('mouseup', (e) => {
isMouseDown = false;
});
};
Code snippet 4. JS method initializeCanvas
Explanation of code snippet 4: First up, we grab hold of the 2D pane using the getContext() method. Then, we toss in a mousedown listener to catch that initial click on the canvas. As the mouse moves, we track its coordinates relative to the canvas with the mouse move listener. With this intel, we call the lineTo() function to sketch out the line, followed by we call the stroke() function to add the color. Finally, we make use of the mouseup listener to figure out if the mouse has gone up; with all that, we wrap up drawing a line. Smooth, huh?
2. Color picker
let's shift our focus to enhancing our lines with colors.

Returning to our Razor file, simply add <input> tag and set its type to "color".
<input type="color"
@bind="selectedColor"
@oninput="ChangeColor" />
Code snippet 5. Color input tag
We've applied two important attributes here
- @bind="selectedColor": This attribute establishes a two-way data binding to the "selectedColor" property. In simple terms, any changes made to the color picker will automatically update the "selected color" property and vice versa.
- @oninput="ChangeColor": This attribute specifies the event handler method to be called when the input value changes.
private string selectedColor = "#000000"; // Default color is black private async Task ChangeColor(ChangeEventArgs e) { selectedColor = e.Value.ToString(); await JSRuntime.InvokeVoidAsync("changeCanvasColor", canvas, selectedColor); }





Join the conversation! Your thoughts help the community grow.