BCA Student Stories & UpdatesLive
Building an Interactive Digital Signature & Sketch Pad (Canvas Mouse & Touch Events)
Digital signatures are standard in modern enterprise web applications (e-banking, delivery verification, university document approvals). In this project, students bind mouse and pointer events to canvas drawing routines, recording fluid handwriting strokes and exporting the resulting image as base64 PNG data.
Day 43 Step-by-Step Practical Flow
šÆ1. What You Will Learn Today
Building an Interactive Digital Signature & Sketch Pad (Canvas Mouse & Touch Events) ā Comprehensive Practical Guide
Har concept ko visual mental models, code anatomy aur practical industry tips ke sath samjhein.
Digital signatures are standard in modern enterprise web applications (e-banking, delivery verification, university document approvals). In this project, students bind mouse and pointer events to canvas drawing routines, recording fluid handwriting strokes and exporting the resulting image as base64 PNG data.
"Imagine holding a felt-tip pen over a sheet of paper. When you press the pen down (mousedown), isDrawing becomes true. As your hand moves across the surface (mousemove), ink flows. When you lift the pen up (mouseup), ink stops."
šØāš«3. Teacher Demonstration Code
See how freehand drawing is captured stroke-by-stroke and exported as a clean transparent image file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Signature Pad Demo</title>
<style>
body { font-family: system-ui, sans-serif; background: #060c18; color: #fff; padding: 20px; text-align: center; }
canvas { background: #ffffff; border-radius: 8px; cursor: crosshair; touch-action: none; }
.toolbar { margin: 12px auto; display: flex; justify-content: center; gap: 10px; align-items: center; }
button { padding: 8px 16px; border-radius: 6px; font-weight: bold; border: none; cursor: pointer; }
</style>
</head>
<body>
<h3>āļø Digital Signature Pad</h3>
<canvas id="pad" width="450" height="200"></canvas>
<div class="toolbar">
<input type="color" id="penColor" value="#000000" />
<input type="range" id="penSize" min="1" max="10" value="2" />
<button id="clearBtn" style="background:#dc2626;color:#fff;">Clear</button>
<button id="saveBtn" style="background:#2563eb;color:#fff;">Save PNG</button>
</div>
<script>
const canvas = document.getElementById("pad");
const ctx = canvas.getContext("2d");
let isDrawing = false;
ctx.lineCap = "round";
ctx.lineJoin = "round";
function getCoords(e) {
const rect = canvas.getBoundingClientRect();
return { x: e.clientX - rect.left, y: e.clientY - rect.top };
}
canvas.addEventListener("mousedown", (e) => {
isDrawing = true;
const { x, y } = getCoords(e);
ctx.beginPath();
ctx.moveTo(x, y);
});
canvas.addEventListener("mousemove", (e) => {
if (!isDrawing) return;
const { x, y } = getCoords(e);
ctx.strokeStyle = document.getElementById("penColor").value;
ctx.lineWidth = document.getElementById("penSize").value;
ctx.lineTo(x, y);
ctx.stroke();
});
window.addEventListener("mouseup", () => { isDrawing = false; });
document.getElementById("clearBtn").addEventListener("click", () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
});
document.getElementById("saveBtn").addEventListener("click", () => {
const url = canvas.toDataURL("image/png");
const a = document.createElement("a");
a.href = url;
a.download = "signature.png";
a.click();
});
</script>
</body>
</html>2. Hands-On Practical Lab: Follow Instructions in VS Code
Code like a professional software engineer! Follow the step-by-step instructions below in Visual Studio Code (VS Code) on your PC, test your output locally, and push to GitHub.
Day 43 Practical Task Test Studio
Configure the 2D context lineCap and lineJoin properties to "round" for smooth handwriting rendering.
Calculate mouse coordinates relative to canvas using getBoundingClientRect.
Call canvas.toDataURL("image/png") to generate a downloadable base64 image.
Spot the Bug: Runaway Line Sticking to Cursor After Mouse Leave
Tap or click on the exact line below that contains the error:
Click any code linecanvas.addEventListener("mousedown", () => { isDrawing = true; });canvas.addEventListener("mouseup", () => { isDrawing = false; });// If cursor leaves the canvas, mouseup is never caught by canvas!Senior Developer Forensic Analysis:
Attaching the mouseup listener solely to the canvas element misses release events if the user drags the mouse outside the canvas boundaries before letting go. Attaching mouseup to the global window ensures isDrawing is always reset.
šIndependent Practice: Assignment
š index.htmlUniversity Student Verification Signature & Whiteboard Studio
- ā¢Create a responsive drawing surface with white canvas background on a dark platform theme.
- ā¢Include a controls dock: color palette buttons (Black, Blue, Crimson, Emerald), line thickness slider (1-8px), and an eraser mode.
- ā¢Implement the canvas.toDataURL() download trigger that saves the signature file named bca-signature.png.
- ā¢Display a live image preview card beneath the canvas immediately when the user clicks "Confirm Signature".
Open Visual Studio Code on your PC (code .), create index.html, paste this complete step-by-step verified code, test in your browser, and push to your GitHub repo:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BCA Day 43 - Signature Studio</title>
<style>
body { font-family: "Segoe UI", sans-serif; background: #060c18; color: #fff; padding: 24px; text-align: center; }
.card { background: #0c1328; border: 1px solid #1e293b; padding: 24px; border-radius: 12px; max-width: 550px; margin: auto; }
canvas { background: #ffffff; border-radius: 8px; cursor: crosshair; touch-action: none; border: 2px dashed #38bdf8; }
.toolbar { display: flex; justify-content: center; gap: 8px; align-items: center; margin: 16px 0; }
button { padding: 8px 16px; border-radius: 6px; font-weight: bold; border: none; cursor: pointer; }
.btn-action { background: #2563eb; color: #fff; }
</style>
</head>
<body>
<div class="card">
<h2>āļø Student Verification Signature Pad</h2>
<canvas id="sigCanvas" width="480" height="220"></canvas>
<div class="toolbar">
<input type="color" id="strokeColor" value="#000000" />
<input type="range" id="strokeWidth" min="1" max="8" value="2" />
<button id="clearBtn" style="background:#dc2626;color:#fff;">Clear</button>
<button id="confirmBtn" class="btn-action">Download PNG</button>
</div>
</div>
<script>
const canvas = document.getElementById("sigCanvas");
const ctx = canvas.getContext("2d");
let isDrawing = false;
ctx.lineCap = "round";
ctx.lineJoin = "round";
function getPos(e) {
const r = canvas.getBoundingClientRect();
return { x: e.clientX - r.left, y: e.clientY - r.top };
}
canvas.addEventListener("mousedown", (e) => {
isDrawing = true;
const pos = getPos(e);
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
});
canvas.addEventListener("mousemove", (e) => {
if (!isDrawing) return;
const pos = getPos(e);
ctx.strokeStyle = document.getElementById("strokeColor").value;
ctx.lineWidth = document.getElementById("strokeWidth").value;
ctx.lineTo(pos.x, pos.y);
ctx.stroke();
});
window.addEventListener("mouseup", () => { isDrawing = false; });
document.getElementById("clearBtn").addEventListener("click", () => ctx.clearRect(0, 0, canvas.width, canvas.height));
document.getElementById("confirmBtn").addEventListener("click", () => {
const a = document.createElement("a");
a.href = canvas.toDataURL("image/png");
a.download = "bca-signature.png";
a.click();
});
</script>
</body>
</html>š4. Viva Check: Mandatory Conceptual Test
What is the purpose of CSS touch-action: none on a drawing canvas on mobile devices?
5. Mandatory Step: Push to GitHub to Unlock Next Lesson
Target Repo: html-css-projects
Exact GitHub Setup Guide & Expected Structure
Day 43 RequirementsFollow these exact folder and file names to prevent verification errors.
index.html: Avoid spaces or custom names like Alan Turing..html or Index.HTML. Linux/GitHub servers are strictly case-sensitive.day-43/ for today so you don't overwrite previous days.main.You are welcome to read all notes, inspect code snippets, and practice all exercises of Day 43 freely! To officially submit your GitHub repository and unlock subsequent days in your portfolio, please complete Day 42: HTML5 2D Canvas API Fundamentals (Coordinates, Shapes & Paths) first.
Complete Day 42 First