Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
480 lines (419 loc) · 16 KB
/
Copy pathscript.js
File metadata and controls
480 lines (419 loc) · 16 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// ── Mode open/close ────────────────────────────
let currentMode = 'whiteboard';
function openBoard(modeInput) {
// modeInput can be a string ('graph', '3d') or HTML element (the card)
let modeName, label;
if(typeof modeInput === 'string') {
modeName = modeInput;
label = modeName === '3d' ? '3D Workspace' : 'Graph Mode';
} else {
modeName = modeInput.dataset.mode;
label = modeInput.dataset.label;
}
currentMode = modeName;
const bp = document.getElementById('board-page');
bp.className = `active mode-${currentMode}`;
document.getElementById('landing').style.display = 'none';
document.getElementById('board-title').textContent = label;
// Toggle specific tool buttons
document.getElementById('tool-add-chart').style.display = modeName === 'graph' ? 'flex' : 'none';
document.getElementById('tool-add-3d').style.display = modeName === '3d' ? 'flex' : 'none';
document.getElementById('tool-add-sticky').style.display = modeName === 'sticky' ? 'flex' : 'none';
document.getElementById('mode-tools-sep').style.display = (modeName === 'graph' || modeName === '3d' || modeName === 'sticky') ? 'block' : 'none';
initCanvas();
// Dynamic Load for Chart and Three JS
if(modeName === 'graph' && !window.Chart) {
loadScript('https://cdn.jsdelivr.net/npm/chart.js');
}
if(modeName === '3d' && !window.THREE) {
loadScript('https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js');
}
}
function loadScript(src) {
const script = document.createElement('script');
script.src = src;
document.head.appendChild(script);
}
function closeBoard() {
document.getElementById('board-page').classList.remove('active');
document.getElementById('board-page').className = '';
document.getElementById('landing').style.display = '';
ctx && ctx.clearRect(0, 0, canvas.width, canvas.height);
draftCtx && draftCtx.clearRect(0, 0, draftCanvas.width, draftCanvas.height);
document.getElementById('board-overlays').innerHTML = ''; // clear stickies/charts
}
document.addEventListener('keydown', e => {
if (e.key === 'Escape') closeBoard();
});
// ── Canvas drawing ───────────────────────────────
const canvas = document.getElementById('board-canvas');
const ctx = canvas.getContext('2d');
const draftCanvas = document.getElementById('draft-canvas');
const draftCtx = draftCanvas.getContext('2d');
let drawing = false;
let startX = 0, startY = 0;
let lastX = 0, lastY = 0;
let tool = 'pen';
let brushSize = 3;
const PALETTE = {
whiteboard: ['#111','#c0392b','#2980b9','#27ae60','#8e44ad','#e67e22'],
blackboard: ['rgba(255,255,255,0.9)','#f9e04b','#7fc87f','#f97b6b','#89d4f0','#f0b87a'],
notebook: ['#1a1a2e','#e74c3c','#2980b9','#27ae60','#8e44ad','#d35400'],
sticky: ['#1a1a2e','#e74c3c','#2980b9','#27ae60','#8e44ad','#d35400'],
manuscript: ['#3a2a12','#8b5a2b','#5a3e28','#a0522d','#6b4c2a','#2c1810'],
graph: ['#fff','#6ad3f7','#f7a26a','#7c6af7','#555','#ff6b6b'],
'3d': ['#fff','#6ad3f7','#f7a26a','#7c6af7','#555','#ff6b6b']
};
function buildColorRow() {
const row = document.getElementById('color-row');
row.innerHTML = '';
const colors = PALETTE[currentMode] || PALETTE.whiteboard;
colors.forEach((c, i) => {
const sw = document.createElement('div');
sw.className = 'color-swatch' + (i === 0 ? ' active' : '');
sw.style.background = c;
sw.title = c;
sw.onclick = () => {
document.querySelectorAll('.color-swatch').forEach(s => s.classList.remove('active'));
sw.classList.add('active');
ctx.strokeStyle = c;
draftCtx.strokeStyle = c;
};
row.appendChild(sw);
});
ctx.strokeStyle = colors[0];
draftCtx.strokeStyle = colors[0];
}
function setTool(t) {
tool = t;
document.querySelectorAll('.tool-btn').forEach(b => b.classList.remove('active'));
const btn = document.getElementById('tool-' + t);
if(btn) btn.classList.add('active');
}
function clearBoard() {
saveState();
ctx.clearRect(0, 0, canvas.width, canvas.height);
draftCtx.clearRect(0, 0, draftCanvas.width, draftCanvas.height);
document.getElementById('board-overlays').innerHTML = '';
}
function resizeCanvas() {
const wrap = canvas.parentElement;
canvas.width = wrap.clientWidth;
canvas.height = wrap.clientHeight;
draftCanvas.width = wrap.clientWidth;
draftCanvas.height = wrap.clientHeight;
}
function initCanvas() {
resizeCanvas();
buildColorRow();
ctx.lineJoin = 'round'; ctx.lineCap = 'round';
draftCtx.lineJoin = 'round'; draftCtx.lineCap = 'round';
document.getElementById('size-slider').value = 3;
brushSize = 3;
setTool('pen');
}
window.addEventListener('resize', () => {
if (document.getElementById('board-page').classList.contains('active')) resizeCanvas();
});
// Setup styles based on tool
function applyToolStyles(context) {
context.lineWidth = tool === 'eraser' ? brushSize * 6 : brushSize;
if(tool === 'eraser') {
context.globalCompositeOperation = 'destination-out';
context.globalAlpha = 1.0;
} else if(tool === 'highlighter') {
context.globalCompositeOperation = 'multiply';
context.globalAlpha = 0.4;
context.lineWidth = brushSize * 3;
} else {
context.globalCompositeOperation = 'source-over';
context.globalAlpha = 1.0;
}
}
function drawShape(context, x0, y0, x1, y1) {
context.beginPath();
if(tool === 'line') {
context.moveTo(x0, y0);
context.lineTo(x1, y1);
} else if(tool === 'rect') {
context.rect(x0, y0, x1 - x0, y1 - y0);
} else if(tool === 'circle') {
const rx = Math.abs(x1 - x0) / 2;
const ry = Math.abs(y1 - y0) / 2;
const cx = x0 + (x1 - x0) / 2;
const cy = y0 + (y1 - y0) / 2;
context.ellipse(cx, cy, rx, ry, 0, 0, 2 * Math.PI);
} else if(tool === 'triangle') {
context.moveTo(x0 + (x1 - x0) / 2, y0);
context.lineTo(x1, y1);
context.lineTo(x0, y1);
context.closePath();
}
context.stroke();
}
// Global Undo Stack
const undoStack = [];
const redoStack = [];
function saveState() {
if (undoStack.length >= 40) undoStack.shift();
undoStack.push(ctx.getImageData(0, 0, canvas.width, canvas.height));
redoStack.length = 0;
}
function undoBoard() {
if (undoStack.length > 0) {
redoStack.push(ctx.getImageData(0, 0, canvas.width, canvas.height));
const prevState = undoStack.pop();
ctx.putImageData(prevState, 0, 0);
}
}
function redoBoard() {
if (redoStack.length > 0) {
undoStack.push(ctx.getImageData(0, 0, canvas.width, canvas.height));
const nextState = redoStack.pop();
ctx.putImageData(nextState, 0, 0);
}
}
// Mouse events on main canvas
const targetCanvas = canvas;
targetCanvas.addEventListener('mousedown', e => {
saveState();
drawing = true;
startX = e.offsetX; startY = e.offsetY;
lastX = startX; lastY = startY;
});
targetCanvas.addEventListener('mousemove', e => {
if (!drawing) return;
const isFreehand = (tool === 'pen' || tool === 'eraser' || tool === 'highlighter');
if (isFreehand) {
applyToolStyles(ctx);
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
} else {
draftCtx.clearRect(0, 0, draftCanvas.width, draftCanvas.height);
applyToolStyles(draftCtx);
drawShape(draftCtx, startX, startY, e.offsetX, e.offsetY);
}
lastX = e.offsetX; lastY = e.offsetY;
});
function confirmDraw(e) {
if (!drawing) return;
drawing = false;
const isFreehand = (tool === 'pen' || tool === 'eraser' || tool === 'highlighter');
if (!isFreehand) {
applyToolStyles(ctx);
drawShape(ctx, startX, startY, e.offsetX, e.offsetY);
draftCtx.clearRect(0, 0, draftCanvas.width, draftCanvas.height);
}
}
targetCanvas.addEventListener('mouseup', confirmDraw);
targetCanvas.addEventListener('mouseleave', () => {
if(drawing && (tool === 'pen' || tool === 'eraser' || tool === 'highlighter')) {
drawing = false;
}
draftCtx.clearRect(0,0,draftCanvas.width,draftCanvas.height);
});
// Touch events map similar to mouse events
function getTouchPos(e) {
const r = targetCanvas.getBoundingClientRect();
return [e.touches[0].clientX - r.left, e.touches[0].clientY - r.top];
}
targetCanvas.addEventListener('touchstart', e => {
e.preventDefault();
saveState();
drawing = true;
[startX, startY] = getTouchPos(e);
lastX = startX; lastY = startY;
}, { passive: false });
targetCanvas.addEventListener('touchmove', e => {
e.preventDefault(); if (!drawing) return;
const [x, y] = getTouchPos(e);
const isFreehand = (tool === 'pen' || tool === 'eraser' || tool === 'highlighter');
if(isFreehand) {
applyToolStyles(ctx);
ctx.beginPath(); ctx.moveTo(lastX, lastY); ctx.lineTo(x, y); ctx.stroke();
} else {
draftCtx.clearRect(0, 0, draftCanvas.width, draftCanvas.height);
applyToolStyles(draftCtx);
drawShape(draftCtx, startX, startY, x, y);
}
lastX = x; lastY = y;
}, { passive: false });
targetCanvas.addEventListener('touchend', e => {
if(!drawing) return; drawing = false;
const isFreehand = (tool === 'pen' || tool === 'eraser' || tool === 'highlighter');
if(!isFreehand) {
applyToolStyles(ctx);
drawShape(ctx, startX, startY, lastX, lastY);
draftCtx.clearRect(0, 0, draftCanvas.width, draftCanvas.height);
}
});
// Brush size sync
document.getElementById('size-slider').addEventListener('input', function() {
brushSize = +this.value;
});
// Keyboard shortcut cards
document.querySelectorAll('.card').forEach(c => {
c.addEventListener('keydown', e => {
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); openBoard(c); }
});
});
// ── Overlays: Sticky Notes, Graph, 3D ──────────────────────
function makeDraggable(el) {
let isDragging = false, startX, startY, initialX, initialY;
el.addEventListener('mousedown', dragStart);
el.addEventListener('touchstart', dragStart, {passive: false});
function dragStart(e) {
if(e.target.tagName === 'BUTTON' || e.target.classList.contains('sticky-close')) return;
isDragging = true;
const clientX = e.type === 'touchstart' ? e.touches[0].clientX : e.clientX;
const clientY = e.type === 'touchstart' ? e.touches[0].clientY : e.clientY;
startX = clientX; startY = clientY;
initialX = el.offsetLeft; initialY = el.offsetTop;
document.addEventListener('mousemove', drag);
document.addEventListener('mouseup', dragEnd);
document.addEventListener('touchmove', drag, {passive: false});
document.addEventListener('touchend', dragEnd);
}
function drag(e) {
if (!isDragging) return;
e.preventDefault();
const clientX = e.type === 'touchmove' ? e.touches[0].clientX : e.clientX;
const clientY = e.type === 'touchmove' ? e.touches[0].clientY : e.clientY;
el.style.left = (initialX + clientX - startX) + 'px';
el.style.top = (initialY + clientY - startY) + 'px';
}
function dragEnd() {
isDragging = false;
document.removeEventListener('mousemove', drag);
document.removeEventListener('mouseup', dragEnd);
document.removeEventListener('touchmove', drag);
document.removeEventListener('touchend', dragEnd);
}
}
const stickyColors = ['sticky-yellow', 'sticky-red', 'sticky-green', 'sticky-blue'];
let stickyCount = 0;
function addStickyNote() {
const container = document.getElementById('board-overlays');
const note = document.createElement('div');
const colorCls = stickyColors[stickyCount % stickyColors.length];
stickyCount++;
note.className = `sticky-note ${colorCls}`;
note.style.left = '100px'; note.style.top = '100px';
note.style.setProperty('--rot', (Math.random() * 6 - 3).toFixed(1));
note.contentEditable = true;
note.innerHTML = "New Note";
const closeBtn = document.createElement('div');
closeBtn.className = 'sticky-close';
closeBtn.innerHTML = '✖';
closeBtn.contentEditable = false;
closeBtn.onclick = () => note.remove();
note.appendChild(closeBtn);
makeDraggable(note);
container.appendChild(note);
}
function addChart() {
if(!window.Chart) { alert("Chart.js is still loading..."); return; }
const container = document.getElementById('board-overlays');
const dialog = document.createElement('div');
dialog.className = 'chart-config-dialog';
dialog.innerHTML = `
<h3>Create Graph</h3>
<label>Type:
<select id="chart-type">
<option value="bar">Bar Graph</option>
<option value="pie">Pie Chart</option>
<option value="line">Line Chart</option>
<option value="doughnut">Doughnut</option>
</select>
</label>
<label>Labels (comma separated): <input type="text" id="chart-labels" value="Q1, Q2, Q3, Q4" /></label>
<label>Values (comma separated): <input type="text" id="chart-values" value="12, 19, 3, 5" /></label>
<div class="dialog-btns">
<button id="chart-cancel-btn">Cancel</button>
<button id="chart-create-btn">Create</button>
</div>
`;
container.appendChild(dialog);
document.getElementById('chart-create-btn').onclick = () => {
const type = document.getElementById('chart-type').value;
const labels = document.getElementById('chart-labels').value.split(',').map(s => s.trim());
const values = document.getElementById('chart-values').value.split(',').map(s => Number(s.trim()));
dialog.remove();
createDraggableChart(type, labels, values);
};
document.getElementById('chart-cancel-btn').onclick = () => dialog.remove();
}
function createDraggableChart(type, labels, values) {
const container = document.getElementById('board-overlays');
const wrapper = document.createElement('div');
wrapper.className = 'chart-drag';
wrapper.style.left = '150px'; wrapper.style.top = '150px';
const closeBtn = document.createElement('div');
closeBtn.className = 'sticky-close';
closeBtn.innerHTML = '✖';
closeBtn.onclick = () => wrapper.remove();
const chartCanvas = document.createElement('canvas');
wrapper.appendChild(closeBtn);
wrapper.appendChild(chartCanvas);
makeDraggable(wrapper);
container.appendChild(wrapper);
new window.Chart(chartCanvas, {
type: type,
data: {
labels: labels,
datasets: [{
label: 'Metric',
data: values,
backgroundColor: ['#6ad3f7', '#f7a26a', '#7c6af7', '#ff6b6b', '#86efac', '#fca5a5', '#93c5fd']
}]
},
options: { responsive: true, maintainAspectRatio: false }
});
}
function add3DObject() {
if(!window.THREE) { alert("Three.js is still loading..."); return; }
const container = document.getElementById('board-overlays');
const wrapper = document.createElement('div');
wrapper.className = 'chart-drag';
wrapper.style.left = '200px'; wrapper.style.top = '150px';
wrapper.style.width = '300px'; wrapper.style.height = '300px';
wrapper.style.background = 'rgba(42, 42, 53, 0.9)';
const closeBtn = document.createElement('div');
closeBtn.className = 'sticky-close';
closeBtn.innerHTML = '✖';
closeBtn.style.color = '#fff';
closeBtn.onclick = () => wrapper.remove();
wrapper.appendChild(closeBtn);
makeDraggable(wrapper);
container.appendChild(wrapper);
// Setup Three JS scene
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({alpha:true});
renderer.setSize(280, 280);
wrapper.appendChild(renderer.domElement);
// We'll vary between box, sphere, cone
let geometry;
const rand = Math.random();
if(rand < 0.33) geometry = new THREE.BoxGeometry();
else if(rand < 0.66) geometry = new THREE.SphereGeometry( 0.7, 32, 16 );
else geometry = new THREE.ConeGeometry( 0.6, 1.2, 32 );
const material = new THREE.MeshPhongMaterial({ color: 0x6ad3f7, shininess: 100 });
const object = new THREE.Mesh(geometry, material);
scene.add(object);
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(1, 1, 2);
scene.add(light);
scene.add(new THREE.AmbientLight(0x404040));
camera.position.z = 2.5;
function animate() {
if(!document.body.contains(wrapper)) return; // Stop if removed
requestAnimationFrame(animate);
object.rotation.x += 0.015;
object.rotation.y += 0.015;
renderer.render(scene, camera);
}
animate();
}