Changed around line 1
+ const canvas = document.getElementById('canvas');
+ const ctx = canvas.getContext('2d');
+ const addRectangleBtn = document.getElementById('add-rectangle');
+ const addCircleBtn = document.getElementById('add-circle');
+ const clearAllBtn = document.getElementById('clear-all');
+
+ let objects = [];
+ let selectedObject = null;
+
+ class Shape {
+ constructor(x, y, width, height, type) {
+ this.x = x;
+ this.y = y;
+ this.width = width;
+ this.height = height;
+ this.type = type;
+ this.color = `hsl(${Math.random() * 360}, 50%, 50%)`;
+ }
+
+ draw() {
+ ctx.fillStyle = this.color;
+ if (this.type === 'rectangle') {
+ ctx.fillRect(this.x, this.y, this.width, this.height);
+ } else if (this.type === 'circle') {
+ ctx.beginPath();
+ ctx.arc(this.x, this.y, this.width / 2, 0, Math.PI * 2);
+ ctx.fill();
+ }
+ }
+
+ isPointInside(px, py) {
+ if (this.type === 'rectangle') {
+ return px >= this.x && px <= this.x + this.width &&
+ py >= this.y && py <= this.y + this.height;
+ } else if (this.type === 'circle') {
+ const dx = px - this.x;
+ const dy = py - this.y;
+ return Math.sqrt(dx * dx + dy * dy) <= this.width / 2;
+ }
+ return false;
+ }
+ }
+
+ function addRectangle() {
+ const rect = new Shape(100, 100, 100, 100, 'rectangle');
+ objects.push(rect);
+ selectedObject = rect;
+ drawScene();
+ }
+
+ function addCircle() {
+ const circle = new Shape(100, 100, 100, 100, 'circle');
+ objects.push(circle);
+ selectedObject = circle;
+ drawScene();
+ }
+
+ function clearAll() {
+ objects = [];
+ selectedObject = null;
+ drawScene();
+ }
+
+ function drawScene() {
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ objects.forEach(obj => obj.draw());
+ if (selectedObject) {
+ ctx.strokeStyle = 'black';
+ ctx.lineWidth = 2;
+ if (selectedObject.type === 'rectangle') {
+ ctx.strokeRect(selectedObject.x, selectedObject.y, selectedObject.width, selectedObject.height);
+ } else if (selectedObject.type === 'circle') {
+ ctx.beginPath();
+ ctx.arc(selectedObject.x, selectedObject.y, selectedObject.width / 2, 0, Math.PI * 2);
+ ctx.stroke();
+ }
+ }
+ }
+
+ function handleMouseDown(e) {
+ const rect = canvas.getBoundingClientRect();
+ const mouseX = e.clientX - rect.left;
+ const mouseY = e.clientY - rect.top;
+
+ selectedObject = objects.find(obj => obj.isPointInside(mouseX, mouseY));
+ drawScene();
+ }
+
+ function handleKeyDown(e) {
+ if (!selectedObject) return;
+
+ const speed = 5;
+ switch (e.key) {
+ case 'ArrowUp':
+ selectedObject.y -= speed;
+ break;
+ case 'ArrowDown':
+ selectedObject.y += speed;
+ break;
+ case 'ArrowLeft':
+ selectedObject.x -= speed;
+ break;
+ case 'ArrowRight':
+ selectedObject.x += speed;
+ break;
+ }
+ drawScene();
+ }
+
+ addRectangleBtn.addEventListener('click', addRectangle);
+ addCircleBtn.addEventListener('click', addCircle);
+ clearAllBtn.addEventListener('click', clearAll);
+ canvas.addEventListener('mousedown', handleMouseDown);
+ window.addEventListener('keydown', handleKeyDown);
+
+ drawScene();