Changed around line 1
+ const canvas = document.getElementById('whiteboard');
+ const ctx = canvas.getContext('2d');
+ let isDrawing = false;
+ let currentTool = 'pencil';
+ let currentColor = '#000000';
+
+ function resizeCanvas() {
+ canvas.width = canvas.offsetWidth;
+ canvas.height = canvas.offsetHeight;
+ }
+
+ window.addEventListener('resize', resizeCanvas);
+ resizeCanvas();
+
+ canvas.addEventListener('mousedown', startDrawing);
+ canvas.addEventListener('mouseup', stopDrawing);
+ canvas.addEventListener('mousemove', draw);
+
+ canvas.addEventListener('touchstart', startDrawing);
+ canvas.addEventListener('touchend', stopDrawing);
+ canvas.addEventListener('touchmove', draw);
+
+ document.getElementById('pencil').addEventListener('click', () => {
+ currentTool = 'pencil';
+ toggleActiveButton('pencil');
+ });
+
+ document.getElementById('eraser').addEventListener('click', () => {
+ currentTool = 'eraser';
+ toggleActiveButton('eraser');
+ });
+
+ document.getElementById('colorPicker').addEventListener('input', (e) => {
+ currentColor = e.target.value;
+ });
+
+ document.getElementById('clear').addEventListener('click', () => {
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ });
+
+ function startDrawing(e) {
+ isDrawing = true;
+ draw(e);
+ }
+
+ function stopDrawing() {
+ isDrawing = false;
+ ctx.beginPath();
+ }
+
+ function draw(e) {
+ if (!isDrawing) return;
+
+ const rect = canvas.getBoundingClientRect();
+ let x, y;
+
+ if (e.touches) {
+ x = e.touches[0].clientX - rect.left;
+ y = e.touches[0].clientY - rect.top;
+ } else {
+ x = e.clientX - rect.left;
+ y = e.clientY - rect.top;
+ }
+
+ ctx.lineWidth = 5;
+ ctx.lineCap = 'round';
+
+ if (currentTool === 'pencil') {
+ ctx.strokeStyle = currentColor;
+ } else if (currentTool === 'eraser') {
+ ctx.strokeStyle = '#ffffff';
+ }
+
+ ctx.lineTo(x, y);
+ ctx.stroke();
+ ctx.beginPath();
+ ctx.moveTo(x, y);
+ }
+
+ function toggleActiveButton(tool) {
+ document.querySelectorAll('.toolbar button').forEach(btn => {
+ btn.classList.remove('active');
+ });
+ document.getElementById(tool).classList.add('active');
+ }