-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
213 lines (179 loc) · 5.83 KB
/
script.js
File metadata and controls
213 lines (179 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Canvas Setup
const canvas = document.getElementById("whiteboard");
const ctx = canvas.getContext("2d");
// Set initial canvas size
resizeCanvas();
// Listen for window resize events to adjust canvas size
window.addEventListener('resize', resizeCanvas);
// Function to resize the canvas
function resizeCanvas() {
// Save current content
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
// Update canvas size to match the container
canvas.width = canvas.parentElement.offsetWidth;
canvas.height = canvas.parentElement.offsetHeight;
// Restore the content after resizing
ctx.putImageData(imageData, 0, 0);
}
// Tool Variables
let isDrawing = false;
let currentTool = "pointer";
let penSize = 10;
let eraserSize = 10;
let brushColor = "#000000";
let startX = 0, startY = 0;
// Utility: Get the current brush size based on the tool
function getBrushSize() {
return currentTool === "eraser" ? eraserSize : penSize;
}
// Utility: Get event coordinates (mouse or touch)
function getEventCoordinates(e) {
if (e.touches) {
const touch = e.touches[0];
const rect = canvas.getBoundingClientRect();
return { x: touch.clientX - rect.left, y: touch.clientY - rect.top };
}
return { x: e.offsetX, y: e.offsetY };
}
// Set Active Tool
function setTool(tool) {
currentTool = tool;
// Highlight active tool
document.querySelectorAll("#side-menu button").forEach((btn) => {
btn.classList.toggle("active", btn.id === `${tool}-tool`);
});
// Show tool options dynamically
document.querySelectorAll(".tool-options").forEach((options) => {
options.classList.toggle("active", options.id === `${tool}-options`);
});
}
// Start Drawing
function startDrawing(e) {
if (currentTool === "pointer") return;
isDrawing = true;
const coords = getEventCoordinates(e);
startX = coords.x;
startY = coords.y;
if (currentTool === "pen" || currentTool === "eraser") {
ctx.beginPath();
ctx.moveTo(startX, startY);
}
e.preventDefault(); // Prevent scrolling on touch devices
}
// Draw on Canvas
function draw(e) {
if (!isDrawing) return;
const coords = getEventCoordinates(e);
ctx.lineWidth = getBrushSize();
if (currentTool === "pen") {
ctx.strokeStyle = brushColor;
ctx.lineTo(coords.x, coords.y);
ctx.stroke();
} else if (currentTool === "eraser") {
ctx.strokeStyle = "#ffffff";
ctx.lineTo(coords.x, coords.y);
ctx.stroke();
}
e.preventDefault(); // Prevent scrolling on touch devices
}
// Stop Drawing
function stopDrawing(e) {
if (!isDrawing) return;
isDrawing = false;
const coords = getEventCoordinates(e);
if (currentTool === "line") {
ctx.beginPath();
ctx.lineWidth = getBrushSize();
ctx.strokeStyle = brushColor;
ctx.moveTo(startX, startY);
ctx.lineTo(coords.x, coords.y);
ctx.stroke();
} else if (currentTool === "rectangle") {
const rectWidth = coords.x - startX;
const rectHeight = coords.y - startY;
ctx.lineWidth = getBrushSize();
ctx.strokeStyle = brushColor;
ctx.strokeRect(startX, startY, rectWidth, rectHeight);
} else if (currentTool === "circle") {
const radius = Math.sqrt((coords.x - startX) ** 2 + (coords.y - startY) ** 2);
ctx.beginPath();
ctx.lineWidth = getBrushSize();
ctx.strokeStyle = brushColor;
ctx.arc(startX, startY, radius, 0, Math.PI * 2);
ctx.stroke();
} else if (currentTool === "text") {
const text = prompt("Enter text:");
if (text) {
const fontSize = document.getElementById("font-size").value;
ctx.font = `${fontSize}px Arial`;
ctx.fillStyle = brushColor;
ctx.fillText(text, startX, startY);
}
}
e.preventDefault(); // Prevent scrolling on touch devices
}
// Attach Mouse and Touch Events
canvas.addEventListener("mousedown", startDrawing);
canvas.addEventListener("mousemove", draw);
canvas.addEventListener("mouseup", stopDrawing);
canvas.addEventListener("touchstart", startDrawing);
canvas.addEventListener("touchmove", draw);
canvas.addEventListener("touchend", stopDrawing);
// Tool Options
document.getElementById("pen-color").addEventListener("input", (e) => {
brushColor = e.target.value;
});
document.getElementById("pen-size").addEventListener("input", (e) => {
penSize = e.target.value;
});
document.getElementById("eraser-size").addEventListener("input", (e) => {
eraserSize = e.target.value;
});
// Save Work
function saveWork() {
const link = document.createElement("a");
link.download = "whiteboard.jpg";
link.href = canvas.toDataURL("image/jpg");
link.click();
}
// Upload Image
function uploadImage() {
const fileInput = document.createElement("input");
fileInput.type = "file";
fileInput.accept = "image/*";
fileInput.onchange = (e) => {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = () => {
const img = new Image();
img.onload = () => {
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
};
img.src = reader.result;
};
reader.readAsDataURL(file);
};
fileInput.click();
}
// Fullscreen toggle
function toggleFullscreen() {
if (!document.fullscreenElement) {
document.documentElement.requestFullscreen();
} else if (document.exitFullscreen) {
document.exitFullscreen();
}
}
function deleteEntire() {
const canvas = document.getElementById('whiteboard'); // Replace with your canvas ID
const ctx = canvas.getContext('2d');
// Clear the entire canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
// Call resizeCanvas when fullscreen changes
document.addEventListener('fullscreenchange', resizeCanvas);
document.addEventListener('webkitfullscreenchange', resizeCanvas); // For Safari
document.addEventListener('mozfullscreenchange', resizeCanvas); // For Firefox
document.addEventListener('msfullscreenchange', resizeCanvas); // For IE/Edge
// Initial Tool Setup
setTool("pointer");