Files
xbcr/src-ts/map/icons.ts
T
zzy 8ece55983a feat(前端): TypeScript 重写版(esbuild IIFE 产物,暂不接管线上)
- 新增 src-ts/(22 个 .ts / 7792 行):按 core / map / card / order / nav / share /
  subscribe / ui / compat 分层,types 里有 BMapGL 与 API 响应声明
- compat/globals.ts 是唯一挂 window 的地方(292 个),覆盖模板 139 处内联事件
- esbuild 产出 static/dist/app.js(226.6 KiB,IIFE,es2019)+ README(构建/切换/回滚)
- 构建可复现:node esbuild.config.mjs、node --check、npx tsc --noEmit 均通过
- .gitignore 增加 node_modules/ 与 static/dist/*.map(sourcemap 可重生成,不入库)
- 暂不改 templates/map.html 与 MAP_ASSETS:等独立核对通过再切换,回滚=换回四个 script
2026-11-16 10:44:11 +08:00

281 lines
10 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* 由 static/js/{navigation,share,subscribe,map}.js 逐块搬运而来;正文与原实现一致。 */
// static/js/map.js:30-30
const iconCache = new Map();
// static/js/map.js:31-34
const ICON_CACHE_MAX = 500; // 最多缓存500个不同图标
// ========== 预创建完整图标(首次加载优化)==========
// 预创建各电量和颜色的完整图标(避免首次加载时大量 Canvas 绘制)
// static/js/map.js:35-43
const BASE_ICON_COLORS = {
available: "#34C759", // 绿色-可用
inuse: "#FF2D55", // 红色-使用中
locked: "#FF9F0A", // 橙色-欠压
reserved: "#5856D6", // 紫色-损坏
lowbattery: "#8E8E93" // 灰色-低电量
};
// 预创建非高亮状态的完整图标(电量档位 0, 20, 40, 60, 80, 100)
// static/js/map.js:44-44
const prebuiltIcons = new Map(); // key: "电量档位|颜色", value: BMapGL.Icon
// static/js/map.js:46-101
export function createFullIcon(size, color, electric, isHighlighted, bikeCode) {
const canvas = document.createElement("canvas");
canvas.width = size;
canvas.height = size;
const ctx = canvas.getContext("2d");
const r = size / 2 - (isHighlighted ? 12 : 8);
const lineWidth = isHighlighted ? 16 : 12;
const centerX = size / 2;
const centerY = size / 2;
// 高亮时加阴影
if (isHighlighted) {
ctx.shadowColor = color;
ctx.shadowBlur = 48;
}
// 白色背景
ctx.beginPath();
ctx.arc(centerX, centerY, r, 0, Math.PI * 2);
ctx.fillStyle = "white";
ctx.fill();
// 边框
ctx.beginPath();
ctx.arc(centerX, centerY, r, 0, Math.PI * 2);
ctx.strokeStyle = isHighlighted ? color : "rgba(200,200,200,0.5)";
ctx.lineWidth = isHighlighted ? 16 : lineWidth;
ctx.stroke();
ctx.shadowBlur = 0;
// 使用中:实心圆环
if (isHighlighted) {
// 高亮时只显示车牌号,没有进度环
} else {
// 非高亮:绘制电量进度环
const startAngle = -Math.PI / 2;
const endAngle = startAngle + (Math.PI * 2 * electric / 100);
ctx.beginPath();
ctx.arc(centerX, centerY, r, startAngle, endAngle);
ctx.strokeStyle = color;
ctx.lineWidth = lineWidth;
ctx.lineCap = "round";
ctx.stroke();
}
// 绘制数字
ctx.fillStyle = "#333";
ctx.font = isHighlighted ? "bold 60px Arial" : "bold 44px Arial";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(isHighlighted ? (bikeCode + "") : (Math.round(electric) + ""), centerX, centerY);
return canvas.toDataURL("image/png");
}
// 预初始化完整图标(在地图加载前调用)
// static/js/map.js:102-121
export function preloadBaseIcons() {
const displaySize = 36;
const imageSize = displaySize * 4; // 高清4倍
const electricLevels = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]; // 21个电量档位(5%分度)
Object.values<any>(BASE_ICON_COLORS).forEach(color => {
// 非高亮状态:每个电量档位创建一个
electricLevels.forEach(electric => {
const key = `${electric}|${color}`;
if (!prebuiltIcons.has(key)) {
// 存储 data URL 而不是 Icon 对象
const dataUrl = createFullIcon(imageSize, color, electric, false, null);
prebuiltIcons.set(key, dataUrl);
}
});
});
console.log("[预创建] 已预创建 " + prebuiltIcons.size + " 个图标");
}
// ========== 用车计时 ==========
// static/js/map.js:141-158
export function getBikeState(bike) {
const state = bike.state;
const status = bike.status;
const electric = Number(bike.electric_quantity ?? bike.electricQuantity ?? 100);
// 使用中:被占用
if (state === 1 && status === 1) return 'inuse';
// 低电量:state=10 或电量<20(与后端 _classify_bike_state 同序)
if (state === 10 || electric < 20) return 'lowbattery';
// 空闲可用
if (state === 1) return 'available';
// 欠压车辆
if (state === 4) return 'locked';
// 损坏车辆
if (state === 2) return 'reserved';
return 'unknown';
}
// ========== 电池绘制 ==========
// static/js/map.js:171-222
export function makeIconDataURI(color, percent, isInUse, size = 36) {
const canvas = document.createElement("canvas");
canvas.width = size;
canvas.height = size;
const ctx = canvas.getContext("2d");
const r = size / 2 - 2;
const lineWidth = 3;
const centerX = size / 2;
const centerY = size / 2;
ctx.beginPath();
ctx.arc(centerX, centerY, r - lineWidth, 0, Math.PI * 2);
ctx.fillStyle = "rgba(255,255,255,0.9)";
ctx.fill();
ctx.beginPath();
ctx.arc(centerX, centerY, r, 0, Math.PI * 2);
ctx.strokeStyle = "rgba(200,200,200,0.5)";
ctx.lineWidth = lineWidth;
ctx.stroke();
if (isInUse) {
ctx.beginPath();
ctx.arc(centerX, centerY, r, 0, Math.PI * 2);
ctx.strokeStyle = color;
ctx.lineWidth = lineWidth;
ctx.stroke();
} else {
const startAngle = -Math.PI / 2;
const endAngle = startAngle + (Math.PI * 2 * percent / 100);
ctx.beginPath();
ctx.arc(centerX, centerY, r, startAngle, endAngle);
ctx.strokeStyle = color;
ctx.lineWidth = lineWidth;
ctx.lineCap = "round";
ctx.stroke();
}
ctx.fillStyle = "#333";
ctx.font = `bold ${size < 36 ? 10 : 11}px Arial`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(Math.round(percent) + "", centerX, centerY);
return canvas.toDataURL("image/png");
}
// ========== 车辆类型(编号规则,按实车号段)==========
// - 编号超过 5 位(如 30000193 / 30000033,共 8 位):新引进红色外观车 → 红车
// - 5 位编号取「前三位号段」:511 及以上=大车,510 及以下=小车
// (实测号段:503/504/505/510=小车,511/512/513=大车)
// 红车电量球统一用柔和砖红(不用"使用中"那种血红 #FF2D55,避免视觉惊扰)
// static/js/map.js:223-223
const RED_BIKE_COLOR = "#B4615A"; // 柔和砖红
// static/js/map.js:225-239
export function getBikeKind(bike) {
const digits = String((bike && bike.code) || "").replace(/\D/g, "");
if (digits.length > 5) {
return { key: "red", label: "红车", color: RED_BIKE_COLOR };
}
const num = parseInt(digits, 10);
if (!isFinite(num)) {
return { key: "small", label: "小车", color: null };
}
// 5 位取前三位号段;不足 5 位直接用编号本身比较
const series = digits.length >= 5 ? Math.floor(num / Math.pow(10, digits.length - 3)) : num;
return series >= 511
? { key: "big", label: "大车", color: null }
: { key: "small", label: "小车", color: null };
}
// static/js/map.js:241-321
export function getBikeIcon(bike, isHighlighted = false) {
const electric = Number(bike.electric_quantity ?? bike.electricQuantity ?? 100);
const bikeState = getBikeState(bike);
// 车辆类型只影响弹起卡片上的标注,地图电量球保持原有「按状态配色」
let color = "#34C759"; // 默认绿色-可用
if (bikeState === 'inuse') {
color = "#FF2D55"; // 红色-使用中
} else if (bikeState === 'locked') {
color = "#FF9F0A"; // 橙色-欠压车辆
} else if (bikeState === 'reserved') {
color = "#5856D6"; // 紫色-损坏车辆
} else if (bikeState === 'lowbattery' || electric < 20) {
color = "#8E8E93"; // 灰色-低电量
}
// 生成缓存 key
const electricLevel = Math.floor(electric / 5) * 5;
const cacheKey = isHighlighted
? `${electricLevel}|${color}|1|${bike.code}`
: `${electricLevel}|${color}|0`;
// 检查缓存
if (iconCache.has(cacheKey)) {
return iconCache.get(cacheKey);
}
const renderSize = isHighlighted ? 192 : 144; // 内部渲染尺寸
const displaySize = isHighlighted ? 48 : 36; // 显示尺寸不变
// 非高亮模式:尝试使用预创建的图标
if (!isHighlighted && prebuiltIcons.size > 0) {
// 找到最接近的电量档位
const levels = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100];
let closestLevel = levels[0];
let minDiff = Math.abs(electric - closestLevel);
for (const lvl of levels) {
const diff = Math.abs(electric - lvl);
if (diff < minDiff) {
minDiff = diff;
closestLevel = lvl;
}
}
const prebuiltKey = `${closestLevel}|${color}`;
if (prebuiltIcons.has(prebuiltKey)) {
// 找到预创建的 data URL,直接创建 Icon
const dataUrl = prebuiltIcons.get(prebuiltKey);
const icon = new BMapGL.Icon(
dataUrl,
new BMapGL.Size(displaySize, displaySize),
new BMapGL.Size(displaySize / 2, displaySize / 2)
);
icon.__xbniuAppearance = { url: dataUrl, size: displaySize };
iconCache.set(cacheKey, icon);
return icon;
}
}
// 如果没有预创建的图标,或者需要高亮状态:直接创建完整图标
// imageSize 用于渲染高清内容,displaySize 控制显示大小
const imageSize = renderSize * 4; // 高清4倍
const dataUrl = createFullIcon(imageSize, color, electric, isHighlighted, bike.code);
const icon = new BMapGL.Icon(
dataUrl,
new BMapGL.Size(displaySize, displaySize),
new BMapGL.Size(displaySize / 2, displaySize / 2)
);
icon.__xbniuAppearance = { url: dataUrl, size: displaySize };
// 缓存图标
if (iconCache.size >= ICON_CACHE_MAX) {
const keysToDelete = Array.from(iconCache.keys()).slice(0, Math.floor(ICON_CACHE_MAX * 0.2));
keysToDelete.forEach(k => iconCache.delete(k));
}
iconCache.set(cacheKey, icon);
return icon;
}
// ========== 高亮相关 ==========
// 稳健查找 marker:优先用索引,索引 miss(如批量渲染未完成/刷新过程中)时按 __animId 或标题扫描 currentMarkers 兜底