import { num, text, choice, width, height, foreground, background, canvas, png, wrap, fit } from './common.js';
const size = () => [width(), height(), foreground(), background()];
const grid = () => [...size(), num('cell', '单元间距(px)', 32, 4, 256)];
const content = () => [...size(), text('content', '文字内容', '蓝色精英\n图片与设计', true), num('font', '字号(px)', 40, 8, 180)];
const fields = {
'checkerboard': grid(), 'dot-grid': [...grid(), num('radius', '圆点半径(px)', 3, 1, 64)],
'stripe-pattern': [...grid(), num('angle', '条纹旋转(度)', 45, 0, 180)],
'noise-texture': [...size(), num('seed', '随机种子', 42, 1, 2147483647), num('strength', '噪点强度', 0.3, 0, 1, 0.05)],
'geometric-pattern': [...grid(), choice('shape', '图形', [['triangle', '三角形'], ['arc', '四分之一圆弧']])],
'grid-paper': [...grid(), choice('mode', '纸张网格', [['square', '方格'], ['iso', '等距']])],
'perspective-grid': [...size(), num('x', '消失点 X(%)', 50, 0, 100), num('horizon', '地平线(%)', 30, 0, 90), num('lines', '射线数量', 16, 4, 60)],
'text-image': [...content(), choice('align', '对齐', [['left', '左对齐'], ['center', '居中'], ['right', '右对齐']])],
'text-badge': [text('left', '左侧标签', '版本'), text('right', '右侧内容', '1.0.0'), foreground(), background(), num('font', '字号(px)', 22, 10, 64)],
'quote-card': [...content(), text('author', '署名', '蓝色精英')],
'social-card': [...content(), text('subtitle', '副标题', '图片工具')],
'label-sheet': [...size(), text('labels', '标签(每行一个)', '产品 A\n产品 B\n产品 C\n产品 D', true), num('cols', '列数', 2, 1, 8), num('rows', '行数', 3, 1, 12), num('font', '字号(px)', 20, 8, 72)],
};
export function getImplementation(id) {
return { fields: fields[id], async run({ values: v, images }) {
if (id === 'text-badge') {
if (v.left.includes('\n') || v.right.includes('\n')) throw new Error('徽标文字应为单行。');
const measure = canvas(1, 1).getContext('2d'); measure.font = `${v.font}px sans-serif`; const left = Math.ceil(measure.measureText(v.left).width + 24), right = Math.ceil(measure.measureText(v.right).width + 24); if (left + right > 2048) throw new Error('徽标文字过长。'); const out = canvas(left + right, v.font + 24, '#35433e'), ctx = out.getContext('2d'); ctx.fillStyle = v.foreground; ctx.fillRect(left, 0, right, out.height); ctx.font = `${v.font}px sans-serif`; ctx.textBaseline = 'middle'; ctx.fillStyle = v.background; ctx.fillText(v.left, 12, out.height / 2); ctx.fillText(v.right, left + 12, out.height / 2); return [await png(out, 'badge.png')];
}
const out = canvas(v.width, v.height, v.background), ctx = out.getContext('2d'); ctx.fillStyle = v.foreground; ctx.strokeStyle = v.foreground;
if (id === 'checkerboard') {
for (let y = 0; y < v.height; y += v.cell) for (let x = 0; x < v.width; x += v.cell) if ((x / v.cell + y / v.cell) % 2 === 0) ctx.fillRect(x, y, v.cell, v.cell);
} else if (id === 'dot-grid') {
if (v.radius * 2 > v.cell) throw new Error('圆点直径不能超过间距。'); for (let y = v.cell / 2; y < v.height; y += v.cell) for (let x = v.cell / 2; x < v.width; x += v.cell) { ctx.beginPath(); ctx.arc(x, y, v.radius, 0, Math.PI * 2); ctx.fill(); }
} else if (id === 'stripe-pattern') {
const length = Math.hypot(v.width, v.height); ctx.translate(v.width / 2, v.height / 2); ctx.rotate(v.angle * Math.PI / 180); for (let x = -length; x < length; x += v.cell * 2) ctx.fillRect(x, -length, v.cell, length * 2);
} else if (id === 'noise-texture') {
const data = ctx.getImageData(0, 0, v.width, v.height); let seed = v.seed | 0; for (let i = 0; i < data.data.length; i += 4) { seed ^= seed << 13; seed ^= seed >>> 17; seed ^= seed << 5; const noise = ((seed >>> 0) / 4294967295 - 0.5) * 255 * v.strength; for (let k = 0; k < 3; k++) data.data[i + k] += noise; } ctx.putImageData(data, 0, 0);
} else if (id === 'geometric-pattern') {
for (let y = 0; y < v.height; y += v.cell) for (let x = 0; x < v.width; x += v.cell) { ctx.save(); ctx.translate(x + v.cell / 2, y + v.cell / 2); ctx.rotate(((x / v.cell + y / v.cell) % 4) * Math.PI / 2); ctx.beginPath(); if (v.shape === 'triangle') { ctx.moveTo(-v.cell / 2, -v.cell / 2); ctx.lineTo(v.cell / 2, -v.cell / 2); ctx.lineTo(-v.cell / 2, v.cell / 2); ctx.closePath(); ctx.fill(); } else { ctx.arc(-v.cell / 2, -v.cell / 2, v.cell * 0.8, 0, Math.PI / 2); ctx.lineWidth = Math.max(1, v.cell / 8); ctx.stroke(); } ctx.restore(); }
} else if (id === 'grid-paper') {
ctx.lineWidth = 1; ctx.beginPath(); for (let y = 0.5; y < v.height; y += v.cell) { ctx.moveTo(0, y); ctx.lineTo(v.width, y); }
if (v.mode === 'square') for (let x = 0.5; x < v.width; x += v.cell) { ctx.moveTo(x, 0); ctx.lineTo(x, v.height); }
else for (let x = -v.height; x < v.width + v.height; x += v.cell * 2 / Math.sqrt(3)) { ctx.moveTo(x, 0); ctx.lineTo(x + v.height / Math.sqrt(3), v.height); ctx.moveTo(x, 0); ctx.lineTo(x - v.height / Math.sqrt(3), v.height); } ctx.stroke();
} else if (id === 'perspective-grid') {
const vx = v.width * v.x / 100, hy = v.height * v.horizon / 100; ctx.beginPath(); ctx.moveTo(0, hy); ctx.lineTo(v.width, hy); for (let i = 0; i <= v.lines; i++) { ctx.moveTo(vx, hy); ctx.lineTo(v.width * i / v.lines, v.height); } for (let i = 1; i <= 9; i++) { const y = hy + (v.height - hy) * (i / 9) ** 2; ctx.moveTo(0, y); ctx.lineTo(v.width, y); } ctx.stroke();
} else if (id === 'label-sheet') {
const labels = v.labels.split('\n').map((s) => s.trim()).filter(Boolean); if (!labels.length || labels.length > v.rows * v.cols) throw new Error('标签数量超过行列容量或内容为空。'); const cw = v.width / v.cols, ch = v.height / v.rows; ctx.font = `${v.font}px sans-serif`; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.setLineDash([4, 4]);
labels.forEach((label, i) => { if (ctx.measureText(label).width > cw - 16 || v.font > ch - 16) throw new Error('标签文字超出单元,请缩小字号或调整行列。'); const x = i % v.cols * cw, y = Math.floor(i / v.cols) * ch; ctx.strokeRect(x + 1, y + 1, cw - 2, ch - 2); ctx.fillText(label, x + cw / 2, y + ch / 2); });
} else {
const margin = Math.max(12, Math.round(v.width * 0.06)); let availableWidth = v.width - margin * 2, y = margin;
if (id === 'social-card') { if (!images[0]) throw new Error('请先选择配图。'); fit(ctx, images[0], v.width / 2, 0, v.width / 2, v.height, true); availableWidth = v.width / 2 - margin * 2; }
if (availableWidth < v.font || v.height < v.font * 2 + margin * 2) throw new Error('画布过小或字号过大。');
ctx.fillStyle = v.foreground; ctx.font = `${v.font}px sans-serif`; ctx.textBaseline = 'top'; let x = margin;
if (id === 'text-image') { ctx.textAlign = v.align; if (v.align === 'center') x = v.width / 2; if (v.align === 'right') x = v.width - margin; }
const footerSpace = ['quote-card', 'social-card'].includes(id) ? v.font * 1.8 : 0;
const lineCount = wrap(ctx, v.content, x, y, availableWidth, v.font * 1.4, Math.floor((v.height - margin * 2 - footerSpace) / (v.font * 1.4)));
if (footerSpace) { const caption = id === 'quote-card' ? v.author : v.subtitle; ctx.font = `${Math.round(v.font * 0.55)}px sans-serif`; if (ctx.measureText(caption).width > availableWidth) throw new Error('署名或副标题过长。'); ctx.fillText(caption, margin, y + lineCount * v.font * 1.4 + v.font * 0.5); }
}
return [await png(out)];
} };
}