render.js 38 KB

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