36 lines
1.4 KiB
JavaScript
36 lines
1.4 KiB
JavaScript
"use strict";
|
|
|
|
// Layer: display/helpers
|
|
// Shared canvas and display-label helpers used by render, item, creature, and weather modules.
|
|
(function (global) {
|
|
const WEATHER_LABELS = Object.freeze({ sunny: "\u6674\u308c", cloudy: "\u66c7\u308a", light_rain: "\u5c0f\u96e8" });
|
|
|
|
function canvasEllipsizeText(ctx, text, maxW) {
|
|
let s = String(text || "");
|
|
const limit = Number(maxW);
|
|
if (!ctx || !Number.isFinite(limit) || limit <= 0) return s ? "\u2026" : "";
|
|
if (ctx.measureText(s).width <= limit) return s;
|
|
while (s.length > 1 && ctx.measureText(`${s}\u2026`).width > limit) s = s.slice(0, -1);
|
|
return `${s}\u2026`;
|
|
}
|
|
|
|
function roundedRect(ctx, x, y, w, h, r) {
|
|
if (!ctx) return;
|
|
const radius = Math.max(0, Math.min(Math.abs(Number(r) || 0), Math.abs(Number(w) || 0) * 0.5, Math.abs(Number(h) || 0) * 0.5));
|
|
ctx.beginPath();
|
|
ctx.moveTo(x + radius, y);
|
|
ctx.arcTo(x + w, y, x + w, y + h, radius);
|
|
ctx.arcTo(x + w, y + h, x, y + h, radius);
|
|
ctx.arcTo(x, y + h, x, y, radius);
|
|
ctx.arcTo(x, y, x + w, y, radius);
|
|
ctx.closePath();
|
|
}
|
|
|
|
function weatherLabel(weather = "sunny") {
|
|
return WEATHER_LABELS[String(weather || "sunny")] || WEATHER_LABELS.sunny;
|
|
}
|
|
|
|
global.canvasEllipsizeText = canvasEllipsizeText;
|
|
global.roundedRect = roundedRect;
|
|
global.weatherLabel = weatherLabel;
|
|
})(typeof window !== "undefined" ? window : globalThis);
|