import { num, text, color, canvas, png, json, plain } from './common.js';
const fields = {
'aspect-ratio': [num('width', '原宽度', 1920, 1, 16000), num('height', '原高度', 1080, 1, 16000), num('target', '目标宽度', 800, 1, 16000)],
'print-layout': [num('paperW', '纸张宽(mm)', 210, 10, 2000, 0.1), num('paperH', '纸张高(mm)', 297, 10, 2000, 0.1), num('itemW', '成品宽(mm)', 90, 1, 1000, 0.1), num('itemH', '成品高(mm)', 54, 1, 1000, 0.1), num('bleed', '单边出血(mm)', 3, 0, 20, 0.1), num('gap', '间距(mm)', 2, 0, 50, 0.1), num('margin', '纸边留白(mm)', 10, 0, 100, 0.1)],
'css-shadow': [num('x', '水平偏移(px)', 8, -80, 80), num('y', '垂直偏移(px)', 12, -80, 80), num('blur', '模糊(px)', 20, 0, 80), num('spread', '扩张(px)', 2, -40, 40), color('color', '阴影色', '#476d6a'), num('opacity', '不透明度', 0.35, 0, 1, 0.05)],
'css-radius': ['tl', 'tr', 'br', 'bl'].map((k, i) => num(k, ['左上(px)', '右上(px)', '右下(px)', '左下(px)'][i], [40, 8, 40, 8][i], 0, 100)),
'css-clip': [text('points', '顶点(x y,百分比;逗号分隔)', '50 0,100 35,80 100,20 100,0 35', true)],
'css-easing': [num('x1', '控制点 1 X', 0.25, 0, 1, 0.01), num('y1', '控制点 1 Y', 0.1, -2, 3, 0.01), num('x2', '控制点 2 X', 0.25, 0, 1, 0.01), num('y2', '控制点 2 Y', 1, -2, 3, 0.01)],
'spacing-scale': [num('base', '基础间距(px)', 4, 1, 64), num('ratio', '倍率', 1.5, 1.1, 3, 0.1), num('steps', '级数', 8, 2, 12)],
};
const css = (value) => plain(value + '\n', 'result.css', 'text/css');
export function getImplementation(id) {
return { fields: fields[id], async run({ values: v }) {
if (id === 'aspect-ratio') { const gcd = (a, b) => b ? gcd(b, a % b) : a, g = gcd(v.width, v.height); return [json({ ratio: `${v.width / g}:${v.height / g}`, targetWidth: v.target, exactTargetHeight: v.target * v.height / v.width, roundedTargetHeight: Math.round(v.target * v.height / v.width) }), css(`aspect-ratio: ${v.width / g} / ${v.height / g};`)]; }
const node = canvas(640, 400, '#ffffff'), ctx = node.getContext('2d'); ctx.fillStyle = '#167d68'; ctx.strokeStyle = '#839a93'; let code, data;
if (id === 'print-layout') {
const aw = v.paperW - v.margin * 2, ah = v.paperH - v.margin * 2; if (aw <= 0 || ah <= 0) throw new Error('留白超过纸张尺寸。');
const a = v.itemW + v.bleed * 2, b = v.itemH + v.bleed * 2, layout = (w, h) => ({ columns: Math.floor((aw + v.gap) / (w + v.gap)), rows: Math.floor((ah + v.gap) / (h + v.gap)), width: w, height: h }); const normal = layout(a, b), rotated = layout(b, a), best = normal.columns * normal.rows >= rotated.columns * rotated.rows ? normal : rotated; const count = best.columns * best.rows;
if (!count || count > 1000) throw new Error('当前参数无法排版或超过 1000 件。'); const scale = Math.min(580 / v.paperW, 340 / v.paperH), ox = (640 - v.paperW * scale) / 2, oy = 30; ctx.strokeRect(ox, oy, v.paperW * scale, v.paperH * scale);
for (let y = 0; y < best.rows; y++) for (let x = 0; x < best.columns; x++) ctx.fillRect(ox + (v.margin + x * (best.width + v.gap)) * scale, oy + (v.margin + y * (best.height + v.gap)) * scale, best.width * scale, best.height * scale);
return [await png(node, 'layout.png'), json({ ...best, count, rotated: best === rotated, paper: [v.paperW, v.paperH], note: '规则网格估算,含出血;未做混合旋转或印刷机咬口优化。' })];
}
if (id === 'css-shadow') {
const hex = v.color.slice(1), r = parseInt(hex.slice(0, 2), 16), g = parseInt(hex.slice(2, 4), 16), b = parseInt(hex.slice(4, 6), 16), rgba = `rgba(${r}, ${g}, ${b}, ${v.opacity})`;
ctx.shadowColor = rgba; ctx.shadowOffsetX = v.x; ctx.shadowOffsetY = v.y; ctx.shadowBlur = v.blur; ctx.fillRect(220 - v.spread, 135 - v.spread, 200 + v.spread * 2, 130 + v.spread * 2); ctx.shadowColor = 'transparent'; ctx.fillStyle = '#e4f1ec'; ctx.fillRect(220, 135, 200, 130); code = `box-shadow: ${v.x}px ${v.y}px ${v.blur}px ${v.spread}px ${rgba};`;
} else if (id === 'css-radius') { ctx.beginPath(); ctx.roundRect(180, 100, 280, 200, [v.tl, v.tr, v.br, v.bl]); ctx.fill(); code = `border-radius: ${v.tl}px ${v.tr}px ${v.br}px ${v.bl}px;`; }
else if (id === 'css-clip') {
const points = v.points.split(',').map((p) => p.trim().split(/\s+/).map(Number)); if (points.length < 3 || points.length > 32 || points.some((p) => p.length !== 2 || p.some((n) => !Number.isFinite(n) || n < 0 || n > 100))) throw new Error('需 3–32 个顶点,每点为 0–100 的 X Y。'); ctx.beginPath(); points.forEach(([x, y], i) => { if (i) ctx.lineTo(120 + x * 4, 40 + y * 3.2); else ctx.moveTo(120 + x * 4, 40 + y * 3.2); }); ctx.closePath(); ctx.fill(); code = `clip-path: polygon(${points.map(([x, y]) => `${x}% ${y}%`).join(', ')});`;
} else if (id === 'css-easing') {
const min = Math.min(0, v.y1, v.y2), max = Math.max(1, v.y1, v.y2), px = (x) => 80 + x * 480, py = (y) => 350 - (y - min) / (max - min) * 300;
ctx.beginPath(); ctx.moveTo(px(0), py(0)); ctx.lineTo(px(v.x1), py(v.y1)); ctx.moveTo(px(1), py(1)); ctx.lineTo(px(v.x2), py(v.y2)); ctx.stroke(); ctx.strokeStyle = '#167d68'; ctx.lineWidth = 4; ctx.beginPath(); ctx.moveTo(px(0), py(0)); ctx.bezierCurveTo(px(v.x1), py(v.y1), px(v.x2), py(v.y2), px(1), py(1)); ctx.stroke(); code = `transition-timing-function: cubic-bezier(${v.x1}, ${v.y1}, ${v.x2}, ${v.y2});`;
data = Array.from({ length: 101 }, (_, i) => { const t = i / 100, a = 3 * (1 - t) ** 2 * t, b = 3 * (1 - t) * t * t; return { progress: a * v.x1 + b * v.x2 + t ** 3, value: a * v.y1 + b * v.y2 + t ** 3 }; });
} else {
const steps = Array.from({ length: v.steps }, (_, i) => Math.round(v.base * v.ratio ** i * 100) / 100); ctx.font = '13px monospace'; const scale = 400 / Math.max(...steps); steps.forEach((value, i) => { ctx.fillRect(100, 20 + i * 360 / v.steps, value * scale, 18); ctx.fillText(`${value}px`, 510, 34 + i * 360 / v.steps); }); code = `:root {\n${steps.map((value, i) => ` --space-${i + 1}: ${value}px;`).join('\n')}\n}`; data = steps;
}
return [await png(node, 'preview.png'), css(code), ...(data ? [json(data)] : [])];
} };
}