render.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. (function () {
  2. const D = window.PL.data;
  3. let uidCounter = 0;
  4. const uid = function (p) { uidCounter += 1; return (p || 'u') + uidCounter; };
  5. const measureCanvas = document.createElement('canvas');
  6. const measureCtx = measureCanvas.getContext('2d');
  7. function esc(s) {
  8. return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) {
  9. return { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c];
  10. });
  11. }
  12. function hexRgb(hex) {
  13. let h = String(hex || '#000').replace('#', '');
  14. if (h.length === 3) h = h[0] + h[0] + h[1] + h[1] + h[2] + h[2];
  15. const n = parseInt(h, 16);
  16. return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
  17. }
  18. function shade(hex, amt) {
  19. const rgb = hexRgb(hex);
  20. const target = amt < 0 ? 0 : 255;
  21. const k = Math.abs(amt);
  22. return '#' + rgb.map(function (v) {
  23. return Math.round(v + (target - v) * k).toString(16).padStart(2, '0');
  24. }).join('');
  25. }
  26. function alpha(hex, a) {
  27. const c = hexRgb(hex);
  28. return 'rgba(' + c[0] + ',' + c[1] + ',' + c[2] + ',' + a + ')';
  29. }
  30. function fontStack(key) {
  31. return (D.fonts[key] || D.fonts.sans).stack;
  32. }
  33. function fontAttr(key) {
  34. return fontStack(key).replace(/"/g, "'");
  35. }
  36. function fontCss(el) {
  37. return (el.italic ? 'italic ' : '') + (el.weight || 400) + ' ' + (el.size || 12) * 3.779527559 + 'px ' + fontStack(el.font);
  38. }
  39. function charWidth(ch, el) {
  40. measureCtx.font = fontCss(el);
  41. return measureCtx.measureText(ch).width / 3.779527559;
  42. }
  43. function segment(text) {
  44. if (typeof Intl !== 'undefined' && Intl.Segmenter) {
  45. const seg = new Intl.Segmenter('zh', { granularity: 'word' });
  46. return Array.from(seg.segment(text), function (s) { return s.segment; });
  47. }
  48. return text.split(/(\s+)/).filter(function (x) { return x !== ''; });
  49. }
  50. function textLines(el) {
  51. const w = Math.max(4, el.w || 60);
  52. const size = el.size || 12;
  53. const spacing = (el.spacing || 0) * size;
  54. const paragraphs = String(el.text || '').split('\n');
  55. const out = [];
  56. paragraphs.forEach(function (p) {
  57. const tokens = segment(p);
  58. let line = '';
  59. let lineW = 0;
  60. const push = function () {
  61. out.push(line.replace(/\s+$/, ''));
  62. line = '';
  63. lineW = 0;
  64. };
  65. tokens.forEach(function (tok) {
  66. const tw = charWidth(tok, el) + Array.from(tok).length * spacing;
  67. if (line && lineW + tw > w) push();
  68. if (tw > w && Array.from(tok).length > 1) {
  69. Array.from(tok).forEach(function (ch) {
  70. const cw = charWidth(ch, el) + spacing;
  71. if (line && lineW + cw > w) push();
  72. line += ch;
  73. lineW += cw;
  74. });
  75. } else {
  76. line += tok;
  77. lineW += tw;
  78. }
  79. });
  80. out.push(line.replace(/\s+$/, ''));
  81. });
  82. return out.length ? out : [''];
  83. }
  84. function textHeight(el) {
  85. const lines = textLines(el);
  86. const lh = (el.lineHeight || 1.25) * (el.size || 12);
  87. return Math.max((el.size || 12) * 1.1, lines.length * lh);
  88. }
  89. function elementBox(el) {
  90. let w = el.w || 0;
  91. let h = el.h || 0;
  92. if (el.type === 'text') h = textHeight(el);
  93. if (el.type === 'badge' || el.type === 'qr') { w = el.w || 40; h = w; }
  94. if (el.type === 'asset') { w = el.w || 30; h = w; }
  95. if (el.type === 'line') h = Math.max(4, el.h || 0);
  96. return { w: Math.max(1, w), h: Math.max(1, h) };
  97. }
  98. function barcodeBits(value) {
  99. const L = ['0001101', '0011001', '0010011', '0111101', '0100011', '0110001', '0101111', '0111011', '0110111', '0001011'];
  100. const G = ['0100111', '0110011', '0011011', '0100001', '0011101', '0111001', '0000101', '0010001', '0001001', '0010111'];
  101. const R = ['1110010', '1100110', '1101100', '1000010', '1011100', '1001110', '1010000', '1000100', '1001000', '1110100'];
  102. const P = ['LLLLLL', 'LLGLGG', 'LLGGLG', 'LLGGGL', 'LGLLGG', 'LGGLLG', 'LGGGLL', 'LGLGLG', 'LGLGGL', 'LGGLGL'];
  103. let digits = String(value || '').replace(/\D/g, '');
  104. if (digits.length < 12) {
  105. let seed = 0;
  106. String(value || 'POUCHLAB').split('').forEach(function (c) { seed = (seed * 31 + c.charCodeAt(0)) % 9973; });
  107. while (digits.length < 12) { seed = (seed * 37 + 11) % 10000; digits += String(seed % 10); }
  108. }
  109. digits = digits.slice(0, 12);
  110. let sum = 0;
  111. for (let i = 0; i < 12; i += 1) sum += Number(digits[i]) * (i % 2 === 0 ? 1 : 3);
  112. const check = (10 - (sum % 10)) % 10;
  113. const code = digits + check;
  114. const parity = P[Number(code[0])];
  115. let bits = '101';
  116. for (let i = 1; i <= 6; i += 1) {
  117. const d = Number(code[i]);
  118. bits += parity[i - 1] === 'L' ? L[d] : G[d];
  119. }
  120. bits += '01010';
  121. for (let i = 7; i <= 12; i += 1) bits += R[Number(code[i])];
  122. bits += '101';
  123. return { bits: bits, code: code };
  124. }
  125. function renderBarcode(el, ctx) {
  126. const w = el.w || 50;
  127. const h = el.h || 20;
  128. const color = el.color || '#17140f';
  129. const bars = barcodeBits(el.value || '6971234567890');
  130. const showText = el.showText !== false;
  131. const textH = showText ? Math.min(h * 0.22, 4.2) : 0;
  132. const barH = h - textH - (showText ? 0.8 : 0);
  133. const module = w / 95;
  134. let rects = '';
  135. let x = 0;
  136. for (let i = 0; i < bars.bits.length; i += 1) {
  137. if (bars.bits[i] === '1') {
  138. rects += '<rect x="' + (x * module).toFixed(3) + '" y="0" width="' + module.toFixed(3) + '" height="' + barH.toFixed(3) + '" fill="' + color + '"/>';
  139. }
  140. x += 1;
  141. }
  142. 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>' : '';
  143. return rects + text;
  144. }
  145. function renderQR(el) {
  146. const size = el.w || 40;
  147. const color = el.color || '#17140f';
  148. const light = el.light === 'none' ? null : (el.light || '#ffffff');
  149. let qr = null;
  150. try {
  151. if (window.PL && window.PL.qrcode && window.PL.qrcode.build) qr = window.PL.qrcode.build(String(el.value || ' '));
  152. } catch (e) { qr = null; }
  153. const bg = light ? '<rect width="' + size + '" height="' + size + '" fill="' + light + '"/>' : '';
  154. if (!qr) {
  155. 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"/>';
  156. }
  157. const quiet = 2;
  158. const total = qr.size + quiet * 2;
  159. const unit = size / total;
  160. let d = '';
  161. for (let y = 0; y < qr.size; y += 1) {
  162. for (let x = 0; x < qr.size; x += 1) {
  163. if (!qr.matrix[y][x]) continue;
  164. const px = (x + quiet) * unit;
  165. const py = (y + quiet) * unit;
  166. d += 'M' + px.toFixed(3) + ' ' + py.toFixed(3) + 'h' + unit.toFixed(3) + 'v' + unit.toFixed(3) + 'h-' + unit.toFixed(3) + 'z';
  167. }
  168. }
  169. return bg + '<path d="' + d + '" fill="' + color + '"/>';
  170. }
  171. function renderAsset(el) {
  172. const lib = window.PL && window.PL.boxlib;
  173. if (!lib || !lib.assetSvg || !el.asset) return '';
  174. const svg = lib.assetSvg(el.asset, el.color || '#17140f');
  175. if (!svg) return '';
  176. const inner = svg.replace(/^<svg[^>]*>/, '').replace(/<\/svg>\s*$/, '');
  177. const scale = (el.w || 24) / 24;
  178. return '<g transform="scale(' + scale.toFixed(4) + ')">' + inner + '</g>';
  179. }
  180. function renderSticker(el) {
  181. const def = D.stickers[el.sticker] || D.stickers.star;
  182. const color = el.color || '#17140f';
  183. const size = el.w || 30;
  184. const scale = size / 24;
  185. const paths = def.paths.map(function (p) {
  186. const style = p.stroke
  187. ? 'fill="none" stroke="' + color + '" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"'
  188. : 'fill="' + color + '"';
  189. return '<path d="' + p.d + '" ' + style + '/>';
  190. }).join('');
  191. return '<g transform="scale(' + scale + ')">' + paths + '</g>';
  192. }
  193. function renderBadge(el, ctx) {
  194. const w = el.w || 40;
  195. const color = el.color || '#17140f';
  196. const r = w / 2 - w * 0.035;
  197. const cid = ctx.uid + '-circle';
  198. const size = el.size || w * 0.09;
  199. const circumference = 2 * Math.PI * r;
  200. 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>' +
  201. '<circle cx="' + (w / 2) + '" cy="' + (w / 2) + '" r="' + r + '" fill="none" stroke="' + color + '" stroke-width="' + (w * 0.012) + '"/>' +
  202. '<circle cx="' + (w / 2) + '" cy="' + (w / 2) + '" r="' + (r * 0.76) + '" fill="none" stroke="' + color + '" stroke-width="' + (w * 0.006) + '"/>' +
  203. '<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>' +
  204. '<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 + '"/>';
  205. }
  206. function renderText(el, ctx) {
  207. const lines = textLines(el);
  208. const size = el.size || 12;
  209. const lh = (el.lineHeight || 1.25) * size;
  210. const ls = (el.spacing || 0) * size;
  211. const anchor = el.align === 'center' ? 'middle' : el.align === 'right' ? 'end' : 'start';
  212. const x = el.align === 'center' ? (el.w || 60) / 2 : el.align === 'right' ? (el.w || 60) : 0;
  213. const fill = el.color === 'none' ? 'none' : (el.color || '#17140f');
  214. // 描边文字最小线宽:字号的 4.5%(发丝级描边在印刷预览/贴图光栅化时不可见)
  215. const strokeW = el.stroke && el.stroke !== 'none' ? Math.max(el.strokeWidth || 0, size * 0.045) : 0;
  216. const stroke = el.stroke && el.stroke !== 'none' ? ' stroke="' + el.stroke + '" stroke-width="' + strokeW.toFixed(3) + '"' : '';
  217. const tspans = lines.map(function (line, i) {
  218. 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>';
  219. }).join('');
  220. return tspans;
  221. }
  222. function renderElement(el, ctx) {
  223. if (el.hidden) return '';
  224. const box = elementBox(el);
  225. const rot = el.rotation || 0;
  226. const cx = box.w / 2;
  227. const cy = box.h / 2;
  228. let body = '';
  229. if (el.type === 'image') {
  230. if (el.src) {
  231. const fit = el.fit === 'cover' ? 'xMidYMid slice' : 'xMidYMid meet';
  232. const filter = el.mono ? ' filter="url(#' + ctx.uid + '-mono)"' : '';
  233. body = '<image href="' + esc(el.src) + '" x="0" y="0" width="' + box.w + '" height="' + box.h + '" preserveAspectRatio="' + fit + '"' + filter + '/>';
  234. } else {
  235. body = '<rect width="' + box.w + '" height="' + box.h + '" fill="#e9e2d5" stroke="#c9bfae" stroke-dasharray="2 2"/>';
  236. }
  237. } else if (el.type === 'text') {
  238. body = renderText(el, ctx);
  239. } else if (el.type === 'badge') {
  240. body = renderBadge(el, ctx);
  241. } else if (el.type === 'barcode') {
  242. body = renderBarcode(el, ctx);
  243. } else if (el.type === 'qr') {
  244. body = renderQR(el);
  245. } else if (el.type === 'asset') {
  246. body = renderAsset(el);
  247. } else if (el.type === 'sticker') {
  248. body = renderSticker(el);
  249. } else if (el.type === 'shape') {
  250. const fill = el.fill || 'none';
  251. const stroke = el.stroke && el.stroke !== 'none' ? ' stroke="' + el.stroke + '" stroke-width="' + (el.strokeWidth || 0.5) + '"' : '';
  252. if (el.shape === 'circle') {
  253. body = '<ellipse cx="' + box.w / 2 + '" cy="' + box.h / 2 + '" rx="' + box.w / 2 + '" ry="' + box.h / 2 + '" fill="' + fill + '"' + stroke + '/>';
  254. } else if (el.shape === 'triangle') {
  255. body = '<polygon points="' + (box.w / 2) + ',0 ' + box.w + ',' + box.h + ' 0,' + box.h + '" fill="' + fill + '"' + stroke + '/>';
  256. } else if (el.shape === 'line') {
  257. 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"/>';
  258. } else {
  259. body = '<rect width="' + box.w + '" height="' + box.h + '" rx="' + (el.radius || 0) + '" fill="' + fill + '"' + stroke + '/>';
  260. }
  261. }
  262. const hit = '<rect width="' + box.w + '" height="' + box.h + '" fill="transparent" stroke="none"/>';
  263. 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>';
  264. }
  265. function defsForBag(product, uidv) {
  266. const base = product.mockColor;
  267. return '<defs>' +
  268. '<linearGradient id="' + uidv + '-body" x1="0" y1="0" x2="1" y2="0">' +
  269. '<stop offset="0" stop-color="' + shade(base, 0.07) + '"/>' +
  270. '<stop offset="0.12" stop-color="' + shade(base, 0.02) + '"/>' +
  271. '<stop offset="0.78" stop-color="' + base + '"/>' +
  272. '<stop offset="1" stop-color="' + shade(base, -0.10) + '"/>' +
  273. '</linearGradient>' +
  274. '<linearGradient id="' + uidv + '-seal" x1="0" y1="0" x2="0" y2="1">' +
  275. '<stop offset="0" stop-color="' + shade(base, 0.1) + '"/>' +
  276. '<stop offset="1" stop-color="' + shade(base, -0.14) + '"/>' +
  277. '</linearGradient>' +
  278. '<linearGradient id="' + uidv + '-hl" x1="0" y1="0" x2="1" y2="0">' +
  279. '<stop offset="0" stop-color="#fff" stop-opacity="0.32"/>' +
  280. '<stop offset="0.5" stop-color="#fff" stop-opacity="0.06"/>' +
  281. '<stop offset="1" stop-color="#fff" stop-opacity="0"/>' +
  282. '</linearGradient>' +
  283. '<linearGradient id="' + uidv + '-shade" x1="0" y1="0" x2="1" y2="0">' +
  284. '<stop offset="0" stop-color="#000" stop-opacity="0"/>' +
  285. '<stop offset="0.78" stop-color="#000" stop-opacity="0"/>' +
  286. '<stop offset="1" stop-color="#000" stop-opacity="0.22"/>' +
  287. '</linearGradient>' +
  288. '<linearGradient id="' + uidv + '-top" x1="0" y1="0" x2="0" y2="1">' +
  289. '<stop offset="0" stop-color="#fff" stop-opacity="0.2"/>' +
  290. '<stop offset="0.35" stop-color="#fff" stop-opacity="0"/>' +
  291. '</linearGradient>' +
  292. '<linearGradient id="' + uidv + '-bottom" x1="0" y1="0" x2="0" y2="1">' +
  293. '<stop offset="0" stop-color="#000" stop-opacity="0"/>' +
  294. '<stop offset="1" stop-color="#000" stop-opacity="0.16"/>' +
  295. '</linearGradient>' +
  296. '<pattern id="' + uidv + '-rib" width="3" height="12" patternUnits="userSpaceOnUse">' +
  297. '<rect x="0" y="0" width="0.7" height="12" fill="#000" fill-opacity="0.08"/>' +
  298. '</pattern>' +
  299. '<pattern id="' + uidv + '-fib" width="7" height="7" patternUnits="userSpaceOnUse">' +
  300. '<path d="M0 7L7 0" stroke="#000" stroke-opacity="0.05" stroke-width="0.25"/>' +
  301. '<path d="M0 2L2 0" stroke="#fff" stroke-opacity="0.12" stroke-width="0.25"/>' +
  302. '</pattern>' +
  303. '<filter id="' + uidv + '-mono" color-interpolation-filters="sRGB"><feColorMatrix type="saturate" values="0"/></filter>' +
  304. '<filter id="' + uidv + '-blur" x="-30%" y="-30%" width="160%" height="160%"><feGaussianBlur stdDeviation="5"/></filter>' +
  305. '<clipPath id="' + uidv + '-bag"><rect x="' + (-product.pad.l) + '" y="' + (-product.pad.t) + '" width="' + 0 + '" height="0"/></clipPath>' +
  306. '</defs>';
  307. }
  308. function effectDefs(u) {
  309. return '<radialGradient id="' + u + '-spot" cx="50%" cy="50%" r="50%">' +
  310. '<stop offset="0" stop-color="#fff" stop-opacity="0.8"/>' +
  311. '<stop offset="0.55" stop-color="#fff" stop-opacity="0.18"/>' +
  312. '<stop offset="1" stop-color="#fff" stop-opacity="0"/>' +
  313. '</radialGradient>' +
  314. '<linearGradient id="' + u + '-foil" x1="0" y1="0" x2="1" y2="1">' +
  315. '<stop offset="0" stop-color="#f7e7b0"/>' +
  316. '<stop offset="0.22" stop-color="#c9a44c"/>' +
  317. '<stop offset="0.44" stop-color="#fff4cf"/>' +
  318. '<stop offset="0.66" stop-color="#b98b34"/>' +
  319. '<stop offset="1" stop-color="#f2d789"/>' +
  320. '</linearGradient>' +
  321. '<pattern id="' + u + '-speck" width="4.5" height="4.5" patternUnits="userSpaceOnUse">' +
  322. '<circle cx="1" cy="1" r="0.28" fill="#1e3a2c" fill-opacity="0.28"/>' +
  323. '<circle cx="3.2" cy="3.3" r="0.22" fill="#7a9a55" fill-opacity="0.4"/>' +
  324. '</pattern>' +
  325. '<pattern id="' + u + '-soft" width="6" height="6" patternUnits="userSpaceOnUse">' +
  326. '<path d="M0 6L6 0" stroke="#fff" stroke-opacity="0.1" stroke-width="0.35"/>' +
  327. '<path d="M0 2L2 0" stroke="#000" stroke-opacity="0.04" stroke-width="0.3"/>' +
  328. '</pattern>';
  329. }
  330. function effectLayer(u, mat, fin, shape, W, H, clipId) {
  331. let s = '';
  332. if (mat === 'gloss') {
  333. 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"/>' +
  334. '<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) + '"/>';
  335. } else if (mat === 'kraft') {
  336. s += '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="url(#' + u + '-fib)" opacity="0.9"/>';
  337. } else if (mat === 'compost') {
  338. s += '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="#6c9a4f" opacity="0.1"/>' +
  339. '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="url(#' + u + '-speck)" opacity="0.75"/>';
  340. } else {
  341. s += '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="#fff" opacity="0.025"/>';
  342. }
  343. if (fin === 'uv') {
  344. 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"/>' +
  345. '<ellipse cx="' + (W * 0.7) + '" cy="' + (H * 0.55) + '" rx="' + (W * 0.2) + '" ry="' + (H * 0.07) + '" fill="url(#' + u + '-spot)" opacity="0.45"/>' +
  346. '<ellipse cx="' + (W * 0.42) + '" cy="' + (H * 0.8) + '" rx="' + (W * 0.16) + '" ry="' + (H * 0.06) + '" fill="url(#' + u + '-spot)" opacity="0.4"/>';
  347. } else if (fin === 'foil') {
  348. s += '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="url(#' + u + '-foil)" opacity="0.16"/>' +
  349. '<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) + '"/>';
  350. } else if (fin === 'soft') {
  351. s += '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="#fff1dc" opacity="0.1"/>' +
  352. '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="url(#' + u + '-soft)" opacity="0.4"/>';
  353. }
  354. return s ? '<g clip-path="url(#' + clipId + ')" pointer-events="none" data-fx="' + mat + ':' + fin + '">' + s + '</g>' : '';
  355. }
  356. function bagShape(product, W, H) {
  357. const p = product.pad;
  358. 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 };
  359. }
  360. function overlayUI(opts, product, sideDesign, ctx) {
  361. const W = opts.size.w;
  362. const H = opts.size.h;
  363. const p = product.pad;
  364. const scale = opts.pxPerMm || 3;
  365. const hs = 5.2 / scale;
  366. const sw = 1 / scale;
  367. let s = '';
  368. if (opts.showGuides !== false) {
  369. s += '<rect x="0" y="0" width="' + W + '" height="' + H + '" fill="none" data-uistroke="1" stroke-width="' + (1.1 * sw) + '" stroke-dasharray="' + (4 * sw) + ' ' + (3 * sw) + '" opacity="0.75" data-ui="1"/>';
  370. 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"/>';
  371. }
  372. if (opts.showDims !== false) {
  373. const lx = -p.l - 13;
  374. const by = H + p.b + 13;
  375. const fs = 4.2;
  376. s += '<g data-ui="1" stroke="#a49a8b" stroke-width="' + (0.7 * sw) + '" fill="none">' +
  377. '<path d="M' + lx + ' 0V' + H + 'M' + (lx - 2.2) + ' 0h4.4M' + (lx - 2.2) + ' ' + H + 'h4.4"/>' +
  378. '<path d="M' + (-p.l) + ' 0h-' + 6 + 'M' + (-p.l) + ' ' + H + 'h-' + 6 + '"/>' +
  379. '<path d="M0 ' + by + 'H' + W + 'M0 ' + (by - 2.2) + 'v4.4M' + W + ' ' + (by - 2.2) + 'v4.4"/>' +
  380. '<path d="M0 ' + H + 'v6M' + W + ' ' + H + 'v6"/>' +
  381. '</g>' +
  382. '<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>' +
  383. '<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>';
  384. // 侧折边宽度(左右各一)与顶部封边高度标注
  385. if (p.l > 2) {
  386. const gx = -p.l / 2;
  387. s += '<g data-ui="1" stroke="#a49a8b" stroke-width="' + (0.55 * sw) + '" fill="none">' +
  388. '<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>' +
  389. '<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>';
  390. const gx2 = W + p.r / 2;
  391. s += '<g data-ui="1" stroke="#a49a8b" stroke-width="' + (0.55 * sw) + '" fill="none">' +
  392. '<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>' +
  393. '<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>';
  394. }
  395. if (p.t > 2) {
  396. const gy = -p.t / 2;
  397. s += '<g data-ui="1" stroke="#a49a8b" stroke-width="' + (0.55 * sw) + '" fill="none">' +
  398. '<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>' +
  399. '<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>';
  400. }
  401. // 印刷面尺寸水印(面内底部居中)
  402. 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>';
  403. }
  404. if (opts.guides) {
  405. 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) + '" data-uistroke="1" stroke-width="' + (1 * sw) + '" stroke-dasharray="' + (3 * sw) + ' ' + (3 * sw) + '"/>';
  406. 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 + '" data-uistroke="1" stroke-width="' + (1 * sw) + '" stroke-dasharray="' + (3 * sw) + ' ' + (3 * sw) + '"/>';
  407. }
  408. const sel = opts.selectedId && sideDesign ? sideDesign.elements.find(function (e) { return e.id === opts.selectedId; }) : null;
  409. if (sel) {
  410. const box = elementBox(sel);
  411. const rot = sel.rotation || 0;
  412. const cx = box.w / 2;
  413. const cy = box.h / 2;
  414. let handles = '';
  415. const pts = [['nw', 0, 0], ['ne', box.w, 0], ['sw', 0, box.h], ['se', box.w, box.h]];
  416. if (sel.type === 'text') pts.push(['w', 0, box.h / 2], ['e', box.w, box.h / 2]);
  417. pts.forEach(function (h) {
  418. 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" data-uistroke="1" stroke-width="' + (1.2 * sw) + '" style="pointer-events:all"/>';
  419. });
  420. const ro = 26 / scale;
  421. handles += '<line x1="' + (box.w / 2) + '" y1="0" x2="' + (box.w / 2) + '" y2="' + (-ro) + '" data-uistroke="1" stroke-width="' + (1.2 * sw) + '"/>';
  422. handles += '<circle data-handle="rot" cx="' + (box.w / 2) + '" cy="' + (-ro) + '" r="' + (hs * 1.25) + '" data-uifill="1" stroke="#fff" stroke-width="' + (1.1 * sw) + '" style="pointer-events:all;cursor:grab"/>';
  423. const label = box.w.toFixed(0) + ' × ' + box.h.toFixed(0) + ' mm';
  424. const labelW = label.length * 5.4 / scale + 6 / scale;
  425. handles += '<g transform="translate(' + (box.w / 2 - labelW / 2) + ' ' + (box.h + 8 / scale) + ')" style="pointer-events:none">' +
  426. '<rect width="' + labelW + '" height="' + (16 / scale) + '" rx="' + (3 / scale) + '" fill="#17140f" opacity="0.86"/>' +
  427. '<text x="' + (labelW / 2) + '" y="' + (11 / scale) + '" text-anchor="middle" font-family="' + fontAttr('latin') + '" font-size="' + (8.5 / scale) + '" fill="#fff">' + label + '</text></g>';
  428. s += '<g data-ui="1" class="sel-overlay" transform="translate(' + sel.x + ' ' + sel.y + ') rotate(' + rot + ' ' + cx + ' ' + cy + ')">' +
  429. '<rect width="' + box.w + '" height="' + box.h + '" fill="none" data-uistroke="1" stroke-width="' + (1.4 * sw) + '"/>' + handles + '</g>';
  430. }
  431. return s;
  432. }
  433. function bagSVG(product, side, sideDesign, opts) {
  434. opts = opts || {};
  435. const size = opts.size || D.sizeOf(product, 'm');
  436. const W = size.w;
  437. const H = size.h;
  438. const p = product.pad;
  439. const u = uid('bag');
  440. const ctx = { uid: u };
  441. const tight = !!opts.tight;
  442. const card = !!opts.card;
  443. const mat = opts.material || 'matte';
  444. const fin = opts.finish || 'none';
  445. const extra = card ? 9 : 0;
  446. const vb = tight
  447. ? { x: -p.l, y: -p.t, w: W + p.l + p.r, h: H + p.t + p.b }
  448. : { 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) };
  449. const shape = bagShape(product, W, H);
  450. const base = product.mockColor;
  451. const design = sideDesign || { bg: null, elements: [] };
  452. const clipId = u + '-bagclip';
  453. const printId = u + '-printclip';
  454. const elements = (design.elements || []).map(function (el) { return renderElement(el, ctx); }).join('');
  455. let body = '';
  456. 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)"/>';
  457. body += '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '" fill="url(#' + u + '-body)"/>';
  458. 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)"/>';
  459. // 设计底色铺满整个袋身(含左右侧折边):真实包装侧折与正面同膜同底色
  460. if (design.bg) body += '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" fill="' + design.bg + '"/>';
  461. body += '<g clip-path="url(#' + clipId + ')"><g clip-path="url(#' + printId + ')">';
  462. body += elements;
  463. body += '</g></g>';
  464. body += '<g clip-path="url(#' + clipId + ')" style="pointer-events:none">' +
  465. '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" fill="url(#' + u + '-hl)"/>' +
  466. '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" fill="url(#' + u + '-shade)"/>' +
  467. '<rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + (p.t + H * 0.3) + '" fill="url(#' + u + '-top)"/>' +
  468. '<rect x="' + shape.x + '" y="' + (H - H * 0.25) + '" width="' + shape.w + '" height="' + (p.t + H * 0.3) + '" fill="url(#' + u + '-bottom)"/>' +
  469. '</g>';
  470. body += effectLayer(u, mat, fin, shape, W, H, clipId);
  471. const seal = p.t * 0.48;
  472. if (product.shape === 'mailer') {
  473. 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)"/>';
  474. 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"/>';
  475. 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"/>';
  476. } else if (product.shape === 'garment') {
  477. 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"/>';
  478. 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"/>';
  479. 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"/>';
  480. } else {
  481. 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)"/>';
  482. 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"/>';
  483. 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"/>';
  484. 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"/>';
  485. if (product.shape === 'kraft') {
  486. 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"/>';
  487. 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"/>';
  488. 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"/>';
  489. }
  490. }
  491. // 自封袋族顶部胶条(压敏双胶带 + 封口折线;对标行业袋型结构)
  492. if (product.shape === 'standup' || product.shape === 'flatbottom' || product.shape === 'coffee') {
  493. const stripH = Math.max(2.6, p.t * 0.15);
  494. const stripX = shape.x + shape.w * 0.07;
  495. const stripW = shape.w * 0.86;
  496. const y0 = shape.y + p.t * 0.18;
  497. if (side === 'front') {
  498. // 两条压敏胶带
  499. body += '<rect x="' + stripX + '" y="' + y0 + '" width="' + stripW + '" height="' + stripH + '" rx="' + (stripH / 2) + '" fill="url(#' + u + '-seal)"/>';
  500. body += '<rect x="' + stripX + '" y="' + (y0 + stripH * 1.9) + '" width="' + stripW + '" height="' + stripH + '" rx="' + (stripH / 2) + '" fill="url(#' + u + '-seal)"/>';
  501. body += '<rect x="' + stripX + '" y="' + y0 + '" width="' + stripW + '" height="' + stripH + '" rx="' + (stripH / 2) + '" fill="#ffffff" opacity="0.18"/>';
  502. // 封口折线
  503. 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"/>';
  504. } else {
  505. // 背面:Folded Flap 翻折盖虚线区
  506. const flapH = p.t * 0.92;
  507. 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"/>';
  508. 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>';
  509. }
  510. }
  511. if (product.shape === 'coffee' && side === 'front') {
  512. body += '<circle cx="' + (W - 15) + '" cy="' + 16 + '" r="4.6" fill="' + shade(base, -0.14) + '" stroke="' + shade(base, -0.4) + '" stroke-width="0.5"/>';
  513. body += '<circle cx="' + (W - 15) + '" cy="' + 16 + '" r="3" fill="none" stroke="' + shade(base, -0.45) + '" stroke-width="0.45" opacity="0.7"/>';
  514. 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"/>';
  515. }
  516. if (product.shape === 'standup') {
  517. 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"/>';
  518. 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"/>';
  519. } else if (product.shape === 'flatbottom') {
  520. 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"/>';
  521. 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"/>';
  522. 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"/>';
  523. }
  524. 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"/>';
  525. const defs = defsForBag(product, u).replace('<clipPath id="' + u + '-bag"><rect x="' + (-product.pad.l) + '" y="' + (-product.pad.t) + '" width="0" height="0"/></clipPath>',
  526. '<clipPath id="' + clipId + '"><rect x="' + shape.x + '" y="' + shape.y + '" width="' + shape.w + '" height="' + shape.h + '" rx="' + shape.rx + '"/></clipPath>' +
  527. '<clipPath id="' + printId + '"><rect x="-3" y="-3" width="' + (W + 6) + '" height="' + (H + 6) + '"/></clipPath>') + effectDefs(u);
  528. const ui = opts.ui ? overlayUI(opts, product, design, ctx) : '';
  529. const vbStr = vb.x + ' ' + vb.y + ' ' + vb.w + ' ' + vb.h;
  530. 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>';
  531. }
  532. function designSVG(design, W, H) {
  533. const u = uid('art');
  534. const ctx = { uid: u };
  535. const elements = (design.elements || []).map(function (el) { return renderElement(el, ctx); }).join('');
  536. return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ' + W + ' ' + H + '" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">' +
  537. '<defs><filter id="' + u + '-mono" color-interpolation-filters="sRGB"><feColorMatrix type="saturate" values="0"/></filter></defs>' +
  538. (design.bg ? '<rect width="' + W + '" height="' + H + '" fill="' + design.bg + '"/>' : '') +
  539. elements + '</svg>';
  540. }
  541. function designFromTemplate(templateId, W, H, side) {
  542. const t = D.templateById(templateId);
  543. if (!t) return { bg: null, elements: [] };
  544. const source = side === 'back' ? (t.back || []) : (t.front || []);
  545. const elements = source.map(function (item) {
  546. const e = Object.assign({}, item);
  547. e.id = uid('el');
  548. e.x = (item.x || 0) * W;
  549. e.y = (item.y || 0) * H;
  550. if (e.type === 'text') {
  551. e.w = (item.w || 1) * W;
  552. e.size = (item.size || 0.04) * W;
  553. e.spacing = item.spacing || 0;
  554. e.lineHeight = item.lineHeight || 1.25;
  555. e.strokeWidth = (item.sw || 0) * W;
  556. delete e.sw;
  557. } else if (e.type === 'shape') {
  558. e.w = (item.w || 0.2) * W;
  559. e.h = item.shape === 'line' ? 4 : (item.h == null ? 0.1 : item.h) * H;
  560. e.strokeWidth = (item.sw || 0) * W;
  561. e.radius = (item.radius || 0) * W;
  562. delete e.sw;
  563. } else if (e.type === 'sticker') {
  564. e.w = (item.w || 0.2) * W;
  565. e.h = e.w;
  566. } else if (e.type === 'badge') {
  567. e.w = (item.w || 0.3) * W;
  568. e.h = e.w;
  569. e.size = (item.size || 0.03) * W;
  570. } else if (e.type === 'barcode') {
  571. e.w = (item.w || 0.5) * W;
  572. e.h = (item.h || 0.12) * H;
  573. }
  574. e.rotation = 0;
  575. return e;
  576. });
  577. return { bg: t.bg, elements: elements };
  578. }
  579. function templatePreview(template, productOverride) {
  580. const product = D.productById(productOverride || template.product);
  581. const size = D.sizeOf(product, product.sizes[Math.floor((product.sizes.length - 1) / 2)].id);
  582. const design = designFromTemplate(template.id, size.w, size.h, 'front');
  583. return bagSVG(product, 'front', design, { size: size, card: true, tight: false });
  584. }
  585. function sampleDesign(product, seed) {
  586. const pool = D.templates.filter(function (t) { return t.product === product.id; });
  587. if (!pool.length) pool.push(D.templateById('citrus'));
  588. const t = pool[(seed || 0) % pool.length];
  589. const size = D.sizeOf(product, product.sizes[Math.floor((product.sizes.length - 1) / 2)].id);
  590. return { size: size, design: designFromTemplate(t.id, size.w, size.h, 'front'), template: t };
  591. }
  592. window.PL.render = {
  593. uid: uid,
  594. esc: esc,
  595. shade: shade,
  596. alpha: alpha,
  597. fontStack: fontStack,
  598. textLines: textLines,
  599. textHeight: textHeight,
  600. elementBox: elementBox,
  601. elementSVG: renderElement,
  602. bagSVG: bagSVG,
  603. designFromTemplate: designFromTemplate,
  604. designSVG: designSVG,
  605. templatePreview: templatePreview,
  606. sampleDesign: sampleDesign,
  607. barcodeBits: barcodeBits
  608. };
  609. })();