You've already forked computing-box
feat(binary): add full binary simulator with unsigned and two’s complement modes
- Introduce new Binary Simulator page with adjustable bit width (4–16 bits) - Support unsigned and two’s complement representations with live conversion - Add left/right shift operations and custom binary/denary input - Implement accessible bulb-and-switch UI with MD3-inspired styling - Add seven-segment display font assets for realistic number output - Establish shared base layout, styles, and tooling for future simulators Signed-off-by: Alexander Lyall <alex@adcm.uk>
This commit is contained in:
137
public/css/tools/binary.css
Normal file
137
public/css/tools/binary.css
Normal file
@@ -0,0 +1,137 @@
|
||||
/* ---------- DSEG7 font ---------- */
|
||||
/* Put your font file here:
|
||||
public/fonts/DSEG7Classic-Regular.woff2
|
||||
*/
|
||||
@font-face {
|
||||
font-family: "DSEG7ClassicRegular";
|
||||
src: url("/fonts/DSEG7Classic-Regular.woff2") format("woff2");
|
||||
font-display: swap;
|
||||
}
|
||||
|
||||
/* ---------- Layout ---------- */
|
||||
.tool-shell {
|
||||
min-height: 100vh;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
padding: 1rem;
|
||||
background: #0b0f14;
|
||||
color: #e7eaf0;
|
||||
font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
|
||||
}
|
||||
|
||||
.tool-card {
|
||||
width: min(1100px, 100%);
|
||||
background: #111824;
|
||||
border: 1px solid rgba(255,255,255,0.08);
|
||||
border-radius: 18px;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.tool-header h1 { margin: 0 0 .25rem 0; font-size: 1.4rem; }
|
||||
.tool-header p { margin: 0 0 1rem 0; opacity: .85; }
|
||||
|
||||
.display-grid {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: .75rem;
|
||||
margin-bottom: .75rem;
|
||||
}
|
||||
|
||||
.display-box {
|
||||
background: #0b0f14;
|
||||
border: 1px solid rgba(255,255,255,0.08);
|
||||
border-radius: 14px;
|
||||
padding: .75rem;
|
||||
}
|
||||
|
||||
.display-label { font-size: .9rem; opacity: .8; margin-bottom: .25rem; }
|
||||
|
||||
.sevenseg {
|
||||
font-family: "DSEG7ClassicRegular", monospace;
|
||||
font-size: clamp(2rem, 4vw, 3.2rem);
|
||||
letter-spacing: 0.08em;
|
||||
line-height: 1.1;
|
||||
}
|
||||
|
||||
/* Buttons under denary/binary (your request) */
|
||||
.actions {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: .5rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
/* ---------- Simple MD3-ish buttons ---------- */
|
||||
.md3-btn {
|
||||
border: 1px solid rgba(255,255,255,0.16);
|
||||
background: rgba(255,255,255,0.06);
|
||||
color: #e7eaf0;
|
||||
padding: .6rem .9rem;
|
||||
border-radius: 999px;
|
||||
cursor: pointer;
|
||||
font-weight: 600;
|
||||
}
|
||||
.md3-btn:hover { background: rgba(255,255,255,0.10); }
|
||||
.md3-btn--tonal { background: rgba(255,255,255,0.10); }
|
||||
|
||||
/* ---------- Switches row ---------- */
|
||||
.switch-row {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(8, minmax(90px, 1fr));
|
||||
gap: .75rem;
|
||||
}
|
||||
|
||||
.switch-col {
|
||||
display: grid;
|
||||
justify-items: center;
|
||||
gap: .35rem;
|
||||
}
|
||||
|
||||
.bit-label { opacity: .85; font-weight: 600; }
|
||||
|
||||
/* ---------- “Light switch” rocker ---------- */
|
||||
.rocker {
|
||||
position: relative;
|
||||
width: 70px;
|
||||
height: 46px;
|
||||
display: inline-block;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.rocker input {
|
||||
opacity: 0;
|
||||
width: 0;
|
||||
height: 0;
|
||||
}
|
||||
|
||||
.rocker-body {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
border-radius: 12px;
|
||||
background: #1a2331;
|
||||
border: 1px solid rgba(255,255,255,0.14);
|
||||
box-shadow: inset 0 0 0 2px rgba(0,0,0,0.35);
|
||||
}
|
||||
|
||||
/* the “toggle” */
|
||||
.rocker-body::after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
left: 6px;
|
||||
top: 6px;
|
||||
width: 58px;
|
||||
height: 18px;
|
||||
border-radius: 10px;
|
||||
background: rgba(255,255,255,0.20);
|
||||
transition: transform 180ms ease, background 180ms ease;
|
||||
}
|
||||
|
||||
/* ON position */
|
||||
.rocker input:checked + .rocker-body::after {
|
||||
transform: translateY(16px);
|
||||
background: rgba(255,255,255,0.55);
|
||||
}
|
||||
|
||||
@media (max-width: 900px) {
|
||||
.switch-row { grid-template-columns: repeat(4, minmax(90px, 1fr)); }
|
||||
}
|
||||
9
public/favicon.svg
Normal file
9
public/favicon.svg
Normal file
@@ -0,0 +1,9 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 128 128">
|
||||
<path d="M50.4 78.5a75.1 75.1 0 0 0-28.5 6.9l24.2-65.7c.7-2 1.9-3.2 3.4-3.2h29c1.5 0 2.7 1.2 3.4 3.2l24.2 65.7s-11.6-7-28.5-7L67 45.5c-.4-1.7-1.6-2.8-2.9-2.8-1.3 0-2.5 1.1-2.9 2.7L50.4 78.5Zm-1.1 28.2Zm-4.2-20.2c-2 6.6-.6 15.8 4.2 20.2a17.5 17.5 0 0 1 .2-.7 5.5 5.5 0 0 1 5.7-4.5c2.8.1 4.3 1.5 4.7 4.7.2 1.1.2 2.3.2 3.5v.4c0 2.7.7 5.2 2.2 7.4a13 13 0 0 0 5.7 4.9v-.3l-.2-.3c-1.8-5.6-.5-9.5 4.4-12.8l1.5-1a73 73 0 0 0 3.2-2.2 16 16 0 0 0 6.8-11.4c.3-2 .1-4-.6-6l-.8.6-1.6 1a37 37 0 0 1-22.4 2.7c-5-.7-9.7-2-13.2-6.2Z" />
|
||||
<style>
|
||||
path { fill: #000; }
|
||||
@media (prefers-color-scheme: dark) {
|
||||
path { fill: #FFF; }
|
||||
}
|
||||
</style>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 749 B |
BIN
public/fonts/DSEG7Classic-Regular.ttf
Normal file
BIN
public/fonts/DSEG7Classic-Regular.ttf
Normal file
Binary file not shown.
BIN
public/fonts/DSEG7Classic-Regular.woff
Normal file
BIN
public/fonts/DSEG7Classic-Regular.woff
Normal file
Binary file not shown.
115
public/js/binary/unsigned-binary.js
Normal file
115
public/js/binary/unsigned-binary.js
Normal file
@@ -0,0 +1,115 @@
|
||||
// Browser-only script. Safe because it's loaded via <script> (not server-imported).
|
||||
|
||||
const BIT_COUNT = 8; // unsigned page = 8 bits
|
||||
const bitValues = [128, 64, 32, 16, 8, 4, 2, 1];
|
||||
|
||||
const elDenary = document.getElementById("denaryNumber");
|
||||
const elBinary = document.getElementById("binaryNumber");
|
||||
const elSwitches = document.getElementById("bitSwitches");
|
||||
|
||||
const btnCustomDenary = document.getElementById("btnCustomDenary");
|
||||
const btnCustomBinary = document.getElementById("btnCustomBinary");
|
||||
const btnReset = document.getElementById("btnReset");
|
||||
|
||||
let bits = new Array(BIT_COUNT).fill(false);
|
||||
|
||||
function renderSwitches() {
|
||||
elSwitches.innerHTML = "";
|
||||
|
||||
bitValues.forEach((value, index) => {
|
||||
const id = `bit-${value}`;
|
||||
|
||||
const wrapper = document.createElement("div");
|
||||
wrapper.className = "switch-col";
|
||||
|
||||
const labelTop = document.createElement("div");
|
||||
labelTop.className = "bit-label";
|
||||
labelTop.textContent = value;
|
||||
|
||||
const label = document.createElement("label");
|
||||
label.className = "rocker";
|
||||
label.setAttribute("for", id);
|
||||
|
||||
const input = document.createElement("input");
|
||||
input.type = "checkbox";
|
||||
input.id = id;
|
||||
input.checked = bits[index];
|
||||
|
||||
input.addEventListener("change", () => {
|
||||
bits[index] = input.checked;
|
||||
updateNumbers();
|
||||
});
|
||||
|
||||
const span = document.createElement("span");
|
||||
span.className = "rocker-body";
|
||||
span.setAttribute("aria-hidden", "true");
|
||||
|
||||
label.appendChild(input);
|
||||
label.appendChild(span);
|
||||
|
||||
wrapper.appendChild(labelTop);
|
||||
wrapper.appendChild(label);
|
||||
|
||||
elSwitches.appendChild(wrapper);
|
||||
});
|
||||
}
|
||||
|
||||
function updateNumbers() {
|
||||
const binary = bits.map(b => (b ? "1" : "0")).join("");
|
||||
const denary = bits.reduce((acc, b, i) => acc + (b ? bitValues[i] : 0), 0);
|
||||
|
||||
elBinary.textContent = binary;
|
||||
elDenary.textContent = denary.toString();
|
||||
}
|
||||
|
||||
function resetAll() {
|
||||
bits = new Array(BIT_COUNT).fill(false);
|
||||
renderSwitches();
|
||||
updateNumbers();
|
||||
}
|
||||
|
||||
function requestCustomDenary() {
|
||||
let input = prompt(`Enter a denary number (0 to 255):`);
|
||||
if (input === null) return;
|
||||
|
||||
const n = Number.parseInt(input, 10);
|
||||
if (Number.isNaN(n) || n < 0 || n > 255) {
|
||||
alert("Invalid input. Enter a number from 0 to 255.");
|
||||
return;
|
||||
}
|
||||
|
||||
let remaining = n;
|
||||
bits = bitValues.map(v => {
|
||||
if (remaining >= v) {
|
||||
remaining -= v;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
renderSwitches();
|
||||
updateNumbers();
|
||||
}
|
||||
|
||||
function requestCustomBinary() {
|
||||
let input = prompt(`Enter an ${BIT_COUNT}-bit binary number (e.g. 01010101):`);
|
||||
if (input === null) return;
|
||||
|
||||
input = input.trim();
|
||||
const re = new RegExp(`^[01]{${BIT_COUNT}}$`);
|
||||
if (!re.test(input)) {
|
||||
alert(`Invalid input. Enter exactly ${BIT_COUNT} digits using only 0 or 1.`);
|
||||
return;
|
||||
}
|
||||
|
||||
bits = input.split("").map(c => c === "1");
|
||||
renderSwitches();
|
||||
updateNumbers();
|
||||
}
|
||||
|
||||
btnCustomDenary?.addEventListener("click", requestCustomDenary);
|
||||
btnCustomBinary?.addEventListener("click", requestCustomBinary);
|
||||
btnReset?.addEventListener("click", resetAll);
|
||||
|
||||
renderSwitches();
|
||||
updateNumbers();
|
||||
72
public/js/tools/unsigned-binary.js
Normal file
72
public/js/tools/unsigned-binary.js
Normal file
@@ -0,0 +1,72 @@
|
||||
// public/js/tools/unsigned-binary.js
|
||||
// Lightweight: no frameworks. Works on weak devices.
|
||||
const BIT_COUNT = 8;
|
||||
const MAX_DENARY = 255;
|
||||
let bits = new Array(BIT_COUNT).fill(false);
|
||||
function bitsToBinaryString(){
|
||||
return bits.map(b => (b ? "1" : "0")).join("");
|
||||
}
|
||||
function bitsToDenary(){
|
||||
// MSB on the left: 128..1
|
||||
const weights = [128,64,32,16,8,4,2,1];
|
||||
return bits.reduce((acc, b, i) => acc + (b ? weights[i] : 0), 0);
|
||||
}
|
||||
function render(){
|
||||
const grid = document.getElementById("bitGrid");
|
||||
grid.innerHTML = "";
|
||||
const weights = [128,64,32,16,8,4,2,1];
|
||||
bits.forEach((on, i) => {
|
||||
const btn = document.createElement("button");
|
||||
btn.type = "button";
|
||||
btn.className = "btn";
|
||||
btn.style.width = "100%";
|
||||
btn.style.justifyContent = "space-between";
|
||||
btn.setAttribute("aria-pressed", on ? "true" : "false");
|
||||
btn.innerHTML = `<span>${weights[i]}</span><b>${on ? "1" : "0"}</b>`;
|
||||
btn.addEventListener("click", () => {
|
||||
bits[i] = !bits[i];
|
||||
update();
|
||||
});
|
||||
grid.appendChild(btn);
|
||||
});
|
||||
}
|
||||
function update(){
|
||||
document.getElementById("binaryNumber").innerText = bitsToBinaryString();
|
||||
document.getElementById("denaryNumber").innerText = bitsToDenary();
|
||||
render();
|
||||
}
|
||||
function requestBinary(){
|
||||
let v;
|
||||
do{
|
||||
v = prompt("Enter an 8-bit binary value (8 digits, only 0 or 1):", bitsToBinaryString());
|
||||
if(v === null) return;
|
||||
v = v.trim();
|
||||
}while(!/^[01]{8}$/.test(v));
|
||||
bits = v.split("").map(ch => ch === "1");
|
||||
update();
|
||||
}
|
||||
function requestDenary(){
|
||||
let v;
|
||||
do{
|
||||
v = prompt("Enter a denary value (0 to 255):", String(bitsToDenary()));
|
||||
if(v === null) return;
|
||||
v = Number(v);
|
||||
}while(!Number.isInteger(v) || v < 0 || v > MAX_DENARY);
|
||||
// set bits from MSB to LSB
|
||||
const weights = [128,64,32,16,8,4,2,1];
|
||||
bits = weights.map(w => {
|
||||
if(v >= w){ v -= w; return true; }
|
||||
return false;
|
||||
});
|
||||
update();
|
||||
}
|
||||
function reset(){
|
||||
bits = new Array(BIT_COUNT).fill(false);
|
||||
update();
|
||||
}
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
document.getElementById("btnCustomBinary")?.addEventListener("click", requestBinary);
|
||||
document.getElementById("btnCustomDenary")?.addEventListener("click", requestDenary);
|
||||
document.getElementById("btnReset")?.addEventListener("click", reset);
|
||||
update();
|
||||
});
|
||||
96
public/styles.css
Normal file
96
public/styles.css
Normal file
@@ -0,0 +1,96 @@
|
||||
/* src/styles/md3-tokens.css */
|
||||
/* MD3-inspired tokens tuned for education: high readability, clear contrast, calm surfaces */
|
||||
:root{
|
||||
/* Typography */
|
||||
--font-sans: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
|
||||
--font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
|
||||
/* Spacing + shape */
|
||||
--radius-1: 10px;
|
||||
--radius-2: 16px;
|
||||
--radius-3: 22px;
|
||||
--shadow-1: 0 1px 2px rgba(0,0,0,.08), 0 2px 8px rgba(0,0,0,.06);
|
||||
/* Color roles (keep simple) */
|
||||
--md-surface: #ffffff;
|
||||
--md-surface-2: #f6f7fb;
|
||||
--md-on-surface: #111318;
|
||||
--md-primary: #2f6fed; /* calm blue */
|
||||
--md-on-primary: #ffffff;
|
||||
--md-secondary: #5a5f72; /* muted */
|
||||
--md-on-secondary: #ffffff;
|
||||
--md-tertiary: #0f766e; /* teal for "practical" tools */
|
||||
--md-on-tertiary: #ffffff;
|
||||
--md-outline: #d7dbe7;
|
||||
--md-success: #1a7f37;
|
||||
--md-warning: #b54708;
|
||||
--md-danger: #b42318;
|
||||
/* Focus ring for accessibility */
|
||||
--md-focus: 0 0 0 3px rgba(47,111,237,.28);
|
||||
}
|
||||
@media (prefers-color-scheme: dark){
|
||||
:root{
|
||||
--md-surface: #0b0e14;
|
||||
--md-surface-2: #121725;
|
||||
--md-on-surface: #e8eaf2;
|
||||
--md-primary: #9bb6ff;
|
||||
--md-on-primary: #0b0e14;
|
||||
--md-secondary: #b8bccd;
|
||||
--md-on-secondary: #0b0e14;
|
||||
--md-tertiary: #4fd1c5;
|
||||
--md-on-tertiary: #0b0e14;
|
||||
--md-outline: #2b3244;
|
||||
--md-focus: 0 0 0 3px rgba(155,182,255,.25);
|
||||
}
|
||||
}
|
||||
|
||||
/* src/styles/base.css */
|
||||
@import "./md3-tokens.css";
|
||||
html, body{ height:100%; }
|
||||
body{
|
||||
margin:0;
|
||||
font-family: var(--font-sans);
|
||||
background: var(--md-surface-2);
|
||||
color: var(--md-on-surface);
|
||||
}
|
||||
a{ color: var(--md-primary); text-decoration: none; }
|
||||
a:hover{ text-decoration: underline; }
|
||||
.container{
|
||||
max-width: 1100px;
|
||||
margin: 0 auto;
|
||||
padding: 16px;
|
||||
}
|
||||
.card{
|
||||
background: var(--md-surface);
|
||||
border: 1px solid var(--md-outline);
|
||||
border-radius: var(--radius-2);
|
||||
box-shadow: var(--shadow-1);
|
||||
padding: 16px;
|
||||
}
|
||||
.btn{
|
||||
display:inline-flex;
|
||||
gap:8px;
|
||||
align-items:center;
|
||||
justify-content:center;
|
||||
border-radius: 999px;
|
||||
border: 1px solid var(--md-outline);
|
||||
background: var(--md-surface);
|
||||
color: var(--md-on-surface);
|
||||
padding: 10px 14px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
.btn:hover{ filter: brightness(0.98); }
|
||||
.btn:focus{ outline:none; box-shadow: var(--md-focus); }
|
||||
.btn-primary{
|
||||
background: var(--md-primary);
|
||||
color: var(--md-on-primary);
|
||||
border-color: transparent;
|
||||
}
|
||||
.badge{
|
||||
display:inline-block;
|
||||
padding: 2px 10px;
|
||||
border-radius: 999px;
|
||||
font-size: 12px;
|
||||
border: 1px solid var(--md-outline);
|
||||
background: var(--md-surface-2);
|
||||
}
|
||||
code, pre{ font-family: var(--font-mono); }
|
||||
Reference in New Issue
Block a user