Below is a complete, self-contained index.html file that implements a simple interactive drawing tool using the HTML <canvas> element. It includes all necessary HTML structure, CSS for layout, and JavaScript for functionality.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Drawing Tool</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f4f4f9;
}
canvas {
border: 2px solid #333;
background-color: #fff;
cursor: crosshair;
}
.controls {
margin-top: 10px;
display: flex;
gap: 10px;
}
.color-swatch {
width: 30px;
height: 30px;
border: 2px solid #000;
border-radius: 50%;
cursor: pointer;
}
.color-swatch.red { background-color: red; }
.color-swatch.blue { background-color: blue; }
.color-swatch.green { background-color: green; }
button {
padding: 5px 10px;
font-size: 16px;
cursor: pointer;
border: none;
border-radius: 5px;
background-color: #007bff;
color: white;
}
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h1>Simple Drawing Tool</h1>
<canvas id="drawingCanvas" width="600" height="400"></canvas>
<div class="controls">
<div class="color-swatch red" data-color="red"></div>
<div class="color-swatch blue" data-color="blue"></div>
<div class="color-swatch green" data-color="green"></div>
<button id="clearButton">Clear</button>
</div>
<script>
const canvas = document.getElementById('drawingCanvas');
const ctx = canvas.getContext('2d');
let isDrawing = false;
let currentColor = 'black';
// Set up drawing logic
canvas.addEventListener('mousedown', (e) => {
isDrawing = true;
ctx.beginPath();
ctx.strokeStyle = currentColor;
ctx.lineWidth = 2;
ctx.moveTo(e.offsetX, e.offsetY);
});
canvas.addEventListener('mousemove', (e) => {
if (isDrawing) {
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
}
});
canvas.addEventListener('mouseup', () => {
isDrawing = false;
ctx.closePath();
});
canvas.addEventListener('mouseleave', () => {
isDrawing = false;
});
// Change color when a color swatch is clicked
document.querySelectorAll('.color-swatch').forEach(swatch => {
swatch.addEventListener('click', () => {
currentColor = swatch.dataset.color;
});
});
// Clear the canvas when the "Clear" button is clicked
document.getElementById('clearButton').addEventListener('click', () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
});
</script>
</body>
</html>
Explanation of Features:
-
Canvas Element:
- A
<canvas> element is used as the drawing area with a fixed size of 600x400 pixels.
- The
border style makes it visually distinct.
-
Drawing Logic:
- Mouse events (
mousedown, mousemove, mouseup, mouseleave) are used to handle drawing.
- When the mouse is pressed (
mousedown), a path begins. As the mouse moves (mousemove), lines are drawn. The path ends when the mouse is released (mouseup).
-
Color Selection:
- Three color swatches (red, blue, green) are provided as clickable elements.
- Clicking a swatch updates the
currentColor variable, which determines the stroke color for subsequent drawings.
-
Clear Button:
- A "Clear" button is included to reset the canvas by clearing all drawn paths.
-
Styling:
- Basic CSS ensures the layout is clean and user-friendly, with styled buttons and circular color swatches.
This file is fully self-contained and can be run in any modern web browser without requiring external resources.