Here's an improved version of the drawing tool with better functionality and error handling:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Drawing Tool</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 20px;
min-height: 100vh;
}
.canvas-container {
position: relative;
margin: 20px;
}
#canvas {
border: 2px solid #333;
background-color: white;
cursor: crosshair;
touch-action: none; /* Prevents scrolling on touch devices */
}
.controls {
display: flex;
gap: 10px;
align-items: center;
padding: 10px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.color-btn {
width: 40px;
height: 40px;
border: 3px solid #333;
border-radius: 50%;
cursor: pointer;
transition: transform 0.2s;
}
.color-btn:hover {
transform: scale(1.1);
}
.color-btn.active {
border-color: #fff;
box-shadow: 0 0 0 3px #333;
}
#clear-btn {
padding: 10px 20px;
font-size: 16px;
background-color: #ff3333;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.2s;
}
#clear-btn:hover {
background-color: #cc0000;
}
.thickness-control {
display: flex;
align-items: center;
gap: 10px;
}
#thickness {
width: 100px;
}
</style>
</head>
<body>
<div class="canvas-container">
<canvas id="canvas" width="800" height="600"></canvas>
</div>
<div class="controls">
<div class="color-btn" style="background-color: #000000" data-color="#000000"></div>
<div class="color-btn" style="background-color: #ff0000" data-color="#ff0000"></div>
<div class="color-btn" style="background-color: #0000ff" data-color="#0000ff"></div>
<div class="color-btn" style="background-color: #00ff00" data-color="#00ff00"></div>
<div class="thickness-control">
<label for="thickness">Line Width:</label>
<input type="range" id="thickness" min="1" max="20" value="2">
</div>
<button id="clear-btn">Clear Canvas</button>
</div>
<script>
// Get DOM elements
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const colorButtons = document.querySelectorAll('.color-btn');
const clearButton = document.getElementById('clear-btn');
const thicknessSlider = document.getElementById('thickness');
// Drawing state
let isDrawing = false;
let currentColor = '#000000';
let lastX = 0;
let lastY = 0;
// Initialize canvas context
function initCanvas() {
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
ctx.lineWidth = thicknessSlider.value;
// Set initial background to white
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
// Get correct mouse position
function getMousePos(e) {
const rect = canvas.getBoundingClientRect();
return {
x: e.clientX - rect.left,
y: e.clientY - rect.top
};
}
// Drawing functions
function startDrawing(e) {
isDrawing = true;
const pos = getMousePos(e);
[lastX, lastY] = [pos.x, pos.y];
}
function draw(e) {
if (!isDrawing) return;
e.preventDefault();
const pos = getMousePos(e);
ctx.beginPath();
ctx.strokeStyle = currentColor;
ctx.lineWidth = thicknessSlider.value;
ctx.moveTo(lastX, lastY);
ctx.lineTo(pos.x, pos.y);
ctx.stroke();
[lastX, lastY] = [pos.x, pos.y];
}
function stopDrawing() {
isDrawing = false;
}
// Event listeners
function addEventListeners() {
// Mouse events
canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseout', stopDrawing);
// Touch events
canvas.addEventListener('touchstart', (e) => {
e.preventDefault();
const touch = e.touches[0];
const mouseEvent = new MouseEvent('mousedown', {
clientX: touch.clientX,
clientY: touch.clientY
});
canvas.dispatchEvent(mouseEvent);
});
canvas.addEventListener('touchmove', (e) => {
e.preventDefault();
const touch = e.touches[0];
const mouseEvent = new MouseEvent('mousemove', {
clientX: touch.clientX,
clientY: touch.clientY
});
canvas.dispatchEvent(mouseEvent);
});
canvas.addEventListener('touchend', (e) => {
const mouseEvent = new MouseEvent('mouseup', {});
canvas.dispatchEvent(mouseEvent);
});
// Color selection
colorButtons.forEach(button => {
button.addEventListener('click', () => {
colorButtons.forEach(btn => btn.classList.remove('active'));
button.classList.add('active');
currentColor = button.dataset.color;
});
});
// Clear canvas
clearButton.addEventListener('click', () => {
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
});
}
// Initialize the drawing tool
function init() {
initCanvas();
addEventListeners();
colorButtons[0].classList.add('active'); // Set initial active color
}
// Start the application
init();
</script>
</body>
</html>