|
| 1 | +// Variables for canvas |
| 2 | +const canvas = document.getElementById("whiteboard"); |
| 3 | +const ctx = canvas.getContext("2d"); |
| 4 | + |
| 5 | +// Set canvas to fill available space dynamically |
| 6 | +function resizeCanvas() { |
| 7 | + canvas.width = canvas.parentElement.offsetWidth; |
| 8 | + canvas.height = canvas.parentElement.offsetHeight; |
| 9 | +} |
| 10 | +resizeCanvas(); |
| 11 | +window.addEventListener("resize", resizeCanvas); |
| 12 | + |
| 13 | +// Variables for drawing |
| 14 | +let isDrawing = false; |
| 15 | +let brushColor = "#000000"; |
| 16 | +let brushSize = 10; |
| 17 | + |
| 18 | +// Event listeners for drawing |
| 19 | +canvas.addEventListener("mousedown", startDrawing); |
| 20 | +canvas.addEventListener("mousemove", draw); |
| 21 | +canvas.addEventListener("mouseup", stopDrawing); |
| 22 | +canvas.addEventListener("mouseout", stopDrawing); |
| 23 | + |
| 24 | +// Event listeners for controls |
| 25 | +document.getElementById("color").addEventListener("input", (e) => { |
| 26 | + brushColor = e.target.value; |
| 27 | +}); |
| 28 | + |
| 29 | +document.getElementById("size").addEventListener("input", (e) => { |
| 30 | + const value = parseInt(e.target.value, 10); |
| 31 | + if (value >= 2 && value <= 50) { |
| 32 | + brushSize = value; |
| 33 | + } |
| 34 | +}); |
| 35 | + |
| 36 | +document.getElementById("clear").addEventListener("click", clearCanvas); |
| 37 | + |
| 38 | +document.getElementById("save").addEventListener("click", saveImage); |
| 39 | + |
| 40 | +document.getElementById("upload").addEventListener("change", uploadImage); |
| 41 | + |
| 42 | +document.getElementById("fullscreen").addEventListener("click", () => { |
| 43 | + document.documentElement.requestFullscreen(); |
| 44 | +}); |
| 45 | + |
| 46 | +// Drawing functions |
| 47 | +function startDrawing(e) { |
| 48 | + isDrawing = true; |
| 49 | + ctx.beginPath(); |
| 50 | + ctx.moveTo(e.offsetX, e.offsetY); |
| 51 | +} |
| 52 | + |
| 53 | +function draw(e) { |
| 54 | + if (!isDrawing) return; |
| 55 | + |
| 56 | + ctx.lineWidth = brushSize; |
| 57 | + ctx.lineCap = "round"; |
| 58 | + ctx.strokeStyle = brushColor; |
| 59 | + |
| 60 | + ctx.lineTo(e.offsetX, e.offsetY); |
| 61 | + ctx.stroke(); |
| 62 | +} |
| 63 | + |
| 64 | +function stopDrawing() { |
| 65 | + isDrawing = false; |
| 66 | + ctx.closePath(); |
| 67 | +} |
| 68 | + |
| 69 | +// Clear the canvas |
| 70 | +function clearCanvas() { |
| 71 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 72 | +} |
| 73 | + |
| 74 | +// Save the canvas as an image |
| 75 | +function saveImage() { |
| 76 | + const link = document.createElement("a"); |
| 77 | + link.download = "whiteboard.png"; |
| 78 | + link.href = canvas.toDataURL("image/png"); |
| 79 | + link.click(); |
| 80 | +} |
| 81 | + |
| 82 | +// Upload an image to the canvas |
| 83 | +function uploadImage(e) { |
| 84 | + const file = e.target.files[0]; |
| 85 | + if (!file) return; |
| 86 | + |
| 87 | + const reader = new FileReader(); |
| 88 | + reader.onload = () => { |
| 89 | + const img = new Image(); |
| 90 | + img.onload = () => { |
| 91 | + ctx.drawImage(img, 0, 0, canvas.width, canvas.height); |
| 92 | + }; |
| 93 | + img.src = reader.result; |
| 94 | + }; |
| 95 | + reader.readAsDataURL(file); |
| 96 | +} |
0 commit comments