From 739c575bfb8c6c9d184ff761035302a7de531fc7 Mon Sep 17 00:00:00 2001 From: personal82555 Date: Mon, 12 Oct 2026 22:03:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8D=B0=E7=AB=A0=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=99=A8=E5=8D=87=E7=BA=A7=E4=B8=89=E6=A0=8F=E4=B8=93=E4=B8=9A?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E5=99=A8=20=E2=80=94=20=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E5=88=97=E8=A1=A8(=E5=85=A8=E9=83=A8/=E6=96=87=E5=AD=97/?= =?UTF-8?q?=E5=9B=BE=E5=BD=A2/=E6=95=88=E6=9E=9Ctab+=E5=90=AF=E5=81=9C?= =?UTF-8?q?=E5=88=A0=E9=99=A4)+mm=E6=A0=87=E5=B0=BA=E7=94=BB=E5=B8=83(?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E9=80=89=E4=B8=AD/=E6=8B=96=E6=8B=BD?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8)+=E5=B1=9E=E6=80=A7=E9=9D=A2=E6=9D=BF(?= =?UTF-8?q?=E5=BD=A2=E7=8A=B6/=E6=96=87=E5=AD=97=E6=8E=92=E5=B8=83/?= =?UTF-8?q?=E6=95=88=E6=9E=9C=E5=8F=82=E6=95=B0)+=E6=A8=A1=E6=9D=BFJSON?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=AF=BC=E5=87=BA+=E5=BF=AB=E6=8D=B7?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E6=A0=8F,=20=E6=94=AF=E6=8C=81=E5=9C=86?= =?UTF-8?q?=E5=BD=A2/=E6=A4=AD=E5=9C=86/=E5=9C=86=E8=A7=92=E6=96=B9?= =?UTF-8?q?=E5=BD=A2=E7=AB=A0,=20=E4=BF=9D=E7=95=99=E7=9B=96=E7=AB=A0?= =?UTF-8?q?=E5=88=B0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tools/seal-maker/seal-maker.vue | 1912 +++++++++++++++++++-------- 1 file changed, 1387 insertions(+), 525 deletions(-) diff --git a/src/tools/seal-maker/seal-maker.vue b/src/tools/seal-maker/seal-maker.vue index d052cf3..f987a0c 100644 --- a/src/tools/seal-maker/seal-maker.vue +++ b/src/tools/seal-maker/seal-maker.vue @@ -8,22 +8,74 @@ useHead({ title: '印章生成器 - IT-TOOLS-PRO' }); const genHist = useGenHistory('seal-maker'); -// ============ 印章参数 ============ -const style = ref<'circle-star' | 'circle-text'>('circle-star'); -// 效果(可叠加) -const effectPattern = ref(false); // 防伪纹路 -const effectFrayed = ref(false); // 毛边 -const effectAged = ref(false); // 做旧 -const mainText = ref('在线工具大全示例专用章'); -const subText = ref('合同专用章'); -const sealSize = ref(300); -const color = ref('#E60012'); +// ============ 数据模型 ============ +type ElemKind = 'text' | 'star' | 'ring' | 'line' | 'image'; +type ElemCat = 'text' | 'shape' | 'effect'; +type TextLayout = 'arcTop' | 'arcBottom' | 'center' | 'straight'; + +interface SealElem { + id: string; + kind: ElemKind; + cat: ElemCat; + label: string; + enabled: boolean; + text?: string; + layout?: TextLayout; + fontSize: number; + x: number; // 相对中心 -1..1(×半径) + y: number; + rotate: number; + r?: number; // ring 半径比 + sw?: number; // ring 线宽 + len?: number; // line 长度比 + vertical?: boolean; + src?: string; +} + +interface SealEffects { + aged: { on: boolean; strength: number }; + frayed: { on: boolean; strength: number }; + pattern: { on: boolean; count: number; len: number; width: number; seed: number }; +} + +const uid = () => Math.random().toString(36).slice(2, 9); + +function defaultElements(): SealElem[] { + return [ + { id: uid(), kind: 'text', cat: 'text', label: '像素工坊有限责任公司', enabled: true, text: '像素工坊有限责任公司', layout: 'arcTop', fontSize: 34, x: 0, y: -0.62, rotate: 0 }, + { id: uid(), kind: 'text', cat: 'text', label: '官方认证', enabled: true, text: '官方认证', layout: 'center', fontSize: 26, x: 0, y: 0.24, rotate: 0 }, + { id: uid(), kind: 'text', cat: 'text', label: 'www.shephe.com', enabled: true, text: 'www.shephe.com', layout: 'straight', fontSize: 15, x: 0, y: 0, rotate: 0 }, + { id: uid(), kind: 'text', cat: 'text', label: '1234567890', enabled: true, text: '1234567890', layout: 'arcBottom', fontSize: 14, x: 0, y: 0.66, rotate: 0 }, + { id: uid(), kind: 'star', cat: 'shape', label: '五角星', enabled: true, fontSize: 40, x: 0, y: 0.55, rotate: 0 }, + ]; +} + +const shape = ref<'circle' | 'ellipse' | 'roundRect'>('ellipse'); +const widthMm = ref(45); +const heightMm = ref(32); +const color = ref('#1B4F9C'); const invert = ref(false); +const elements = ref(defaultElements()); +const effects = ref({ + aged: { on: true, strength: 5 }, + frayed: { on: true, strength: 5 }, + pattern: { on: true, count: 5, len: 3, width: 0.15, seed: 1 }, +}); + +const selectedId = ref(''); +const selectedEffect = ref(''); +const selected = computed(() => elements.value.find(e => e.id === selectedId.value) || null); + +const cw = computed(() => (shape.value === 'circle' ? 360 : 440)); +const ch = computed(() => (shape.value === 'circle' ? 360 : 300)); const sealCanvas = ref(); -const sealReady = computed(() => sealDataUrl.value !== ''); +const selCanvas = ref(); const sealDataUrl = ref(''); const sealPngBytes = ref(null); +const sealReady = computed(() => sealDataUrl.value !== ''); +let bboxes: Record = {}; +const imageCache = new Map(); function hexToRgb(hex: string) { const m = /^#?([0-9a-f]{6})$/i.exec(hex.trim()); @@ -34,6 +86,14 @@ function hexToRgb(hex: string) { return { r: (n >> 16) & 255, g: (n >> 8) & 255, b: n & 255 }; } +function prng(seed: number) { + let s = seed || 1; + return () => { + s = (s * 16807) % 2147483647; + return (s - 1) / 2147483646; + }; +} + function drawStar(ctx: CanvasRenderingContext2D, cx: number, cy: number, r: number, fill: string) { ctx.save(); ctx.fillStyle = fill; @@ -55,158 +115,360 @@ function drawStar(ctx: CanvasRenderingContext2D, cx: number, cy: number, r: numb ctx.restore(); } -function drawArcText(ctx: CanvasRenderingContext2D, text: string, cx: number, cy: number, r: number, fontPx: number, fill: string) { +// 沿椭圆弧排文字(rx=ry 退化为圆);dir=1 顶部,dir=-1 底部 +function drawArcText(ctx: CanvasRenderingContext2D, text: string, cx: number, cy: number, rx: number, ry: number, fontPx: number, fill: string, dir: 1 | -1) { const chars = [...text]; const n = chars.length; if (!n) { return; } const perChar = fontPx * 1.02; - let spread = (n * perChar) / r; + let spread = (n * perChar) / ((rx + ry) / 2); spread = Math.min(spread, Math.PI * 1.05); const step = n > 1 ? spread / (n - 1) : 0; - const begin = -Math.PI / 2 - spread / 2; + const base = dir === 1 ? -Math.PI / 2 - spread / 2 : Math.PI / 2 + Math.PI + spread / 2 - spread; ctx.save(); ctx.fillStyle = fill; ctx.font = `bold ${fontPx}px "Microsoft YaHei", "PingFang SC", sans-serif`; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; for (let i = 0; i < n; i++) { - const a = begin + step * i; + const a = base + step * i; + const px = cx + rx * Math.cos(a); + const py = cy + ry * Math.sin(a); ctx.save(); - ctx.translate(cx + r * Math.cos(a), cy + r * Math.sin(a)); - ctx.rotate(a + Math.PI / 2); + ctx.translate(px, py); + if (dir === 1) { + ctx.rotate(a + Math.PI / 2); + } + else { + ctx.rotate(a - Math.PI / 2); + } ctx.fillText(chars[i], 0, 0); ctx.restore(); } ctx.restore(); } +function getImage(src: string): HTMLImageElement | null { + if (!src) { + return null; + } + let img = imageCache.get(src); + if (!img) { + img = new Image(); + img.src = src; + img.onload = () => renderSeal(); + imageCache.set(src, img); + } + return img.complete && img.naturalWidth > 0 ? img : null; +} + +// ============ 元素增删 ============ +function addElement(kind: ElemKind) { + const id = uid(); + if (kind === 'text') { + elements.value.push({ id, kind, cat: 'text', label: '新文字', enabled: true, text: '新文字', layout: 'straight', fontSize: 18, x: 0, y: 0, rotate: 0 }); + } + else if (kind === 'ring') { + elements.value.push({ id, kind, cat: 'shape', label: '内圈', enabled: true, fontSize: 0, x: 0, y: 0, rotate: 0, r: 0.62, sw: 2 }); + } + else if (kind === 'line') { + elements.value.push({ id, kind, cat: 'shape', label: '横线', enabled: true, fontSize: 0, x: 0, y: 0.38, rotate: 0, len: 0.9 }); + } + else if (kind === 'star') { + elements.value.push({ id, kind, cat: 'shape', label: '五角星', enabled: true, fontSize: 44, x: 0, y: 0.5, rotate: 0 }); + } + selectElem(id); + renderSeal(); +} + +function addCompany() { + const id = uid(); + elements.value.push({ id, kind: 'text', cat: 'text', label: '公司名称', enabled: true, text: '公司名称', layout: 'arcTop', fontSize: 34, x: 0, y: -0.6, rotate: 0 }); + selectElem(id); + renderSeal(); +} + +function addSealType() { + const id = uid(); + elements.value.push({ id, kind: 'text', cat: 'text', label: '印章类型', enabled: true, text: '专用章', layout: 'center', fontSize: 24, x: 0, y: 0.2, rotate: 0 }); + selectElem(id); + renderSeal(); +} + +const imgInput = ref(); +function onPickImage(e: Event) { + const input = e.target as HTMLInputElement; + const f = input.files?.[0]; + input.value = ''; + if (!f) { + return; + } + const reader = new FileReader(); + reader.onload = () => { + const id = uid(); + elements.value.push({ id, kind: 'image', cat: 'shape', label: f.name, enabled: true, fontSize: 60, x: 0, y: 0, rotate: 0, src: String(reader.result) }); + selectElem(id); + renderSeal(); + }; + reader.readAsDataURL(f); +} + +function removeElem(id: string) { + elements.value = elements.value.filter(e => e.id !== id); + if (selectedId.value === id) { + selectedId.value = ''; + } + renderSeal(); +} + +function toggleElem(e: SealElem) { + e.enabled = !e.enabled; + renderSeal(); +} + +function labelOf(e: SealElem) { + return e.text || e.label; +} + +function selectElem(id: string) { + selectedId.value = id; + selectedEffect.value = ''; + drawSelection(); +} + +function selectEffect(key: string) { + selectedEffect.value = key; + selectedId.value = ''; + drawSelection(); +} + +// ============ 渲染 ============ function renderSeal() { const c = sealCanvas.value; if (!c) { return; } - const size = sealSize.value; - c.width = size; - c.height = size; + const W = cw.value; + const H = ch.value; + c.width = W; + c.height = H; const ctx = c.getContext('2d')!; - ctx.clearRect(0, 0, size, size); + ctx.clearRect(0, 0, W, H); const { r: R, g: G, b: B } = hexToRgb(color.value); const ink = `rgb(${R},${G},${B})`; - const bg = ink; - const paper = invert.value ? '#ffffff' : bg; - const cx = size / 2; - const cy = size / 2; - const Rr = size / 2 - size * 0.02; - const ringW = Math.max(3, size * 0.035); + const paper = invert.value ? '#ffffff' : ink; + const cx = W / 2; + const cy = H / 2; + const Rx = W / 2 - 6; + const Ry = H / 2 - 6; + const ringW = Math.max(3, Math.min(W, H) * 0.035); + bboxes = {}; - if (invert.value) { - ctx.fillStyle = bg; + // ---- 外框 ---- + ctx.save(); + if (shape.value === 'roundRect') { + const rr = Math.min(Rx, Ry) * 0.35; + const x0 = cx - Rx; + const y0 = cy - Ry; + const path = () => { + ctx.beginPath(); + ctx.moveTo(x0 + rr, y0); + ctx.lineTo(x0 + Rx * 2 - rr, y0); + ctx.quadraticCurveTo(x0 + Rx * 2, y0, x0 + Rx * 2, y0 + rr); + ctx.lineTo(x0 + Rx * 2, y0 + Ry * 2 - rr); + ctx.quadraticCurveTo(x0 + Rx * 2, y0 + Ry * 2, x0 + Rx * 2 - rr, y0 + Ry * 2); + ctx.lineTo(x0 + rr, y0 + Ry * 2); + ctx.quadraticCurveTo(x0, y0 + Ry * 2, x0, y0 + Ry * 2 - rr); + ctx.lineTo(x0, y0 + rr); + ctx.quadraticCurveTo(x0, y0, x0 + rr, y0); + ctx.closePath(); + }; + if (invert.value) { + ctx.fillStyle = ink; + path(); + ctx.fill(); + } + else { + ctx.strokeStyle = ink; + ctx.lineWidth = ringW; + path(); + ctx.stroke(); + } + } + else if (invert.value) { + ctx.fillStyle = ink; ctx.beginPath(); - ctx.arc(cx, cy, Rr, 0, Math.PI * 2); + ctx.ellipse(cx, cy, Rx, Ry, 0, 0, Math.PI * 2); ctx.fill(); } else { ctx.strokeStyle = ink; ctx.lineWidth = ringW; ctx.beginPath(); - ctx.arc(cx, cy, Rr - ringW / 2, 0, Math.PI * 2); + ctx.ellipse(cx, cy, Rx - ringW / 2, Ry - ringW / 2, 0, 0, Math.PI * 2); ctx.stroke(); } + ctx.restore(); - const fontPx = Math.max(12, Math.min(size * 0.135, (Math.PI * Rr * 0.82) / Math.max([...mainText.value].length, 1) * 1.6)); - drawArcText(ctx, mainText.value, cx, cy, Rr * 0.76, fontPx, paper); - - if (style.value === 'circle-star') { - drawStar(ctx, cx, cy + size * 0.01, Rr * 0.26, paper); - } - if (subText.value.trim()) { + // ---- 元素 ---- + for (const el of elements.value) { + if (!el.enabled || el.cat === 'effect') { + continue; + } + const ex = cx + el.x * Rx; + const ey = cy + el.y * Ry; ctx.save(); - ctx.fillStyle = paper; - const subFont = Math.max(10, size * (style.value === 'circle-star' ? 0.075 : 0.13)); - ctx.font = `bold ${subFont}px "Microsoft YaHei", "PingFang SC", sans-serif`; - ctx.textAlign = 'center'; - ctx.textBaseline = 'middle'; - ctx.fillText(subText.value, cx, cy + (style.value === 'circle-star' ? size * 0.3 : size * 0.02)); + if (el.rotate) { + ctx.translate(ex, ey); + ctx.rotate((el.rotate * Math.PI) / 180); + ctx.translate(-ex, -ey); + } + if (el.kind === 'text') { + const fontPx = Math.max(8, el.fontSize * (Math.min(W, H) / 360)); + if (el.layout === 'arcTop') { + drawArcText(ctx, el.text || '', cx, cy, Rx - ringW - fontPx, Ry - ringW - fontPx, fontPx, paper, 1); + bboxes[el.id] = { x: cx - Rx * 0.9, y: cy - Ry * 0.92, w: Rx * 1.8, h: Ry * 0.5 }; + } + else if (el.layout === 'arcBottom') { + drawArcText(ctx, el.text || '', cx, cy, Rx - ringW - fontPx * 1.5, Ry - ringW - fontPx * 1.5, fontPx, paper, -1); + bboxes[el.id] = { x: cx - Rx * 0.9, y: cy + Ry * 0.34, w: Rx * 1.8, h: Ry * 0.56 }; + } + else { + ctx.fillStyle = paper; + ctx.font = `bold ${fontPx}px "Microsoft YaHei", "PingFang SC", sans-serif`; + ctx.textAlign = 'center'; + ctx.textBaseline = 'middle'; + ctx.fillText(el.text || '', ex, ey); + const w = ctx.measureText(el.text || '').width; + bboxes[el.id] = { x: ex - w / 2 - 4, y: ey - fontPx * 0.75, w: w + 8, h: fontPx * 1.5 }; + } + } + else if (el.kind === 'star') { + const r = el.fontSize * (Math.min(W, H) / 360); + drawStar(ctx, ex, ey, r, paper); + bboxes[el.id] = { x: ex - r, y: ey - r, w: r * 2, h: r * 2 }; + } + else if (el.kind === 'ring') { + const r = (el.r ?? 0.62) * Rx; + const ry = (el.r ?? 0.62) * Ry; + ctx.strokeStyle = paper; + ctx.lineWidth = Math.max(1, el.sw ?? 2); + ctx.beginPath(); + ctx.ellipse(cx, cy, r, ry, 0, 0, Math.PI * 2); + ctx.stroke(); + bboxes[el.id] = { x: cx - r, y: cy - ry, w: r * 2, h: ry * 2 }; + } + else if (el.kind === 'line') { + const half = ((el.len ?? 0.9) / 2) * Rx; + ctx.strokeStyle = paper; + ctx.lineWidth = 2; + ctx.beginPath(); + if (el.vertical) { + ctx.moveTo(ex, ey - half); + ctx.lineTo(ex, ey + half); + } + else { + ctx.moveTo(ex - half, ey); + ctx.lineTo(ex + half, ey); + } + ctx.stroke(); + bboxes[el.id] = el.vertical + ? { x: ex - 8, y: ey - half, w: 16, h: half * 2 } + : { x: ex - half, y: ey - 8, w: half * 2, h: 16 }; + } + else if (el.kind === 'image') { + const img = getImage(el.src || ''); + if (img) { + const w = el.fontSize * (Math.min(W, H) / 360); + const h = (w * img.naturalHeight) / img.naturalWidth; + ctx.drawImage(img, ex - w / 2, ey - h / 2, w, h); + bboxes[el.id] = { x: ex - w / 2, y: ey - h / 2, w, h }; + } + } ctx.restore(); } - // ---- 防伪纹路:只在有墨区域叠加同心细线 + 外圈放射短线 ---- - if (effectPattern.value) { + // ---- 效果 ---- + const fx = effects.value; + if (fx.pattern.on) { ctx.save(); ctx.globalCompositeOperation = 'source-atop'; - ctx.strokeStyle = 'rgba(0,0,0,0.15)'; - ctx.lineWidth = Math.max(0.6, size * 0.003); - const gap = Math.max(3, size * 0.014); - for (let rr = gap; rr < Rr; rr += gap) { + const rnd = prng(fx.pattern.seed); + const lw = Math.max(0.4, ((fx.pattern.width / 10) * Math.min(W, H)) / 36); + ctx.strokeStyle = 'rgba(0,0,0,0.16)'; + ctx.lineWidth = lw; + const gap = Math.max(2.5, (Ry * 0.75) / Math.max(2, fx.pattern.count)); + for (let i = 1; i <= fx.pattern.count; i++) { ctx.beginPath(); - ctx.arc(cx, cy, rr, 0, Math.PI * 2); + ctx.ellipse(cx, cy, Math.max(4, Rx * 0.94 - i * gap * 0.5), Math.max(4, Ry * 0.94 - i * gap * 0.5), 0, 0, Math.PI * 2); ctx.stroke(); } - // 外圈放射状防伪短线 - const spokes = 96; + const spokes = Math.max(24, Math.round(fx.pattern.count * 16)); + const lenPx = Math.max(3, ((fx.pattern.len / 10) * Math.min(W, H)) / 36); ctx.beginPath(); for (let i = 0; i < spokes; i++) { - const a = (i / spokes) * Math.PI * 2; - const r0 = Rr * 0.9; - const r1 = Rr * 0.985; - ctx.moveTo(cx + Math.cos(a) * r0, cy + Math.sin(a) * r0); - ctx.lineTo(cx + Math.cos(a) * r1, cy + Math.sin(a) * r1); + const a = (i / spokes) * Math.PI * 2 + rnd() * 0.012; + const r0x = (Rx - ringW * 0.4) * Math.cos(a); + const r0y = (Ry - ringW * 0.4) * Math.sin(a); + const scale = 1 - lenPx / Math.max(Rx, Ry); + ctx.moveTo(cx + r0x, cy + r0y); + ctx.lineTo(cx + r0x * scale, cy + r0y * scale); } ctx.stroke(); ctx.restore(); } - - // ---- 毛边效果:沿外轮廓与文字边缘随机撕裂缺口 ---- - if (effectFrayed.value) { + if (fx.frayed.on) { + const rnd = prng(1234 + fx.frayed.strength); ctx.save(); ctx.globalCompositeOperation = 'destination-out'; ctx.fillStyle = 'black'; - // 外圆轮廓毛刺 - const nEdge = Math.round(size / 4); + const k = fx.frayed.strength / 5; + const nEdge = Math.round((Math.min(W, H) / 4) * k); for (let i = 0; i < nEdge; i++) { - const a = Math.random() * Math.PI * 2; - const rr = Rr + (Math.random() - 0.5) * ringW * 1.8; - const sz = 0.6 + Math.random() * (size * 0.011); + const a = rnd() * Math.PI * 2; + const rxp = Rx - ringW / 2 + (rnd() - 0.5) * ringW * 1.8; + const ryp = Ry - ringW / 2 + (rnd() - 0.5) * ringW * 1.8; + const sz = 0.6 + rnd() * (Math.min(W, H) * 0.011 * k); ctx.beginPath(); - ctx.arc(cx + Math.cos(a) * rr, cy + Math.sin(a) * rr, sz, 0, Math.PI * 2); + ctx.arc(cx + rxp * Math.cos(a), cy + ryp * Math.sin(a), sz, 0, Math.PI * 2); ctx.fill(); } - // 文字环带毛刺 - const nText = Math.round(size / 6); + const nText = Math.round((Math.min(W, H) / 6) * k); for (let i = 0; i < nText; i++) { - const a = Math.random() * Math.PI * 2; - const rr = Rr * 0.76 + (Math.random() - 0.5) * fontPx * 1.2; + const a = rnd() * Math.PI * 2; + const rxp = Rx * 0.72 + (rnd() - 0.5) * Ry * 0.3; + const ryp = Ry * 0.72 + (rnd() - 0.5) * Ry * 0.3; ctx.beginPath(); - ctx.arc(cx + Math.cos(a) * rr, cy + Math.sin(a) * rr, 0.5 + Math.random() * (size * 0.006), 0, Math.PI * 2); + ctx.arc(cx + rxp * Math.cos(a), cy + ryp * Math.sin(a), 0.5 + rnd() * (Math.min(W, H) * 0.006), 0, Math.PI * 2); ctx.fill(); } ctx.restore(); } - - // ---- 做旧效果:全局斑驳磨损(墨色不均 + 随机磨损块) ---- - if (effectAged.value) { + if (fx.aged.on) { + const rnd = prng(777 + fx.aged.strength); ctx.save(); ctx.globalCompositeOperation = 'destination-out'; ctx.fillStyle = 'black'; - const dots = Math.round((size * size) / 500); + const k = fx.aged.strength / 5; + const dots = Math.round(((W * H) / 500) * k); for (let i = 0; i < dots; i++) { - const x = Math.random() * size; - const y = Math.random() * size; - const sz = 0.4 + Math.random() * 1.6; - ctx.globalAlpha = 0.25 + Math.random() * 0.75; + const x = rnd() * W; + const y = rnd() * H; + const sz = 0.4 + rnd() * 1.6; + ctx.globalAlpha = 0.25 + rnd() * 0.75; ctx.beginPath(); ctx.arc(x, y, sz, 0, Math.PI * 2); ctx.fill(); } - const patches = Math.round(size / 25); + const patches = Math.round((Math.min(W, H) / 25) * k); for (let i = 0; i < patches; i++) { - const x = Math.random() * size; - const y = Math.random() * size; - ctx.globalAlpha = 0.12 + Math.random() * 0.38; + const x = rnd() * W; + const y = rnd() * H; + ctx.globalAlpha = 0.12 + rnd() * 0.38; ctx.beginPath(); - ctx.arc(x, y, 1 + Math.random() * size * 0.02, 0, Math.PI * 2); + ctx.arc(x, y, 1 + rnd() * Math.min(W, H) * 0.02, 0, Math.PI * 2); ctx.fill(); } ctx.restore(); @@ -220,27 +482,213 @@ function renderSeal() { bytes[i] = bin.charCodeAt(i); } sealPngBytes.value = bytes; + drawSelection(); +} + +// 选中高亮(独立 overlay,不进导出) +const showAllProps = ref(false); + +function drawSelection() { + const c = selCanvas.value; + if (!c) { + return; + } + c.width = cw.value; + c.height = ch.value; + const ctx = c.getContext('2d')!; + ctx.clearRect(0, 0, c.width, c.height); + if (!selectedId.value) { + return; + } + const bb = bboxes[selectedId.value]; + if (bb) { + ctx.strokeStyle = '#2563eb'; + ctx.lineWidth = 1.5; + ctx.setLineDash([5, 4]); + ctx.strokeRect(bb.x, bb.y, bb.w, bb.h); + ctx.setLineDash([]); + ctx.fillStyle = '#2563eb'; + for (const [hx, hy] of [[bb.x, bb.y], [bb.x + bb.w, bb.y], [bb.x, bb.y + bb.h], [bb.x + bb.w, bb.y + bb.h]] as const) { + ctx.fillRect(hx - 3, hy - 3, 6, 6); + } + } +} + +// ============ 画布交互(点击选中 + 拖拽移动) ============ +const dragging = ref(false); +let dragOff = { x: 0, y: 0 }; + +function canvasPos(e: MouseEvent) { + const c = selCanvas.value!; + const rect = c.getBoundingClientRect(); + return { + x: ((e.clientX - rect.left) / rect.width) * c.width, + y: ((e.clientY - rect.top) / rect.height) * c.height, + }; +} + +function onCanvasDown(e: MouseEvent) { + const p = canvasPos(e); + let hit = ''; + for (const el of [...elements.value].reverse()) { + if (!el.enabled || el.cat === 'effect') { + continue; + } + const bb = bboxes[el.id]; + if (bb && p.x >= bb.x && p.x <= bb.x + bb.w && p.y >= bb.y && p.y <= bb.y + bb.h) { + hit = el.id; + break; + } + } + selectedId.value = hit; + selectedEffect.value = ''; + if (hit) { + const el = elements.value.find(x2 => x2.id === hit)!; + if (el.layout !== 'arcTop' && el.layout !== 'arcBottom') { + dragging.value = true; + const ex = cw.value / 2 + el.x * (cw.value / 2 - 6); + const ey = ch.value / 2 + el.y * (ch.value / 2 - 6); + dragOff = { x: p.x - ex, y: p.y - ey }; + } + } + drawSelection(); +} + +function onCanvasMove(e: MouseEvent) { + if (!dragging.value || !selectedId.value) { + return; + } + const el = elements.value.find(x2 => x2.id === selectedId.value); + if (!el) { + return; + } + const p = canvasPos(e); + const Rx = cw.value / 2 - 6; + const Ry = ch.value / 2 - 6; + el.x = Math.max(-1, Math.min(1, (p.x - dragOff.x - cw.value / 2) / Rx)); + el.y = Math.max(-1, Math.min(1, (p.y - dragOff.y - ch.value / 2) / Ry)); + renderSeal(); +} + +function onCanvasUp() { + dragging.value = false; +} + +// ============ 左栏 ============ +const effectItems = computed(() => [ + { key: 'aged', label: '做旧效果', icon: '⏳', on: effects.value.aged.on }, + { key: 'frayed', label: '毛边效果', icon: '〰️', on: effects.value.frayed.on }, + { key: 'pattern', label: '防伪纹路', icon: '🛡️', on: effects.value.pattern.on }, +]); + +function toggleEffect(key: string) { + if (key === 'aged') { + effects.value.aged.on = !effects.value.aged.on; + } + else if (key === 'frayed') { + effects.value.frayed.on = !effects.value.frayed.on; + } + else { + effects.value.pattern.on = !effects.value.pattern.on; + } + renderSeal(); +} + +const listTab = ref<'all' | 'text' | 'shape' | 'effect'>('all'); +const listItems = computed(() => { + const els = elements.value.map(e => ({ type: 'elem' as const, e })); + const fxs = effectItems.value.map(f => ({ type: 'effect' as const, f })); + if (listTab.value === 'text') { + return els.filter(x => x.e.cat === 'text'); + } + if (listTab.value === 'shape') { + return els.filter(x => x.e.cat === 'shape'); + } + if (listTab.value === 'effect') { + return fxs; + } + return [...els, ...fxs]; +}); + +// ============ 模板导入导出 ============ +const tplInput = ref(); + +function exportTemplate() { + const data = { + v: 1, + shape: shape.value, + widthMm: widthMm.value, + heightMm: heightMm.value, + color: color.value, + invert: invert.value, + elements: elements.value, + effects: effects.value, + }; + const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }); + const a = document.createElement('a'); + a.href = URL.createObjectURL(blob); + a.download = `印章模板-${Date.now()}.json`; + a.click(); + URL.revokeObjectURL(a.href); + genHist.add({ title: '导出印章模板', detail: `${elements.value.length} 个元素` }); +} + +function onPickTpl(e: Event) { + const input = e.target as HTMLInputElement; + const f = input.files?.[0]; + input.value = ''; + if (!f) { + return; + } + const reader = new FileReader(); + reader.onload = () => { + try { + const d = JSON.parse(String(reader.result)); + shape.value = d.shape || 'circle'; + widthMm.value = d.widthMm || 45; + heightMm.value = d.heightMm || 32; + color.value = d.color || '#E60012'; + invert.value = !!d.invert; + if (Array.isArray(d.elements)) { + elements.value = d.elements; + } + if (d.effects) { + effects.value = d.effects; + } + selectedId.value = ''; + selectedEffect.value = ''; + renderSeal(); + } + catch { + alert('模板文件格式错误'); + } + }; + reader.readAsText(f); +} + +function resetSeal() { + shape.value = 'ellipse'; + widthMm.value = 45; + heightMm.value = 32; + color.value = '#1B4F9C'; + invert.value = false; + elements.value = defaultElements(); + effects.value = { + aged: { on: true, strength: 5 }, + frayed: { on: true, strength: 5 }, + pattern: { on: true, count: 5, len: 3, width: 0.15, seed: 1 }, + }; + selectedId.value = ''; + selectedEffect.value = ''; + renderSeal(); } function downloadSeal() { const a = document.createElement('a'); a.href = sealDataUrl.value; - a.download = `${mainText.value || '印章'}.png`; + a.download = '印章.png'; a.click(); - genHist.add({ title: `生成印章: ${mainText.value}`, detail: `${sealSize.value}px · ${color.value}${invert.value ? ' · 反转' : ''}` }); -} - -function resetSeal() { - style.value = 'circle-star'; - mainText.value = '在线工具大全示例专用章'; - subText.value = '合同专用章'; - sealSize.value = 300; - color.value = '#E60012'; - invert.value = false; - effectPattern.value = false; - effectFrayed.value = false; - effectAged.value = false; - renderSeal(); + genHist.add({ title: '下载印章 PNG', detail: `${shape.value} · ${color.value}` }); } // ============ 盖章到文件 ============ @@ -253,7 +701,6 @@ const stampResultName = ref(''); const stampResultSize = ref(0); const stampSpent = ref(0); -// 印章参数 const stampWidthMm = ref(40); const stampOpacity = ref(85); const stampRotation = ref(0); @@ -342,7 +789,7 @@ async function stampImage() { ctx.drawImage(img, 0, 0); URL.revokeObjectURL(url); - const w = Math.round((canvas.width * stampWidthMm.value) / 100); // 图片模式下宽度按百分比 + const w = Math.round((canvas.width * stampWidthMm.value) / 100); const h = w; const margin = Math.round(Math.min(canvas.width, canvas.height) * 0.05); let cx = canvas.width - w / 2 - margin; @@ -373,7 +820,7 @@ async function stampImage() { } stampResultUrl.value = URL.createObjectURL(blob!); stampResultSize.value = blob!.size; - stampResultName.value = f.name.replace(/\\.[^.]+$/, '') + '-盖章.png'; + stampResultName.value = f.name.replace(/\.[^.]+$/, '') + '-盖章.png'; stampSpent.value = Math.round((Date.now() - t0) / 1000); } @@ -404,7 +851,7 @@ async function stampPdf() { let tcx: number; let tcy: number; if (stampPos.value === 'edge') { - tcx = width; // 骑缝:印章中心贴右边缘,半枚在页外 + tcx = width; tcy = stampVertical.value === 'top' ? height - margin - h / 2 : stampVertical.value === 'bottom' ? margin + h / 2 : height / 2; @@ -487,265 +934,848 @@ onMounted(() => {