Changed around line 1
+ class DrawSpace {
+ constructor() {
+ this.canvas = document.getElementById('canvas');
+ this.shapes = [];
+ this.selectedShape = null;
+ this.isDragging = false;
+ this.isResizing = false;
+ this.pan = { x: 0, y: 0 };
+ this.zoom = 1;
+
+ this.setupEventListeners();
+ }
+
+ setupEventListeners() {
+ // Tool buttons
+ document.querySelectorAll('.tool-btn').forEach(btn => {
+ btn.addEventListener('click', () => this.addShape(btn.dataset.shape));
+ });
+
+ // Canvas pan and zoom
+ this.canvas.addEventListener('mousedown', this.startPan.bind(this));
+ this.canvas.addEventListener('mousemove', this.doPan.bind(this));
+ this.canvas.addEventListener('mouseup', this.endPan.bind(this));
+ this.canvas.addEventListener('wheel', this.handleZoom.bind(this));
+
+ // Property changes
+ document.getElementById('fillColor').addEventListener('change', this.updateShapeProperties.bind(this));
+ document.getElementById('strokeColor').addEventListener('change', this.updateShapeProperties.bind(this));
+ document.getElementById('strokeWidth').addEventListener('change', this.updateShapeProperties.bind(this));
+ document.getElementById('deleteShape').addEventListener('click', this.deleteSelectedShape.bind(this));
+ }
+
+ addShape(type) {
+ const shape = document.createElementNS('http://www.w3.org/2000/svg', type === 'rect' ? 'rect' :
+ type === 'ellipse' ? 'ellipse' :
+ type === 'line' ? 'line' :
+ type === 'triangle' ? 'polygon' :
+ 'text');
+
+ shape.classList.add('shape');
+
+ // Set default properties based on shape type
+ if (type === 'rect') {
+ shape.setAttribute('x', '100');
+ shape.setAttribute('y', '100');
+ shape.setAttribute('width', '100');
+ shape.setAttribute('height', '100');
+ } else if (type === 'ellipse') {
+ shape.setAttribute('cx', '150');
+ shape.setAttribute('cy', '150');
+ shape.setAttribute('rx', '50');
+ shape.setAttribute('ry', '50');
+ } else if (type === 'line') {
+ shape.setAttribute('x1', '100');
+ shape.setAttribute('y1', '100');
+ shape.setAttribute('x2', '200');
+ shape.setAttribute('y2', '200');
+ } else if (type === 'triangle') {
+ shape.setAttribute('points', '150,50 200,150 100,150');
+ } else if (type === 'text') {
+ shape.setAttribute('x', '100');
+ shape.setAttribute('y', '100');
+ shape.textContent = 'Text';
+ }
+
+ shape.setAttribute('fill', document.getElementById('fillColor').value);
+ shape.setAttribute('stroke', document.getElementById('strokeColor').value);
+ shape.setAttribute('stroke-width', document.getElementById('strokeWidth').value);
+
+ this.canvas.appendChild(shape);
+ this.shapes.push(shape);
+ this.selectShape(shape);
+ }
+
+ selectShape(shape) {
+ if (this.selectedShape) {
+ this.selectedShape.classList.remove('selected');
+ }
+ this.selectedShape = shape;
+ shape.classList.add('selected');
+ }
+
+ startPan(e) {
+ if (e.target === this.canvas) {
+ this.isDragging = true;
+ this.canvas.classList.add('grabbing');
+ this.lastX = e.clientX;
+ this.lastY = e.clientY;
+ }
+ }
+
+ doPan(e) {
+ if (this.isDragging) {
+ const dx = e.clientX - this.lastX;
+ const dy = e.clientY - this.lastY;
+ this.pan.x += dx;
+ this.pan.y += dy;
+ this.updateTransform();
+ this.lastX = e.clientX;
+ this.lastY = e.clientY;
+ }
+ }
+
+ endPan() {
+ this.isDragging = false;
+ this.canvas.classList.remove('grabbing');
+ }
+
+ handleZoom(e) {
+ e.preventDefault();
+ const delta = e.deltaY > 0 ? 0.9 : 1.1;
+ this.zoom *= delta;
+ this.zoom = Math.min(Math.max(0.1, this.zoom), 10);
+ this.updateTransform();
+ }
+
+ updateTransform() {
+ this.canvas.style.transform = `translate(${this.pan.x}px, ${this.pan.y}px) scale(${this.zoom})`;
+ }
+
+ updateShapeProperties() {
+ if (this.selectedShape) {
+ this.selectedShape.setAttribute('fill', document.getElementById('fillColor').value);
+ this.selectedShape.setAttribute('stroke', document.getElementById('strokeColor').value);
+ this.selectedShape.setAttribute('stroke-width', document.getElementById('strokeWidth').value);
+ }
+ }
+
+ deleteSelectedShape() {
+ if (this.selectedShape) {
+ this.canvas.removeChild(this.selectedShape);
+ this.shapes = this.shapes.filter(shape => shape !== this.selectedShape);
+ this.selectedShape = null;
+ }
+ }
+ }
+
+ // Initialize when DOM is loaded
+ document.addEventListener('DOMContentLoaded', () => {
+ new DrawSpace();
+ });