好的,这是一个使用单个HTML文件实现的Mini LED分区直观展示网页。它包含了控制面板、模拟屏幕、分区排布、缩放、平移以及缩放比例显示等功能。
一个HTML文件 (index.html):
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mini LED 分区可视化</title>
<style>
body {
margin: 0;
overflow: hidden; /* 防止body滚动条出现 */
font-family: sans-serif;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
#controls {
position: fixed;
top: 10px;
left: 10px;
background: rgba(255, 255, 255, 0.9);
padding: 15px;
border-radius: 8px;
box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.2);
z-index: 100; /* 确保在屏幕上方 */
max-width: 250px;
box-sizing: border-box;
}
#controls h2 {
margin-top: 0;
font-size: 1.2em;
border-bottom: 1px solid #ccc;
padding-bottom: 8px;
margin-bottom: 10px;
}
#controls div {
margin-bottom: 10px;
}
#controls label {
display: block;
margin-bottom: 4px;
font-weight: bold;
font-size: 0.9em;
}
#controls input[type="number"],
#controls input[type="text"] {
width: 100%;
padding: 6px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
font-size: 0.9em;
}
#zoomInfo {
position: fixed;
top: 10px;
right: 10px;
background: rgba(0, 0, 0, 0.6);
color: white;
padding: 5px 10px;
border-radius: 4px;
z-index: 100; /* 确保在屏幕上方 */
font-size: 0.9em;
}
#screenContainer {
position: absolute; /* 允许内部元素绝对定位 */
width: 100%;
height: 100%;
overflow: hidden; /* 隐藏超出容器的内容 */
/* cursor: grab; /* 可抓取光标,由JS控制切换 */
}
#screen {
position: absolute;
background-color: #222; /* 屏幕背景色 */
border: 4px solid #444; /* 屏幕边框 */
box-sizing: border-box;
transform-origin: 0 0; /* 缩放平移的基准点 */
/* transform will be applied by JS */
display: grid; /* 使用Grid布局方便排布分区 */
transition: width 0.3s ease, height 0.3s ease, left 0.3s ease, top 0.3s ease; /* 平滑调整大小位置 */
}
.zone {
box-sizing: border-box;
border: 1px solid rgba(255, 255, 255, 0.1); /* 分区间隔线 */
background-color: rgba(255, 165, 0, 0.1); /* 分区颜色(半透明) */
display: flex;
justify-content: center;
align-items: center;
font-size: 0.8em;
color: rgba(255, 255, 255, 0.3);
user-select: none; /* 防止文字被选中影响拖拽 */
transition: background-color 0.2s ease; /* 悬停效果 */
}
.zone:nth-child(odd) {
/* Alternate colors for better visual separation */
background-color: rgba(0, 128, 255, 0.1);
}
.zone:hover {
background-color: rgba(255, 165, 0, 0.5);
color: rgba(255, 255, 255, 0.8);
}
.zone:nth-child(odd):hover {
background-color: rgba(0, 128, 255, 0.5);
}
</style>
</head>
<body>
<div id="controls">
<h2>Mini LED 分区可视化</h2>
<div>
<label for="diagonal">屏幕对角线 (英寸):</label>
<input type="number" id="diagonal" value="32" min="1">
</div>
<div>
<label for="aspectRatio">屏幕比例 (W:H):</label>
<input type="text" id="aspectRatio" value="16:9">
</div>
<div>
<label for="totalZones">分区总数:</label>
<input type="number" id="totalZones" value="1152" min="1">
</div>
</div>
<div id="zoomInfo">当前缩放: 100%</div>
<div id="screenContainer">
<div id="screen">
<!-- 分区将由JavaScript生成 -->
</div>
</div>
<script>
const controls = document.getElementById('controls');
const diagonalInput = document.getElementById('diagonal');
const aspectRatioInput = document.getElementById('aspectRatio');
const totalZonesInput = document.getElementById('totalZones');
const screenContainer = document.getElementById('screenContainer');
const screenDiv = document.getElementById('screen');
const zoomInfo = document.getElementById('zoomInfo');
// --- State Variables ---
let currentZoom = 1.0;
let currentPanX = 0;
let currentPanY = 0;
let isPanning = false;
let startMouseX = 0;
let startMouseY = 0;
let initialPanX = 0;
let initialPanY = 0;
// --- Helper Functions ---
// 解析屏幕比例字符串 "W:H"
function parseAspectRatio(ratioString) {
const parts = ratioString.split(':').map(Number);
if (parts.length === 2 && !isNaN(parts[0]) && !isNaN(parts[1]) && parts[0] > 0 && parts[1] > 0) {
return { width: parts[0], height: parts[1] };
}
return null; // 无效格式
}
// 根据总分区数和屏幕比例,找到最接近的行数和列数组合
function findBestGrid(totalZones, screenRatio) {
let bestRows = 1;
let bestCols = totalZones;
let minRatioDiff = Math.abs((totalZones / 1) - screenRatio); // 初始为 1 x N 组合
// 遍历所有可能的行数 (不超过总分区的平方根,因为因子是成对出现的)
for (let rows = 1; rows <= Math.sqrt(totalZones); rows++) {
if (totalZones % rows === 0) {
const cols = totalZones / rows;
// 检查 rows x cols 组合
let currentRatioDiff = Math.abs((cols / rows) - screenRatio);
if (currentRatioDiff < minRatioDiff) {
minRatioDiff = currentRatioDiff;
bestRows = rows;
bestCols = cols;
}
// 检查 cols x rows 组合 (如果 cols != rows)
if (rows !== cols) {
currentRatioDiff = Math.abs((rows / cols) - screenRatio);
if (currentRatioDiff < minRatioDiff) {
minRatioDiff = currentRatioDiff;
bestRows = cols; // Note: now cols is the row count
bestCols = rows; // Note: now rows is the col count
}
}
}
}
// Ensure cols >= rows is generally preferred unless ratio dictates otherwise
if (bestCols < bestRows && (bestCols/bestRows) < screenRatio && (bestRows/bestCols) > screenRatio * 1.5) {
// Don't swap if cols < rows severely breaks the ratio
} else if (bestCols < bestRows && screenRatio > 1) {
// If screen is wider than tall, prefer cols > rows if ratio is similar
const temp = bestRows;
bestRows = bestCols;
bestCols = temp;
}
return { rows: bestRows, cols: bestCols };
}
// 生成分区DOM元素
function createZonesHTML(rows, cols) {
let zonesHTML = '';
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
// 使用Grid布局,每个分区自动占据一个网格单元
// 不需要绝对定位和计算left/top/width/height的百分比了
zonesHTML += `<div class="zone" style="grid-row:${i + 1}; grid-column:${j + 1};"></div>`;
// Optional: Add zone index or coordinate if needed
// zonesHTML += `<div class="zone" style="grid-row:${i + 1}; grid-column:${j + 1};">${i * cols + j + 1}</div>`;
}
}
screenDiv.innerHTML = zonesHTML;
// 设置Grid模板
screenDiv.style.gridTemplateRows = `repeat(${rows}, 1fr)`;
screenDiv.style.gridTemplateColumns = `repeat(${cols}, 1fr)`;
}
// 应用当前的缩放和平移变换
function applyTransform() {
screenDiv.style.transform = `translate(${currentPanX}px, ${currentPanY}px) scale(${currentZoom})`;
zoomInfo.textContent = `当前缩放: ${Math.round(currentZoom * 100)}%`;
// Update cursor based on panning state
screenContainer.style.cursor = isPanning ? 'grabbing' : 'grab';
}
// 更新可视化(计算尺寸、生成分区、定位屏幕)
function updateVisualization() {
const diagonal = parseFloat(diagonalInput.value);
const aspectRatioString = aspectRatioInput.value.trim();
const totalZones = parseInt(totalZonesInput.value);
if (isNaN(diagonal) || diagonal <= 0 || isNaN(totalZones) || totalZones <= 0) {
console.error("无效的输入: 对角线或分区总数必须是正数");
// 清空屏幕或显示错误信息
screenDiv.innerHTML = '';
screenDiv.style.width = '0px';
screenDiv.style.height = '0px';
screenDiv.style.left = '50%';
screenDiv.style.top = '50%';
screenDiv.style.transform = 'translate(-50%, -50%)'; // Reset transform before pan/zoom
currentPanX = 0;
currentPanY = 0;
currentZoom = 1.0;
applyTransform();
return;
}
const aspectRatio = parseAspectRatio(aspectRatioString);
if (!aspectRatio) {
console.error("无效的屏幕比例格式。请使用 W:H 格式,例如 16:9");
screenDiv.innerHTML = '';
screenDiv.style.width = '0px';
screenDiv.style.height = '0px';
screenDiv.style.left = '50%';
screenDiv.style.top = '50%';
screenDiv.style.transform = 'translate(-50%, -50%)'; // Reset transform before pan/zoom
currentPanX = 0;
currentPanY = 0;
currentZoom = 1.0;
applyTransform();
return;
}
// 计算屏幕在容器中应该占据的像素尺寸以保持比例并最大化
const containerWidth = screenContainer.clientWidth;
const containerHeight = screenContainer.clientHeight;
const screenRatio = aspectRatio.width / aspectRatio.height;
let screenPixelWidth;
let screenPixelHeight;
// 尝试基于容器宽度计算
let potentialWidth = containerWidth;
let potentialHeight = containerWidth / screenRatio;
if (potentialHeight > containerHeight) {
// 如果基于宽度计算的高度超出了容器,则基于容器高度计算
screenPixelHeight = containerHeight;
screenPixelWidth = containerHeight * screenRatio;
} else {
// 否则,基于宽度计算的尺寸是合适的
screenPixelWidth = potentialWidth;
screenPixelHeight = potentialHeight;
}
// 为了确保屏幕即使在最大化时也有边框显示且不紧贴边缘,可以稍微留白
const padding = 20; // 20px padding
const maxScreenPixelWidth = containerWidth - padding * 2;
const maxScreenPixelHeight = containerHeight - padding * 2;
if (screenPixelWidth > maxScreenPixelWidth) {
screenPixelWidth = maxScreenPixelWidth;
screenPixelHeight = screenPixelWidth / screenRatio;
}
if (screenPixelHeight > maxScreenPixelHeight) {
screenPixelHeight = maxScreenPixelHeight;
screenPixelWidth = screenPixelHeight * screenRatio;
}
// 设置屏幕div的像素尺寸
screenDiv.style.width = `${screenPixelWidth}px`;
screenDiv.style.height = `${screenPixelHeight}px`;
// 将屏幕div居中在容器中 (这是初始位置,pan/zoom transform在此基础上应用)
screenDiv.style.left = `${(containerWidth - screenPixelWidth) / 2}px`;
screenDiv.style.top = `${(containerHeight - screenPixelHeight) / 2}px`;
// 找到最佳的行数和列数
const grid = findBestGrid(totalZones, screenRatio);
console.log(`Total Zones: ${totalZones}, Screen Ratio: ${screenRatio.toFixed(2)} -> Grid: ${grid.rows} rows x ${grid.cols} cols`);
// 生成分区DOM
createZonesHTML(grid.rows, grid.cols);
// 应用当前的缩放和平移(重置位置后需要重新应用)
// Note: When recalculating size/position, we should perhaps reset zoom/pan
// Or, calculate the new pan/zoom to keep the *center* of the screen in the same place?
// For simplicity, let's reset pan and zoom to 100% when screen size changes significantly (like window resize or control change)
currentZoom = 1.0;
currentPanX = 0;
currentPanY = 0;
applyTransform(); // Apply initial transform (translate 0,0 scale 1)
// The centering using left/top properties correctly positions the *untransformed* screen.
// The transform property then applies pan/zoom relative to this initial position.
// We need to account for the left/top offset when calculating zoom/pan relative to mouse position.
}
// --- Event Handlers ---
// 滚轮缩放
screenContainer.addEventListener('wheel', (event) => {
event.preventDefault(); // 阻止页面滚动
const containerRect = screenContainer.getBoundingClientRect();
const screenRect = screenDiv.getBoundingClientRect(); // Current transformed screen position/size
// Mouse position relative to the container
const mouseX_rel_container = event.clientX - containerRect.left;
const mouseY_rel_container = event.clientY - containerRect.top;
// Mouse position relative to the *untransformed* screen coordinates
// This is the point on the screen's content that should stay under the cursor
const mouseX_untransformed = (mouseX_rel_container - (screenRect.left - containerRect.left)) / currentZoom;
const mouseY_untransformed = (mouseY_rel_container - (screenRect.top - containerRect.top)) / currentZoom;
const zoomFactor = event.deltaY > 0 ? 0.9 : 1.1; // 滚轮向下缩小,向上放大
const newZoom = Math.max(0.1, Math.min(10.0, currentZoom * zoomFactor)); // 限制缩放范围 (10% - 1000%)
// Calculate new pan offset to keep the point under the mouse cursor
// newPanX = mouseX_rel_container - (mouseX_untransformed * newZoom) - screen.offsetLeft
// The screenDiv.style.left/top is its initial position before transform
const screenInitialLeft = parseFloat(screenDiv.style.left);
const screenInitialTop = parseFloat(screenDiv.style.top);
const newPanX = (mouseX_rel_container - screenInitialLeft) - (mouseX_untransformed * newZoom);
const newPanY = (mouseY_rel_container - screenInitialTop) - (mouseY_untransformed * newZoom);
currentZoom = newZoom;
currentPanX = newPanX;
currentPanY = newPanY;
applyTransform();
});
// 鼠标按下开始平移
screenContainer.addEventListener('mousedown', (event) => {
if (event.button === 0) { // 检查是否是左键
isPanning = true;
startMouseX = event.clientX;
startMouseY = event.clientY;
initialPanX = currentPanX;
initialPanY = currentPanY;
screenContainer.style.cursor = 'grabbing'; // 改变光标样式
// Prevent dragging selected text etc.
event.preventDefault();
}
});
// 鼠标移动进行平移 (监听document,即使鼠标移出container也能继续拖拽直到松开)
document.addEventListener('mousemove', (event) => {
if (isPanning) {
const deltaX = event.clientX - startMouseX;
const deltaY = event.clientY - startMouseY;
currentPanX = initialPanX + deltaX;
currentPanY = initialPanY + deltaY;
applyTransform();
}
});
// 鼠标松开结束平移
document.addEventListener('mouseup', () => {
if (isPanning) {
isPanning = false;
screenContainer.style.cursor = 'grab'; // 恢复光标样式
}
});
// 监听控制面板输入变化实时更新
diagonalInput.addEventListener('input', updateVisualization);
aspectRatioInput.addEventListener('input', updateVisualization);
totalZonesInput.addEventListener('input', updateVisualization);
// 监听窗口尺寸变化,重新计算屏幕大小和位置
window.addEventListener('resize', updateVisualization);
// --- Initial Setup ---
// 页面加载完成后首次渲染
updateVisualization();
</script>
</body>
</html>
如何使用:
- 将上面的代码复制到一个名为
index.html 的文件中。
- 用浏览器打开这个
index.html 文件。
功能说明:
- 控制面板: 左上角浮动的白色框,包含:
- 屏幕对角线 (英寸): 输入屏幕的物理对角线尺寸。
- 屏幕比例 (W:H): 输入屏幕的宽高比,例如
16:9、4:3、21:9 等。
- 分区总数: 输入Mini LED背光的分区总数。
- 修改任何输入框的值,可视化会实时更新。
- 模拟屏幕: 页面中央的深色矩形框,模拟屏幕显示区域。其尺寸会根据窗口大小和输入的屏幕比例自动调整,尽量最大化显示。
- 分区排布: 模拟屏幕内部由许多小矩形组成,每个小矩形代表一个Mini LED背光分区。系统会根据输入的总分区数和屏幕比例,计算出一个最接近屏幕比例的网格(行x列)来容纳所有分区。
- 分区样式: 每个分区都有边框分隔,并且使用不同的颜色(或颜色透明度)间隔,使其区分明显。鼠标悬停在分区上会有高亮效果。
- 缩放: 在模拟屏幕区域滚动鼠标滚轮可以对屏幕进行缩放,支持放大到超过物理尺寸的比例。缩放中心是鼠标指针的位置。
- 平移: 按住鼠标左键在模拟屏幕区域拖动可以平移屏幕视图,即使在缩放状态下也可以自由移动查看细节。
- 缩放比例显示: 右上角会实时显示当前模拟屏幕的缩放比例百分比。这里的100%表示模拟屏幕的像素尺寸被计算调整后,当前缩放级别下其在网页上的显示尺寸。
- 布局调整: 窗口大小变化时,模拟屏幕会自动重新计算尺寸和位置以适应新的窗口大小。
这个实现使用CSS Grid布局来方便地排布分区,简化了JavaScript计算每个分区的绝对位置和尺寸。JavaScript负责计算整个屏幕区域的像素尺寸和位置,找到最佳的行列数,生成分区元素,并处理缩放和平移的交互逻辑。