Changed around line 1
+ let selectedShape = null;
+ let isDragging = false;
+ let startX, startY;
+ let currentScale = 1;
+ let panOffset = { x: 0, y: 0 };
+
+ const canvas = document.getElementById('main-canvas');
+ const shapeButtons = document.querySelectorAll('.shape-btn');
+ const strokeColor = document.getElementById('stroke-color');
+ const fillColor = document.getElementById('fill-color');
+ const strokeWidth = document.getElementById('stroke-width');
+ const deleteButton = document.getElementById('delete-shape');
+
+ // Add shape event listeners
+ shapeButtons.forEach(button => {
+ button.addEventListener('click', () => {
+ const type = button.dataset.type;
+ addShape(type);
+ });
+ });
+
+ // Add shape to canvas
+ function addShape(type) {
+ const shape = document.createElementNS('http://www.w3.org/2000/svg', type);
+ shape.setAttribute('stroke', strokeColor.value);
+ shape.setAttribute('fill', fillColor.value === 'none' ? 'none' : fillColor.value);
+ shape.setAttribute('stroke-width', strokeWidth.value);
+
+ // Set initial position and size based on shape type
+ switch(type) {
+ case 'line':
+ shape.setAttribute('x1', 100);
+ shape.setAttribute('y1', 100);
+ shape.setAttribute('x2', 200);
+ shape.setAttribute('y2', 200);
+ break;
+ case 'rect':
+ shape.setAttribute('x', 100);
+ shape.setAttribute('y', 100);
+ shape.setAttribute('width', 100);
+ shape.setAttribute('height', 100);
+ break;
+ case 'triangle':
+ shape.setAttribute('points', '150,50 100,150 200,150');
+ break;
+ case 'ellipse':
+ shape.setAttribute('cx', 150);
+ shape.setAttribute('cy', 100);
+ shape.setAttribute('rx', 50);
+ shape.setAttribute('ry', 30);
+ break;
+ case 'text':
+ shape.setAttribute('x', 100);
+ shape.setAttribute('y', 100);
+ shape.textContent = 'Text';
+ break;
+ }
+
+ shape.addEventListener('click', (e) => {
+ e.stopPropagation();
+ selectShape(shape);
+ });
+
+ canvas.appendChild(shape);
+ selectShape(shape);
+ }
+
+ // Select shape
+ function selectShape(shape) {
+ if (selectedShape) {
+ selectedShape.classList.remove('selected');
+ }
+ selectedShape = shape;
+ selectedShape.classList.add('selected');
+ }
+
+ // Delete selected shape
+ deleteButton.addEventListener('click', () => {
+ if (selectedShape) {
+ canvas.removeChild(selectedShape);
+ selectedShape = null;
+ }
+ });
+
+ // Pan and zoom functionality
+ canvas.addEventListener('wheel', (e) => {
+ e.preventDefault();
+ const scaleFactor = 1.1;
+ const rect = canvas.getBoundingClientRect();
+ const offsetX = e.clientX - rect.left;
+ const offsetY = e.clientY - rect.top;
+
+ if (e.deltaY < 0) {
+ currentScale *= scaleFactor;
+ } else {
+ currentScale /= scaleFactor;
+ }
+
+ canvas.style.transform = `scale(${currentScale})`;
+ panOffset.x = offsetX - (offsetX - panOffset.x) * (e.deltaY < 0 ? scaleFactor : 1/scaleFactor);
+ panOffset.y = offsetY - (offsetY - panOffset.y) * (e.deltaY < 0 ? scaleFactor : 1/scaleFactor);
+ canvas.style.transformOrigin = `${panOffset.x}px ${panOffset.y}px`;
+ });
+
+ canvas.addEventListener('mousedown', (e) => {
+ if (e.target === canvas) {
+ isDragging = true;
+ startX = e.clientX - panOffset.x;
+ startY = e.clientY - panOffset.y;
+ }
+ });
+
+ canvas.addEventListener('mousemove', (e) => {
+ if (isDragging) {
+ panOffset.x = e.clientX - startX;
+ panOffset.y = e.clientY - startY;
+ canvas.style.transform = `translate(${panOffset.x}px, ${panOffset.y}px) scale(${currentScale})`;
+ }
+ });
+
+ canvas.addEventListener('mouseup', () => {
+ isDragging = false;
+ });
+
+ canvas.addEventListener('mouseleave', () => {
+ isDragging = false;
+ });