feat: establish coffee game and grinder baseline
This commit is contained in:
Vendored
+218
@@ -0,0 +1,218 @@
|
||||
(() => {
|
||||
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 AnalogControl {
|
||||
constructor(element, options) {
|
||||
this.element = element;
|
||||
this.value = options.value ?? 0;
|
||||
this.onChange = options.onChange;
|
||||
this.label = options.label;
|
||||
this.dragging = false;
|
||||
this.build();
|
||||
this.bindPointer();
|
||||
this.setValue(this.value, false);
|
||||
}
|
||||
|
||||
bindPointer() {
|
||||
this.element.addEventListener("pointerdown", event => {
|
||||
if (event.pointerType === "mouse" && event.button !== 0) return;
|
||||
event.preventDefault();
|
||||
this.element.focus({ preventScroll: true });
|
||||
this.element.setPointerCapture(event.pointerId);
|
||||
this.dragging = true;
|
||||
this.element.classList.add("dragging");
|
||||
this.setFromPointer(event);
|
||||
});
|
||||
this.element.addEventListener("pointermove", event => {
|
||||
if (!this.dragging) return;
|
||||
event.preventDefault();
|
||||
this.setFromPointer(event);
|
||||
});
|
||||
const finish = event => {
|
||||
this.dragging = false;
|
||||
this.element.classList.remove("dragging");
|
||||
if (this.element.hasPointerCapture(event.pointerId)) this.element.releasePointerCapture(event.pointerId);
|
||||
};
|
||||
this.element.addEventListener("pointerup", finish);
|
||||
this.element.addEventListener("pointercancel", finish);
|
||||
this.element.addEventListener("lostpointercapture", () => {
|
||||
this.dragging = false;
|
||||
this.element.classList.remove("dragging");
|
||||
});
|
||||
this.element.addEventListener("keydown", event => {
|
||||
const step = event.key === "PageUp" || event.key === "PageDown" ? 0.1 : 0.01;
|
||||
const directions = { ArrowLeft: -step, ArrowDown: -step, ArrowRight: step, ArrowUp: step, PageDown: -step, PageUp: step };
|
||||
if (event.key === "Home" || event.key === "End") {
|
||||
event.preventDefault();
|
||||
this.setValue(event.key === "Home" ? 0 : 1);
|
||||
} else if (event.key in directions) {
|
||||
event.preventDefault();
|
||||
this.setValue(this.value + directions[event.key]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setValue(value, notify = true) {
|
||||
this.value = clamp(0, 1, value);
|
||||
this.render();
|
||||
if (notify && this.onChange) this.onChange(this.value);
|
||||
}
|
||||
}
|
||||
|
||||
class RotaryValve extends AnalogControl {
|
||||
point(angle, radius) {
|
||||
const radians = angle * Math.PI / 180;
|
||||
return { x: 160 + radius * Math.sin(radians), y: 160 - radius * Math.cos(radians) };
|
||||
}
|
||||
|
||||
build() {
|
||||
this.element.classList.add("machine-control", "rotary-valve");
|
||||
this.element.setAttribute("viewBox", "0 0 320 260");
|
||||
this.element.setAttribute("role", "slider");
|
||||
this.element.setAttribute("tabindex", "0");
|
||||
this.element.setAttribute("aria-label", this.label);
|
||||
this.element.setAttribute("aria-valuemin", "0");
|
||||
this.element.setAttribute("aria-valuemax", "180");
|
||||
const arc = Array.from({ length: 61 }, (_, index) => {
|
||||
const point = this.point(-90 + index * 3, 124);
|
||||
return `${index ? "L" : "M"}${point.x.toFixed(2)},${point.y.toFixed(2)}`;
|
||||
}).join(" ");
|
||||
this.element.append(svgElement("path", {
|
||||
d: arc, fill: "none", stroke: "rgba(238,243,232,.22)",
|
||||
"stroke-width": 7, "stroke-linecap": "round"
|
||||
}));
|
||||
for (let index = 0; index <= 36; index += 1) {
|
||||
const angle = -90 + index * 5;
|
||||
const major = index % 6 === 0;
|
||||
const outer = this.point(angle, 124);
|
||||
const inner = this.point(angle, major ? 110 : 117);
|
||||
this.element.append(svgElement("line", {
|
||||
x1: inner.x, y1: inner.y, x2: outer.x, y2: outer.y,
|
||||
stroke: major ? "#b8c3b2" : "#71806f", "stroke-width": major ? 2 : 1
|
||||
}));
|
||||
if (major) {
|
||||
const label = this.point(angle, 143);
|
||||
const text = svgElement("text", {
|
||||
x: label.x, y: label.y + 3, fill: "#aebba9", "font-size": 10,
|
||||
"text-anchor": "middle", "font-family": "ui-monospace, monospace"
|
||||
});
|
||||
text.textContent = `${index * 5}°`;
|
||||
this.element.append(text);
|
||||
}
|
||||
}
|
||||
this.element.append(svgElement("circle", {
|
||||
cx: 160, cy: 160, r: 67, fill: "#2d382f", stroke: "#66765f", "stroke-width": 2
|
||||
}));
|
||||
this.handle = svgElement("g", { class: "control-handle" });
|
||||
this.handle.append(
|
||||
svgElement("line", {
|
||||
x1: 160, y1: 160, x2: 160, y2: 72,
|
||||
stroke: "#b99c63", "stroke-width": 13, "stroke-linecap": "round"
|
||||
}),
|
||||
svgElement("rect", {
|
||||
x: 148, y: 48, width: 24, height: 48, rx: 9,
|
||||
fill: "#e2d3b3", stroke: "#8e754b", "stroke-width": 3
|
||||
}),
|
||||
svgElement("line", {
|
||||
x1: 160, y1: 59, x2: 160, y2: 85,
|
||||
stroke: "rgba(91,68,37,.34)", "stroke-width": 3, "stroke-linecap": "round"
|
||||
})
|
||||
);
|
||||
this.element.append(this.handle);
|
||||
this.element.append(
|
||||
svgElement("circle", { cx: 160, cy: 160, r: 19, fill: "#d8c5a0", stroke: "#8e754b", "stroke-width": 3 }),
|
||||
svgElement("circle", { cx: 160, cy: 160, r: 6, fill: "#45523f" })
|
||||
);
|
||||
this.angleText = svgElement("text", {
|
||||
x: 160, y: 233, fill: "#edf2e7", "font-size": 24,
|
||||
"text-anchor": "middle", "font-family": "ui-monospace, monospace"
|
||||
});
|
||||
this.element.append(this.angleText);
|
||||
}
|
||||
|
||||
setFromPointer(event) {
|
||||
const rect = this.element.getBoundingClientRect();
|
||||
const x = (event.clientX - rect.left) * 320 / rect.width - 160;
|
||||
const y = (event.clientY - rect.top) * 260 / rect.height - 160;
|
||||
const angle = Math.atan2(x, -y) * 180 / Math.PI;
|
||||
this.setValue(angle < -90 ? 0 : angle > 90 ? 1 : (angle + 90) / 180);
|
||||
}
|
||||
|
||||
render() {
|
||||
const degrees = this.value * 180;
|
||||
this.handle.setAttribute("transform", `rotate(${-90 + degrees} 160 160)`);
|
||||
this.angleText.textContent = `${Math.round(degrees)}°`;
|
||||
this.element.setAttribute("aria-valuenow", String(Math.round(degrees)));
|
||||
this.element.setAttribute("aria-valuetext", `${Math.round(degrees)} 度`);
|
||||
}
|
||||
}
|
||||
|
||||
class FlowLever extends AnalogControl {
|
||||
build() {
|
||||
this.element.classList.add("machine-control", "flow-lever");
|
||||
this.element.setAttribute("viewBox", "0 0 320 260");
|
||||
this.element.setAttribute("role", "slider");
|
||||
this.element.setAttribute("tabindex", "0");
|
||||
this.element.setAttribute("aria-label", this.label);
|
||||
this.element.setAttribute("aria-valuemin", "0");
|
||||
this.element.setAttribute("aria-valuemax", "100");
|
||||
this.element.append(
|
||||
svgElement("line", { x1: 40, y1: 140, x2: 280, y2: 140, stroke: "#141b16", "stroke-width": 20, "stroke-linecap": "round" }),
|
||||
svgElement("line", { x1: 40, y1: 140, x2: 280, y2: 140, stroke: "#63705f", "stroke-width": 3, "stroke-linecap": "round" })
|
||||
);
|
||||
for (let index = 0; index <= 20; index += 1) {
|
||||
const major = index % 5 === 0;
|
||||
const x = 40 + index * 12;
|
||||
this.element.append(svgElement("line", {
|
||||
x1: x, y1: 155, x2: x, y2: major ? 169 : 163,
|
||||
stroke: major ? "#b8c3b2" : "#71806f", "stroke-width": major ? 2 : 1
|
||||
}));
|
||||
if (major) {
|
||||
const text = svgElement("text", {
|
||||
x, y: 190, fill: "#aebba9", "font-size": 11,
|
||||
"text-anchor": "middle", "font-family": "ui-monospace, monospace"
|
||||
});
|
||||
text.textContent = String(index * 5);
|
||||
this.element.append(text);
|
||||
}
|
||||
}
|
||||
this.handle = svgElement("g", { class: "control-handle" });
|
||||
this.handle.append(
|
||||
svgElement("line", { x1: 0, y1: 141, x2: 0, y2: 85, stroke: "#b99c63", "stroke-width": 14, "stroke-linecap": "round" }),
|
||||
svgElement("rect", { x: -16, y: 47, width: 32, height: 51, rx: 10, fill: "#e2d3b3", stroke: "#8e754b", "stroke-width": 3 }),
|
||||
svgElement("line", { x1: 0, y1: 59, x2: 0, y2: 85, stroke: "rgba(91,68,37,.34)", "stroke-width": 3, "stroke-linecap": "round" }),
|
||||
svgElement("circle", { cx: 0, cy: 140, r: 13, fill: "#d8c5a0", stroke: "#8e754b", "stroke-width": 3 }),
|
||||
svgElement("circle", { cx: 0, cy: 140, r: 5, fill: "#45523f" })
|
||||
);
|
||||
this.element.append(this.handle);
|
||||
const hint = svgElement("text", {
|
||||
x: 160, y: 229, fill: "#aebba9", "font-size": 11,
|
||||
"text-anchor": "middle", "font-family": "ui-sans-serif, sans-serif", "letter-spacing": 2
|
||||
});
|
||||
hint.textContent = "关闭 · 萃取水流 · 全开";
|
||||
this.element.append(hint);
|
||||
}
|
||||
|
||||
setFromPointer(event) {
|
||||
const rect = this.element.getBoundingClientRect();
|
||||
const x = (event.clientX - rect.left) * 320 / rect.width;
|
||||
this.setValue((x - 40) / 240);
|
||||
}
|
||||
|
||||
render() {
|
||||
const percent = Math.round(this.value * 100);
|
||||
this.handle.setAttribute("transform", `translate(${40 + this.value * 240} 0)`);
|
||||
this.element.setAttribute("aria-valuenow", String(percent));
|
||||
this.element.setAttribute("aria-valuetext", `开度 ${percent}%`);
|
||||
}
|
||||
}
|
||||
|
||||
window.MachineControls = { RotaryValve, FlowLever };
|
||||
})();
|
||||
Reference in New Issue
Block a user