boxdieline.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /**
  2. * boxdieline.js — 纸盒刀版几何构建(共享)
  3. * 由 box.js 抽出:不依赖设计器 state,纯函数输入盒型/尺寸/材质/结构参数,
  4. * 输出 { panels, folds, size, minX, minY, bounds },供 box3d.js 折叠 3D 使用。
  5. * box.js 编辑器与 m.js 手机端 3D 预览共用同一份几何逻辑。
  6. */
  7. (function () {
  8. 'use strict';
  9. /* 材质(与 box.js MATERIALS 保持一致,仅几何所需字段) */
  10. const MATERIALS = [
  11. { id: 'card', name: '350g白卡(0.5mm)', thickness: 0.5, paper: '#fbf7ee', paperEdge: '#e6ddcc', edgeW: 1, matte: false, grain: '', grainOpacity: 0, flute: '#e2d8c5' },
  12. { id: 'grey', name: '灰板2mm', thickness: 2.0, paper: '#c9c1b2', paperEdge: '#8f877a', edgeW: 4, matte: true, grainOpacity: 0.5, grain: '', flute: '#b3aa9a' },
  13. { id: 'kraft', name: '牛皮纸(0.4mm)', thickness: 0.4, paper: '#d7b17c', paperEdge: '#a97c4e', edgeW: 2, matte: true, grainOpacity: 0.75, grain: '', flute: '#b98a56' },
  14. { id: 'corrugated', name: 'E楞瓦楞(1.5mm)', thickness: 1.5, paper: '#d3aa74', paperEdge: '#a97c4e', edgeW: 3, matte: true, grainOpacity: 0.85, grain: '', flute: '#a97c4e', edgePattern: true }
  15. ];
  16. /* 结构参数默认值(与 box.js state.smart 一致) */
  17. const DEFAULT_PARAMS = { bleed: 3, glue: 15, tuckH: 12, cornerR: 1.5, dustW: 14, lipW: 16, topFold: 22, holeR: 4 };
  18. function materialById(id) {
  19. return MATERIALS.find(function (m) { return m.id === id; }) || MATERIALS[0];
  20. }
  21. function rect(x, y, w, hh, face, kind) { return { x: x, y: y, w: w, h: hh, face: face, kind: kind || 'body' }; }
  22. function poly(points, face, kind) {
  23. return { poly: points, face: face, kind: kind || 'flap', x: Math.min.apply(null, points.map(function (p) { return p[0]; })), y: Math.min.apply(null, points.map(function (p) { return p[1]; })), w: 0, h: 0 };
  24. }
  25. function panelBounds(p) {
  26. if (!p.poly) return { x: p.x, y: p.y, w: p.w, h: p.h };
  27. const xs = p.poly.map(function (pt) { return pt[0]; });
  28. const ys = p.poly.map(function (pt) { return pt[1]; });
  29. const x = Math.min.apply(null, xs), y = Math.min.apply(null, ys);
  30. return { x: x, y: y, w: Math.max.apply(null, xs) - x, h: Math.max.apply(null, ys) - y };
  31. }
  32. const ACCESSORY = { glue: 1, flap: 1, dust: 1, tuck: 1, lip: 1 };
  33. function isBodyKind(k) { return !ACCESSORY[k]; }
  34. function panelEdges(p) {
  35. const pts = p.poly || [[p.x, p.y], [p.x + p.w, p.y], [p.x + p.w, p.y + p.h], [p.x, p.y + p.h]];
  36. const out = [];
  37. for (let i = 0; i < pts.length; i += 1) {
  38. const a = pts[i], b = pts[(i + 1) % pts.length];
  39. if (Math.abs(a[0] - b[0]) < 0.05) out.push({ axis: 'v', c: a[0], lo: Math.min(a[1], b[1]), hi: Math.max(a[1], b[1]) });
  40. else if (Math.abs(a[1] - b[1]) < 0.05) out.push({ axis: 'h', c: a[1], lo: Math.min(a[0], b[0]), hi: Math.max(a[0], b[0]) });
  41. }
  42. return out;
  43. }
  44. function sharedEdge(a, b) {
  45. let best = null;
  46. panelEdges(a).forEach(function (ea) {
  47. panelEdges(b).forEach(function (eb) {
  48. if (ea.axis !== eb.axis || Math.abs(ea.c - eb.c) > 0.6) return;
  49. const lo = Math.max(ea.lo, eb.lo), hi = Math.min(ea.hi, eb.hi), len = hi - lo;
  50. if (len <= 2) return;
  51. if (!best || len > best.len) best = { axis: ea.axis, c: ea.c, lo: lo, hi: hi, len: len };
  52. });
  53. });
  54. return best;
  55. }
  56. function canHinge(a, b) {
  57. const ka = a.kind || 'body', kb = b.kind || 'body';
  58. if (isBodyKind(ka) !== isBodyKind(kb)) return true;
  59. if (isBodyKind(ka) && isBodyKind(kb)) return true;
  60. return (ka === 'tuck' && kb === 'flap') || (ka === 'flap' && kb === 'tuck');
  61. }
  62. function autoFolds(panels) {
  63. const out = [];
  64. for (let i = 0; i < panels.length; i += 1) for (let j = i + 1; j < panels.length; j += 1) {
  65. if (!canHinge(panels[i], panels[j])) continue;
  66. const e = sharedEdge(panels[i], panels[j]);
  67. if (!e) continue;
  68. if (e.axis === 'v') out.push({ x1: e.c, y1: e.lo, x2: e.c, y2: e.hi });
  69. else out.push({ x1: e.lo, y1: e.c, x2: e.hi, y2: e.c });
  70. }
  71. return out;
  72. }
  73. function finishDieline(panels) {
  74. let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
  75. panels.forEach(function (p) {
  76. const b = panelBounds(p);
  77. minX = Math.min(minX, b.x); minY = Math.min(minY, b.y);
  78. maxX = Math.max(maxX, b.x + b.w); maxY = Math.max(maxY, b.y + b.h);
  79. });
  80. return { panels: panels, folds: autoFolds(panels), size: { w: maxX - minX, h: maxY - minY }, minX: minX, minY: minY, bounds: { w: maxX - minX, h: maxY - minY, minX: minX, minY: minY } };
  81. }
  82. function roundedRectPoly(x, y, w, hh, r) {
  83. r = Math.min(r, w / 2, hh / 2);
  84. if (r <= 0.05) return [[x, y], [x + w, y], [x + w, y + hh], [x, y + hh]];
  85. const pts = [];
  86. const arc = function (cx, cy, a0, a1) {
  87. for (let i = 0; i <= 6; i += 1) {
  88. const a = a0 + (a1 - a0) * i / 6;
  89. pts.push([cx + r * Math.cos(a), cy + r * Math.sin(a)]);
  90. }
  91. };
  92. arc(x + w - r, y + r, -Math.PI / 2, 0);
  93. arc(x + w - r, y + hh - r, 0, Math.PI / 2);
  94. arc(x + r, y + hh - r, Math.PI / 2, Math.PI);
  95. arc(x + r, y + r, Math.PI, Math.PI * 1.5);
  96. return pts;
  97. }
  98. function circlePoly(cx, cy, r) {
  99. const pts = [];
  100. for (let i = 0; i < 36; i += 1) {
  101. const a = i / 36 * Math.PI * 2;
  102. pts.push([cx + r * Math.cos(a), cy + r * Math.sin(a)]);
  103. }
  104. return pts;
  105. }
  106. /* 卡袋/封套等异形结构的盒号白名单(圆角卡盒) */
  107. const ROUND_CARD_BOXNOS = { '260040': 1, '270020': 1, '261002': 1 };
  108. function buildBase(type, w, d, hh, params, mat) {
  109. const t = mat.thickness;
  110. const W = w + t * 2, D2 = d + t * 2, H = hh + t * 2;
  111. const P = params || DEFAULT_PARAMS;
  112. const panels = [];
  113. if (type === 'card') {
  114. const isRound = !!ROUND_CARD_BOXNOS[P.boxNo || ''];
  115. const hole = P.holeR || 0;
  116. const p1 = isRound
  117. ? poly(circlePoly(W / 2, H / 2, Math.min(W, H) / 2), 'front', 'body')
  118. : poly(roundedRectPoly(0, 0, W, H, P.cornerR || 0), 'front', 'body');
  119. if (hole && !isRound) {
  120. p1.holes = [{ x: W / 2, y: hole + 4, r: hole }];
  121. p1.holeH = hole * 2 + 8;
  122. }
  123. panels.push(p1);
  124. const back = isRound
  125. ? poly(circlePoly(W / 2, H / 2, Math.min(W, H) / 2), 'back', 'body')
  126. : poly(roundedRectPoly(W + 10, 0, W, H, P.cornerR || 0), 'back', 'body');
  127. panels.push(back);
  128. return finishDieline(panels);
  129. }
  130. if (type === 'sleeve') {
  131. const glue = P.glue;
  132. let x = 0;
  133. panels.push(rect(x, 0, glue, H, null, 'glue')); x += glue;
  134. panels.push(rect(x, 0, W, H, 'front', 'body')); x += W;
  135. panels.push(rect(x, 0, D2, H, null, 'flap')); x += D2;
  136. panels.push(rect(x, 0, W, H, 'back', 'body')); x += W;
  137. panels.push(rect(x, 0, D2, H, null, 'flap'));
  138. return finishDieline(panels);
  139. }
  140. if (type === 'bag') {
  141. const tf = P.topFold || 20;
  142. const holeR = P.holeR || 4;
  143. const gus = D2 / 2;
  144. let x = 0;
  145. panels.push(rect(x, 0, W, H, 'front', 'body'));
  146. panels.push(rect(x, -tf, W, tf, null, 'flap'));
  147. const fP = panels[0], fT = panels[1];
  148. fP.holes = []; fT.holes = [{ x: x + W * 0.22, y: -tf + tf / 2, r: holeR }, { x: x + W * 0.78, y: -tf + tf / 2, r: holeR }];
  149. x += W;
  150. panels.push(rect(x, 0, gus, H, null, 'body')); x += gus;
  151. panels.push(rect(x, 0, gus, H, null, 'body')); x += gus;
  152. panels.push(rect(x, 0, W, H, 'back', 'body'));
  153. panels.push(rect(x, -tf, W, tf, null, 'flap'));
  154. panels[panels.length - 1].holes = [{ x: x + W * 0.22, y: -tf + tf / 2, r: holeR }, { x: x + W * 0.78, y: -tf + tf / 2, r: holeR }];
  155. x += W;
  156. panels.push(rect(x, 0, gus, H, null, 'body')); x += gus;
  157. panels.push(rect(x, 0, gus, H, null, 'body'));
  158. panels.push(rect(0, H, W, D2, 'bottom', 'body'));
  159. return finishDieline(panels);
  160. }
  161. // 管式盒族
  162. const glue = P.glue;
  163. const ch = Math.max(3, Math.min(8, D2 * 0.06)) * (1 - Math.min(1, (P.cornerR || 0) / 8) * 0.5);
  164. const half = D2 / 2;
  165. const xF = glue, xR = xF + W, xB = xR + D2, xL = xB + W;
  166. panels.push(rect(0, 0, glue, H, null, 'glue'));
  167. panels.push(rect(xF, 0, W, H, 'front', 'body'));
  168. panels.push(rect(xR, 0, D2, H, 'right', 'body'));
  169. panels.push(rect(xB, 0, W, H, 'back', 'body'));
  170. panels.push(rect(xL, 0, D2, H, 'left', 'body'));
  171. const big = function (x, y, face, dir) {
  172. const yy = dir * (half + 2);
  173. return poly([[x, y], [x + ch, y + yy], [x + W - ch, y + yy], [x + W, y]], face, 'flap');
  174. };
  175. const dust = function (x, y, dir) {
  176. const yy = dir * (half - 2);
  177. return poly([[x, y], [x + ch, y + yy], [x + D2 - ch, y + yy], [x + D2, y]], null, 'dust');
  178. };
  179. const tuckH = P.tuckH;
  180. if (type === 'rsc') {
  181. [[xF, W, 'top', true], [xR, D2, null, false], [xB, W, null, false], [xL, D2, null, false]].forEach(function (f) {
  182. const top = rect(f[0], -half, f[1], half, f[2], 'flap');
  183. if (!f[2]) top.paint = 'top';
  184. panels.push(top);
  185. const bottom = rect(f[0], H, f[1], half, f[3] ? 'bottom' : null, 'flap');
  186. if (!f[3]) bottom.paint = 'bottom';
  187. panels.push(bottom);
  188. });
  189. } else if (type === 'carton') {
  190. panels.push(big(xF, 0, 'top', -1));
  191. const topBack = big(xB, 0, null, -1);
  192. topBack.paint = 'top';
  193. panels.push(topBack);
  194. panels.push(dust(xR, 0, -1));
  195. panels.push(dust(xL, 0, -1));
  196. panels.push(big(xF, H, 'bottom', 1));
  197. const bottomBack = big(xB, H, null, 1);
  198. bottomBack.paint = 'bottom';
  199. panels.push(bottomBack);
  200. panels.push(dust(xR, H, 1));
  201. panels.push(dust(xL, H, 1));
  202. } else if (type === 'mailer') {
  203. const dustW = P.dustW;
  204. panels.push(poly([[xB, 0], [xB + ch, -(D2 + 2)], [xB + W - ch, -(D2 + 2)], [xB + W, 0]], 'top', 'flap'));
  205. const mt = rect(xB + dustW, -(D2 + 2) - tuckH, W - dustW * 2, tuckH, null, 'tuck');
  206. if ((P.holeR || 0) > 1 && tuckH > (P.holeR || 4) * 2 + 1) {
  207. mt.holes = [{ x: xB + dustW + (W - dustW * 2) / 2, y: -(D2 + 2) - tuckH / 2, r: P.holeR }];
  208. }
  209. panels.push(mt);
  210. panels.push(rect(xB - dustW, -(D2 + 2), dustW, D2, null, 'dust'));
  211. panels.push(rect(xB + W, -(D2 + 2), dustW, D2, null, 'dust'));
  212. panels.push(poly([[xF, H], [xF + ch, H + D2 + 2], [xF + W - ch, H + D2 + 2], [xF + W, H]], 'bottom', 'flap'));
  213. const mb = rect(xF + dustW, H + D2 + 2, W - dustW * 2, tuckH, null, 'tuck');
  214. if ((P.holeR || 0) > 1 && tuckH > (P.holeR || 4) * 2 + 1) {
  215. mb.holes = [{ x: xF + dustW + (W - dustW * 2) / 2, y: H + D2 + 2 + tuckH / 2, r: P.holeR }];
  216. }
  217. panels.push(mb);
  218. panels.push(rect(xF - dustW, H + D2 + 2, dustW, D2, null, 'dust'));
  219. panels.push(rect(xF + W, H + D2 + 2, dustW, D2, null, 'dust'));
  220. } else {
  221. // tuck / straight / autobottom:上盖挂后板
  222. const lidD = D2 + 2;
  223. panels.push(poly([[xB, 0], [xB + ch, -lidD], [xB + W - ch, -lidD], [xB + W, 0]], 'top', 'flap'));
  224. const tuckTop = rect(xB + 18, -lidD - tuckH, W - 36, tuckH, null, 'tuck');
  225. if ((P.holeR || 0) > 1 && tuckH > (P.holeR || 4) * 2 + 1) {
  226. tuckTop.holes = [{ x: xB + W / 2, y: -lidD - tuckH / 2, r: P.holeR }];
  227. }
  228. panels.push(tuckTop);
  229. panels.push(dust(xR, 0, -1));
  230. panels.push(dust(xL, 0, -1));
  231. if (type === 'straight') {
  232. panels.push(poly([[xB, H], [xB + ch, H + lidD], [xB + W - ch, H + lidD], [xB + W, H]], 'bottom', 'flap'));
  233. const tuckBot = rect(xB + 18, H + lidD, W - 36, tuckH, null, 'tuck');
  234. if ((P.holeR || 0) > 1 && tuckH > (P.holeR || 4) * 2 + 1) {
  235. tuckBot.holes = [{ x: xB + W / 2, y: H + lidD + tuckH / 2, r: P.holeR }];
  236. }
  237. panels.push(tuckBot);
  238. panels.push(dust(xR, H, 1));
  239. panels.push(dust(xL, H, 1));
  240. } else if (type === 'autobottom') {
  241. const d1 = half + 5, d2 = half + 2, c2 = ch * 0.6;
  242. panels.push(poly([
  243. [xF, H], [xF + c2, H + d1], [xF + W * 0.32, H + d1], [xF + W * 0.32, H + d1 - 4],
  244. [xF + W * 0.68, H + d1 - 4], [xF + W * 0.68, H + d1], [xF + W - c2, H + d1], [xF + W, H]
  245. ], 'bottom', 'flap'));
  246. const bottomBack = poly([[xB, H], [xB + c2, H + d2], [xB + W - c2, H + d2], [xB + W, H]], null, 'flap');
  247. bottomBack.paint = 'bottom';
  248. panels.push(bottomBack);
  249. const bottomR = poly([[xR, H], [xR + D2, H], [xR + D2 / 2, H + half + 3]], null, 'dust');
  250. bottomR.paint = 'bottom';
  251. panels.push(bottomR);
  252. const bottomL = poly([[xL, H], [xL + D2, H], [xL + D2 / 2, H + half + 3]], null, 'dust');
  253. bottomL.paint = 'bottom';
  254. panels.push(bottomL);
  255. } else {
  256. // tuck 反向插锁:下盖挂前板
  257. panels.push(poly([[xF, H], [xF + ch, H + lidD], [xF + W - ch, H + lidD], [xF + W, H]], 'bottom', 'flap'));
  258. panels.push(rect(xF + 18, H + lidD, W - 36, tuckH, null, 'tuck'));
  259. panels.push(dust(xR, H, 1));
  260. panels.push(dust(xL, H, 1));
  261. }
  262. }
  263. return finishDieline(panels);
  264. }
  265. function buildMailbox(type, w, d, hh, params, mat) {
  266. const t = mat.thickness;
  267. const W = w + t * 2, D2 = d + t * 2, H = hh + t * 2;
  268. const P = params || DEFAULT_PARAMS;
  269. if (type === 'mailbox') {
  270. const lipW = P.lipW;
  271. const xRw = 0, xB = D2, xSpine = xB + W, xF = xSpine + D2;
  272. const panels = [
  273. rect(xRw, 0, D2, H, 'right', 'body'),
  274. rect(xB, 0, W, H, 'back', 'body'),
  275. rect(xSpine, 0, D2, H, 'left', 'body'),
  276. rect(xF, 0, W, H, 'front', 'body'),
  277. rect(xF + W, 0, lipW, H, null, 'lip'),
  278. rect(xB, -D2, W, D2, 'top', 'body'),
  279. rect(xB, H, W, D2, 'bottom', 'body')
  280. ];
  281. return finishDieline(panels);
  282. }
  283. if (type === 'drawer') {
  284. const glue = P.glue;
  285. const xF = glue, xR = xF + W, xB = xR + D2, xL = xB + W;
  286. const panels = [
  287. rect(0, 0, glue, H, null, 'glue'),
  288. rect(xF, 0, W, H, 'front', 'body'),
  289. rect(xR, 0, D2, H, 'right', 'body'),
  290. rect(xB, 0, W, H, 'back', 'body'),
  291. rect(xL, 0, D2, H, 'left', 'body')
  292. ];
  293. const W2 = Math.max(10, W - 2), D3 = Math.max(10, D2 - 2), TH = Math.max(10, H - 8);
  294. const ty = H + 26, tx0 = 24, txF = tx0 + glue, txR = txF + W2, txB = txR + D3, txL = txB + W2;
  295. panels.push(rect(tx0, ty, glue, TH, null, 'glue'));
  296. panels.push(rect(txF, ty, W2, TH, 'front', 'tray'));
  297. panels.push(rect(txR, ty, D3, TH, null, 'tray'));
  298. panels.push(rect(txB, ty, W2, TH, null, 'tray'));
  299. panels.push(rect(txL, ty, D3, TH, null, 'tray'));
  300. panels.push(rect(txF, ty + TH, W2, D3, null, 'bottom'));
  301. return finishDieline(panels);
  302. }
  303. return null;
  304. }
  305. /**
  306. * @param {string} type 盒型 id(carton/rsc/tuck/straight/autobottom/mailer/mailbox/drawer/card/sleeve/bag)
  307. * @param {number} w 内宽 mm
  308. * @param {number} d 内深 mm
  309. * @param {number} hh 内高 mm
  310. * @param {object} [params] 结构参数(glue/tuckH/cornerR/dustW/lipW/topFold/holeR/boxNo)
  311. * @param {string|object} [material] 材质 id 或材质对象(默认白卡)
  312. */
  313. function build(type, w, d, hh, params, material) {
  314. const mat = typeof material === 'object' && material
  315. ? material
  316. : materialById(typeof material === 'string' ? material : 'card');
  317. const P = Object.assign({}, DEFAULT_PARAMS, params || {});
  318. return buildMailbox(type, w, d, hh, P, mat) || buildBase(type, w, d, hh, P, mat);
  319. }
  320. window.PL = window.PL || {};
  321. window.PL.boxDieline = {
  322. build: build,
  323. MATERIALS: MATERIALS,
  324. DEFAULT_PARAMS: DEFAULT_PARAMS,
  325. materialById: materialById,
  326. finishDieline: finishDieline,
  327. panelBounds: panelBounds
  328. };
  329. })();