lonestar108 commited on
Commit
d882be1
·
verified ·
1 Parent(s): 49388ab

fix the app

Browse files
Files changed (3) hide show
  1. components/control-panel.js +4 -14
  2. components/navbar.js +4 -6
  3. script.js +26 -12
components/control-panel.js CHANGED
@@ -60,7 +60,7 @@ class CustomControlPanel extends HTMLElement {
60
  </div>
61
 
62
  <script>
63
- document.querySelectorAll('.component-item').forEach(item => {
64
  item.addEventListener('click', () => {
65
  const type = item.dataset.type;
66
  document.dispatchEvent(new CustomEvent('component-selected', {
@@ -69,20 +69,10 @@ class CustomControlPanel extends HTMLElement {
69
  });
70
  });
71
 
72
- document.getElementById('delete-btn').addEventListener('click', () => {
73
- if (window.selectedComponent) {
74
- const type = window.selectedComponent.type;
75
- const arr = window.components[type + 's'];
76
- const index = arr.indexOf(window.selectedComponent);
77
- if (index > -1) {
78
- arr.splice(index, 1);
79
- window.selectedComponent = null;
80
- window.drawField();
81
- window.updateComponentDetails();
82
- }
83
- }
84
  });
85
- </script>
86
  <div id="component-details" class="bg-gray-700 p-4 rounded-lg">
87
  <p class="text-gray-400">No component selected</p>
88
  </div>
 
60
  </div>
61
 
62
  <script>
63
+ this.shadowRoot.querySelectorAll('.component-item').forEach(item => {
64
  item.addEventListener('click', () => {
65
  const type = item.dataset.type;
66
  document.dispatchEvent(new CustomEvent('component-selected', {
 
69
  });
70
  });
71
 
72
+ this.shadowRoot.getElementById('delete-btn').addEventListener('click', () => {
73
+ document.dispatchEvent(new CustomEvent('delete-selected'));
 
 
 
 
 
 
 
 
 
 
74
  });
75
+ </script>
76
  <div id="component-details" class="bg-gray-700 p-4 rounded-lg">
77
  <p class="text-gray-400">No component selected</p>
78
  </div>
components/navbar.js CHANGED
@@ -23,14 +23,12 @@ class CustomNavbar extends HTMLElement {
23
  <i data-feather="compass" class="text-primary-500"></i>
24
  <span class="text-xl font-bold bg-gradient-to-r from-primary-500 to-secondary-500 bg-clip-text text-transparent">EM Constructor</span>
25
  </div>
26
-
27
  <div class="hidden md:flex space-x-6">
28
- <a href="#" class="nav-link text-gray-300 hover:text-primary-500">Simulator</a>
29
- <a href="#" class="nav-link text-gray-300 hover:text-primary-500">Tutorial</a>
30
- <a href="#" class="nav-link text-gray-300 hover:text-primary-500">About</a>
31
  </div>
32
-
33
- <div class="flex items-center space-x-4">
34
  <button id="theme-toggle" class="p-2 rounded-full hover:bg-gray-800 transition-colors">
35
  <i data-feather="moon" class="text-gray-300"></i>
36
  </button>
 
23
  <i data-feather="compass" class="text-primary-500"></i>
24
  <span class="text-xl font-bold bg-gradient-to-r from-primary-500 to-secondary-500 bg-clip-text text-transparent">EM Constructor</span>
25
  </div>
 
26
  <div class="hidden md:flex space-x-6">
27
+ <a href="/" class="nav-link text-gray-300 hover:text-primary-500">Simulator</a>
28
+ <a href="/tutorial.html" class="nav-link text-gray-300 hover:text-primary-500">Tutorial</a>
29
+ <a href="/about.html" class="nav-link text-gray-300 hover:text-primary-500">About</a>
30
  </div>
31
+ <div class="flex items-center space-x-4">
 
32
  <button id="theme-toggle" class="p-2 rounded-full hover:bg-gray-800 transition-colors">
33
  <i data-feather="moon" class="text-gray-300"></i>
34
  </button>
script.js CHANGED
@@ -34,9 +34,9 @@ function resizeCanvas() {
34
  ctx.clearRect(0, 0, canvas.width, canvas.height);
35
 
36
  // Draw wires first (background)
37
- components.magnets.forEach(m => {
38
- wires.forEach(wire => {
39
- if (wire.points.length < 2) return;
40
 
41
  ctx.strokeStyle = '#3b82f6';
42
  ctx.lineWidth = 3;
@@ -155,21 +155,21 @@ const dx = x - m.x * canvas.width;
155
  };
156
  return colors[type] || '#ffffff';
157
  }
158
-
159
- // Handle component dragging from palette
160
  document.addEventListener('component-selected', (e) => {
161
  if (e.detail.type === 'wire') {
162
  isDrawingWire = true;
 
163
  return;
164
  }
165
 
166
- const rect = canvas.getBoundingClientRect();
167
  const newComponent = {
168
  type: e.detail.type,
169
  x: 0.5, // Center of canvas
170
  y: 0.5,
171
  strength: 0.5,
172
- polarity: 1
 
173
  };
174
 
175
  components[e.detail.type + 's'].push(newComponent);
@@ -178,7 +178,21 @@ const dx = x - m.x * canvas.width;
178
  updateComponentDetails();
179
  });
180
 
181
- // Canvas interaction handlers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
  canvas.addEventListener('mousedown', (e) => {
183
  const rect = canvas.getBoundingClientRect();
184
  const x = e.clientX - rect.left;
@@ -236,21 +250,21 @@ const dx = x - m.x * canvas.width;
236
  updateComponentDetails();
237
  }
238
  });
239
-
240
  canvas.addEventListener('mouseup', () => {
241
  if (isDrawingWire && wireStartPoint && currentWirePoints.length > 1) {
242
  components.wires.push({
243
  type: 'wire',
244
- points: [...currentWirePoints]
 
245
  });
246
  isDrawingWire = false;
 
247
  wireStartPoint = null;
248
  currentWirePoints = [];
249
  drawField();
250
  }
251
  });
252
-
253
- function updateComponentDetails() {
254
  const detailsPanel = document.getElementById('component-details');
255
  if (!detailsPanel) return;
256
 
 
34
  ctx.clearRect(0, 0, canvas.width, canvas.height);
35
 
36
  // Draw wires first (background)
37
+ components.magnets.forEach(magnet => {
38
+ components.wires.forEach(wire => {
39
+ if (wire.points.length < 2) return;
40
 
41
  ctx.strokeStyle = '#3b82f6';
42
  ctx.lineWidth = 3;
 
155
  };
156
  return colors[type] || '#ffffff';
157
  }
158
+ // Handle component creation from palette
 
159
  document.addEventListener('component-selected', (e) => {
160
  if (e.detail.type === 'wire') {
161
  isDrawingWire = true;
162
+ overlay.style.cursor = 'crosshair';
163
  return;
164
  }
165
 
 
166
  const newComponent = {
167
  type: e.detail.type,
168
  x: 0.5, // Center of canvas
169
  y: 0.5,
170
  strength: 0.5,
171
+ polarity: 1,
172
+ turns: e.detail.type === 'coil' ? 5 : 0
173
  };
174
 
175
  components[e.detail.type + 's'].push(newComponent);
 
178
  updateComponentDetails();
179
  });
180
 
181
+ // Handle component deletion
182
+ document.addEventListener('delete-selected', () => {
183
+ if (selectedComponent) {
184
+ const type = selectedComponent.type;
185
+ const arr = components[type + 's'];
186
+ const index = arr.indexOf(selectedComponent);
187
+ if (index > -1) {
188
+ arr.splice(index, 1);
189
+ selectedComponent = null;
190
+ drawField();
191
+ updateComponentDetails();
192
+ }
193
+ }
194
+ });
195
+ // Canvas interaction handlers
196
  canvas.addEventListener('mousedown', (e) => {
197
  const rect = canvas.getBoundingClientRect();
198
  const x = e.clientX - rect.left;
 
250
  updateComponentDetails();
251
  }
252
  });
 
253
  canvas.addEventListener('mouseup', () => {
254
  if (isDrawingWire && wireStartPoint && currentWirePoints.length > 1) {
255
  components.wires.push({
256
  type: 'wire',
257
+ points: [...currentWirePoints],
258
+ current: 0.5
259
  });
260
  isDrawingWire = false;
261
+ overlay.style.cursor = 'default';
262
  wireStartPoint = null;
263
  currentWirePoints = [];
264
  drawField();
265
  }
266
  });
267
+ function updateComponentDetails() {
 
268
  const detailsPanel = document.getElementById('component-details');
269
  if (!detailsPanel) return;
270