enhance the sim - first, clean up the layout and add a palette from which I can drag elements - we need a proper EM construction kit - coils and cores and wires etc
Browse files- components/control-panel.js +67 -10
- components/em-component.js +87 -0
- components/navbar.js +2 -2
- index.html +39 -14
- script.js +16 -4
components/control-panel.js
CHANGED
|
@@ -23,31 +23,88 @@ class CustomControlPanel extends HTMLElement {
|
|
| 23 |
.btn:hover {
|
| 24 |
transform: translateY(-2px);
|
| 25 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
</style>
|
| 27 |
<div class="panel p-6 rounded-xl shadow-lg border border-gray-700">
|
| 28 |
-
<h2 class="text-xl font-semibold mb-4">
|
| 29 |
|
| 30 |
-
<div class="space-y-
|
| 31 |
<div>
|
| 32 |
-
<
|
| 33 |
-
<
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
</div>
|
| 36 |
|
| 37 |
<div>
|
| 38 |
-
<
|
| 39 |
-
<
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
</div>
|
| 42 |
|
| 43 |
<div class="pt-2">
|
| 44 |
<button class="btn w-full bg-primary-500 hover:bg-primary-600 text-white py-2 px-4 rounded-lg">
|
| 45 |
-
Reset
|
| 46 |
</button>
|
| 47 |
</div>
|
| 48 |
</div>
|
| 49 |
</div>
|
| 50 |
-
|
| 51 |
}
|
| 52 |
}
|
| 53 |
|
|
|
|
| 23 |
.btn:hover {
|
| 24 |
transform: translateY(-2px);
|
| 25 |
}
|
| 26 |
+
.component-palette {
|
| 27 |
+
display: grid;
|
| 28 |
+
grid-template-columns: repeat(2, 1fr);
|
| 29 |
+
gap: 0.5rem;
|
| 30 |
+
}
|
| 31 |
+
.component-item {
|
| 32 |
+
padding: 0.5rem;
|
| 33 |
+
border-radius: 0.5rem;
|
| 34 |
+
background: rgba(55, 65, 81, 0.5);
|
| 35 |
+
cursor: grab;
|
| 36 |
+
text-align: center;
|
| 37 |
+
transition: all 0.2s ease;
|
| 38 |
+
}
|
| 39 |
+
.component-item:hover {
|
| 40 |
+
background: rgba(59, 130, 246, 0.5);
|
| 41 |
+
transform: translateY(-2px);
|
| 42 |
+
}
|
| 43 |
+
.component-icon {
|
| 44 |
+
width: 24px;
|
| 45 |
+
height: 24px;
|
| 46 |
+
margin: 0 auto 0.25rem;
|
| 47 |
+
}
|
| 48 |
</style>
|
| 49 |
<div class="panel p-6 rounded-xl shadow-lg border border-gray-700">
|
| 50 |
+
<h2 class="text-xl font-semibold mb-4">EM Construction Kit</h2>
|
| 51 |
|
| 52 |
+
<div class="space-y-6">
|
| 53 |
<div>
|
| 54 |
+
<h3 class="text-sm font-medium mb-2 uppercase tracking-wider text-gray-400">Components</h3>
|
| 55 |
+
<div class="component-palette">
|
| 56 |
+
<div class="component-item" data-type="coil">
|
| 57 |
+
<i data-feather="circle" class="component-icon text-yellow-500"></i>
|
| 58 |
+
<div class="text-xs">Coil</div>
|
| 59 |
+
</div>
|
| 60 |
+
<div class="component-item" data-type="core">
|
| 61 |
+
<i data-feather="box" class="component-icon text-gray-400"></i>
|
| 62 |
+
<div class="text-xs">Core</div>
|
| 63 |
+
</div>
|
| 64 |
+
<div class="component-item" data-type="wire">
|
| 65 |
+
<i data-feather="zap" class="component-icon text-blue-400"></i>
|
| 66 |
+
<div class="text-xs">Wire</div>
|
| 67 |
+
</div>
|
| 68 |
+
<div class="component-item" data-type="magnet">
|
| 69 |
+
<i data-feather="compass" class="component-icon text-red-500"></i>
|
| 70 |
+
<div class="text-xs">Magnet</div>
|
| 71 |
+
</div>
|
| 72 |
+
<div class="component-item" data-type="battery">
|
| 73 |
+
<i data-feather="battery" class="component-icon text-green-500"></i>
|
| 74 |
+
<div class="text-xs">Battery</div>
|
| 75 |
+
</div>
|
| 76 |
+
<div class="component-item" data-type="switch">
|
| 77 |
+
<i data-feather="toggle-right" class="component-icon text-purple-500"></i>
|
| 78 |
+
<div class="text-xs">Switch</div>
|
| 79 |
+
</div>
|
| 80 |
+
</div>
|
| 81 |
</div>
|
| 82 |
|
| 83 |
<div>
|
| 84 |
+
<h3 class="text-sm font-medium mb-2 uppercase tracking-wider text-gray-400">Properties</h3>
|
| 85 |
+
<div class="space-y-4">
|
| 86 |
+
<div>
|
| 87 |
+
<label class="block text-sm font-medium mb-1">Current</label>
|
| 88 |
+
<input type="range" min="0" max="1" step="0.1" value="0.5"
|
| 89 |
+
class="slider w-full h-2 bg-gray-700 rounded-lg appearance-none cursor-pointer">
|
| 90 |
+
</div>
|
| 91 |
+
|
| 92 |
+
<div>
|
| 93 |
+
<label class="block text-sm font-medium mb-1">Turns</label>
|
| 94 |
+
<input type="range" min="1" max="10" value="5"
|
| 95 |
+
class="slider w-full h-2 bg-gray-700 rounded-lg appearance-none cursor-pointer">
|
| 96 |
+
</div>
|
| 97 |
+
</div>
|
| 98 |
</div>
|
| 99 |
|
| 100 |
<div class="pt-2">
|
| 101 |
<button class="btn w-full bg-primary-500 hover:bg-primary-600 text-white py-2 px-4 rounded-lg">
|
| 102 |
+
Reset Circuit
|
| 103 |
</button>
|
| 104 |
</div>
|
| 105 |
</div>
|
| 106 |
</div>
|
| 107 |
+
`;
|
| 108 |
}
|
| 109 |
}
|
| 110 |
|
components/em-component.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class EMComponent extends HTMLElement {
|
| 2 |
+
constructor() {
|
| 3 |
+
super();
|
| 4 |
+
this.attachShadow({ mode: 'open' });
|
| 5 |
+
this.type = this.getAttribute('type') || 'magnet';
|
| 6 |
+
this.x = 0;
|
| 7 |
+
this.y = 0;
|
| 8 |
+
this.rotation = 0;
|
| 9 |
+
this.properties = {};
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
connectedCallback() {
|
| 13 |
+
this.render();
|
| 14 |
+
this.setupDrag();
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
render() {
|
| 18 |
+
const colors = {
|
| 19 |
+
magnet: '#ef4444',
|
| 20 |
+
coil: '#f59e0b',
|
| 21 |
+
core: '#6b7280',
|
| 22 |
+
wire: '#3b82f6',
|
| 23 |
+
battery: '#10b981',
|
| 24 |
+
switch: '#8b5cf6'
|
| 25 |
+
};
|
| 26 |
+
|
| 27 |
+
this.shadowRoot.innerHTML = `
|
| 28 |
+
<style>
|
| 29 |
+
.component {
|
| 30 |
+
position: absolute;
|
| 31 |
+
cursor: move;
|
| 32 |
+
touch-action: none;
|
| 33 |
+
transform-origin: center;
|
| 34 |
+
}
|
| 35 |
+
.component-icon {
|
| 36 |
+
pointer-events: none;
|
| 37 |
+
}
|
| 38 |
+
</style>
|
| 39 |
+
<div class="component" style="left: ${this.x}px; top: ${this.y}px;">
|
| 40 |
+
<i data-feather="${this.getIcon()}" class="component-icon"
|
| 41 |
+
style="color: ${colors[this.type] || '#ffffff'}; width: 32px; height: 32px;"></i>
|
| 42 |
+
</div>
|
| 43 |
+
`;
|
| 44 |
+
feather.replace();
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
getIcon() {
|
| 48 |
+
const icons = {
|
| 49 |
+
magnet: 'compass',
|
| 50 |
+
coil: 'circle',
|
| 51 |
+
core: 'box',
|
| 52 |
+
wire: 'zap',
|
| 53 |
+
battery: 'battery',
|
| 54 |
+
switch: 'toggle-right'
|
| 55 |
+
};
|
| 56 |
+
return icons[this.type] || 'help-circle';
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
setupDrag() {
|
| 60 |
+
const component = this.shadowRoot.querySelector('.component');
|
| 61 |
+
|
| 62 |
+
component.addEventListener('mousedown', (e) => {
|
| 63 |
+
e.preventDefault();
|
| 64 |
+
const startX = e.clientX;
|
| 65 |
+
const startY = e.clientY;
|
| 66 |
+
const startLeft = parseInt(component.style.left) || 0;
|
| 67 |
+
const startTop = parseInt(component.style.top) || 0;
|
| 68 |
+
|
| 69 |
+
function moveHandler(e) {
|
| 70 |
+
const dx = e.clientX - startX;
|
| 71 |
+
const dy = e.clientY - startY;
|
| 72 |
+
component.style.left = `${startLeft + dx}px`;
|
| 73 |
+
component.style.top = `${startTop + dy}px`;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
function upHandler() {
|
| 77 |
+
document.removeEventListener('mousemove', moveHandler);
|
| 78 |
+
document.removeEventListener('mouseup', upHandler);
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
document.addEventListener('mousemove', moveHandler);
|
| 82 |
+
document.addEventListener('mouseup', upHandler);
|
| 83 |
+
});
|
| 84 |
+
}
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
customElements.define('em-component', EMComponent);
|
components/navbar.js
CHANGED
|
@@ -21,8 +21,8 @@ class CustomNavbar extends HTMLElement {
|
|
| 21 |
<div class="container mx-auto flex justify-between items-center">
|
| 22 |
<div class="flex items-center space-x-2">
|
| 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">
|
| 25 |
-
|
| 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>
|
|
|
|
| 21 |
<div class="container mx-auto flex justify-between items-center">
|
| 22 |
<div class="flex items-center space-x-2">
|
| 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>
|
index.html
CHANGED
|
@@ -28,40 +28,65 @@
|
|
| 28 |
</head>
|
| 29 |
<body class="bg-gray-900 text-gray-100 min-h-screen">
|
| 30 |
<custom-navbar></custom-navbar>
|
| 31 |
-
|
| 32 |
<main class="container mx-auto px-4 py-8">
|
| 33 |
<div class="flex flex-col lg:flex-row gap-8">
|
| 34 |
<div class="lg:w-1/4">
|
| 35 |
<custom-control-panel></custom-control-panel>
|
| 36 |
</div>
|
| 37 |
|
| 38 |
-
<div class="lg:w-3/4">
|
| 39 |
<div class="bg-gray-800 rounded-xl p-4 shadow-lg">
|
| 40 |
<div id="visualization-container" class="relative w-full h-96 lg:h-[32rem] rounded-lg overflow-hidden">
|
| 41 |
<canvas id="fieldCanvas" class="absolute inset-0 w-full h-full"></canvas>
|
|
|
|
| 42 |
</div>
|
| 43 |
</div>
|
| 44 |
|
| 45 |
-
<div class="
|
| 46 |
-
<div
|
| 47 |
-
<h3 class="text-
|
| 48 |
-
<div
|
| 49 |
-
<div class="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
</div>
|
| 51 |
</div>
|
| 52 |
-
<div
|
| 53 |
-
<h3 class="text-
|
| 54 |
-
<div class="
|
| 55 |
-
<
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
</div>
|
| 58 |
</div>
|
| 59 |
</div>
|
| 60 |
</div>
|
| 61 |
</div>
|
| 62 |
</main>
|
| 63 |
-
|
| 64 |
-
<script src="components/navbar.js"></script>
|
| 65 |
<script src="components/control-panel.js"></script>
|
| 66 |
<script src="script.js"></script>
|
| 67 |
<script>
|
|
|
|
| 28 |
</head>
|
| 29 |
<body class="bg-gray-900 text-gray-100 min-h-screen">
|
| 30 |
<custom-navbar></custom-navbar>
|
|
|
|
| 31 |
<main class="container mx-auto px-4 py-8">
|
| 32 |
<div class="flex flex-col lg:flex-row gap-8">
|
| 33 |
<div class="lg:w-1/4">
|
| 34 |
<custom-control-panel></custom-control-panel>
|
| 35 |
</div>
|
| 36 |
|
| 37 |
+
<div class="lg:w-3/4 space-y-6">
|
| 38 |
<div class="bg-gray-800 rounded-xl p-4 shadow-lg">
|
| 39 |
<div id="visualization-container" class="relative w-full h-96 lg:h-[32rem] rounded-lg overflow-hidden">
|
| 40 |
<canvas id="fieldCanvas" class="absolute inset-0 w-full h-full"></canvas>
|
| 41 |
+
<div id="component-overlay" class="absolute inset-0 pointer-events-none"></div>
|
| 42 |
</div>
|
| 43 |
</div>
|
| 44 |
|
| 45 |
+
<div class="bg-gray-800 p-4 rounded-xl shadow grid grid-cols-1 md:grid-cols-3 gap-4">
|
| 46 |
+
<div>
|
| 47 |
+
<h3 class="text-sm font-medium mb-2 uppercase tracking-wider text-gray-400">Measurements</h3>
|
| 48 |
+
<div class="space-y-2">
|
| 49 |
+
<div class="flex justify-between">
|
| 50 |
+
<span>Magnetic Flux:</span>
|
| 51 |
+
<span class="font-mono">0.42 T</span>
|
| 52 |
+
</div>
|
| 53 |
+
<div class="flex justify-between">
|
| 54 |
+
<span>Current:</span>
|
| 55 |
+
<span class="font-mono">1.5 A</span>
|
| 56 |
+
</div>
|
| 57 |
</div>
|
| 58 |
</div>
|
| 59 |
+
<div>
|
| 60 |
+
<h3 class="text-sm font-medium mb-2 uppercase tracking-wider text-gray-400">Field</h3>
|
| 61 |
+
<div class="space-y-2">
|
| 62 |
+
<div class="flex justify-between">
|
| 63 |
+
<span>Strength:</span>
|
| 64 |
+
<span class="font-mono">0.67</span>
|
| 65 |
+
</div>
|
| 66 |
+
<div class="flex justify-between">
|
| 67 |
+
<span>Direction:</span>
|
| 68 |
+
<span class="font-mono">N→S</span>
|
| 69 |
+
</div>
|
| 70 |
+
</div>
|
| 71 |
+
</div>
|
| 72 |
+
<div>
|
| 73 |
+
<h3 class="text-sm font-medium mb-2 uppercase tracking-wider text-gray-400">Components</h3>
|
| 74 |
+
<div class="space-y-2">
|
| 75 |
+
<div class="flex justify-between">
|
| 76 |
+
<span>Active:</span>
|
| 77 |
+
<span class="font-mono">4</span>
|
| 78 |
+
</div>
|
| 79 |
+
<div class="flex justify-between">
|
| 80 |
+
<span>Connections:</span>
|
| 81 |
+
<span class="font-mono">3</span>
|
| 82 |
+
</div>
|
| 83 |
</div>
|
| 84 |
</div>
|
| 85 |
</div>
|
| 86 |
</div>
|
| 87 |
</div>
|
| 88 |
</main>
|
| 89 |
+
<script src="components/navbar.js"></script>
|
|
|
|
| 90 |
<script src="components/control-panel.js"></script>
|
| 91 |
<script src="script.js"></script>
|
| 92 |
<script>
|
script.js
CHANGED
|
@@ -1,9 +1,21 @@
|
|
| 1 |
|
| 2 |
document.addEventListener('DOMContentLoaded', () => {
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
const canvas = document.getElementById('fieldCanvas');
|
| 8 |
const ctx = canvas.getContext('2d');
|
| 9 |
// Set canvas to full size of container
|
|
|
|
| 1 |
|
| 2 |
document.addEventListener('DOMContentLoaded', () => {
|
| 3 |
+
// Components data structure
|
| 4 |
+
const components = {
|
| 5 |
+
magnets: [
|
| 6 |
+
{ x: 0.3, y: 0.5, strength: 0.7, polarity: 1, type: 'magnet' },
|
| 7 |
+
{ x: 0.7, y: 0.5, strength: 0.7, polarity: -1, type: 'magnet' }
|
| 8 |
+
],
|
| 9 |
+
coils: [],
|
| 10 |
+
cores: [],
|
| 11 |
+
wires: [],
|
| 12 |
+
batteries: [],
|
| 13 |
+
switches: []
|
| 14 |
+
};
|
| 15 |
+
|
| 16 |
+
let draggedComponent = null;
|
| 17 |
+
let isDraggingFromPalette = false;
|
| 18 |
+
let currentTool = null;
|
| 19 |
const canvas = document.getElementById('fieldCanvas');
|
| 20 |
const ctx = canvas.getContext('2d');
|
| 21 |
// Set canvas to full size of container
|