Drawing Canvas & Paint App

Interactive HTML5 Canvas whiteboard with custom stroke sizes, color pickers, eraser mode, undo, and PNG artwork export.

Difficulty: Beginner Est. Time: 25 mins Vanilla JS (ES6+) Includes HTML, CSS, JS & README (.ZIP)
Live UI Runner

Drawing Canvas & Paint App

live-sandbox://drawing-canvas

1. Project Overview & Features

The Drawing Canvas & Paint App is an essential frontend project designed to help you bridge the gap between learning JavaScript syntax and building interactive, responsive web applications.

Key Features Implemented:

  • Freehand sketch drawing with HTML5 Canvas
  • Brush color picker and size slider (2px to 40px)
  • Eraser mode and clear canvas button
  • Download artwork directly as PNG image

2. Step-by-Step Code Walkthrough

A Step 1: Semantic HTML Structure

We structure the interface using clean, semantic HTML elements. This ensures maximum accessibility and provides intuitive hooks for CSS classes and JavaScript query selectors.

HTML (index.html)
<div class="canvas-card">
  <div class="canvas-toolbar">
    <input type="color" id="brush-color" value="#8b5cf6" title="Brush Color">
    <div class="tool-group">
      <label>Size: <span id="size-val">5</span>px</label>
      <input type="range" id="brush-size" min="1" max="40" value="5">
    </div>
    <button id="eraser-btn" class="tool-btn" title="Eraser"><i class="fas fa-eraser"></i></button>
    <button id="clear-btn" class="tool-btn" title="Clear Canvas"><i class="fas fa-trash-alt"></i></button>
    <button id="download-canvas-btn" class="tool-btn primary" title="Save Artwork"><i class="fas fa-download"></i></button>
  </div>
  <canvas id="paint-canvas"></canvas>
</div>

B Step 2: Styling & Responsive CSS

Modern CSS flexbox and grid layouts are used to make the UI look polished, centered, and responsive across desktop, tablet, and mobile screens.

CSS (style.css)
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}
body {
  background: #0f172a;
  color: #f8fafc;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.canvas-card {
  background: #1e293b;
  border: 1px solid #334155;
  border-radius: 24px;
  padding: 20px;
  width: 100%;
  max-width: 540px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.4);
}
.canvas-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  background: #0f172a;
  padding: 10px 14px;
  border-radius: 14px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}
.canvas-toolbar input[type="color"] {
  border: none;
  background: none;
  width: 32px;
  height: 32px;
  cursor: pointer;
}
.tool-group {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #94a3b8;
  font-size: 0.85rem;
  flex: 1;
}
.tool-group input[type="range"] {
  accent-color: #8b5cf6;
}
.tool-btn {
  background: #334155;
  border: none;
  color: #f8fafc;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.tool-btn.active { background: #8b5cf6; }
.tool-btn.primary { background: #10b981; }
#paint-canvas {
  background: #ffffff;
  border-radius: 14px;
  width: 100%;
  height: 300px;
  display: block;
  cursor: crosshair;
}

C Step 3: JavaScript Logic & Event Handling

Here is the complete JavaScript code handling the state, event listeners, mathematical logic, and dynamic DOM rendering:

JavaScript (script.js)
const canvas = document.getElementById('paint-canvas');
const ctx = canvas.getContext('2d');
const colorPicker = document.getElementById('brush-color');
const sizeSlider = document.getElementById('brush-size');
const sizeVal = document.getElementById('size-val');
const eraserBtn = document.getElementById('eraser-btn');
const clearBtn = document.getElementById('clear-btn');
const downloadBtn = document.getElementById('download-canvas-btn');

let isDrawing = false;
let isEraser = false;

function resizeCanvas() {
  canvas.width = canvas.offsetWidth;
  canvas.height = canvas.offsetHeight;
  ctx.fillStyle = '#ffffff';
  ctx.fillRect(0, 0, canvas.width, canvas.height);
}
window.addEventListener('load', resizeCanvas);

function startDraw(e) {
  isDrawing = true;
  ctx.beginPath();
  draw(e);
}

function stopDraw() {
  isDrawing = false;
  ctx.beginPath();
}

function draw(e) {
  if (!isDrawing) return;
  const rect = canvas.getBoundingClientRect();
  const x = e.clientX - rect.left;
  const y = e.clientY - rect.top;

  ctx.lineWidth = sizeSlider.value;
  ctx.lineCap = 'round';
  ctx.strokeStyle = isEraser ? '#ffffff' : colorPicker.value;

  ctx.lineTo(x, y);
  ctx.stroke();
  ctx.beginPath();
  ctx.moveTo(x, y);
}

canvas.addEventListener('mousedown', startDraw);
canvas.addEventListener('mouseup', stopDraw);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseleave', stopDraw);

sizeSlider.addEventListener('input', (e) => {
  sizeVal.textContent = e.target.value;
});

eraserBtn.addEventListener('click', () => {
  isEraser = !isEraser;
  eraserBtn.classList.toggle('active', isEraser);
});

clearBtn.addEventListener('click', () => {
  ctx.fillStyle = '#ffffff';
  ctx.fillRect(0, 0, canvas.width, canvas.height);
});

downloadBtn.addEventListener('click', () => {
  const link = document.createElement('a');
  link.download = 'my-drawing.png';
  link.href = canvas.toDataURL();
  link.click();
});

3. Core JavaScript Concepts You Learned

HTML5 Canvas 2D Context

Crucial concept utilized to power the dynamic state, user interactions, and styling of this project.

Mouse & Touch Events

Crucial concept utilized to power the dynamic state, user interactions, and styling of this project.

Canvas Image Export (toDataURL)

Crucial concept utilized to power the dynamic state, user interactions, and styling of this project.

Stroke State Management

Crucial concept utilized to power the dynamic state, user interactions, and styling of this project.

4. How to Run Locally on Your Computer

  1. Download the project ZIP: Click the Download Project (.ZIP) button at the top or in the live runner to save drawing-canvas-project.zip.
  2. Extract the archive: Unzip the downloaded file. Inside you will find separate index.html, style.css, script.js, index-standalone.html, and a comprehensive README.md.
  3. Open in any browser: Simply double-click index.html (or index-standalone.html) to open and test immediately in Chrome, Firefox, Safari, or Edge.
  4. Edit in Visual Studio Code: Open the extracted folder in VS Code. Right-click index.html and select "Open with Live Server" to enjoy instant live reloading as you modify HTML, CSS, or JS!

Bonus Challenges for Learners

Ready to level up? Try adding these extra features on your own:

  • Add sound effects or audio feedback for user button clicks.
  • Add custom theme color customizer with CSS variables.
  • Export the results to PDF or copy as a formatted text summary.

Other Beginner Projects You Might Like

Beginner30 mins
Math & Calculators

Interactive Calculator

Build a sleek, fully functional calculator with arithmetic operations, decimal support, backspace, and calculation history.

DOM ManipulationEvent Listenerseval & Math Logic+2
Beginner25 mins
Productivity & UI

Smart To-Do List with LocalStorage

A full-featured task manager with local storage persistence, filtering (All, Active, Completed), task counter, and priority levels.

Safe LocalStorage APIArray Methods (map, filter, unshift)Event Delegation & Keyboard Triggers+2
Beginner20 mins
Timers & Clocks

Digital & Analog Dual Clock

A dynamic dual-mode clock with synchronized analog ticking hands, 12h/24h digital time, date formatting, and light/dark theme switch.

setInterval & requestAnimationFrameDate Object MethodsCSS transform: rotate()+2