Changed around line 1
+ const canvas = document.getElementById('main-canvas');
+ const ctx = canvas.getContext('2d');
+ const imageUpload = document.getElementById('image-upload');
+ const clearButton = document.getElementById('clear-canvas');
+
+ let images = [];
+ let currentUser = Math.random().toString(36).substring(7);
+
+ // Set canvas dimensions
+ canvas.width = canvas.offsetWidth;
+ canvas.height = canvas.offsetHeight;
+
+ // Handle image upload
+ imageUpload.addEventListener('change', (e) => {
+ const file = e.target.files[0];
+ if (file) {
+ const reader = new FileReader();
+ reader.onload = (event) => {
+ const img = new Image();
+ img.src = event.target.result;
+ img.onload = () => {
+ const imageData = {
+ img: img,
+ x: 100,
+ y: 100,
+ width: 200,
+ height: 200,
+ user: currentUser
+ };
+ images.push(imageData);
+ drawCanvas();
+ };
+ };
+ reader.readAsDataURL(file);
+ }
+ });
+
+ // Clear canvas
+ clearButton.addEventListener('click', () => {
+ images = [];
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ });
+
+ // Draw all elements on canvas
+ function drawCanvas() {
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ images.forEach(image => {
+ ctx.drawImage(image.img, image.x, image.y, image.width, image.height);
+ });
+ }
+
+ // Handle mouse events for moving images
+ let selectedImage = null;
+ let offset = {x: 0, y: 0};
+
+ canvas.addEventListener('mousedown', (e) => {
+ const rect = canvas.getBoundingClientRect();
+ const mouseX = e.clientX - rect.left;
+ const mouseY = e.clientY - rect.top;
+
+ // Find clicked image
+ for (let i = images.length - 1; i >= 0; i--) {
+ const img = images[i];
+ if (mouseX > img.x && mouseX < img.x + img.width &&
+ mouseY > img.y && mouseY < img.y + img.height &&
+ img.user === currentUser) {
+ selectedImage = img;
+ offset.x = mouseX - img.x;
+ offset.y = mouseY - img.y;
+ break;
+ }
+ }
+ });
+
+ canvas.addEventListener('mousemove', (e) => {
+ if (selectedImage) {
+ const rect = canvas.getBoundingClientRect();
+ selectedImage.x = e.clientX - rect.left - offset.x;
+ selectedImage.y = e.clientY - rect.top - offset.y;
+ drawCanvas();
+ }
+ });
+
+ canvas.addEventListener('mouseup', () => {
+ selectedImage = null;
+ });
+
+ canvas.addEventListener('mouseleave', () => {
+ selectedImage = null;
+ });
+
+ // Simple WebSocket implementation for real-time updates
+ const ws = new WebSocket('wss://collabcanvas.scroll.pub/ws');
+
+ ws.onmessage = (event) => {
+ const data = JSON.parse(event.data);
+ if (data.type === 'update') {
+ images = data.images;
+ drawCanvas();
+ }
+ };
+
+ setInterval(() => {
+ if (images.length > 0) {
+ ws.send(JSON.stringify({
+ type: 'update',
+ images: images
+ }));
+ }
+ }, 1000);
+
+ // Clean up images when user leaves
+ window.addEventListener('beforeunload', () => {
+ images = images.filter(img => img.user !== currentUser);
+ ws.send(JSON.stringify({
+ type: 'update',
+ images: images
+ }));
+ });