5m4ck3r's picture
Upload 11 files
d7508be verified
raw
history blame
2.75 kB
class Whiteboard {
constructor() {
this.canvas = document.getElementById('whiteboard');
this.ctx = this.canvas.getContext('2d');
this.isDrawing = false;
this.setupCanvas();
this.setupEventListeners();
}
setupCanvas() {
const resize = () => {
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
this.ctx.strokeStyle = '#000';
this.ctx.lineWidth = 2;
this.ctx.lineCap = 'round';
this.ctx.lineJoin = 'round';
};
window.addEventListener('resize', resize);
resize();
}
setupEventListeners() {
this.canvas.addEventListener('mousedown', this.startDrawing.bind(this));
this.canvas.addEventListener('mousemove', this.draw.bind(this));
this.canvas.addEventListener('mouseup', this.stopDrawing.bind(this));
this.canvas.addEventListener('mouseout', this.stopDrawing.bind(this));
this.canvas.addEventListener('touchstart', (e) => {
e.preventDefault();
const touch = e.touches[0];
const mouseEvent = new MouseEvent('mousedown', {
clientX: touch.clientX,
clientY: touch.clientY
});
this.canvas.dispatchEvent(mouseEvent);
});
this.canvas.addEventListener('touchmove', (e) => {
e.preventDefault();
const touch = e.touches[0];
const mouseEvent = new MouseEvent('mousemove', {
clientX: touch.clientX,
clientY: touch.clientY
});
this.canvas.dispatchEvent(mouseEvent);
});
this.canvas.addEventListener('touchend', (e) => {
e.preventDefault();
const mouseEvent = new MouseEvent('mouseup', {});
this.canvas.dispatchEvent(mouseEvent);
});
document.getElementById('processBtn').addEventListener('click', this.processDrawing.bind(this));
document.getElementById('clearBtn').addEventListener('click', this.clearCanvas.bind(this));
}
startDrawing(e) {
this.isDrawing = true;
const rect = this.canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
this.ctx.beginPath();
this.ctx.moveTo(x, y);
}
draw(e) {
if (!this.isDrawing) return;
const rect = this.canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
this.ctx.lineTo(x, y);
this.ctx.stroke();
}
stopDrawing() {
this.isDrawing = false;
}
clearCanvas() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
processDrawing() {
const imageData = this.canvas.toDataURL('image/png');
console.log('Processing drawing...');
console.log(imageData.substring(0, 100) + '...');
}
}
document.addEventListener('DOMContentLoaded', () => {
new Whiteboard();
});