Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 673x 4x 12x 5x 18x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 1x 1x 1x 1x 1x 12x 12x 12x 12x 22x 12x 12x 12x 12x 12x 1x 1x 12x 12x 12x 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 24x 12x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 36x 72x 3x 3x 1x 3x 3x 3x 2x 2x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 11x 11x 3x 3x 3x 3x 3x 30x 6x 1x 1x 5x 5x 30x 3x 3x 3x 3x 3x 3x 3x 1x 1x 3x 3x 30x 2x 2x 2x 5x 11x 11x 11x 3x 3x 3x 3x 3x 3x 31x 30x 31x 30x 30x 16x 12x 30x 30x 30x 30x 143x 53x 53x 69x 1x 1x 1x 1x 6x 3x 3x 3x 3x 8x 8x 8x 8x 8x 14x 14x 14x 13x 13x 13x 26x 26x 13x 13x 13x 13x 13x 13x 13x 1177x 673x 20x 14x 14x 14x 14x 14x 14x 14x 14x 17x 17x 15x 15x 15x 1190x 1190x 1190x 1176x 1190x 15x 15x 14x 1181x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 1177x 1177x 1177x 1x 1x 1x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 30x 30x 4x 4x 4x 4x 4x 30x 30x 30x 2520x 2520x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 60x 30x 30x 30x 60x 60x 60x 30x 30x 30x 30x 7x 23x 23x 23x 23x 30x 30x 30x 180x 23x 1764x 21x 30x 30x 4x 4x 4x 4x 4x 13x | import { IbmDiscFormat } from "../disc-format.js";
import { discReport } from "../disc.js";
import { setText } from "./dom-utils.js";
import { FloatingPanel, MenuGroup } from "./floating-panel.js";
import { EndEdge, StartEdge } from "./panel-layout.js";
import {
DensityPalette,
DensityRampHex,
DiscGeometry,
ErrorHex,
MaxDensity,
MinDensity,
RegionStyles,
RegionPalette,
UnformattedHex,
clampZoom,
renderTracks,
trackPulseDensity,
trackRegions,
} from "../disc-surface.js";
const MinPanelSize = { w: 300, h: 300 };
/** 300 rpm. */
const RevolutionMs = 200;
/** Not on either palette: the head and the index marker have to sit clear of the surface. */
const HeadColour = "#eb6834";
const IndexColour = "#c3c2b7";
const HoverColour = "#ffffff";
const MarkOutline = "#0d0d0d";
const ScanBudgetMs = 16;
const HeadZoom = 6;
// Decoding a side takes tens of milliseconds, so while a program writes the report waits for it to stop.
const ReportSettleMs = 500;
/** A trackpad reports deltas in the tens, a mouse notch about 100. */
const WheelZoomRate = 0.003;
/** Firefox reports wheel deltas in lines, not pixels. */
const PixelsPerWheelLine = 16;
const Views = {
density: {
palette: DensityPalette,
analyse: (track) => ({ codes: trackPulseDensity(track) }),
describe: ({ codes }, word) => (codes[word] === 0 ? "no flux" : `${codes[word]} pulses`),
legend: () =>
swatch(UnformattedHex, "unformatted") +
`<span class="disc-legend-item disc-legend-grow">${MinDensity}` +
`<span class="disc-legend-ramp" style="background:linear-gradient(to right, ${DensityRampHex.join(", ")})"></span>` +
`${MaxDensity} pulses per 64µs</span>`,
},
format: {
palette: RegionPalette,
analyse: (track) => trackRegions(track),
describe: ({ codes, sectorNumbers, errors }, word) => {
const what = RegionStyles[codes[word]].name;
const named = sectorNumbers[word] < 0 ? what : `sector ${sectorNumbers[word]} ${what}`;
const error = errors.find(({ firstWord, lastWord }) =>
lastWord <= firstWord ? word >= firstWord || word < lastWord : word >= firstWord && word < lastWord,
);
return `${named}${error ? ` · ${error.kind} error` : ""}`;
},
legend: () => RegionStyles.map(({ hex, name }) => swatch(hex, name)).join("") + swatch(ErrorHex, "CRC error"),
},
};
/** One atomic legend entry, so a swatch never gets orphaned from its label by a line break. */
function swatch(colour, label) {
return `<span class="disc-legend-item"><span class="disc-legend-swatch" style="background:${colour}"></span>${label}</span>`;
}
/**
* The disc surface panel: one side of one drive's disc drawn as the physical platter, coloured
* either by raw pulse density or by what the decoder makes of each word, with the head drawn
* where it sits.
*/
export class DiscVisualiser {
/**
* @param {object} options
* @param {object} options.fdc
* @param {import("./floating-panel.js").PanelArrangement} options.arrangement
*/
constructor({ fdc, arrangement }) {
this._fdc = fdc;
this.panel = document.getElementById("disc-panel");
this.surfaceCanvas = document.getElementById("disc-surface");
this.overlayCanvas = document.getElementById("disc-overlay");
this.statusElem = document.getElementById("disc-status");
this.nameElem = document.getElementById("disc-name");
this.hoverWhereElem = document.getElementById("disc-hover-where");
this.hoverWhatElem = document.getElementById("disc-hover-what");
this.legendElem = document.getElementById("disc-legend");
this.sideControls = document.getElementById("disc-side-controls");
this.reportToggle = document.getElementById("disc-report-toggle");
this.reportElem = document.getElementById("disc-report");
this._view = "density";
this._driveIndex = 0;
this._isSideUpper = false;
this._disc = null;
this._geometry = null;
this._imageData = null;
this._pixels = null;
/** @type {(Uint8Array|null)[]} */
this._codes = [];
/** @type {(object|null)[]} */
this._info = [];
this._staleTracks = new Set();
this._needsFullRepaint = true;
this._scanCursor = 0;
this._scanHandle = null;
this._hover = null;
this._pan = null;
this._surfaceStale = false;
this._showingReport = false;
this._reportStale = true;
this._lastWriteAt = -Infinity;
this._onTrackWrite = (isSideUpper, trackNum) => {
Iif (isSideUpper !== this._isSideUpper) return;
this._staleTracks.add(trackNum);
this._reportStale = true;
this._lastWriteAt = performance.now();
this._refresh.drawSoon();
};
this._onResize = () => this._resize();
this._resizeObserver = new ResizeObserver(this._onResize);
this.floating = new FloatingPanel({
panel: this.panel,
id: "disc",
title: "Disc surface",
menuGroup: MenuGroup.hardware,
group: null,
place: () => ({ x: EndEdge, y: StartEdge }),
minSize: MinPanelSize,
arrangement,
});
// Before the first draw, which needs the canvas sized.
this.floating.addEventListener("open", () => this._start());
this._refresh = this.floating.whileOpen(() => this.update());
this.floating.addEventListener("close", () => this._stop());
this._bindChoice("[data-drive]", (button) => this._select(Number(button.dataset.drive), this._isSideUpper));
this._bindChoice("[data-side]", (button) => this._select(this._driveIndex, button.dataset.side === "1"));
this._bindChoice("[data-view]", (button) => this._setView(button.dataset.view));
this.reportToggle.addEventListener("click", () => {
this._showReport(!this._showingReport);
this.update();
});
this.overlayCanvas.addEventListener("mousemove", (e) => this._hoverAt(this._canvasPoint(e)));
this.overlayCanvas.addEventListener("mouseleave", () => this._hoverAt(null));
this._bindZoomAndPan();
this._buildLegend();
}
_bindZoomAndPan() {
const canvas = this.overlayCanvas;
canvas.addEventListener("wheel", (e) => {
e.preventDefault();
Iif (!this._geometry) return;
const { x, y } = this._canvasPoint(e);
const at = this._geometry.toDisc(x, y);
const lines = e.deltaMode === WheelEvent.DOM_DELTA_LINE ? PixelsPerWheelLine : 1;
const zoom = clampZoom(this._geometry.zoom * Math.exp(-e.deltaY * lines * WheelZoomRate));
// Keep whatever is under the pointer under the pointer.
this._setViewport(zoom, at.x - x / zoom, at.y - y / zoom);
});
canvas.addEventListener("pointerdown", (e) => {
if (e.button !== 0) return;
const { originX, originY } = this._geometry;
this._pan = { pointerId: e.pointerId, ...this._canvasPoint(e), originX, originY, moved: false };
canvas.setPointerCapture(e.pointerId);
});
canvas.addEventListener("pointermove", (e) => {
if (this._pan?.pointerId !== e.pointerId) return;
const { x, y } = this._canvasPoint(e);
const { zoom } = this._geometry;
this._pan.moved = true;
this._setViewport(
zoom,
this._pan.originX - (x - this._pan.x) / zoom,
this._pan.originY - (y - this._pan.y) / zoom,
);
});
for (const ending of ["pointerup", "pointercancel"])
canvas.addEventListener(ending, (e) => {
if (this._pan?.pointerId !== e.pointerId) return;
if (this._pan.moved) this._viewMoved();
this._pan = null;
});
canvas.addEventListener("dblclick", () => this._setViewport(1, 0, 0));
}
_hoverAt(point) {
this._hover = point;
this._refresh.drawSoon();
}
_viewMoved() {
this._surfaceStale = true;
this._refresh.drawSoon();
}
/** @returns {{x: number, y: number}} the event's position in canvas pixels */
_canvasPoint(e) {
const rect = this.overlayCanvas.getBoundingClientRect();
const scale = this.overlayCanvas.width / rect.width;
return { x: (e.clientX - rect.left) * scale, y: (e.clientY - rect.top) * scale };
}
_setViewport(zoom, originX, originY) {
const was = { ...this._geometry };
this._geometry.setView(zoom, originX, originY);
Eif (
was.zoom !== this._geometry.zoom ||
was.originX !== this._geometry.originX ||
was.originY !== this._geometry.originY
)
this._viewMoved();
}
_bindChoice(selector, onClick) {
for (const button of this.panel.querySelectorAll(selector))
button.addEventListener("click", () => {
onClick(button);
this.update();
});
}
/** Draws again at the next frame, for a disc put in or taken out while the machine stands still. */
redraw() {
this._refresh.drawSoon();
}
/** Opens the panel showing the given drive's lower side. */
openOn(driveIndex) {
this._select(driveIndex, false);
this.floating.open();
this.update();
}
/** Opens the panel on the given drive's lower side, listing what is odd about it. */
openReport(driveIndex) {
this._showReport(true);
this.openOn(driveIndex);
}
/** Opens the panel on the side of the drive's disc under its head, zoomed in on the head where it is now. */
zoomToHead(driveIndex) {
const drive = this._fdc.drives[driveIndex];
this._select(driveIndex, drive.isSideUpper);
this.floating.open();
this._zoomTo(drive.track, drive.positionFraction);
this.update();
}
/** Zooms in on a place on the side shown: a track, and how far round it. */
_zoomTo(trackNum, fraction) {
const geometry = this._geometry;
Iif (!geometry) return;
const radius = geometry.radiusOf(Math.min(trackNum, geometry.numTracks - 1));
const angle = geometry.angleOf(fraction);
const halfView = geometry.size / (2 * HeadZoom);
this._setViewport(
HeadZoom,
geometry.centre + radius * Math.cos(angle) - halfView,
geometry.centre + radius * Math.sin(angle) - halfView,
);
}
/** Lists the findings at the next draw, for a disc or side that is new to the list. */
_reportAfresh() {
this._reportStale = true;
this._lastWriteAt = -Infinity;
}
_showReport(on) {
this._showingReport = on;
this._reportAfresh();
this.reportElem.hidden = !on;
this.reportToggle.classList.toggle("active", on);
this.reportToggle.setAttribute("aria-pressed", String(on));
}
/** Lists the side's findings again, once any writes to the disc have stopped. */
_updateReport() {
if (!this._showingReport || !this._reportStale) return;
if (performance.now() - this._lastWriteAt < ReportSettleMs) {
this._refresh.drawSoon();
return;
}
this._reportStale = false;
const findings = this._disc ? discReport(this._disc, this._isSideUpper) : [];
const items = findings.map(({ trackNum, fraction, text }) => {
const item = document.createElement("li");
const button = document.createElement("button");
button.type = "button";
button.className = "txt-btn";
button.textContent = text;
button.title = "Zoom in on it";
button.addEventListener("click", () => {
this._zoomTo(trackNum, fraction);
this.update();
});
item.append(button);
return item;
});
if (!items.length) {
const item = document.createElement("li");
item.textContent = this._disc ? "Nothing odd on this side." : "No disc.";
items.push(item);
}
this.reportElem.replaceChildren(...items);
}
_start() {
// Catches a zoom, which changes the device pixel ratio but not the canvas's size.
window.addEventListener("resize", this._onResize);
this._resizeObserver.observe(this.surfaceCanvas);
this._resize();
}
_stop() {
this._cancelScan();
this._detach();
this._hover = null;
this._needsFullRepaint = true;
this._resizeObserver.disconnect();
window.removeEventListener("resize", this._onResize);
}
/** Cheap enough to call every frame. */
update() {
if (!this.floating.isOpen || !this._geometry) return;
const disc = this._drive?.disc ?? null;
Iif (this._isSideUpper && !disc?.isDoubleSided) this._select(this._driveIndex, false);
if (disc !== this._disc) this._attach(disc);
if (this._needsFullRepaint) this._beginFullRepaint();
else if (this._surfaceStale) this._repaintSurface();
else if (this._staleTracks.size && !this._scanning) this._repaintStaleTracks();
this._drawOverlay();
this._updateStatus();
this._updateReport();
// The machine may stand still while a scan or stale tracks are left to paint.
if (this._scanning || this._staleTracks.size) this._refresh.drawSoon();
}
get _drive() {
return this._fdc.drives[this._driveIndex];
}
get _showingHead() {
const drive = this._drive;
return !!drive?.disc && drive.isSideUpper === this._isSideUpper;
}
get _scanning() {
return this._geometry !== null && this._scanCursor <= this._geometry.numTracks;
}
_setView(view) {
Iif (view === this._view || !Views[view]) return;
this._view = view;
this._needsFullRepaint = true;
this._buildLegend();
}
_select(driveIndex, isSideUpper) {
if (driveIndex === this._driveIndex && isSideUpper === this._isSideUpper) return;
this._driveIndex = driveIndex;
this._isSideUpper = isSideUpper;
this._detach();
this._needsFullRepaint = true;
}
_attach(disc) {
this._detach();
this._disc = disc;
disc?.addTrackWriteListener(this._onTrackWrite);
this._needsFullRepaint = true;
this._reportAfresh();
}
_detach() {
this._disc?.removeTrackWriteListener(this._onTrackWrite);
this._disc = null;
this._staleTracks.clear();
}
_resize() {
const size = Math.round(this.surfaceCanvas.clientWidth * (window.devicePixelRatio || 1));
Iif (size <= 0 || size === this._geometry?.size) return;
for (const canvas of [this.surfaceCanvas, this.overlayCanvas]) {
canvas.width = size;
canvas.height = size;
}
const previous = this._geometry;
this._geometry = new DiscGeometry(size, IbmDiscFormat.tracksPerDisc);
if (previous) this._geometry.adoptView(previous);
this._imageData = this.surfaceCanvas.getContext("2d").createImageData(size, size);
this._pixels = new Uint32Array(this._imageData.data.buffer);
this._needsFullRepaint = true;
this._refresh.drawSoon();
}
/** @returns {{codes: Uint8Array, sectorNumbers: Int16Array|null, errors: object[]|null}|null} */
_analyse(trackNum) {
if (!this._disc) return null;
return Views[this._view].analyse(this._disc.getTrack(this._isSideUpper, trackNum));
}
_blit() {
this.surfaceCanvas.getContext("2d").putImageData(this._imageData, 0, 0);
}
_beginFullRepaint() {
this._needsFullRepaint = false;
this._cancelScan();
const count = this._geometry.numTracks;
this._codes = new Array(count).fill(null);
this._info = new Array(count).fill(null);
this._scanCursor = 0;
this._pixels.fill(0);
this._advanceScan();
}
_cancelScan() {
Iif (this._scanHandle !== null) cancelAnimationFrame(this._scanHandle);
this._scanHandle = null;
}
/** Driven by requestAnimationFrame rather than the emulator's loop, so it completes while paused. */
_advanceScan() {
const count = this._geometry.numTracks;
const deadline = performance.now() + ScanBudgetMs;
do {
if (this._scanCursor < count) this._analyseInto(this._scanCursor);
// A track's edge pixels sample its neighbours, so paint one behind the scan.
const paintTrack = this._scanCursor - 1;
if (paintTrack >= 0)
renderTracks(this._pixels, this._geometry, this._codes, this._palette, paintTrack, paintTrack);
this._scanCursor++;
} while (this._scanCursor <= count && performance.now() < deadline);
this._blit();
if (this._scanning) this._scanHandle = requestAnimationFrame(() => this._advanceScan());
else this._scanHandle = null;
}
get _palette() {
return Views[this._view].palette;
}
_repaintStaleTracks() {
const deadline = performance.now() + ScanBudgetMs;
for (const trackNum of this._staleTracks) {
this._repaintTrack(trackNum);
this._staleTracks.delete(trackNum);
Iif (performance.now() >= deadline) break;
}
this._blit();
}
/** For when the view moved rather than the disc. */
_repaintSurface() {
this._surfaceStale = false;
this._pixels.fill(0);
// Mid-drag the whole surface is redrawn every frame, so trade smoothed edges for the frame rate.
const supersample = this._pan ? 1 : undefined;
const last = this._geometry.numTracks - 1;
renderTracks(this._pixels, this._geometry, this._codes, this._palette, 0, last, supersample);
this._blit();
}
_analyseInto(trackNum) {
const analysis = this._analyse(trackNum);
this._info[trackNum] = analysis;
this._codes[trackNum] = analysis?.codes ?? null;
}
_repaintTrack(trackNum) {
Iif (trackNum >= this._geometry.numTracks) return;
this._analyseInto(trackNum);
renderTracks(this._pixels, this._geometry, this._codes, this._palette, trackNum, trackNum);
}
_drawOverlay() {
const geometry = this._geometry;
const ctx = this.overlayCanvas.getContext("2d");
const scale = window.devicePixelRatio || 1;
ctx.clearRect(0, 0, geometry.size, geometry.size);
const centreX = geometry.screenCentreX;
const centreY = geometry.screenCentreY;
ctx.lineWidth = scale;
ctx.strokeStyle = `${IndexColour}44`;
ctx.beginPath();
ctx.arc(centreX, centreY, geometry.outerRadius * geometry.zoom, 0, 2 * Math.PI);
ctx.moveTo(centreX + geometry.hubRadius * geometry.zoom, centreY);
ctx.arc(centreX, centreY, geometry.hubRadius * geometry.zoom, 0, 2 * Math.PI);
ctx.stroke();
this._strokeRadial(ctx, 0, `${IndexColour}66`, scale);
this._drawErrors(ctx, scale);
const drive = this._drive;
if (this._showingHead) {
const track = Math.min(drive.track, geometry.numTracks - 1);
const fraction = drive.positionFraction;
ctx.globalAlpha = drive.spinning ? 1 : 0.45;
ctx.strokeStyle = `${HeadColour}55`;
ctx.lineWidth = Math.max(geometry.trackPitch * geometry.zoom, 2 * scale);
ctx.beginPath();
ctx.arc(centreX, centreY, geometry.screenRadiusOf(track), 0, 2 * Math.PI);
ctx.stroke();
this._strokeRadial(ctx, fraction, `${HeadColour}66`, scale);
const { x, y } = geometry.pointAt(track, fraction);
ctx.fillStyle = HeadColour;
ctx.beginPath();
ctx.arc(x, y, 3.5 * scale, 0, 2 * Math.PI);
ctx.fill();
ctx.globalAlpha = 1;
}
const hover = this._hoverPosition();
if (hover) {
ctx.strokeStyle = `${HoverColour}55`;
ctx.lineWidth = scale;
ctx.beginPath();
ctx.arc(centreX, centreY, geometry.screenRadiusOf(hover.track), 0, 2 * Math.PI);
ctx.stroke();
}
}
_drawErrors(ctx, scale) {
const geometry = this._geometry;
const width = Math.max(geometry.trackPitch * geometry.zoom, 2.5 * scale);
for (let trackNum = 0; trackNum < this._info.length; ++trackNum) {
const info = this._info[trackNum];
Eif (!info?.errors?.length) continue;
const radius = geometry.screenRadiusOf(trackNum);
for (const error of info.errors) {
const start = error.firstWord / info.codes.length;
let end = error.lastWord / info.codes.length;
if (end <= start) end += 1;
for (const [lineWidth, style] of [
[width + 3 * scale, MarkOutline],
[width, ErrorHex],
]) {
ctx.lineWidth = lineWidth;
ctx.strokeStyle = style;
ctx.beginPath();
ctx.arc(
geometry.screenCentreX,
geometry.screenCentreY,
radius,
geometry.angleOf(start),
geometry.angleOf(end),
);
ctx.stroke();
}
}
}
}
_strokeRadial(ctx, fraction, style, scale) {
const geometry = this._geometry;
const angle = geometry.angleOf(fraction);
const cos = Math.cos(angle);
const sin = Math.sin(angle);
const inner = geometry.innerRadius * geometry.zoom;
const outer = geometry.outerRadius * geometry.zoom;
ctx.strokeStyle = style;
ctx.lineWidth = scale;
ctx.beginPath();
ctx.moveTo(geometry.screenCentreX + inner * cos, geometry.screenCentreY + inner * sin);
ctx.lineTo(geometry.screenCentreX + outer * cos, geometry.screenCentreY + outer * sin);
ctx.stroke();
}
/** @returns {{track: number, fraction: number}|null} */
_hoverPosition() {
return this._hover ? this._geometry.positionAt(this._hover.x, this._hover.y) : null;
}
_updateStatus() {
const drive = this._drive;
const disc = drive?.disc;
this.sideControls.hidden = !disc?.isDoubleSided;
this._markActive("[data-drive]", (button) => Number(button.dataset.drive) === this._driveIndex);
this._markActive("[data-side]", (button) => (button.dataset.side === "1") === this._isSideUpper);
this._markActive("[data-view]", (button) => button.dataset.view === this._view);
const name = disc?.name ?? "";
setText(this.nameElem, name);
if (this.nameElem.title !== name) this.nameElem.title = name;
if (!disc) {
setText(this.statusElem, `Drive ${this._driveIndex}: no disc`);
} else {
const head = this._showingHead
? `head track ${drive.track} · ${(drive.positionFraction * RevolutionMs).toFixed(1)} ms`
: `head track ${drive.track}, other side`;
const spin = drive.spinning ? "spinning" : "stopped";
const zoom = this._geometry.zoom > 1 ? ` · ${this._geometry.zoom.toFixed(1)}x` : "";
setText(this.statusElem, `${spin} · ${head}${zoom}${this._scanNote()}`);
}
const { where, what } = this._describeHover();
setText(this.hoverWhereElem, where);
setText(this.hoverWhatElem, what);
}
_markActive(selector, isActive) {
for (const button of this.panel.querySelectorAll(selector)) button.classList.toggle("active", isActive(button));
}
_scanNote() {
if (this._scanning) return " · reading surface…";
const errors = this._info.reduce((count, info) => count + (info?.errors?.length ?? 0), 0);
Eif (!errors) return "";
return ` · ${errors} CRC error${errors === 1 ? "" : "s"}`;
}
/** @returns {{where: string, what: string}} the pointer's position, and what the surface holds */
_describeHover() {
const hover = this._hoverPosition();
if (!hover) return { where: "Point at the surface to read it", what: "" };
const info = this._info[hover.track];
Iif (!info?.codes?.length) return { where: `Track ${hover.track}`, what: "not read yet" };
const word = Math.min((hover.fraction * info.codes.length) | 0, info.codes.length - 1);
const ms = (hover.fraction * RevolutionMs).toFixed(1);
return {
where: `Track ${hover.track} · word ${word} of ${info.codes.length} · ${ms} ms`,
what: Views[this._view].describe(info, word),
};
}
_buildLegend() {
this.legendElem.innerHTML = Views[this._view].legend();
}
}
|