155 lines
6.1 KiB
JavaScript
155 lines
6.1 KiB
JavaScript
(() => {
|
||
const SVG_NS = "http://www.w3.org/2000/svg";
|
||
const clamp = (min, max, value) => Math.min(max, Math.max(min, value));
|
||
|
||
function svgElement(tag, attributes = {}) {
|
||
const element = document.createElementNS(SVG_NS, tag);
|
||
Object.entries(attributes).forEach(([key, value]) => element.setAttribute(key, value));
|
||
return element;
|
||
}
|
||
|
||
class Gauge {
|
||
constructor(element, options) {
|
||
this.element = element;
|
||
this.options = { sweep: 180, decimals: 0, ...options };
|
||
this.build();
|
||
}
|
||
|
||
point(angle, radius) {
|
||
const radians = angle * Math.PI / 180;
|
||
return { x: 160 + radius * Math.sin(radians), y: 150 - radius * Math.cos(radians) };
|
||
}
|
||
|
||
arc(from, to, radius) {
|
||
return Array.from({ length: 41 }, (_, index) => {
|
||
const point = this.point(from + (to - from) * index / 40, radius);
|
||
return `${index ? "L" : "M"}${point.x.toFixed(2)},${point.y.toFixed(2)}`;
|
||
}).join(" ");
|
||
}
|
||
|
||
build() {
|
||
const { min, max, target, tolerance, unit, label, sweep, decimals } = this.options;
|
||
const angleFor = value => -sweep / 2 + sweep * (value - min) / (max - min);
|
||
this.element.classList.add("gauge");
|
||
this.element.setAttribute("viewBox", "0 0 320 205");
|
||
this.element.setAttribute("role", "img");
|
||
this.element.replaceChildren();
|
||
this.element.append(svgElement("path", {
|
||
d: this.arc(-sweep / 2, sweep / 2, 113), fill: "none", stroke: "rgba(238,243,232,.13)",
|
||
"stroke-width": 13, "stroke-linecap": "round"
|
||
}));
|
||
if (Number.isFinite(target) && Number.isFinite(tolerance) && tolerance > 0) {
|
||
const targetStart = angleFor(clamp(min, max, target - tolerance));
|
||
const targetEnd = angleFor(clamp(min, max, target + tolerance));
|
||
this.element.append(svgElement("path", {
|
||
d: this.arc(targetStart, targetEnd, 113), fill: "none", stroke: "#b8d69b",
|
||
"stroke-width": 13, "stroke-linecap": "round"
|
||
}));
|
||
}
|
||
for (let index = 0; index <= 20; index += 1) {
|
||
const angle = -sweep / 2 + index * sweep / 20;
|
||
const outer = this.point(angle, 94);
|
||
const inner = this.point(angle, index % 5 === 0 ? 82 : 88);
|
||
this.element.append(svgElement("line", {
|
||
x1: inner.x, y1: inner.y, x2: outer.x, y2: outer.y,
|
||
stroke: "rgba(238,243,232,.42)", "stroke-width": index % 5 === 0 ? 2 : 1
|
||
}));
|
||
if (index % 5 === 0) {
|
||
const textPoint = this.point(angle, 67);
|
||
const text = svgElement("text", {
|
||
x: textPoint.x, y: textPoint.y + 3, fill: "#96a394", "font-size": 9,
|
||
"text-anchor": "middle", "font-family": "ui-monospace, monospace"
|
||
});
|
||
text.textContent = (min + (max - min) * index / 20).toFixed(decimals);
|
||
this.element.append(text);
|
||
}
|
||
}
|
||
this.needle = svgElement("line", {
|
||
x1: 160, y1: 150, x2: 160, y2: 55, stroke: "#f1f4e9", "stroke-width": 3,
|
||
"stroke-linecap": "round"
|
||
});
|
||
this.element.append(this.needle, svgElement("circle", { cx: 160, cy: 150, r: 9, fill: "#e9efe1" }));
|
||
this.element.append(svgElement("circle", { cx: 160, cy: 150, r: 4, fill: "#314536" }));
|
||
const unitText = svgElement("text", {
|
||
x: 160, y: 186, fill: "#9da99b", "font-size": 11, "text-anchor": "middle",
|
||
"font-family": "ui-monospace, monospace"
|
||
});
|
||
unitText.textContent = `${unit} · ${label}`;
|
||
this.element.append(unitText);
|
||
}
|
||
|
||
setTarget(target, tolerance) {
|
||
this.options.target = target;
|
||
this.options.tolerance = tolerance;
|
||
this.build();
|
||
}
|
||
|
||
update(value) {
|
||
const { min, max, unit, sweep } = this.options;
|
||
const angle = -sweep / 2 + sweep * (clamp(min, max, value) - min) / (max - min);
|
||
this.needle.setAttribute("transform", `rotate(${angle} 160 150)`);
|
||
this.element.setAttribute("aria-label", `当前 ${value.toFixed(2)} ${unit}`);
|
||
}
|
||
}
|
||
|
||
class TimerScale {
|
||
constructor(element, options) {
|
||
this.element = element;
|
||
this.max = options.max;
|
||
this.output = element.querySelector("[data-time]");
|
||
this.band = element.querySelector("[data-target-band]");
|
||
this.marker = element.querySelector("[data-time-marker]");
|
||
this.targetLabel = element.querySelector("[data-target]");
|
||
this.element.querySelector("[data-max]").textContent = `${this.max} s`;
|
||
this.update(0, false);
|
||
}
|
||
|
||
setTarget(min, max) {
|
||
this.band.style.left = `${min / this.max * 100}%`;
|
||
this.band.style.width = `${(max - min) / this.max * 100}%`;
|
||
this.targetLabel.textContent = `${min}–${max} s`;
|
||
this.element.setAttribute("aria-label", `${this.element.dataset.label},目标 ${min} 到 ${max} 秒`);
|
||
}
|
||
|
||
update(value, active) {
|
||
this.output.textContent = value.toFixed(1).padStart(4, "0");
|
||
this.marker.style.left = `${clamp(0, 100, value / this.max * 100)}%`;
|
||
this.element.classList.toggle("active", active);
|
||
}
|
||
}
|
||
|
||
class MetricBar {
|
||
constructor(element, options) {
|
||
this.element = element;
|
||
this.max = options.max;
|
||
this.fill = element.querySelector("[data-fill]");
|
||
this.marker = element.querySelector("[data-marker]");
|
||
this.output = element.querySelector("[data-value]");
|
||
this.detail = element.querySelector("[data-detail]");
|
||
this.targetLabel = element.querySelector("[data-target]");
|
||
this.element.setAttribute("role", "meter");
|
||
this.element.setAttribute("aria-valuemin", "0");
|
||
this.element.setAttribute("aria-valuemax", String(this.max));
|
||
}
|
||
|
||
setTarget(value, label) {
|
||
this.marker.style.left = `${clamp(0, 100, value / this.max * 100)}%`;
|
||
this.targetLabel.textContent = label;
|
||
}
|
||
|
||
setDetail(text) {
|
||
this.detail.textContent = text;
|
||
}
|
||
|
||
update(value, text) {
|
||
const percent = clamp(0, 100, value / this.max * 100);
|
||
this.fill.style.width = `${percent}%`;
|
||
this.output.textContent = text;
|
||
this.element.setAttribute("aria-valuenow", String(value));
|
||
this.element.setAttribute("aria-valuetext", text);
|
||
}
|
||
}
|
||
|
||
window.Lab = { clamp, Gauge, TimerScale, MetricBar };
|
||
})();
|