You've already forked computing-box
Compare commits
2 Commits
e6da9c8c98
...
ac585701a3
| Author | SHA1 | Date | |
|---|---|---|---|
| ac585701a3 | |||
| 002fbb8b6c |
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "computing-box",
|
||||
"name": "Computing:Box",
|
||||
"type": "module",
|
||||
"version": "0.0.1",
|
||||
"version": "2.0.0 Alpha",
|
||||
"scripts": {
|
||||
"dev": "astro dev",
|
||||
"build": "astro build",
|
||||
|
||||
@@ -1,263 +0,0 @@
|
||||
const bitsRows = document.getElementById("bitsRows");
|
||||
const denaryEl = document.getElementById("denaryNumber");
|
||||
const binaryEl = document.getElementById("binaryNumber");
|
||||
|
||||
const modeToggle = document.getElementById("modeToggle");
|
||||
const modeHint = document.getElementById("modeHint");
|
||||
|
||||
const bitsInput = document.getElementById("bitsInput");
|
||||
const btnBitsUp = document.getElementById("btnBitsUp");
|
||||
const btnBitsDown = document.getElementById("btnBitsDown");
|
||||
|
||||
const btnShiftLeft = document.getElementById("btnShiftLeft");
|
||||
const btnShiftRight = document.getElementById("btnShiftRight");
|
||||
const btnCustomBinary = document.getElementById("btnCustomBinary");
|
||||
const btnCustomDenary = document.getElementById("btnCustomDenary");
|
||||
|
||||
let bitCount = clampInt(Number(bitsInput.value || 8), 1, 64);
|
||||
let bits = new Array(bitCount).fill(false); // index 0 = MSB
|
||||
|
||||
function clampInt(n, min, max){
|
||||
n = Number(n);
|
||||
if (!Number.isFinite(n)) n = min;
|
||||
n = Math.floor(n);
|
||||
return Math.max(min, Math.min(max, n));
|
||||
}
|
||||
|
||||
function isTwos(){
|
||||
return !!modeToggle.checked;
|
||||
}
|
||||
|
||||
function msbValue(){
|
||||
return 2 ** (bitCount - 1);
|
||||
}
|
||||
|
||||
function unsignedValueAt(i){
|
||||
// i=0 is MSB
|
||||
return 2 ** (bitCount - 1 - i);
|
||||
}
|
||||
|
||||
function computeUnsigned(){
|
||||
let sum = 0;
|
||||
for (let i = 0; i < bitCount; i++){
|
||||
if (bits[i]) sum += unsignedValueAt(i);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
function computeDenary(){
|
||||
const u = computeUnsigned();
|
||||
if (!isTwos()) return u;
|
||||
|
||||
// Two's complement:
|
||||
// if MSB is 1, value = unsigned - 2^n
|
||||
if (bits[0]) return u - (2 ** bitCount);
|
||||
return u;
|
||||
}
|
||||
|
||||
function bitsToString(){
|
||||
return bits.map(b => (b ? "1" : "0")).join("");
|
||||
}
|
||||
|
||||
function updateModeHint(){
|
||||
if (isTwos()){
|
||||
modeHint.textContent = "Tip: In two's complement, the left-most bit (MSB) represents a negative value.";
|
||||
} else {
|
||||
modeHint.textContent = "Tip: In unsigned binary, all bits represent positive values.";
|
||||
}
|
||||
}
|
||||
|
||||
function buildBitsUI(){
|
||||
bitsRows.innerHTML = "";
|
||||
|
||||
// Build rows of 8 bits
|
||||
const rowCount = Math.ceil(bitCount / 8);
|
||||
|
||||
for (let r = 0; r < rowCount; r++){
|
||||
const row = document.createElement("div");
|
||||
row.className = "byteRow";
|
||||
|
||||
const start = r * 8;
|
||||
const end = Math.min(start + 8, bitCount);
|
||||
|
||||
for (let i = start; i < end; i++){
|
||||
const bitEl = document.createElement("div");
|
||||
bitEl.className = "bit";
|
||||
|
||||
// label: show -MSB in two's complement
|
||||
const labelVal = (isTwos() && i === 0) ? -msbValue() : unsignedValueAt(i);
|
||||
|
||||
bitEl.innerHTML = `
|
||||
<span class="bulb" id="bulb-${i}" aria-hidden="true">💡</span>
|
||||
<div class="bitVal">${labelVal}</div>
|
||||
<label class="switch" aria-label="Toggle bit ${labelVal}">
|
||||
<input type="checkbox" data-index="${i}">
|
||||
<span class="slider"></span>
|
||||
</label>
|
||||
`;
|
||||
|
||||
row.appendChild(bitEl);
|
||||
}
|
||||
|
||||
bitsRows.appendChild(row);
|
||||
}
|
||||
|
||||
// Hook switches
|
||||
bitsRows.querySelectorAll('input[type="checkbox"][data-index]').forEach(input => {
|
||||
input.addEventListener("change", () => {
|
||||
const i = Number(input.dataset.index);
|
||||
bits[i] = input.checked;
|
||||
updateReadout();
|
||||
});
|
||||
});
|
||||
|
||||
syncUI();
|
||||
}
|
||||
|
||||
function syncUI(){
|
||||
// sync inputs
|
||||
bitsRows.querySelectorAll('input[type="checkbox"][data-index]').forEach(input => {
|
||||
const i = Number(input.dataset.index);
|
||||
input.checked = !!bits[i];
|
||||
});
|
||||
|
||||
// sync bulbs
|
||||
for (let i = 0; i < bitCount; i++){
|
||||
const bulb = document.getElementById(`bulb-${i}`);
|
||||
if (bulb) bulb.classList.toggle("on", !!bits[i]);
|
||||
}
|
||||
|
||||
updateReadout();
|
||||
}
|
||||
|
||||
function updateReadout(){
|
||||
denaryEl.textContent = String(computeDenary());
|
||||
binaryEl.textContent = bitsToString();
|
||||
}
|
||||
|
||||
function setFromBinary(bin){
|
||||
const clean = String(bin).replace(/\s+/g, "");
|
||||
if (!/^[01]+$/.test(clean)) return false;
|
||||
|
||||
const padded = clean.slice(-bitCount).padStart(bitCount, "0");
|
||||
for (let i = 0; i < bitCount; i++){
|
||||
bits[i] = padded[i] === "1";
|
||||
}
|
||||
|
||||
syncUI();
|
||||
return true;
|
||||
}
|
||||
|
||||
function setFromDenary(n){
|
||||
n = Number(n);
|
||||
if (!Number.isInteger(n)) return false;
|
||||
|
||||
if (!isTwos()){
|
||||
// unsigned: 0 .. (2^n - 1)
|
||||
const max = (2 ** bitCount) - 1;
|
||||
if (n < 0 || n > max) return false;
|
||||
|
||||
for (let i = 0; i < bitCount; i++){
|
||||
const v = unsignedValueAt(i);
|
||||
if (n >= v){
|
||||
bits[i] = true;
|
||||
n -= v;
|
||||
} else {
|
||||
bits[i] = false;
|
||||
}
|
||||
}
|
||||
syncUI();
|
||||
return true;
|
||||
}
|
||||
|
||||
// two's complement: -(2^(n-1)) .. (2^(n-1)-1)
|
||||
const min = -(2 ** (bitCount - 1));
|
||||
const max = (2 ** (bitCount - 1)) - 1;
|
||||
if (n < min || n > max) return false;
|
||||
|
||||
// convert to unsigned representation
|
||||
let u = n;
|
||||
if (u < 0) u = (2 ** bitCount) + u; // wrap
|
||||
|
||||
for (let i = 0; i < bitCount; i++){
|
||||
const v = unsignedValueAt(i);
|
||||
if (u >= v){
|
||||
bits[i] = true;
|
||||
u -= v;
|
||||
} else {
|
||||
bits[i] = false;
|
||||
}
|
||||
}
|
||||
|
||||
syncUI();
|
||||
return true;
|
||||
}
|
||||
|
||||
function shiftLeft(){
|
||||
bits.shift(); // drop MSB
|
||||
bits.push(false); // add LSB
|
||||
syncUI();
|
||||
}
|
||||
|
||||
function shiftRight(){
|
||||
bits.pop(); // drop LSB
|
||||
bits.unshift(false); // add MSB
|
||||
syncUI();
|
||||
}
|
||||
|
||||
function setBitCount(newCount){
|
||||
newCount = clampInt(newCount, 4, 64);
|
||||
if (newCount === bitCount) return;
|
||||
|
||||
// preserve right-most bits (LSB side) when resizing
|
||||
const old = bits.slice();
|
||||
const next = new Array(newCount).fill(false);
|
||||
|
||||
const copy = Math.min(old.length, next.length);
|
||||
for (let k = 0; k < copy; k++){
|
||||
// copy from end (LSB)
|
||||
next[newCount - 1 - k] = old[old.length - 1 - k];
|
||||
}
|
||||
|
||||
bitCount = newCount;
|
||||
bits = next;
|
||||
|
||||
bitsInput.value = String(bitCount);
|
||||
buildBitsUI();
|
||||
}
|
||||
|
||||
/* -------------------- events -------------------- */
|
||||
|
||||
modeToggle.addEventListener("change", () => {
|
||||
updateModeHint();
|
||||
buildBitsUI(); // rebuild labels (MSB becomes negative/positive)
|
||||
});
|
||||
|
||||
btnBitsUp.addEventListener("click", () => setBitCount(bitCount + 1));
|
||||
btnBitsDown.addEventListener("click", () => setBitCount(bitCount - 1));
|
||||
|
||||
bitsInput.addEventListener("change", () => setBitCount(Number(bitsInput.value)));
|
||||
|
||||
btnShiftLeft.addEventListener("click", shiftLeft);
|
||||
btnShiftRight.addEventListener("click", shiftRight);
|
||||
|
||||
btnCustomBinary.addEventListener("click", () => {
|
||||
const v = prompt(`Enter ${bitCount}-bit binary:`);
|
||||
if (v === null) return;
|
||||
if (!setFromBinary(v)) alert("Invalid binary. Use only 0 and 1.");
|
||||
});
|
||||
|
||||
btnCustomDenary.addEventListener("click", () => {
|
||||
const min = isTwos() ? -(2 ** (bitCount - 1)) : 0;
|
||||
const max = isTwos() ? (2 ** (bitCount - 1)) - 1 : (2 ** bitCount) - 1;
|
||||
|
||||
const v = prompt(`Enter a denary number (${min} to ${max}):`);
|
||||
if (v === null) return;
|
||||
if (!setFromDenary(Number(v))) alert("Invalid denary for current mode/bit width.");
|
||||
});
|
||||
|
||||
/* -------------------- init -------------------- */
|
||||
|
||||
bitsInput.value = String(bitCount);
|
||||
updateModeHint();
|
||||
buildBitsUI();
|
||||
@@ -1,7 +1,29 @@
|
||||
<footer class="siteFooter">
|
||||
<div class="siteFooter__inner">
|
||||
<div>Computer Science Concept Simulators</div>
|
||||
<div>© 2025 Computing:Box · Created with ❤️ by Mr Lyall</div>
|
||||
<div>Powered by ADCM Networks</div>
|
||||
<div class="inner">
|
||||
<div class="title">Computer Science Concept Simulators</div>
|
||||
<div class="meta">
|
||||
© 2025 Computing:Box · Created with 💗 by Mr Lyall<br />
|
||||
Powered by ADCM Networks
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
<style>
|
||||
.siteFooter{
|
||||
border-top: 1px solid rgba(255,255,255,.10);
|
||||
background: rgba(0,0,0,.10);
|
||||
}
|
||||
.inner{
|
||||
max-width:1200px;
|
||||
margin:0 auto;
|
||||
padding: 18px 20px;
|
||||
color: rgba(255,255,255,.65);
|
||||
font-size: 12px;
|
||||
line-height: 1.6;
|
||||
}
|
||||
.title{
|
||||
color: rgba(255,255,255,.80);
|
||||
font-weight: 800;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -1,13 +1,57 @@
|
||||
<header class="siteHeader">
|
||||
<div class="siteHeader__inner">
|
||||
<a class="brand" href="/">Computing:Box</a>
|
||||
<div class="inner">
|
||||
<div class="brand">
|
||||
<a href="/">Computing:Box</a>
|
||||
</div>
|
||||
|
||||
<nav class="nav">
|
||||
<a class="nav__link" href="/binary">Binary</a>
|
||||
<a class="nav__link" href="/hexadecimal">Hexadecimal</a>
|
||||
<a class="nav__link" href="/hex-colours">Hex Colours</a>
|
||||
<a class="nav__link" href="/logic-gates">Logic Gates</a>
|
||||
<a class="nav__link" href="/about">About</a>
|
||||
<a href="/binary">Binary</a>
|
||||
<a href="/hexadecimal">Hexadecimal</a>
|
||||
<a href="/hex-colours">Hex Colours</a>
|
||||
<a href="/logic-gates">Logic Gates</a>
|
||||
<a href="/about">About</a>
|
||||
</nav>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<style>
|
||||
.siteHeader{
|
||||
height: 64px;
|
||||
display:flex;
|
||||
align-items:center;
|
||||
border-bottom: 1px solid rgba(255,255,255,.10);
|
||||
background: rgba(0,0,0,.10);
|
||||
backdrop-filter: blur(8px);
|
||||
}
|
||||
.inner{
|
||||
max-width: 1200px;
|
||||
width: 100%;
|
||||
margin: 0 auto;
|
||||
padding: 0 20px;
|
||||
display:flex;
|
||||
align-items:center;
|
||||
justify-content: space-between;
|
||||
gap: 18px;
|
||||
}
|
||||
.brand a{
|
||||
color:#fff;
|
||||
text-decoration:none;
|
||||
font-weight: 800;
|
||||
letter-spacing: .02em;
|
||||
}
|
||||
.nav{
|
||||
display:flex;
|
||||
gap: 16px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.nav a{
|
||||
color: rgba(255,255,255,.78);
|
||||
text-decoration:none;
|
||||
font-weight: 700;
|
||||
font-size: 13px;
|
||||
letter-spacing:.02em;
|
||||
}
|
||||
.nav a:hover{
|
||||
color:#fff;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -1,381 +0,0 @@
|
||||
---
|
||||
const { mode = "unsigned", defaultBits = 8 } = Astro.props;
|
||||
|
||||
// For unsigned: min 1 bit, max 16 bits (tweak if you want)
|
||||
const minBits = 4;
|
||||
const maxBits = 16;
|
||||
const initialBits = Math.min(Math.max(defaultBits, minBits), maxBits);
|
||||
---
|
||||
|
||||
<section class="binary-sim" data-mode={mode} data-bits={initialBits}>
|
||||
<div class="top">
|
||||
<div class="left-brand">
|
||||
<!-- Replace with your logo/img -->
|
||||
<div class="brand-box" aria-hidden="true">Computing:Box</div>
|
||||
</div>
|
||||
|
||||
<div class="display" aria-live="polite">
|
||||
<div class="label">DENARY</div>
|
||||
<div id="denaryNumber" class="value">0</div>
|
||||
|
||||
<div class="label">BINARY</div>
|
||||
<div id="binaryNumber" class="value mono">00000000</div>
|
||||
</div>
|
||||
|
||||
<div class="actions">
|
||||
<button class="btn" type="button" data-action="custom-binary">Custom Binary</button>
|
||||
<button class="btn" type="button" data-action="custom-denary">Custom Denary</button>
|
||||
<button class="btn" type="button" data-action="shift-left">Left Shift</button>
|
||||
<button class="btn" type="button" data-action="shift-right">Right Shift</button>
|
||||
|
||||
<div class="bits-control">
|
||||
<div class="bits-title">Bits</div>
|
||||
<div class="bits-buttons">
|
||||
<button class="btn small" type="button" data-action="bits-minus" aria-label="Decrease bits">−</button>
|
||||
<span id="bitsCount" class="bits-count">{initialBits}</span>
|
||||
<button class="btn small" type="button" data-action="bits-plus" aria-label="Increase bits">+</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="bitsRow" class="bits-row" aria-label="Binary bit switches"></div>
|
||||
</section>
|
||||
|
||||
<script is:inline>
|
||||
(() => {
|
||||
const root = document.currentScript.closest(".binary-sim");
|
||||
const bitsRow = root.querySelector("#bitsRow");
|
||||
const binaryEl = root.querySelector("#binaryNumber");
|
||||
const denaryEl = root.querySelector("#denaryNumber");
|
||||
const bitsCountEl = root.querySelector("#bitsCount");
|
||||
|
||||
let bitCount = parseInt(root.dataset.bits || "8", 10);
|
||||
const minBits = 4;
|
||||
const maxBits = 16;
|
||||
|
||||
// state: array of 0/1, MSB at index 0
|
||||
let bits = new Array(bitCount).fill(0);
|
||||
|
||||
function placeValues(n) {
|
||||
// unsigned place values: [2^(n-1), ..., 2^0]
|
||||
return Array.from({ length: n }, (_, i) => 2 ** (n - 1 - i));
|
||||
}
|
||||
|
||||
function bitsToDenary(bitsArr) {
|
||||
const pv = placeValues(bitsArr.length);
|
||||
return bitsArr.reduce((acc, b, i) => acc + (b ? pv[i] : 0), 0);
|
||||
}
|
||||
|
||||
function render() {
|
||||
const binaryStr = bits.join("");
|
||||
binaryEl.textContent = binaryStr;
|
||||
denaryEl.textContent = String(bitsToDenary(bits));
|
||||
bitsCountEl.textContent = String(bitCount);
|
||||
|
||||
const pv = placeValues(bitCount);
|
||||
|
||||
bitsRow.innerHTML = pv.map((value, i) => {
|
||||
const id = `bit_${bitCount}_${i}`;
|
||||
const checked = bits[i] === 1 ? "checked" : "";
|
||||
return `
|
||||
<div class="bit-col">
|
||||
<div class="bulb ${bits[i] ? "on" : "off"}" aria-hidden="true"></div>
|
||||
<div class="place">${value}</div>
|
||||
|
||||
<label class="switch" for="${id}">
|
||||
<input id="${id}" type="checkbox" ${checked} data-index="${i}">
|
||||
<span class="slider" aria-hidden="true"></span>
|
||||
<span class="sr-only">Toggle bit value ${value}</span>
|
||||
</label>
|
||||
</div>
|
||||
`;
|
||||
}).join("");
|
||||
}
|
||||
|
||||
function setBitsFromBinaryString(str) {
|
||||
// allow shorter input; pad left
|
||||
const clean = (str || "").trim();
|
||||
if (!/^[01]+$/.test(clean) || clean.length > bitCount) return false;
|
||||
const padded = clean.padStart(bitCount, "0");
|
||||
bits = padded.split("").map(c => c === "1" ? 1 : 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
function setBitsFromDenary(num) {
|
||||
if (!Number.isInteger(num)) return false;
|
||||
const max = (2 ** bitCount) - 1;
|
||||
if (num < 0 || num > max) return false;
|
||||
|
||||
const bin = num.toString(2).padStart(bitCount, "0");
|
||||
bits = bin.split("").map(c => c === "1" ? 1 : 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
function shiftLeft() {
|
||||
bits = bits.slice(1).concat(0);
|
||||
}
|
||||
|
||||
function shiftRight() {
|
||||
bits = [0].concat(bits.slice(0, -1));
|
||||
}
|
||||
|
||||
function resizeBits(newCount) {
|
||||
const clamped = Math.max(minBits, Math.min(maxBits, newCount));
|
||||
if (clamped === bitCount) return;
|
||||
|
||||
// keep value as best as possible: preserve LSBs when resizing
|
||||
const currentBinary = bits.join("");
|
||||
bitCount = clamped;
|
||||
bits = new Array(bitCount).fill(0);
|
||||
|
||||
const take = currentBinary.slice(-bitCount);
|
||||
setBitsFromBinaryString(take);
|
||||
}
|
||||
|
||||
// Switch input handler
|
||||
bitsRow.addEventListener("change", (e) => {
|
||||
const input = e.target;
|
||||
if (!(input instanceof HTMLInputElement)) return;
|
||||
const idx = parseInt(input.dataset.index || "-1", 10);
|
||||
if (idx < 0) return;
|
||||
bits[idx] = input.checked ? 1 : 0;
|
||||
render();
|
||||
});
|
||||
|
||||
// Button actions
|
||||
root.querySelector(".actions").addEventListener("click", (e) => {
|
||||
const btn = e.target.closest("button[data-action]");
|
||||
if (!btn) return;
|
||||
|
||||
const action = btn.dataset.action;
|
||||
|
||||
if (action === "custom-binary") {
|
||||
const entered = prompt(`Enter a ${bitCount}-bit binary value (0s and 1s):`, bits.join(""));
|
||||
if (entered === null) return;
|
||||
if (!setBitsFromBinaryString(entered)) {
|
||||
alert(`Invalid binary. Use only 0 and 1, up to ${bitCount} digits.`);
|
||||
return;
|
||||
}
|
||||
render();
|
||||
}
|
||||
|
||||
if (action === "custom-denary") {
|
||||
const max = (2 ** bitCount) - 1;
|
||||
const entered = prompt(`Enter a denary value (0 to ${max}):`, String(bitsToDenary(bits)));
|
||||
if (entered === null) return;
|
||||
const num = Number.parseInt(entered, 10);
|
||||
if (!setBitsFromDenary(num)) {
|
||||
alert(`Invalid denary. Enter a whole number from 0 to ${max}.`);
|
||||
return;
|
||||
}
|
||||
render();
|
||||
}
|
||||
|
||||
if (action === "shift-left") { shiftLeft(); render(); }
|
||||
if (action === "shift-right") { shiftRight(); render(); }
|
||||
|
||||
if (action === "bits-minus") { resizeBits(bitCount - 1); render(); }
|
||||
if (action === "bits-plus") { resizeBits(bitCount + 1); render(); }
|
||||
});
|
||||
|
||||
// initial
|
||||
render();
|
||||
})();
|
||||
</script>
|
||||
|
||||
<style>
|
||||
/* Layout */
|
||||
.binary-sim {
|
||||
padding: 2rem 1rem 3rem;
|
||||
}
|
||||
|
||||
.top {
|
||||
display: grid;
|
||||
grid-template-columns: 260px 1fr 220px;
|
||||
gap: 1.5rem;
|
||||
align-items: start;
|
||||
}
|
||||
|
||||
@media (max-width: 980px) {
|
||||
.top {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
.actions {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
/* Brand placeholder */
|
||||
.brand-box {
|
||||
width: 180px;
|
||||
height: 180px;
|
||||
border-radius: 12px;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
font-weight: 700;
|
||||
opacity: 0.9;
|
||||
border: 1px solid rgba(255,255,255,0.08);
|
||||
background: rgba(255,255,255,0.04);
|
||||
}
|
||||
|
||||
/* Display */
|
||||
.display {
|
||||
text-align: center;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.label {
|
||||
letter-spacing: 0.12em;
|
||||
opacity: 0.85;
|
||||
margin-top: 0.5rem;
|
||||
}
|
||||
|
||||
.value {
|
||||
font-size: 3rem;
|
||||
line-height: 1.1;
|
||||
margin: 0.25rem 0 0.75rem;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.mono {
|
||||
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
||||
}
|
||||
|
||||
/* Buttons */
|
||||
.actions {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.btn {
|
||||
border: 0;
|
||||
border-radius: 10px;
|
||||
padding: 0.75rem 1rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
background: rgba(255,255,255,0.08);
|
||||
color: inherit;
|
||||
}
|
||||
|
||||
.btn:hover { background: rgba(255,255,255,0.12); }
|
||||
|
||||
.btn.small {
|
||||
padding: 0.45rem 0.75rem;
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
.bits-control {
|
||||
margin-top: 0.25rem;
|
||||
padding-top: 0.5rem;
|
||||
border-top: 1px solid rgba(255,255,255,0.08);
|
||||
}
|
||||
|
||||
.bits-title { opacity: 0.8; margin-bottom: 0.35rem; }
|
||||
|
||||
.bits-buttons {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.bits-count {
|
||||
min-width: 2ch;
|
||||
text-align: center;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
/* Bits row */
|
||||
.bits-row {
|
||||
margin-top: 2rem;
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(88px, 1fr));
|
||||
gap: 1.25rem;
|
||||
align-items: end;
|
||||
}
|
||||
|
||||
.bit-col {
|
||||
text-align: center;
|
||||
padding: 0.5rem 0.25rem;
|
||||
}
|
||||
|
||||
.place {
|
||||
margin: 0.5rem 0 0.5rem;
|
||||
font-size: 2rem;
|
||||
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
||||
}
|
||||
|
||||
/* Bulb */
|
||||
.bulb {
|
||||
width: 26px;
|
||||
height: 26px;
|
||||
margin: 0 auto 0.25rem;
|
||||
border-radius: 50%;
|
||||
border: 1px solid rgba(255,255,255,0.15);
|
||||
}
|
||||
.bulb.off { opacity: 0.15; }
|
||||
.bulb.on { opacity: 1; }
|
||||
|
||||
/* Light switch (accessible checkbox) */
|
||||
.switch {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 64px;
|
||||
height: 36px;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.switch input {
|
||||
position: absolute;
|
||||
opacity: 0;
|
||||
width: 1px;
|
||||
height: 1px;
|
||||
}
|
||||
|
||||
.slider {
|
||||
width: 64px;
|
||||
height: 36px;
|
||||
border-radius: 999px;
|
||||
background: rgba(255,255,255,0.10);
|
||||
border: 1px solid rgba(255,255,255,0.14);
|
||||
position: relative;
|
||||
transition: transform 120ms ease, background 120ms ease;
|
||||
}
|
||||
|
||||
.slider::after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
top: 4px;
|
||||
left: 4px;
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border-radius: 999px;
|
||||
background: rgba(255,255,255,0.85);
|
||||
transition: transform 120ms ease;
|
||||
}
|
||||
|
||||
.switch input:checked + .slider {
|
||||
background: rgba(255,255,255,0.18);
|
||||
}
|
||||
|
||||
.switch input:checked + .slider::after {
|
||||
transform: translateX(28px);
|
||||
}
|
||||
|
||||
/* focus */
|
||||
.switch input:focus-visible + .slider {
|
||||
outline: 3px solid rgba(255,255,255,0.35);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
.sr-only {
|
||||
position: absolute;
|
||||
width: 1px; height: 1px;
|
||||
padding: 0; margin: -1px;
|
||||
overflow: hidden; clip: rect(0,0,0,0);
|
||||
white-space: nowrap; border: 0;
|
||||
}
|
||||
</style>
|
||||
@@ -1,7 +1,4 @@
|
||||
---
|
||||
import Header from "../components/Header.astro";
|
||||
import Footer from "../components/Footer.astro";
|
||||
|
||||
const { title = "Computing:Box" } = Astro.props;
|
||||
---
|
||||
|
||||
@@ -11,19 +8,34 @@ const { title = "Computing:Box" } = Astro.props;
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
||||
<title>{title}</title>
|
||||
|
||||
<!-- Global site styling -->
|
||||
<link rel="stylesheet" href="/styles/site.css" />
|
||||
|
||||
<!-- Page specific styles can be added by each page -->
|
||||
<slot name="head" />
|
||||
<link rel="stylesheet" href="/src/styles/site.css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<Header />
|
||||
<main class="page">
|
||||
<header class="site-header">
|
||||
<div class="site-header__inner">
|
||||
<div class="brand">Computing:Box</div>
|
||||
|
||||
<nav class="nav">
|
||||
<a class="nav__link" href="/binary">Binary</a>
|
||||
<a class="nav__link" href="/hexadecimal">Hexadecimal</a>
|
||||
<a class="nav__link" href="/hex-colours">Hex Colours</a>
|
||||
<a class="nav__link" href="/logic-gates">Logic Gates</a>
|
||||
<a class="nav__link" href="/about">About</a>
|
||||
</nav>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main class="site-main">
|
||||
<slot />
|
||||
</main>
|
||||
<Footer />
|
||||
|
||||
<footer class="site-footer">
|
||||
<div class="site-footer__inner">
|
||||
<div>Computer Science Concept Simulators</div>
|
||||
<div>© 2025 Computing:Box · Created with 💜 by Mr Lyall</div>
|
||||
<div>Powered by ADCM Networks</div>
|
||||
</div>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,36 +1,43 @@
|
||||
---
|
||||
import BaseLayout from "../layouts/BaseLayout.astro";
|
||||
import "../styles/binary.css";
|
||||
|
||||
// ✅ Correct Astro v5 way: bundle script from src/ and get its final URL
|
||||
import binaryScriptUrl from "../scripts/binary.js?url";
|
||||
---
|
||||
|
||||
<BaseLayout title="Binary | Computing:Box">
|
||||
<link rel="stylesheet" href="/styles/binary.css" />
|
||||
|
||||
<section class="binaryWrap">
|
||||
<section class="binary-wrap">
|
||||
<div class="topGrid">
|
||||
<!-- LEFT -->
|
||||
<!-- LEFT: readout + main buttons -->
|
||||
<div>
|
||||
<div class="readout">
|
||||
<div class="label">Denary</div>
|
||||
<div id="denaryNumber" class="num denary">0</div>
|
||||
|
||||
<div class="label">Binary</div>
|
||||
<div id="binaryNumber" class="num binary">00000000</div>
|
||||
<pre id="binaryNumber" class="num binary" aria-label="Binary value">0</pre>
|
||||
|
||||
<div class="controls">
|
||||
<button class="btn" id="btnCustomBinary" type="button">Custom Binary</button>
|
||||
<button class="btn" id="btnCustomDenary" type="button">Custom Denary</button>
|
||||
<!-- ORANGE: custom + shifts on separate lines -->
|
||||
<div class="controls controls--twoRows">
|
||||
<div class="controlsRow">
|
||||
<button class="btn btn--green" id="btnCustomBinary" type="button">Custom Binary</button>
|
||||
<button class="btn btn--green" id="btnCustomDenary" type="button">Custom Denary</button>
|
||||
</div>
|
||||
<div class="controlsRow">
|
||||
<button class="btn" id="btnShiftLeft" type="button">Left Shift</button>
|
||||
<button class="btn" id="btnShiftRight" type="button">Right Shift</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="divider"></div>
|
||||
|
||||
<!-- Bit rows injected by JS -->
|
||||
<section id="bitsRows" class="bitsRows" aria-label="Bit switches"></section>
|
||||
<!-- Bits render here -->
|
||||
<section class="bits" id="bitsGrid" aria-label="Bit switches"></section>
|
||||
</div>
|
||||
|
||||
<!-- RIGHT -->
|
||||
<!-- RIGHT: red buttons go above/below this panel (not in the middle) -->
|
||||
<aside class="panelCol">
|
||||
<div class="card">
|
||||
<div class="cardTitle">Mode</div>
|
||||
@@ -43,7 +50,7 @@ import BaseLayout from "../layouts/BaseLayout.astro";
|
||||
<span class="slider"></span>
|
||||
</label>
|
||||
|
||||
<div class="toggleLabel" id="lblTwos">Two's complement</div>
|
||||
<div class="toggleLabel" id="lblTwos">Two's complement</div>
|
||||
</div>
|
||||
|
||||
<div class="hint" id="modeHint">
|
||||
@@ -51,6 +58,20 @@ import BaseLayout from "../layouts/BaseLayout.astro";
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- RED: extra buttons ABOVE bit width -->
|
||||
<div class="card">
|
||||
<div class="cardTitle">Tools</div>
|
||||
<div class="toolsGrid">
|
||||
<button class="btn" id="btnClear" type="button">Clear</button>
|
||||
<button class="btn btn--spin" id="btnMinus1" type="button">−1</button>
|
||||
<button class="btn btn--spin" id="btnPlus1" type="button">+1</button>
|
||||
<button class="btn" id="btnAutoRandom" type="button">Auto Random</button>
|
||||
</div>
|
||||
<div class="hint">
|
||||
Auto Random runs briefly then stops automatically.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div class="cardTitle">Bit width</div>
|
||||
|
||||
@@ -59,17 +80,27 @@ import BaseLayout from "../layouts/BaseLayout.astro";
|
||||
|
||||
<div class="bitInputWrap">
|
||||
<div class="bitInputLabel">Bits</div>
|
||||
<input id="bitsInput" class="bitInput" type="number" inputmode="numeric" min="4" max="64" step="1" value="8" />
|
||||
<input
|
||||
id="bitsInput"
|
||||
class="bitInput"
|
||||
type="number"
|
||||
inputmode="numeric"
|
||||
min="1"
|
||||
max="64"
|
||||
step="1"
|
||||
value="8"
|
||||
aria-label="Number of bits"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button class="miniBtn" id="btnBitsUp" type="button" aria-label="Increase bits">+</button>
|
||||
</div>
|
||||
|
||||
<div class="hint">Minimum 4 bits, maximum 64 bits.</div>
|
||||
<div class="hint">Minimum 1 bit, maximum 64 bits.</div>
|
||||
</div>
|
||||
</aside>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<script type="module" src="/scripts/binary.js"></script>
|
||||
<script type="module" src={binaryScriptUrl}></script>
|
||||
</section>
|
||||
</BaseLayout>
|
||||
|
||||
347
src/scripts/binary.js
Normal file
347
src/scripts/binary.js
Normal file
@@ -0,0 +1,347 @@
|
||||
const bitsGrid = document.getElementById("bitsGrid");
|
||||
const denaryEl = document.getElementById("denaryNumber");
|
||||
const binaryEl = document.getElementById("binaryNumber");
|
||||
|
||||
const modeToggle = document.getElementById("modeToggle");
|
||||
const modeHint = document.getElementById("modeHint");
|
||||
|
||||
const bitsInput = document.getElementById("bitsInput");
|
||||
const btnBitsUp = document.getElementById("btnBitsUp");
|
||||
const btnBitsDown = document.getElementById("btnBitsDown");
|
||||
|
||||
const btnShiftLeft = document.getElementById("btnShiftLeft");
|
||||
const btnShiftRight = document.getElementById("btnShiftRight");
|
||||
const btnCustomBinary = document.getElementById("btnCustomBinary");
|
||||
const btnCustomDenary = document.getElementById("btnCustomDenary");
|
||||
|
||||
const btnClear = document.getElementById("btnClear");
|
||||
const btnMinus1 = document.getElementById("btnMinus1");
|
||||
const btnPlus1 = document.getElementById("btnPlus1");
|
||||
const btnAutoRandom = document.getElementById("btnAutoRandom");
|
||||
|
||||
let bitCount = clampInt(Number(bitsInput?.value ?? 8), 1, 64);
|
||||
let isTwos = Boolean(modeToggle?.checked);
|
||||
|
||||
let bits = new Array(bitCount).fill(false); // MSB at index 0
|
||||
let autoTimer = null;
|
||||
|
||||
function clampInt(n, min, max){
|
||||
n = Number(n);
|
||||
if (!Number.isFinite(n)) return min;
|
||||
n = Math.trunc(n);
|
||||
return Math.max(min, Math.min(max, n));
|
||||
}
|
||||
|
||||
/* ----------------------------
|
||||
Label values (MSB..LSB)
|
||||
Unsigned: [2^(n-1) ... 1]
|
||||
Two's: [-2^(n-1), 2^(n-2) ... 1]
|
||||
----------------------------- */
|
||||
function getLabelValues(){
|
||||
const vals = [];
|
||||
for (let i = 0; i < bitCount; i++){
|
||||
const pow = bitCount - 1 - i;
|
||||
let v = 2 ** pow;
|
||||
if (isTwos && i === 0) v = -v; // ✅ MSB label becomes negative
|
||||
vals.push(v);
|
||||
}
|
||||
return vals;
|
||||
}
|
||||
|
||||
function buildBits(){
|
||||
// wrap every 8 bits
|
||||
bitsGrid.style.setProperty("--cols", String(Math.min(8, bitCount)));
|
||||
|
||||
bitsGrid.innerHTML = "";
|
||||
const labelValues = getLabelValues();
|
||||
|
||||
for (let i = 0; i < bitCount; i++){
|
||||
const bit = document.createElement("div");
|
||||
bit.className = "bit";
|
||||
bit.innerHTML = `
|
||||
<div class="bulb" id="bulb-${i}" aria-hidden="true">💡</div>
|
||||
<div class="bitVal num" id="label-${i}">${labelValues[i]}</div>
|
||||
<label class="switch" aria-label="Toggle bit">
|
||||
<input type="checkbox" data-index="${i}">
|
||||
<span class="slider"></span>
|
||||
</label>
|
||||
`;
|
||||
bitsGrid.appendChild(bit);
|
||||
}
|
||||
|
||||
// hook switches
|
||||
bitsGrid.querySelectorAll('input[type="checkbox"][data-index]').forEach((input) => {
|
||||
input.addEventListener("change", () => {
|
||||
const idx = Number(input.dataset.index);
|
||||
bits[idx] = input.checked;
|
||||
updateUI();
|
||||
});
|
||||
});
|
||||
|
||||
updateUI();
|
||||
}
|
||||
|
||||
function setLabels(){
|
||||
const labelValues = getLabelValues();
|
||||
for (let i = 0; i < bitCount; i++){
|
||||
const el = document.getElementById(`label-${i}`);
|
||||
if (el) el.textContent = String(labelValues[i]);
|
||||
}
|
||||
}
|
||||
|
||||
function bitsToUnsigned(){
|
||||
let n = 0;
|
||||
for (let i = 0; i < bitCount; i++){
|
||||
if (!bits[i]) continue;
|
||||
const pow = bitCount - 1 - i;
|
||||
n += 2 ** pow;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
function bitsToTwos(){
|
||||
// Two's complement interpretation
|
||||
// value = -MSB*2^(n-1) + sum(other set bits)
|
||||
let n = 0;
|
||||
for (let i = 0; i < bitCount; i++){
|
||||
if (!bits[i]) continue;
|
||||
const pow = bitCount - 1 - i;
|
||||
const v = 2 ** pow;
|
||||
if (i === 0) n -= v;
|
||||
else n += v;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
function getCurrentValue(){
|
||||
return isTwos ? bitsToTwos() : bitsToUnsigned();
|
||||
}
|
||||
|
||||
function setFromUnsignedValue(n){
|
||||
// clamp to range of bitCount
|
||||
const max = (2 ** bitCount) - 1;
|
||||
n = clampInt(n, 0, max);
|
||||
|
||||
for (let i = 0; i < bitCount; i++){
|
||||
const pow = bitCount - 1 - i;
|
||||
const v = 2 ** pow;
|
||||
if (n >= v){
|
||||
bits[i] = true;
|
||||
n -= v;
|
||||
} else {
|
||||
bits[i] = false;
|
||||
}
|
||||
}
|
||||
syncSwitchesAndBulbs();
|
||||
updateUI(false);
|
||||
}
|
||||
|
||||
function setFromTwosValue(n){
|
||||
// represent in two's complement with bitCount bits:
|
||||
// allowed range: [-2^(n-1), 2^(n-1)-1]
|
||||
const min = -(2 ** (bitCount - 1));
|
||||
const max = (2 ** (bitCount - 1)) - 1;
|
||||
n = clampInt(n, min, max);
|
||||
|
||||
// Convert to unsigned representation modulo 2^bitCount
|
||||
const mod = 2 ** bitCount;
|
||||
let u = ((n % mod) + mod) % mod;
|
||||
|
||||
// then set bits from unsigned u
|
||||
for (let i = 0; i < bitCount; i++){
|
||||
const pow = bitCount - 1 - i;
|
||||
const v = 2 ** pow;
|
||||
if (u >= v){
|
||||
bits[i] = true;
|
||||
u -= v;
|
||||
} else {
|
||||
bits[i] = false;
|
||||
}
|
||||
}
|
||||
syncSwitchesAndBulbs();
|
||||
updateUI(false);
|
||||
}
|
||||
|
||||
function formatBinary(groupsOf = 4){
|
||||
const raw = bits.map(b => (b ? "1" : "0")).join("");
|
||||
// group for readability (keeps your “wrap every 8 bits” layout for switches;
|
||||
// this just formats the readout)
|
||||
let out = "";
|
||||
for (let i = 0; i < raw.length; i++){
|
||||
out += raw[i];
|
||||
const isLast = i === raw.length - 1;
|
||||
if (!isLast && (i + 1) % groupsOf === 0) out += " ";
|
||||
}
|
||||
return out.trim();
|
||||
}
|
||||
|
||||
function syncSwitchesAndBulbs(){
|
||||
// ✅ Bulbs always update (unsigned OR two's)
|
||||
bitsGrid.querySelectorAll('input[type="checkbox"][data-index]').forEach((input) => {
|
||||
const idx = Number(input.dataset.index);
|
||||
input.checked = Boolean(bits[idx]);
|
||||
});
|
||||
|
||||
for (let i = 0; i < bitCount; i++){
|
||||
const bulb = document.getElementById(`bulb-${i}`);
|
||||
if (bulb) bulb.classList.toggle("on", Boolean(bits[i]));
|
||||
}
|
||||
}
|
||||
|
||||
function updateUI(sync = true){
|
||||
if (sync) syncSwitchesAndBulbs();
|
||||
|
||||
// labels update when mode changes
|
||||
setLabels();
|
||||
|
||||
// readouts
|
||||
const value = getCurrentValue();
|
||||
denaryEl.textContent = String(value);
|
||||
binaryEl.textContent = formatBinary(4);
|
||||
|
||||
// hint
|
||||
if (isTwos){
|
||||
modeHint.textContent = "Tip: In two’s complement, the left-most bit (MSB) represents a negative value.";
|
||||
} else {
|
||||
modeHint.textContent = "Tip: In unsigned binary, all bits represent positive values.";
|
||||
}
|
||||
}
|
||||
|
||||
/* ----------------------------
|
||||
Controls
|
||||
----------------------------- */
|
||||
btnShiftLeft?.addEventListener("click", () => {
|
||||
// shift left: drop MSB, append 0 to LSB
|
||||
bits.shift();
|
||||
bits.push(false);
|
||||
updateUI();
|
||||
});
|
||||
|
||||
btnShiftRight?.addEventListener("click", () => {
|
||||
// shift right: drop LSB, insert 0 at MSB
|
||||
bits.pop();
|
||||
bits.unshift(false);
|
||||
updateUI();
|
||||
});
|
||||
|
||||
btnClear?.addEventListener("click", () => {
|
||||
bits = new Array(bitCount).fill(false);
|
||||
updateUI();
|
||||
});
|
||||
|
||||
btnMinus1?.addEventListener("click", () => {
|
||||
const v = getCurrentValue();
|
||||
if (isTwos) setFromTwosValue(v - 1);
|
||||
else setFromUnsignedValue(v - 1);
|
||||
});
|
||||
|
||||
btnPlus1?.addEventListener("click", () => {
|
||||
const v = getCurrentValue();
|
||||
if (isTwos) setFromTwosValue(v + 1);
|
||||
else setFromUnsignedValue(v + 1);
|
||||
});
|
||||
|
||||
btnAutoRandom?.addEventListener("click", () => {
|
||||
// stop if already running
|
||||
if (autoTimer){
|
||||
clearInterval(autoTimer);
|
||||
autoTimer = null;
|
||||
btnAutoRandom.textContent = "Auto Random";
|
||||
return;
|
||||
}
|
||||
|
||||
btnAutoRandom.textContent = "Stop Random";
|
||||
|
||||
// run briefly then stop automatically
|
||||
const start = Date.now();
|
||||
const durationMs = 2200; // auto stop
|
||||
|
||||
autoTimer = setInterval(() => {
|
||||
const now = Date.now();
|
||||
if (now - start > durationMs){
|
||||
clearInterval(autoTimer);
|
||||
autoTimer = null;
|
||||
btnAutoRandom.textContent = "Auto Random";
|
||||
return;
|
||||
}
|
||||
|
||||
// random within correct range for current mode
|
||||
if (isTwos){
|
||||
const min = -(2 ** (bitCount - 1));
|
||||
const max = (2 ** (bitCount - 1)) - 1;
|
||||
const n = Math.floor(Math.random() * (max - min + 1)) + min;
|
||||
setFromTwosValue(n);
|
||||
} else {
|
||||
const max = (2 ** bitCount) - 1;
|
||||
const n = Math.floor(Math.random() * (max + 1));
|
||||
setFromUnsignedValue(n);
|
||||
}
|
||||
}, 90);
|
||||
});
|
||||
|
||||
btnCustomBinary?.addEventListener("click", () => {
|
||||
const v = prompt(`Enter a ${bitCount}-bit binary number (0/1):`);
|
||||
if (v === null) return;
|
||||
|
||||
const clean = v.replace(/\s+/g, "");
|
||||
if (!/^[01]+$/.test(clean)){
|
||||
alert("Invalid binary. Use only 0 and 1.");
|
||||
return;
|
||||
}
|
||||
|
||||
const padded = clean.slice(-bitCount).padStart(bitCount, "0");
|
||||
bits = [...padded].map(ch => ch === "1");
|
||||
updateUI();
|
||||
});
|
||||
|
||||
btnCustomDenary?.addEventListener("click", () => {
|
||||
const v = prompt(isTwos
|
||||
? `Enter a denary number (${-(2 ** (bitCount - 1))} to ${(2 ** (bitCount - 1)) - 1}):`
|
||||
: `Enter a denary number (0 to ${(2 ** bitCount) - 1}):`
|
||||
);
|
||||
if (v === null) return;
|
||||
|
||||
const n = Number(v);
|
||||
if (!Number.isFinite(n) || !Number.isInteger(n)){
|
||||
alert("Invalid denary. Enter a whole number.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (isTwos) setFromTwosValue(n);
|
||||
else setFromUnsignedValue(n);
|
||||
});
|
||||
|
||||
/* ----------------------------
|
||||
Mode + Bit width
|
||||
----------------------------- */
|
||||
modeToggle?.addEventListener("change", () => {
|
||||
isTwos = Boolean(modeToggle.checked);
|
||||
// keep the same underlying bit pattern; just reinterpret and relabel
|
||||
updateUI(false);
|
||||
});
|
||||
|
||||
btnBitsUp?.addEventListener("click", () => {
|
||||
bitCount = clampInt(bitCount + 1, 1, 64);
|
||||
bitsInput.value = String(bitCount);
|
||||
bits = new Array(bitCount).fill(false);
|
||||
buildBits();
|
||||
});
|
||||
|
||||
btnBitsDown?.addEventListener("click", () => {
|
||||
bitCount = clampInt(bitCount - 1, 1, 64);
|
||||
bitsInput.value = String(bitCount);
|
||||
bits = new Array(bitCount).fill(false);
|
||||
buildBits();
|
||||
});
|
||||
|
||||
bitsInput?.addEventListener("change", () => {
|
||||
bitCount = clampInt(Number(bitsInput.value), 1, 64);
|
||||
bitsInput.value = String(bitCount);
|
||||
bits = new Array(bitCount).fill(false);
|
||||
buildBits();
|
||||
});
|
||||
|
||||
/* ----------------------------
|
||||
Init
|
||||
----------------------------- */
|
||||
buildBits();
|
||||
@@ -1,16 +1,16 @@
|
||||
/* Font */
|
||||
/* DSEG7ClassicRegular font */
|
||||
@font-face{
|
||||
font-family: "DSEG7ClassicRegular";
|
||||
src:
|
||||
url("/fonts/DSEG7Classic-Regular.woff") format("woff"),
|
||||
url("/fonts/DSEG7Classic-Regular.ttf") format("truetype");
|
||||
url("/fonts/DSEG7Classic-Regular.ttf") format("truetype"),
|
||||
url("/fonts/DSEG7Classic-Regular.woff") format("woff");
|
||||
font-weight: 400;
|
||||
font-style: normal;
|
||||
font-display: swap;
|
||||
}
|
||||
|
||||
.binaryWrap{
|
||||
padding-top: 8px;
|
||||
.binary-wrap{
|
||||
padding-top: 6px;
|
||||
}
|
||||
|
||||
.topGrid{
|
||||
@@ -21,6 +21,7 @@
|
||||
}
|
||||
|
||||
.readout{
|
||||
background: transparent;
|
||||
text-align:center;
|
||||
padding: 10px 10px 0;
|
||||
}
|
||||
@@ -42,24 +43,40 @@
|
||||
}
|
||||
|
||||
.denary{
|
||||
font-size: 70px; /* smaller than before */
|
||||
line-height: 1;
|
||||
font-size: 72px; /* smaller */
|
||||
line-height: 1.0;
|
||||
margin: 6px 0 10px;
|
||||
}
|
||||
|
||||
.binary{
|
||||
font-size: 54px; /* smaller than before */
|
||||
font-size: 46px; /* smaller */
|
||||
letter-spacing: .12em;
|
||||
line-height: 1;
|
||||
margin: 6px 0 16px;
|
||||
line-height: 1.15;
|
||||
margin: 6px 0 14px;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
display:inline-block;
|
||||
text-align:center;
|
||||
}
|
||||
|
||||
.controls{
|
||||
margin-top: 10px;
|
||||
margin-top: 8px;
|
||||
display:flex;
|
||||
justify-content:center;
|
||||
gap: 12px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.controls--twoRows{
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.controlsRow{
|
||||
display:flex;
|
||||
gap: 12px;
|
||||
justify-content:center;
|
||||
flex-wrap:wrap;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.btn{
|
||||
@@ -68,18 +85,32 @@
|
||||
color: #fff;
|
||||
padding: 12px 14px;
|
||||
border-radius: 12px;
|
||||
font-weight: 700;
|
||||
font-weight: 800;
|
||||
cursor: pointer;
|
||||
min-width: 160px;
|
||||
}
|
||||
|
||||
.btn:active{ transform: translateY(1px); }
|
||||
|
||||
.btn--green{
|
||||
background: rgba(51,255,122,.16);
|
||||
border-color: rgba(51,255,122,.45);
|
||||
}
|
||||
|
||||
.btn--green:hover{
|
||||
background: rgba(51,255,122,.22);
|
||||
}
|
||||
|
||||
.btn--spin{
|
||||
min-width: 120px;
|
||||
font-size: 18px; /* bigger */
|
||||
}
|
||||
|
||||
.divider{
|
||||
margin-top: 26px;
|
||||
margin-top: 22px;
|
||||
border-top: 1px solid var(--line);
|
||||
}
|
||||
|
||||
/* Right-side cards */
|
||||
.panelCol{
|
||||
display:flex;
|
||||
flex-direction:column;
|
||||
@@ -87,8 +118,8 @@
|
||||
}
|
||||
|
||||
.card{
|
||||
background: var(--panel2);
|
||||
border: 1px solid rgba(255,255,255,.10);
|
||||
background: var(--panel);
|
||||
border: 1px solid var(--panel-border);
|
||||
border-radius: 14px;
|
||||
padding: 14px;
|
||||
}
|
||||
@@ -122,16 +153,14 @@
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
/* Switch (reused for mode + bits) */
|
||||
.switch{
|
||||
position: relative;
|
||||
width: 56px;
|
||||
height: 34px;
|
||||
position:relative;
|
||||
width:56px;
|
||||
height:34px;
|
||||
display:inline-block;
|
||||
flex: 0 0 auto;
|
||||
flex:0 0 auto;
|
||||
}
|
||||
.switch input{
|
||||
position:absolute;
|
||||
opacity:0;
|
||||
width:0;
|
||||
height:0;
|
||||
@@ -141,19 +170,19 @@
|
||||
inset:0;
|
||||
background: rgba(255,255,255,.10);
|
||||
border: 1px solid rgba(255,255,255,.14);
|
||||
border-radius: 999px;
|
||||
transition: .18s ease;
|
||||
border-radius:999px;
|
||||
transition:.18s ease;
|
||||
}
|
||||
.slider::before{
|
||||
content:"";
|
||||
position:absolute;
|
||||
height: 28px;
|
||||
width: 28px;
|
||||
left: 3px;
|
||||
top: 2px;
|
||||
height:28px;
|
||||
width:28px;
|
||||
left:3px;
|
||||
top:2px;
|
||||
background: rgba(255,255,255,.92);
|
||||
border-radius: 50%;
|
||||
transition: .18s ease;
|
||||
border-radius:50%;
|
||||
transition:.18s ease;
|
||||
}
|
||||
.switch input:checked + .slider{
|
||||
background: rgba(51,255,122,.20);
|
||||
@@ -164,54 +193,53 @@
|
||||
background: var(--accent);
|
||||
}
|
||||
|
||||
/* Bit-width control */
|
||||
.bitWidthRow{
|
||||
display:grid;
|
||||
grid-template-columns: 44px 1fr 44px;
|
||||
gap: 10px;
|
||||
grid-template-columns:44px 1fr 44px;
|
||||
gap:10px;
|
||||
align-items:center;
|
||||
}
|
||||
|
||||
.miniBtn{
|
||||
height: 44px;
|
||||
width: 44px;
|
||||
border-radius: 12px;
|
||||
height:44px;
|
||||
width:44px;
|
||||
border-radius:12px;
|
||||
background: rgba(255,255,255,.06);
|
||||
border: 1px solid rgba(255,255,255,.14);
|
||||
color: #fff;
|
||||
cursor: pointer;
|
||||
font-weight: 900;
|
||||
font-size: 18px;
|
||||
color:#fff;
|
||||
cursor:pointer;
|
||||
font-weight:900;
|
||||
font-size:18px;
|
||||
}
|
||||
|
||||
.bitInputWrap{
|
||||
background: rgba(255,255,255,.06);
|
||||
border: 1px solid rgba(255,255,255,.14);
|
||||
border-radius: 12px;
|
||||
padding: 10px 12px;
|
||||
border-radius:12px;
|
||||
padding:10px 12px;
|
||||
display:flex;
|
||||
align-items:center;
|
||||
justify-content:space-between;
|
||||
gap: 12px;
|
||||
gap:12px;
|
||||
}
|
||||
|
||||
.bitInputLabel{
|
||||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
font-weight: 900;
|
||||
letter-spacing: .18em;
|
||||
text-transform: uppercase;
|
||||
font-size:12px;
|
||||
font-weight:900;
|
||||
letter-spacing:.18em;
|
||||
text-transform:uppercase;
|
||||
}
|
||||
|
||||
.bitInput{
|
||||
width: 86px;
|
||||
width:86px;
|
||||
text-align:right;
|
||||
background: transparent;
|
||||
border:none;
|
||||
outline:none;
|
||||
border: none;
|
||||
outline: none;
|
||||
color: var(--accent);
|
||||
font-family: "DSEG7ClassicRegular", ui-monospace, monospace;
|
||||
font-size: 28px;
|
||||
font-family:"DSEG7ClassicRegular", ui-monospace, monospace;
|
||||
font-size:28px;
|
||||
}
|
||||
.bitInput::-webkit-outer-spin-button,
|
||||
.bitInput::-webkit-inner-spin-button{
|
||||
@@ -219,66 +247,51 @@
|
||||
margin:0;
|
||||
}
|
||||
|
||||
/* Bits: wrap every 8 (rows built in JS) */
|
||||
.bitsRows{
|
||||
margin-top: 22px;
|
||||
display:flex;
|
||||
flex-direction:column;
|
||||
/* Bits: wrap every 8 */
|
||||
.bits{
|
||||
--cols: 8;
|
||||
margin-top: 26px;
|
||||
padding-top: 18px;
|
||||
display:grid;
|
||||
grid-template-columns: repeat(var(--cols), 90px);
|
||||
justify-content: center; /* centres when < 8 too */
|
||||
gap: 18px;
|
||||
align-items:end;
|
||||
text-align:center;
|
||||
}
|
||||
|
||||
/* A row of up to 8 bits */
|
||||
.byteRow{
|
||||
display:flex;
|
||||
justify-content:center;
|
||||
gap: 18px;
|
||||
flex-wrap:nowrap;
|
||||
}
|
||||
|
||||
/* A single bit */
|
||||
.bit{
|
||||
width: 110px;
|
||||
display:flex;
|
||||
flex-direction:column;
|
||||
align-items:center;
|
||||
gap: 10px;
|
||||
padding: 6px 4px;
|
||||
padding: 8px 4px;
|
||||
}
|
||||
|
||||
/* Bulb emoji: bigger */
|
||||
/* Bulb emoji bigger */
|
||||
.bulb{
|
||||
font-size: 30px;
|
||||
font-size: 26px; /* bigger */
|
||||
line-height: 1;
|
||||
opacity: .20;
|
||||
filter: grayscale(1);
|
||||
opacity: .35;
|
||||
transform: translateY(2px);
|
||||
}
|
||||
.bulb.on{
|
||||
opacity: 1;
|
||||
filter: grayscale(0);
|
||||
text-shadow: 0 0 16px rgba(255,216,107,.65);
|
||||
opacity: 1;
|
||||
text-shadow: 0 0 16px rgba(255,216,107,.55);
|
||||
}
|
||||
|
||||
.bitVal{
|
||||
font-family: "DSEG7ClassicRegular", ui-monospace, monospace;
|
||||
font-size: 30px;
|
||||
font-size: 28px;
|
||||
color: var(--text);
|
||||
opacity: .92;
|
||||
opacity: .95;
|
||||
line-height: 1;
|
||||
min-height: 34px;
|
||||
}
|
||||
|
||||
/* Responsiveness */
|
||||
@media (max-width: 980px){
|
||||
.topGrid{ grid-template-columns: 1fr; }
|
||||
.denary{ font-size: 62px; }
|
||||
.binary{ font-size: 48px; }
|
||||
.bit{ width: 96px; }
|
||||
}
|
||||
|
||||
@media (max-width: 520px){
|
||||
.denary{ font-size: 56px; }
|
||||
.binary{ font-size: 42px; }
|
||||
.btn{ min-width: 140px; }
|
||||
.byteRow{ gap: 12px; }
|
||||
.bit{ width: 86px; }
|
||||
.binary{ font-size: 40px; }
|
||||
}
|
||||
85
src/styles/global.css
Normal file
85
src/styles/global.css
Normal file
@@ -0,0 +1,85 @@
|
||||
:root{
|
||||
--bg: #1f2027;
|
||||
--panel: #22242d;
|
||||
--panel2: rgba(255,255,255,.04);
|
||||
--text: #e8e8ee;
|
||||
--muted: #a9acb8;
|
||||
--accent: #33ff7a;
|
||||
--accent-dim: rgba(51,255,122,.15);
|
||||
--line: rgba(255,255,255,.12);
|
||||
}
|
||||
|
||||
*{ box-sizing:border-box; }
|
||||
|
||||
body{
|
||||
margin:0;
|
||||
font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
|
||||
background: var(--bg);
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.siteHeader{
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 10;
|
||||
background: rgba(0,0,0,.15);
|
||||
backdrop-filter: blur(8px);
|
||||
border-bottom: 1px solid rgba(255,255,255,.06);
|
||||
}
|
||||
|
||||
.siteHeaderInner{
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 14px 20px;
|
||||
display:flex;
|
||||
align-items:center;
|
||||
justify-content:space-between;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.brand{
|
||||
color: var(--text);
|
||||
text-decoration:none;
|
||||
font-weight: 900;
|
||||
letter-spacing:.02em;
|
||||
}
|
||||
|
||||
.nav{
|
||||
display:flex;
|
||||
gap: 14px;
|
||||
flex-wrap:wrap;
|
||||
justify-content:flex-end;
|
||||
}
|
||||
.nav a{
|
||||
color: var(--muted);
|
||||
text-decoration:none;
|
||||
font-weight: 700;
|
||||
font-size: 14px;
|
||||
}
|
||||
.nav a:hover{ color: var(--text); }
|
||||
|
||||
.siteMain{
|
||||
min-height: calc(100vh - 140px);
|
||||
}
|
||||
|
||||
.siteFooter{
|
||||
border-top: 1px solid rgba(255,255,255,.08);
|
||||
margin-top: 32px;
|
||||
background: rgba(0,0,0,.10);
|
||||
}
|
||||
|
||||
.siteFooterInner{
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 18px 20px 26px;
|
||||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.footerTitle{
|
||||
color: var(--text);
|
||||
opacity:.9;
|
||||
font-weight: 800;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
:root{
|
||||
--bg: #1f2027;
|
||||
--panel: rgba(255,255,255,.04);
|
||||
--panel-border: rgba(255,255,255,.10);
|
||||
--text: #e8e8ee;
|
||||
--muted: #a9acb8;
|
||||
--line: rgba(255,255,255,.12);
|
||||
--accent: #33ff7a;
|
||||
--accent-dim: rgba(51,255,122,.15);
|
||||
--line: rgba(255,255,255,.12);
|
||||
}
|
||||
|
||||
*{ box-sizing: border-box; }
|
||||
@@ -17,62 +18,58 @@ body{
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.page{
|
||||
min-height: calc(100vh - 120px);
|
||||
.site-header{
|
||||
border-bottom: 1px solid rgba(255,255,255,.08);
|
||||
background: rgba(0,0,0,.12);
|
||||
}
|
||||
|
||||
.siteHeader{
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 10;
|
||||
background: rgba(0,0,0,.15);
|
||||
border-bottom: 1px solid var(--line);
|
||||
backdrop-filter: blur(10px);
|
||||
}
|
||||
|
||||
.siteHeader__inner{
|
||||
.site-header__inner{
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 14px 20px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
display:flex;
|
||||
align-items:center;
|
||||
justify-content:space-between;
|
||||
gap: 18px;
|
||||
}
|
||||
|
||||
.brand{
|
||||
color: var(--text);
|
||||
text-decoration: none;
|
||||
font-weight: 800;
|
||||
letter-spacing: .02em;
|
||||
}
|
||||
|
||||
.nav{
|
||||
display: flex;
|
||||
display:flex;
|
||||
gap: 14px;
|
||||
flex-wrap: wrap;
|
||||
justify-content: flex-end;
|
||||
justify-content:flex-end;
|
||||
}
|
||||
|
||||
.nav__link{
|
||||
color: var(--muted);
|
||||
text-decoration: none;
|
||||
text-decoration:none;
|
||||
font-weight: 700;
|
||||
font-size: 14px;
|
||||
font-size: 13px;
|
||||
}
|
||||
.nav__link:hover{ color: var(--text); }
|
||||
|
||||
.siteFooter{
|
||||
border-top: 1px solid var(--line);
|
||||
.site-main{
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 28px 20px 40px;
|
||||
min-height: calc(100vh - 140px);
|
||||
}
|
||||
|
||||
.site-footer{
|
||||
border-top: 1px solid rgba(255,255,255,.08);
|
||||
background: rgba(0,0,0,.10);
|
||||
}
|
||||
|
||||
.siteFooter__inner{
|
||||
.site-footer__inner{
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 18px 20px;
|
||||
padding: 16px 20px;
|
||||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
display: grid;
|
||||
gap: 6px;
|
||||
line-height: 1.5;
|
||||
}
|
||||
Reference in New Issue
Block a user