Skip to content

Commit e64f5a5

Browse files
committed
Use 'r' key to rotate a part
The rotation itself is stored in number of degrees (not radians).
1 parent b7bdfbb commit e64f5a5

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

simulator.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,9 @@ class Part {
11421142
this.pins = {};
11431143
this.parent = parent;
11441144
this.tooltipPin = null;
1145+
1146+
// Update data if needed.
1147+
data.rotation = data.rotation || 0;
11451148
}
11461149

11471150
// Load the given part and return it (because constructor() can't be async).
@@ -1401,6 +1404,14 @@ class Part {
14011404
this.applyWires();
14021405
}
14031406

1407+
// Rotate the part by the given number of degrees, relative to the center.
1408+
rotate(angle) {
1409+
this.data.rotation = (this.data.rotation + angle) % 360;
1410+
this.updatePosition();
1411+
this.calculateWires();
1412+
this.applyWires();
1413+
}
1414+
14041415
// Update position according to this.data.x and this.data.y.
14051416
updatePosition() {
14061417
// Set part position relative to the center of the schematic.
@@ -1409,9 +1420,10 @@ class Part {
14091420
let scale = this.schematic.scale;
14101421
let translateX = this.schematic.translateX;
14111422
let translateY = this.schematic.translateY;
1412-
let x = `calc(${this.data.x*scale}mm - ${this.width} * ${scale/2})`;
1413-
let y = `calc(${this.data.y*scale}mm - ${this.height} * ${scale/2})`;
1414-
this.container.style.transform = `translate(${translateX}mm, ${translateY}mm) translate(${x}, ${y})`;
1423+
let r = this.data.rotation / 180 * Math.PI;
1424+
let x = `calc(${this.data.x*scale}mm + ${this.width} * ${-Math.cos(r)/2*scale} + ${this.height} * ${Math.sin(r)/2*scale})`;
1425+
let y = `calc(${this.data.y*scale}mm + ${this.width} * ${-Math.sin(r)/2*scale} + ${this.height} * ${(-Math.cos(r))/2*scale})`;
1426+
this.container.style.transform = `translate(${translateX}mm, ${translateY}mm) translate(${x}, ${y}) rotate(${this.data.rotation}deg)`;
14151427
this.svgWrapper.style.transform = `scale(${scale})`;
14161428
this.background.style.width = `calc(${this.width} * ${scale})`;
14171429
this.background.style.height = `calc(${this.height} * ${scale})`;
@@ -1758,6 +1770,9 @@ document.addEventListener('keydown', e => {
17581770
selected = null;
17591771
simulator.saveState();
17601772
simulator.worker.postMessage(message);
1773+
} else if (e.key.toLowerCase() === 'r' && selected) {
1774+
selected.rotate(e.key === 'r' ? 45 : -45);
1775+
selected.schematic.simulator.saveState();
17611776
}
17621777
});
17631778

0 commit comments

Comments
 (0)