| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652 |
- (function () {
- const D = window.PL.data;
- let uidCounter = 0;
- const uid = function (p) { uidCounter += 1; return (p || 'u') + uidCounter; };
- const measureCanvas = document.createElement('canvas');
- const measureCtx = measureCanvas.getContext('2d');
- function esc(s) {
- return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) {
- return { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c];
- });
- }
- function hexRgb(hex) {
- let h = String(hex || '#000').replace('#', '');
- if (h.length === 3) h = h[0] + h[0] + h[1] + h[1] + h[2] + h[2];
- const n = parseInt(h, 16);
- return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
- }
- function shade(hex, amt) {
- const rgb = hexRgb(hex);
- const target = amt < 0 ? 0 : 255;
- const k = Math.abs(amt);
- return '#' + rgb.map(function (v) {
- return Math.round(v + (target - v) * k).toString(16).padStart(2, '0');
- }).join('');
- }
- function alpha(hex, a) {
- const c = hexRgb(hex);
- return 'rgba(' + c[0] + ',' + c[1] + ',' + c[2] + ',' + a + ')';
- }
- function fontStack(key) {
- return (D.fonts[key] || D.fonts.sans).stack;
- }
- function fontAttr(key) {
- return fontStack(key).replace(/"/g, "'");
- }
- function fontCss(el) {
- return (el.italic ? 'italic ' : '') + (el.weight || 400) + ' ' + (el.size || 12) * 3.779527559 + 'px ' + fontStack(el.font);
- }
- function charWidth(ch, el) {
- measureCtx.font = fontCss(el);
- return measureCtx.measureText(ch).width / 3.779527559;
- }
- function segment(text) {
- if (typeof Intl !== 'undefined' && Intl.Segmenter) {
- const seg = new Intl.Segmenter('zh', { granularity: 'word' });
- return Array.from(seg.segment(text), function (s) { return s.segment; });
- }
- return text.split(/(\s+)/).filter(function (x) { return x !== ''; });
- }
- function textLines(el) {
- const w = Math.max(4, el.w || 60);
- const size = el.size || 12;
- const spacing = (el.spacing || 0) * size;
- const paragraphs = String(el.text || '').split('\n');
- const out = [];
- paragraphs.forEach(function (p) {
- const tokens = segment(p);
- let line = '';
- let lineW = 0;
- const push = function () {
- out.push(line.replace(/\s+$/, ''));
- line = '';
- lineW = 0;
- };
- tokens.forEach(function (tok) {
- const tw = charWidth(tok, el) + Array.from(tok).length * spacing;
- if (line && lineW + tw > w) push();
- if (tw > w && Array.from(tok).length > 1) {
- Array.from(tok).forEach(function (ch) {
- const cw = charWidth(ch, el) + spacing;
- if (line && lineW + cw > w) push();
- line += ch;
- lineW += cw;
- });
- } else {
- line += tok;
- lineW += tw;
- }
- });
- out.push(line.replace(/\s+$/, ''));
- });
- return out.length ? out : [''];
- }
- function textHeight(el) {
- const lines = textLines(el);
- const lh = (el.lineHeight || 1.25) * (el.size || 12);
- return Math.max((el.size || 12) * 1.1, lines.length * lh);
- }
- function elementBox(el) {
- let w = el.w || 0;
- let h = el.h || 0;
- if (el.type === 'text') h = textHeight(el);
- if (el.type === 'badge' || el.type === 'qr') { w = el.w || 40; h = w; }
- if (el.type === 'asset') { w = el.w || 30; h = w; }
- if (el.type === 'line') h = Math.max(4, el.h || 0);
- return { w: Math.max(1, w), h: Math.max(1, h) };
- }
- function barcodeBits(value) {
- const L = ['0001101', '0011001', '0010011', '0111101', '0100011', '0110001', '0101111', '0111011', '0110111', '0001011'];
- const G = ['0100111', '0110011', '0011011', '0100001', '0011101', '0111001', '0000101', '0010001', '0001001', '0010111'];
- const R = ['1110010', '1100110', '1101100', '1000010', '1011100', '1001110', '1010000', '1000100', '1001000', '1110100'];
- const P = ['LLLLLL', 'LLGLGG', 'LLGGLG', 'LLGGGL', 'LGLLGG', 'LGGLLG', 'LGGGLL', 'LGLGLG', 'LGLGGL', 'LGGLGL'];
- let digits = String(value || '').replace(/\D/g, '');
- if (digits.length < 12) {
- let seed = 0;
- String(value || 'POUCHLAB').split('').forEach(function (c) { seed = (seed * 31 + c.charCodeAt(0)) % 9973; });
- while (digits.length < 12) { seed = (seed * 37 + 11) % 10000; digits += String(seed % 10); }
- }
- digits = digits.slice(0, 12);
- let sum = 0;
- for (let i = 0; i < 12; i += 1) sum += Number(digits[i]) * (i % 2 === 0 ? 1 : 3);
- const check = (10 - (sum % 10)) % 10;
- const code = digits + check;
- const parity = P[Number(code[0])];
- let bits = '101';
- for (let i = 1; i <= 6; i += 1) {
- const d = Number(code[i]);
- bits += parity[i - 1] === 'L' ? L[d] : G[d];
- }
- bits += '01010';
- for (let i = 7; i <= 12; i += 1) bits += R[Number(code[i])];
- bits += '101';
- return { bits: bits, code: code };
- }
- function renderBarcode(el, ctx) {
- const w = el.w || 50;
- const h = el.h || 20;
- const color = el.color || '#17140f';
- const bars = barcodeBits(el.value || '6971234567890');
- const showText = el.showText !== false;
- const textH = showText ? Math.min(h * 0.22, 4.2) : 0;
- const barH = h - textH - (showText ? 0.8 : 0);
- const module = w / 95;
- let rects = '';
- let x = 0;
- for (let i = 0; i < bars.bits.length; i += 1) {
- if (bars.bits[i] === '1') {
- rects += '<rect x="' + (x * module).toFixed(3) + '" y="0" width="' + module.toFixed(3) + '" height="' + barH.toFixed(3) + '" fill="' + color + '"/>';
- }
- x += 1;
- }
- const text = showText ? '<text x="' + (w / 2) + '" y="' + (barH + textH * 0.95).toFixed(2) + '" text-anchor="middle" font-family="' + fontAttr('mono') + '" font-size="' + (textH * 0.82).toFixed(2) + '" letter-spacing="' + (textH * 0.16).toFixed(2) + '" fill="' + color + '">' + esc(bars.code) + '</text>' : '';
- return rects + text;
- }
- function renderQR(el) {
- const size = el.w || 40;
- const color = el.color || '#17140f';
- const light = el.light === 'none' ? null : (el.light || '#ffffff');
- let qr = null;
- try {
- if (window.PL && window.PL.qrcode && window.PL.qrcode.build) qr = window.PL.qrcode.build(String(el.value || ' '));
- } catch (e) { qr = null; }
- const bg = light ? '<rect width="' + size + '" height="' + size + '" fill="' + light + '"/>' : '';
- if (!qr) {
- return bg + '<rect x="1" y="1" width="' + (size - 2) + '" height="' + (size - 2) + '" fill="none" stroke="' + color + '" stroke-width="0.6" stroke-dasharray="1 1"/>';
- }
- const quiet = 2;
- const total = qr.size + quiet * 2;
- const unit = size / total;
- let d = '';
- for (let y = 0; y < qr.size; y += 1) {
- for (let x = 0; x < qr.size; x += 1) {
- if (!qr.matrix[y][x]) continue;
- const px = (x + quiet) * unit;
- const py = (y + quiet) * unit;
- d += 'M' + px.toFixed(3) + ' ' + py.toFixed(3) + 'h' + unit.toFixed(3) + 'v' + unit.toFixed(3) + 'h-' + unit.toFixed(3) + 'z';
- }
- }
- return bg + '<path d="' + d + '" fill="' + color + '"/>';
- }
- function renderAsset(el) {
- const lib = window.PL && window.PL.boxlib;
- if (!lib || !lib.assetSvg || !el.asset) return '';
- const svg = lib.assetSvg(el.asset, el.color || '#17140f');
- if (!svg) return '';
- const inner = svg.replace(/^<svg[^>]*>/, '').replace(/<\/svg>\s*$/, '');
- const scale = (el.w || 24) / 24;
- return '<g transform="scale(' + scale.toFixed(4) + ')">' + inner + '</g>';
- }
- function renderSticker(el) {
- const def = D.stickers[el.sticker] || D.stickers.star;
- const color = el.color || '#17140f';
- const size = el.w || 30;
- const scale = size / 24;
- const paths = def.paths.map(function (p) {
- const style = p.stroke
- ? 'fill="none" stroke="' + color + '" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"'
- : 'fill="' + color + '"';
- return '<path d="' + p.d + '" ' + style + '/>';
- }).join('');
- return '<g transform="scale(' + scale + ')">' + paths + '</g>';
- }
- function renderBadge(el, ctx) {
- const w = el.w || 40;
- const color = el.color || '#17140f';
- const r = w / 2 - w * 0.035;
- const cid = ctx.uid + '-circle';
- const size = el.size || w * 0.09;
- const circumference = 2 * Math.PI * r;
- return '<defs><path id="' + cid + '" d="M ' + (w / 2) + ' ' + (w / 2) + ' m -' + r + ' 0 a ' + r + ' ' + r + ' 0 1 1 ' + (2 * r) + ' 0 a ' + r + ' ' + r + ' 0 1 1 -' + (2 * r) + ' 0"/></defs>' +
- '<circle cx="' + (w / 2) + '" cy="' + (w / 2) + '" r="' + r + '" fill="none" stroke="' + color + '" stroke-width="' + (w * 0.012) + '"/>' +
- '<circle cx="' + (w / 2) + '" cy="' + (w / 2) + '" r="' + (r * 0.76) + '" fill="none" stroke="' + color + '" stroke-width="' + (w * 0.006) + '"/>' +
- '<text font-family="' + fontAttr('latin') + '" font-size="' + size + '" font-weight="700" letter-spacing="' + (size * 0.08) + '" fill="' + color + '"><textPath href="#' + cid + '" textLength="' + (circumference * 0.97).toFixed(2) + '" lengthAdjust="spacing">' + esc(el.text || 'POUCHLAB · ') + '</textPath></text>' +
- '<path d="M ' + (w / 2) + ' ' + (w * 0.39) + ' l ' + (w * 0.035) + ' ' + (w * 0.085) + ' ' + (w * 0.09) + ' ' + (w * 0.012) + ' l -' + (w * 0.068) + ' ' + (w * 0.062) + ' ' + (w * 0.02) + ' ' + (w * 0.09) + ' l -' + (w * 0.077) + ' ' + (w * 0.048) + ' l -' + (w * 0.077) + ' -' + (w * 0.048) + ' l ' + (w * 0.02) + ' -' + (w * 0.09) + ' l -' + (w * 0.068) + ' -' + (w * 0.062) + ' l ' + (w * 0.09) + ' -' + (w * 0.012) + ' Z" fill="' + color + '"/>';
- }
- function renderText(el, ctx) {
- const lines = textLines(el);
- const size = el.size || 12;
- const lh = (el.lineHeight || 1.25) * size;
- const ls = (el.spacing || 0) * size;
- const anchor = el.align === 'center' ? 'middle' : el.align === 'right' ? 'end' : 'start';
- const x = el.align === 'center' ? (el.w || 60) / 2 : el.align === 'right' ? (el.w || 60) : 0;
- const fill = el.color === 'none' ? 'none' : (el.color || '#17140f');
- // 描边文字最小线宽:字号的 4.5%(发丝级描边在印刷预览/贴图光栅化时不可见)
- const strokeW = el.stroke && el.stroke !== 'none' ? Math.max(el.strokeWidth || 0, size * 0.045) : 0;
- const stroke = el.stroke && el.stroke !== 'none' ? ' stroke="' + el.stroke + '" stroke-width="' + strokeW.toFixed(3) + '"' : '';
- const tspans = lines.map(function (line, i) {
- return '<text x="' + x + '" y="' + (size * 0.83 + i * lh).toFixed(2) + '" text-anchor="' + anchor + '" font-family="' + fontAttr(el.font) + '" font-size="' + size + '" font-weight="' + (el.weight || 400) + '"' + (el.italic ? ' font-style="italic"' : '') + ' letter-spacing="' + ls.toFixed(3) + '" fill="' + fill + '"' + stroke + '>' + esc(line) + '</text>';
- }).join('');
- return tspans;
- }
- function renderElement(el, ctx) {
- if (el.hidden) return '';
- const box = elementBox(el);
- const rot = el.rotation || 0;
- const cx = box.w / 2;
- const cy = box.h / 2;
- let body = '';
- if (el.type === 'image') {
- if (el.src) {
- const fit = el.fit === 'cover' ? 'xMidYMid slice' : 'xMidYMid meet';
- const filter = el.mono ? ' filter="url(#' + ctx.uid + '-mono)"' : '';
- body = '<image href="' + esc(el.src) + '" x="0" y="0" width="' + box.w + '" height="' + box.h + '" preserveAspectRatio="' + fit + '"' + filter + '/>';
- } else {
- body = '<rect width="' + box.w + '" height="' + box.h + '" fill="#e9e2d5" stroke="#c9bfae" stroke-dasharray="2 2"/>';
- }
- } else if (el.type === 'text') {
- body = renderText(el, ctx);
- } else if (el.type === 'badge') {
- body = renderBadge(el, ctx);
- } else if (el.type === 'barcode') {
- body = renderBarcode(el, ctx);
- } else if (el.type === 'qr') {
- body = renderQR(el);
- } else if (el.type === 'asset') {
- body = renderAsset(el);
- } else if (el.type === 'sticker') {
- body = renderSticker(el);
- } else if (el.type === 'shape') {
- const fill = el.fill || 'none';
- const stroke = el.stroke && el.stroke !== 'none' ? ' stroke="' + el.stroke + '" stroke-width="' + (el.strokeWidth || 0.5) + '"' : '';
- if (el.shape === 'circle') {
- body = '<ellipse cx="' + box.w / 2 + '" cy="' + box.h / 2 + '" rx="' + box.w / 2 + '" ry="' + box.h / 2 + '" fill="' + fill + '"' + stroke + '/>';
- } else if (el.shape === 'triangle') {
- body = '<polygon points="' + (box.w / 2) + ',0 ' + box.w + ',' + box.h + ' 0,' + box.h + '" fill="' + fill + '"' + stroke + '/>';
- } else if (el.shape === 'line') {
- body = '<line x1="0" y1="' + box.h / 2 + '" x2="' + box.w + '" y2="' + box.h / 2 + '" stroke="' + (el.stroke || el.fill || '#17140f') + '" stroke-width="' + (el.strokeWidth || 0.6) + '" stroke-linecap="round"/>';
- } else {
- body = '<rect width="' + box.w + '" height="' + box.h + '" rx="' + (el.radius || 0) + '" fill="' + fill + '"' + stroke + '/>';
- }
- }
- const hit = '<rect width="' + box.w + '" height="' + box.h + '" fill="transparent" stroke="none"/>';
- return '<g data-id="' + el.id + '" class="el" transform="translate(' + el.x + ' ' + el.y + ') rotate(' + rot + ' ' + cx + ' ' + cy + ')" opacity="' + (el.opacity == null ? 1 : el.opacity) + '">' + hit + body + '</g>';
- }
- function defsForBag(product, uidv) {
- const base = product.mockColor;
- return '<defs>' +
- '<linearGradient id="' + uidv + '-body" x1="0" y1="0" x2="1" y2="0">' +
- '<stop offset="0" stop-color="' + shade(base, 0.07) + '"/>' +
- '<stop offset="0.12" stop-color="' + shade(base, 0.02) + '"/>' +
- '<stop offset="0.78" stop-color="' + base + '"/>' +
- '<stop offset="1" stop-color="' + shade(base, -0.10) + '"/>' +
- '</linearGradient>' +
- '<linearGradient id="' + uidv + '-seal" x1="0" y1="0" x2="0" y2="1">' +
- '<stop offset="0" stop-color="' + shade(base, 0.1) + '"/>' +
- '<stop offset="1" stop-color="' + shade(base, -0.14) + '"/>' +
- '</linearGradient>' +
- '<linearGradient id="' + uidv + '-hl" x1="0" y1="0" x2="1" y2="0">' +
- '<stop offset="0" stop-color="#fff" stop-opacity="0.32"/>' +
- '<stop offset="0.5" stop-color="#fff" stop-opacity="0.06"/>' +
- '<stop offset="1" stop-color="#fff" stop-opacity="0"/>' +
- '</linearGradient>' +
- '<linearGradient id="' + uidv + '-shade" x1="0" y1="0" x2="1" y2="0">' +
- '<stop offset="0" stop-color="#000" stop-opacity="0"/>' +
- '<stop offset="0.78" stop-color="#000" stop-opacity="0"/>' +
- '<stop offset="1" stop-color="#000" stop-opacity="0.22"/>' +
- '</linearGradient>' +
- '<linearGradient id="' + uidv + '-top" x1="0" y1="0" x2="0" y2="1">' +
- '<stop offset="0" stop-color="#fff" stop-opacity="0.2"/>' +
- '<stop offset="0.35" stop-color="#fff" stop-opacity="0"/>' +
- '</linearGradient>' +
- '<linearGradient id="' + uidv + '-bottom" x1="0" y1="0" x2="0" y2="1">' +
- '<stop offset="0" stop-color="#000" stop-opacity="0"/>' +
- '<stop offset="1" stop-color="#000" stop-opacity="0.16"/>' +
- '</linearGradient>' +
- '<pattern id="' + uidv + '-rib" width="3" height="12" patternUnits="userSpaceOnUse">' +
- '<rect x="0" y="0" width="0.7" height="12" fill="#000" fill-opacity="0.08"/>' +
- '</pattern>' +
- '<pattern id="' + uidv + '-fib" width="7" height="7" patternUnits="userSpaceOnUse">' +
- '<path d="M0 7L7 0" stroke="#000" stroke-opacity="0.05" stroke-width="0.25"/>' +
- '<path d="M0 2L2 0" stroke="#fff" stroke-opacity="0.12" stroke-width="0.25"/>' +
- '</pattern>' +
- '<filter id="' + uidv + '-mono" color-interpolation-filters="sRGB"><feColorMatrix type="saturate" values="0"/></filter>' +
- '<filter id="' + uidv + '-blur" x="-30%" y="-30%" width="160%" height="160%"><feGaussianBlur stdDeviation="5"/></filter>' +
- '<clipPath id="' + uidv + '-bag"><rect x="' + (-product.pad.l) + '" y="' + (-product.pad.t) + '" width="' + 0 + '" height="0"/></clipPath>' +
- '</defs>';
- }
- function effectDefs(u) {
- return '<radialGradient id="' + u + '-spot" cx="50%" cy="50%" r="50%">' +
- '<stop offset="0" stop-color="#fff" stop-opacity="0.8"/>' +
- '<stop offset="0.55" stop-color="#fff" stop-opacity="0.18"/>' +
- '<stop offset="1" stop-color="#fff" stop-opacity="0"/>' +
- '</radialGradient>' +
- '<linearGradient id="' + u + '-foil" x1="0" y1="0" x2="1" y2="1">' +
- '<stop offset="0" stop-color="#f7e7b0"/>' +
- '<stop offset="0.22" stop-color="#c9a44c"/>' +
- '<stop offset="0.44" stop-color="#fff4cf"/>' +
- '<stop offset="0.66" stop-color="#b98b34"/>' +
- '<stop offset="1" stop-color="#f2d789"/>' +
- '</linearGradient>' +
- '<pattern id="' + u + '-speck" width="4.5" height="4.5" patternUnits="userSpaceOnUse">' +
- '<circle cx="1" cy="1" r="0.28" fill="#1e3a2c" fill-opacity="0.28"/>' +
- '<circle cx="3.2" cy="3.3" r="0.22" fill="#7a9a55" fill-opacity="0.4"/>' +
- '</pattern>' +
- '<pattern id="' + u + '-soft" width="6" height="6" patternUnits="userSpaceOnUse">' +
- '<path d="M0 6L6 0" stroke="#fff" stroke-opacity="0.1" stroke-width="0.35"/>' +
- '<path d="M0 2L2 0" stroke="#000" stroke-opacity="0.04" stroke-width="0.3"/>' +
- '</pattern>';
- }
- function effectLayer(u, mat, fin, shape, W, H, clipId) {
- let s = '';
- if (mat === 'gloss') {
- s += '<ellipse cx="' + (W * 0.74) + '" cy="' + (H * 0.2) + '" rx="' + (W * 0.34) + '" ry="' + (H * 0.18) + '" fill="url(#' + u + '-spot)" opacity="0.42"/>' +
- '<path d="M' + (shape.x + shape.w * 0.08) + ' ' + (shape.y + shape.h * 0.86) + 'L' + (shape.x + shape.w * 0.52) + ' ' + (shape.y + shape.h * 0.12) + '" stroke="#fff" stroke-opacity="0.18" stroke-width="' + (shape.w * 0.07) + '"/>';
- } else if (mat === 'kraft') {
- s += '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="url(#' + u + '-fib)" opacity="0.9"/>';
- } else if (mat === 'compost') {
- s += '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="#6c9a4f" opacity="0.1"/>' +
- '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="url(#' + u + '-speck)" opacity="0.75"/>';
- } else {
- s += '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="#fff" opacity="0.025"/>';
- }
- if (fin === 'uv') {
- s += '<ellipse cx="' + (W * 0.26) + '" cy="' + (H * 0.28) + '" rx="' + (W * 0.18) + '" ry="' + (H * 0.08) + '" fill="url(#' + u + '-spot)" opacity="0.5"/>' +
- '<ellipse cx="' + (W * 0.7) + '" cy="' + (H * 0.55) + '" rx="' + (W * 0.2) + '" ry="' + (H * 0.07) + '" fill="url(#' + u + '-spot)" opacity="0.45"/>' +
- '<ellipse cx="' + (W * 0.42) + '" cy="' + (H * 0.8) + '" rx="' + (W * 0.16) + '" ry="' + (H * 0.06) + '" fill="url(#' + u + '-spot)" opacity="0.4"/>';
- } else if (fin === 'foil') {
- s += '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="url(#' + u + '-foil)" opacity="0.16"/>' +
- '<path d="M' + (shape.x + shape.w * 0.1) + ' ' + (shape.y + shape.h * 0.72) + 'L' + (shape.x + shape.w * 0.9) + ' ' + (shape.y + shape.h * 0.34) + '" stroke="#fff8dc" stroke-opacity="0.35" stroke-width="' + (shape.w * 0.035) + '"/>';
- } else if (fin === 'soft') {
- s += '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="#fff1dc" opacity="0.1"/>' +
- '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="url(#' + u + '-soft)" opacity="0.4"/>';
- }
- return s ? '<g clip-path="url(#' + clipId + ')" pointer-events="none" data-fx="' + mat + ':' + fin + '">' + s + '</g>' : '';
- }
- function bagShape(product, W, H) {
- const p = product.pad;
- return { x: -p.l, y: -p.t, w: W + p.l + p.r, h: H + p.t + p.b, rx: product.shape === 'kraft' ? 2.5 : product.shape === 'mailer' ? 4 : 6 };
- }
- function overlayUI(opts, product, sideDesign, ctx) {
- const W = opts.size.w;
- const H = opts.size.h;
- const p = product.pad;
- const scale = opts.pxPerMm || 3;
- const hs = 5.2 / scale;
- const sw = 1 / scale;
- let s = '';
- if (opts.showGuides !== false) {
- s += '<rect x="0" y="0" width="' + W + '" height="' + H + '" fill="none" stroke="#ff5c2b" stroke-width="' + (1.1 * sw) + '" stroke-dasharray="' + (4 * sw) + ' ' + (3 * sw) + '" opacity="0.75" data-ui="1"/>';
- s += '<rect x="6" y="6" width="' + (W - 12) + '" height="' + (H - 12) + '" fill="none" stroke="#8a8276" stroke-width="' + (0.8 * sw) + '" stroke-dasharray="' + (2 * sw) + ' ' + (3 * sw) + '" opacity="0.55" data-ui="1"/>';
- }
- if (opts.showDims !== false) {
- const lx = -p.l - 13;
- const by = H + p.b + 13;
- const fs = 4.2;
- s += '<g data-ui="1" stroke="#a49a8b" stroke-width="' + (0.7 * sw) + '" fill="none">' +
- '<path d="M' + lx + ' 0V' + H + 'M' + (lx - 2.2) + ' 0h4.4M' + (lx - 2.2) + ' ' + H + 'h4.4"/>' +
- '<path d="M' + (-p.l) + ' 0h-' + 6 + 'M' + (-p.l) + ' ' + H + 'h-' + 6 + '"/>' +
- '<path d="M0 ' + by + 'H' + W + 'M0 ' + (by - 2.2) + 'v4.4M' + W + ' ' + (by - 2.2) + 'v4.4"/>' +
- '<path d="M0 ' + H + 'v6M' + W + ' ' + H + 'v6"/>' +
- '</g>' +
- '<text data-ui="1" x="' + (lx - 3) + '" y="' + (H / 2) + '" transform="rotate(-90 ' + (lx - 3) + ' ' + (H / 2) + ')" text-anchor="middle" font-family="' + fontAttr('latin') + '" font-size="' + fs + '" fill="#8a8276">' + H + ' mm</text>' +
- '<text data-ui="1" x="' + (W / 2) + '" y="' + (by + 5.2) + '" text-anchor="middle" font-family="' + fontAttr('latin') + '" font-size="' + fs + '" fill="#8a8276">' + W + ' mm</text>';
- // 侧折边宽度(左右各一)与顶部封边高度标注
- if (p.l > 2) {
- const gx = -p.l / 2;
- s += '<g data-ui="1" stroke="#a49a8b" stroke-width="' + (0.55 * sw) + '" fill="none">' +
- '<path d="M' + gx + ' ' + (H * 0.08) + 'V' + (H * 0.92) + 'M' + (gx - 1.6) + ' ' + (H * 0.08) + 'h3.2M' + (gx - 1.6) + ' ' + (H * 0.92) + 'h3.2"/></g>' +
- '<text data-ui="1" x="' + (gx - 2) + '" y="' + (H / 2) + '" transform="rotate(-90 ' + (gx - 2) + ' ' + (H / 2) + ')" text-anchor="middle" font-size="' + (fs * 0.82) + '" fill="#8a8276">折边 ' + Math.round(p.l) + '</text>';
- const gx2 = W + p.r / 2;
- s += '<g data-ui="1" stroke="#a49a8b" stroke-width="' + (0.55 * sw) + '" fill="none">' +
- '<path d="M' + gx2 + ' ' + (H * 0.08) + 'V' + (H * 0.92) + 'M' + (gx2 - 1.6) + ' ' + (H * 0.08) + 'h3.2M' + (gx2 - 1.6) + ' ' + (H * 0.92) + 'h3.2"/></g>' +
- '<text data-ui="1" x="' + (gx2 + 2) + '" y="' + (H / 2) + '" transform="rotate(90 ' + (gx2 + 2) + ' ' + (H / 2) + ')" text-anchor="middle" font-size="' + (fs * 0.82) + '" fill="#8a8276">折边 ' + Math.round(p.r) + '</text>';
- }
- if (p.t > 2) {
- const gy = -p.t / 2;
- s += '<g data-ui="1" stroke="#a49a8b" stroke-width="' + (0.55 * sw) + '" fill="none">' +
- '<path d="M' + (W * 0.1) + ' ' + gy + 'H' + (W * 0.9) + 'M' + (W * 0.1) + ' ' + (gy - 1.6) + 'v3.2M' + (W * 0.9) + ' ' + (gy - 1.6) + 'v3.2"/></g>' +
- '<text data-ui="1" x="' + (W / 2) + '" y="' + (gy + 2.4) + '" text-anchor="middle" font-size="' + (fs * 0.82) + '" fill="#8a8276">封边 ' + Math.round(p.t) + '</text>';
- }
- // 印刷面尺寸水印(面内底部居中)
- s += '<text data-ui="1" x="' + (W / 2) + '" y="' + (H - 8) + '" text-anchor="middle" font-size="' + (fs * 0.9) + '" fill="#8a8276" opacity="0.75">面 ' + W + ' × ' + H + ' mm</text>';
- }
- if (opts.guides) {
- if (opts.guides.x != null) s += '<line data-ui="1" x1="' + opts.guides.x + '" y1="' + (-p.t - 4) + '" x2="' + opts.guides.x + '" y2="' + (H + p.b + 4) + '" stroke="#ff5c2b" stroke-width="' + (1 * sw) + '" stroke-dasharray="' + (3 * sw) + ' ' + (3 * sw) + '"/>';
- if (opts.guides.y != null) s += '<line data-ui="1" x1="' + (-p.l - 4) + '" y1="' + opts.guides.y + '" x2="' + (W + p.r + 4) + '" y2="' + opts.guides.y + '" stroke="#ff5c2b" stroke-width="' + (1 * sw) + '" stroke-dasharray="' + (3 * sw) + ' ' + (3 * sw) + '"/>';
- }
- const sel = opts.selectedId && sideDesign ? sideDesign.elements.find(function (e) { return e.id === opts.selectedId; }) : null;
- if (sel) {
- const box = elementBox(sel);
- const rot = sel.rotation || 0;
- const cx = box.w / 2;
- const cy = box.h / 2;
- let handles = '';
- const pts = [['nw', 0, 0], ['ne', box.w, 0], ['sw', 0, box.h], ['se', box.w, box.h]];
- if (sel.type === 'text') pts.push(['w', 0, box.h / 2], ['e', box.w, box.h / 2]);
- pts.forEach(function (h) {
- handles += '<rect data-handle="' + h[0] + '" x="' + (h[1] - hs) + '" y="' + (h[2] - hs) + '" width="' + (hs * 2) + '" height="' + (hs * 2) + '" rx="' + (hs * 0.35) + '" fill="#fff" stroke="#ff5c2b" stroke-width="' + (1.2 * sw) + '" style="pointer-events:all"/>';
- });
- const ro = 26 / scale;
- handles += '<line x1="' + (box.w / 2) + '" y1="0" x2="' + (box.w / 2) + '" y2="' + (-ro) + '" stroke="#ff5c2b" stroke-width="' + (1.2 * sw) + '"/>';
- handles += '<circle data-handle="rot" cx="' + (box.w / 2) + '" cy="' + (-ro) + '" r="' + (hs * 1.25) + '" fill="#ff5c2b" stroke="#fff" stroke-width="' + (1.1 * sw) + '" style="pointer-events:all;cursor:grab"/>';
- const label = box.w.toFixed(0) + ' × ' + box.h.toFixed(0) + ' mm';
- const labelW = label.length * 5.4 / scale + 6 / scale;
- handles += '<g transform="translate(' + (box.w / 2 - labelW / 2) + ' ' + (box.h + 8 / scale) + ')" style="pointer-events:none">' +
- '<rect width="' + labelW + '" height="' + (16 / scale) + '" rx="' + (3 / scale) + '" fill="#17140f" opacity="0.86"/>' +
- '<text x="' + (labelW / 2) + '" y="' + (11 / scale) + '" text-anchor="middle" font-family="' + fontAttr('latin') + '" font-size="' + (8.5 / scale) + '" fill="#fff">' + label + '</text></g>';
- s += '<g data-ui="1" class="sel-overlay" transform="translate(' + sel.x + ' ' + sel.y + ') rotate(' + rot + ' ' + cx + ' ' + cy + ')">' +
- '<rect width="' + box.w + '" height="' + box.h + '" fill="none" stroke="#ff5c2b" stroke-width="' + (1.4 * sw) + '"/>' + handles + '</g>';
- }
- return s;
- }
- function bagSVG(product, side, sideDesign, opts) {
- opts = opts || {};
- const size = opts.size || D.sizeOf(product, 'm');
- const W = size.w;
- const H = size.h;
- const p = product.pad;
- const u = uid('bag');
- const ctx = { uid: u };
- const tight = !!opts.tight;
- const card = !!opts.card;
- const mat = opts.material || 'matte';
- const fin = opts.finish || 'none';
- const extra = card ? 9 : 0;
- const vb = tight
- ? { x: -p.l, y: -p.t, w: W + p.l + p.r, h: H + p.t + p.b }
- : { x: -p.l - (opts.ui ? 18 : extra), y: -p.t - (opts.ui ? 12 : extra), w: W + p.l + p.r + (opts.ui ? 30 : extra * 2), h: H + p.t + p.b + (opts.ui ? 34 : extra * 2) };
- const shape = bagShape(product, W, H);
- const base = product.mockColor;
- const design = sideDesign || { bg: null, elements: [] };
- const clipId = u + '-bagclip';
- const printId = u + '-printclip';
- const elements = (design.elements || []).map(function (el) { return renderElement(el, ctx); }).join('');
- let body = '';
- body += '<ellipse cx="' + (W / 2) + '" cy="' + (H + p.b + (tight ? 2 : 4)) + '" rx="' + (shape.w * 0.42) + '" ry="' + (tight ? 3 : 6) + '" fill="#17140f" opacity="0.16" filter="url(#' + u + '-blur)"/>';
- body += '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="url(#' + u + '-body)"/>';
- if (product.shape === 'kraft') body += '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="url(#' + u + '-fib)"/>';
- // 设计底色铺满整个袋身(含左右侧折边):真实包装侧折与正面同膜同底色
- if (design.bg) body += '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" fill="' + design.bg + '"/>';
- body += '<g clip-path="url(#' + clipId + ')"><g clip-path="url(#' + printId + ')">';
- body += elements;
- body += '</g></g>';
- body += '<g clip-path="url(#' + clipId + ')" style="pointer-events:none">' +
- '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" fill="url(#' + u + '-hl)"/>' +
- '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" fill="url(#' + u + '-shade)"/>' +
- '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + (p.t + H * 0.3) + '" fill="url(#' + u + '-top)"/>' +
- '<rect x="' + shape.x + '" y="' + (H - H * 0.25) + '" width="' + shape.w + '" height="' + (p.t + H * 0.3) + '" fill="url(#' + u + '-bottom)"/>' +
- '</g>';
- body += effectLayer(u, mat, fin, shape, W, H, clipId);
- const seal = p.t * 0.48;
- if (product.shape === 'mailer') {
- body += '<rect x="' + (shape.x + 1.5) + '" y="' + (shape.y + 1.5) + '" width="' + (shape.w - 3) + '" height="' + (p.t - 5) + '" rx="3" fill="url(#' + u + '-seal)"/>';
- body += '<line x1="' + (shape.x + 4) + '" y1="' + (shape.y + p.t - 5) + '" x2="' + (shape.x + shape.w - 4) + '" y2="' + (shape.y + p.t - 5) + '" stroke="' + shade(base, -0.35) + '" stroke-width="0.5" stroke-dasharray="1.1 1.1" opacity="0.6"/>';
- body += '<path d="M' + (shape.x + 2) + ' ' + (shape.y + 4) + 'l3 3-3 3M' + (shape.x + shape.w - 2) + ' ' + (shape.y + 4) + 'l-3 3 3 3" fill="none" stroke="' + shade(base, -0.3) + '" stroke-width="0.5" opacity="0.6"/>';
- } else if (product.shape === 'garment') {
- body += '<rect x="' + (shape.x + 1.5) + '" y="' + (shape.y + 1.5) + '" width="' + (shape.w - 3) + '" height="' + (p.t - 4) + '" rx="2" fill="#fffdf8" opacity="0.9"/>';
- body += '<circle cx="' + (W / 2) + '" cy="' + (shape.y + p.t * 0.42) + '" r="' + (p.t * 0.18) + '" fill="' + base + '" stroke="' + shade(base, -0.3) + '" stroke-width="0.5"/>';
- body += '<line x1="' + (shape.x + 3) + '" y1="' + (shape.y + p.t - 3) + '" x2="' + (shape.x + shape.w - 3) + '" y2="' + (shape.y + p.t - 3) + '" stroke="' + shade(base, -0.3) + '" stroke-width="0.45" stroke-dasharray="1 1.2" opacity="0.5"/>';
- } else {
- body += '<rect x="' + (shape.x + 1.2) + '" y="' + (shape.y + 1.2) + '" width="' + (shape.w - 2.4) + '" height="' + seal + '" rx="3" fill="url(#' + u + '-seal)"/>';
- body += '<rect x="' + (shape.x + 1.2) + '" y="' + (shape.y + 1.2) + '" width="' + (shape.w - 2.4) + '" height="' + seal + '" rx="3" fill="url(#' + u + '-rib)" opacity="0.7"/>';
- body += '<line x1="' + (shape.x + 2) + '" y1="' + (shape.y + seal + 2) + '" x2="' + (shape.x + shape.w - 2) + '" y2="' + (shape.y + seal + 2) + '" stroke="' + shade(base, -0.34) + '" stroke-width="0.6" stroke-dasharray="1.5 0.9" opacity="0.55"/>';
- body += '<rect x="' + (W * 0.46) + '" y="' + (shape.y + seal + 1) + '" width="' + Math.max(5, W * 0.06) + '" height="1.6" rx="0.8" fill="' + shade(base, -0.4) + '" opacity="0.55"/>';
- if (product.shape === 'kraft') {
- body += '<line x1="' + (shape.x + 4) + '" y1="' + (shape.y + p.t * 0.72) + '" x2="' + (shape.x + shape.w - 4) + '" y2="' + (shape.y + p.t * 0.72) + '" stroke="' + shade(base, -0.28) + '" stroke-width="0.5" opacity="0.4"/>';
- body += '<line x1="' + (shape.x + 6) + '" y1="' + (shape.y + p.t) + '" x2="' + (shape.x + 6) + '" y2="' + (shape.y + shape.h - 6) + '" stroke="' + shade(base, -0.24) + '" stroke-width="0.55" opacity="0.35"/>';
- body += '<line x1="' + (shape.x + shape.w - 6) + '" y1="' + (shape.y + p.t) + '" x2="' + (shape.x + shape.w - 6) + '" y2="' + (shape.y + shape.h - 6) + '" stroke="' + shade(base, -0.24) + '" stroke-width="0.55" opacity="0.35"/>';
- }
- }
- // 自封袋族顶部胶条(压敏双胶带 + 封口折线;对标行业袋型结构)
- if (product.shape === 'standup' || product.shape === 'flatbottom' || product.shape === 'coffee') {
- const stripH = Math.max(2.6, p.t * 0.15);
- const stripX = shape.x + shape.w * 0.07;
- const stripW = shape.w * 0.86;
- const y0 = shape.y + p.t * 0.18;
- if (side === 'front') {
- // 两条压敏胶带
- body += '<rect x="' + stripX + '" y="' + y0 + '" width="' + stripW + '" height="' + stripH + '" rx="' + (stripH / 2) + '" fill="url(#' + u + '-seal)"/>';
- body += '<rect x="' + stripX + '" y="' + (y0 + stripH * 1.9) + '" width="' + stripW + '" height="' + stripH + '" rx="' + (stripH / 2) + '" fill="url(#' + u + '-seal)"/>';
- body += '<rect x="' + stripX + '" y="' + y0 + '" width="' + stripW + '" height="' + stripH + '" rx="' + (stripH / 2) + '" fill="#ffffff" opacity="0.18"/>';
- // 封口折线
- body += '<line x1="' + (shape.x + 2) + '" y1="' + (shape.y + p.t * 0.74) + '" x2="' + (shape.x + shape.w - 2) + '" y2="' + (shape.y + p.t * 0.74) + '" stroke="#e05a4e" stroke-width="0.7" opacity="0.7"/>';
- } else {
- // 背面:Folded Flap 翻折盖虚线区
- const flapH = p.t * 0.92;
- body += '<rect x="' + (shape.x + shape.w * 0.05) + '" y="' + (shape.y + p.t * 0.1) + '" width="' + (shape.w * 0.9) + '" height="' + flapH + '" fill="none" stroke="#e8c33a" stroke-width="0.7" stroke-dasharray="2.6 1.7" opacity="0.9"/>';
- body += '<text x="' + (shape.x + shape.w / 2) + '" y="' + (shape.y + p.t * 0.1 + flapH / 2) + '" text-anchor="middle" dominant-baseline="middle" font-size="' + Math.max(4.5, W * 0.035) + '" fill="#e8c33a" font-family="Helvetica Neue, PingFang SC, sans-serif" opacity="0.95">Folded Flap</text>';
- }
- }
- if (product.shape === 'coffee' && side === 'front') {
- body += '<circle cx="' + (W - 15) + '" cy="' + 16 + '" r="4.6" fill="' + shade(base, -0.14) + '" stroke="' + shade(base, -0.4) + '" stroke-width="0.5"/>';
- body += '<circle cx="' + (W - 15) + '" cy="' + 16 + '" r="3" fill="none" stroke="' + shade(base, -0.45) + '" stroke-width="0.45" opacity="0.7"/>';
- body += '<path d="M' + (W - 17.5) + ' ' + 16 + 'h5M' + (W - 15) + ' ' + 13.5 + 'v5" stroke="' + shade(base, -0.5) + '" stroke-width="0.5" opacity="0.55"/>';
- }
- if (product.shape === 'standup') {
- body += '<path d="M' + shape.x + ' ' + (H + 1) + ' Q' + (W / 2) + ' ' + (H + p.b * 1.1) + ' ' + (shape.x + shape.w) + ' ' + (H + 1) + ' L' + (shape.x + shape.w) + ' ' + (shape.y + shape.h - shape.rx) + ' Q' + (shape.x + shape.w) + ' ' + (shape.y + shape.h) + ' ' + (shape.x + shape.w - shape.rx) + ' ' + (shape.y + shape.h) + ' L' + (shape.x + shape.rx) + ' ' + (shape.y + shape.h) + ' Q' + shape.x + ' ' + (shape.y + shape.h) + ' ' + shape.x + ' ' + (shape.y + shape.h - shape.rx) + ' Z" fill="' + shade(base, -0.12) + '" opacity="0.35"/>';
- body += '<path d="M' + shape.x + ' ' + (H + 2) + ' Q' + (W / 2) + ' ' + (H + p.b * 0.95) + ' ' + (shape.x + shape.w) + ' ' + (H + 2) + '" fill="none" stroke="' + shade(base, -0.35) + '" stroke-width="0.6" opacity="0.5"/>';
- } else if (product.shape === 'flatbottom') {
- body += '<path d="M' + shape.x + ' ' + (H + 1) + ' L' + (shape.x + 6) + ' ' + (shape.y + shape.h) + ' L' + (shape.x + shape.w - 6) + ' ' + (shape.y + shape.h) + ' L' + (shape.x + shape.w) + ' ' + (H + 1) + ' Z" fill="' + shade(base, -0.14) + '" opacity="0.32"/>';
- body += '<line x1="' + shape.x + '" y1="' + (H + 1) + '" x2="' + (shape.x + shape.w) + '" y2="' + (H + 1) + '" stroke="' + shade(base, -0.4) + '" stroke-width="0.6" opacity="0.5"/>';
- body += '<line x1="' + (W / 2) + '" y1="' + (H + 1) + '" x2="' + (W / 2) + '" y2="' + (shape.y + shape.h) + '" stroke="' + shade(base, -0.35) + '" stroke-width="0.5" opacity="0.35"/>';
- }
- body += '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="none" stroke="' + shade(base, -0.4) + '" stroke-width="0.55" opacity="0.5"/>';
- const defs = defsForBag(product, u).replace('<clipPath id="' + u + '-bag"><rect x="' + (-product.pad.l) + '" y="' + (-product.pad.t) + '" width="0" height="0"/></clipPath>',
- '<clipPath id="' + clipId + '"><rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '"/></clipPath>' +
- '<clipPath id="' + printId + '"><rect x="-3" y="-3" width="' + (W + 6) + '" height="' + (H + 6) + '"/></clipPath>') + effectDefs(u);
- const ui = opts.ui ? overlayUI(opts, product, design, ctx) : '';
- const vbStr = vb.x + ' ' + vb.y + ' ' + vb.w + ' ' + vb.h;
- return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="' + vbStr + '" class="bag-svg" preserveAspectRatio="xMidYMid meet"' + (opts.width ? ' width="' + opts.width + '" height="' + (opts.width * vb.h / vb.w) + '"' : '') + '>' + defs + body + ui + '</svg>';
- }
- function designSVG(design, W, H) {
- const u = uid('art');
- const ctx = { uid: u };
- const elements = (design.elements || []).map(function (el) { return renderElement(el, ctx); }).join('');
- return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ' + W + ' ' + H + '" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">' +
- '<defs><filter id="' + u + '-mono" color-interpolation-filters="sRGB"><feColorMatrix type="saturate" values="0"/></filter></defs>' +
- (design.bg ? '<rect width="' + W + '" height="' + H + '" fill="' + design.bg + '"/>' : '') +
- elements + '</svg>';
- }
- function designFromTemplate(templateId, W, H, side) {
- const t = D.templateById(templateId);
- if (!t) return { bg: null, elements: [] };
- const source = side === 'back' ? (t.back || []) : (t.front || []);
- const elements = source.map(function (item) {
- const e = Object.assign({}, item);
- e.id = uid('el');
- e.x = (item.x || 0) * W;
- e.y = (item.y || 0) * H;
- if (e.type === 'text') {
- e.w = (item.w || 1) * W;
- e.size = (item.size || 0.04) * W;
- e.spacing = item.spacing || 0;
- e.lineHeight = item.lineHeight || 1.25;
- e.strokeWidth = (item.sw || 0) * W;
- delete e.sw;
- } else if (e.type === 'shape') {
- e.w = (item.w || 0.2) * W;
- e.h = item.shape === 'line' ? 4 : (item.h == null ? 0.1 : item.h) * H;
- e.strokeWidth = (item.sw || 0) * W;
- e.radius = (item.radius || 0) * W;
- delete e.sw;
- } else if (e.type === 'sticker') {
- e.w = (item.w || 0.2) * W;
- e.h = e.w;
- } else if (e.type === 'badge') {
- e.w = (item.w || 0.3) * W;
- e.h = e.w;
- e.size = (item.size || 0.03) * W;
- } else if (e.type === 'barcode') {
- e.w = (item.w || 0.5) * W;
- e.h = (item.h || 0.12) * H;
- }
- e.rotation = 0;
- return e;
- });
- return { bg: t.bg, elements: elements };
- }
- function templatePreview(template, productOverride) {
- const product = D.productById(productOverride || template.product);
- const size = D.sizeOf(product, product.sizes[Math.floor((product.sizes.length - 1) / 2)].id);
- const design = designFromTemplate(template.id, size.w, size.h, 'front');
- return bagSVG(product, 'front', design, { size: size, card: true, tight: false });
- }
- function sampleDesign(product, seed) {
- const pool = D.templates.filter(function (t) { return t.product === product.id; });
- if (!pool.length) pool.push(D.templateById('citrus'));
- const t = pool[(seed || 0) % pool.length];
- const size = D.sizeOf(product, product.sizes[Math.floor((product.sizes.length - 1) / 2)].id);
- return { size: size, design: designFromTemplate(t.id, size.w, size.h, 'front'), template: t };
- }
- window.PL.render = {
- uid: uid,
- esc: esc,
- shade: shade,
- alpha: alpha,
- fontStack: fontStack,
- textLines: textLines,
- textHeight: textHeight,
- elementBox: elementBox,
- elementSVG: renderElement,
- bagSVG: bagSVG,
- designFromTemplate: designFromTemplate,
- designSVG: designSVG,
- templatePreview: templatePreview,
- sampleDesign: sampleDesign,
- barcodeBits: barcodeBits
- };
- })();
|