| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- /**
- * boxdieline.js — 纸盒刀版几何构建(共享)
- * 由 box.js 抽出:不依赖设计器 state,纯函数输入盒型/尺寸/材质/结构参数,
- * 输出 { panels, folds, size, minX, minY, bounds },供 box3d.js 折叠 3D 使用。
- * box.js 编辑器与 m.js 手机端 3D 预览共用同一份几何逻辑。
- */
- (function () {
- 'use strict';
- /* 材质(与 box.js MATERIALS 保持一致,仅几何所需字段) */
- const MATERIALS = [
- { id: 'card', name: '350g白卡(0.5mm)', thickness: 0.5, paper: '#fbf7ee', paperEdge: '#e6ddcc', edgeW: 1, matte: false, grain: '', grainOpacity: 0, flute: '#e2d8c5' },
- { id: 'grey', name: '灰板2mm', thickness: 2.0, paper: '#c9c1b2', paperEdge: '#8f877a', edgeW: 4, matte: true, grainOpacity: 0.5, grain: '', flute: '#b3aa9a' },
- { id: 'kraft', name: '牛皮纸(0.4mm)', thickness: 0.4, paper: '#d7b17c', paperEdge: '#a97c4e', edgeW: 2, matte: true, grainOpacity: 0.75, grain: '', flute: '#b98a56' },
- { 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 }
- ];
- /* 结构参数默认值(与 box.js state.smart 一致) */
- const DEFAULT_PARAMS = { bleed: 3, glue: 15, tuckH: 12, cornerR: 1.5, dustW: 14, lipW: 16, topFold: 22, holeR: 4 };
- function materialById(id) {
- return MATERIALS.find(function (m) { return m.id === id; }) || MATERIALS[0];
- }
- function rect(x, y, w, hh, face, kind) { return { x: x, y: y, w: w, h: hh, face: face, kind: kind || 'body' }; }
- function poly(points, face, kind) {
- 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 };
- }
- function panelBounds(p) {
- if (!p.poly) return { x: p.x, y: p.y, w: p.w, h: p.h };
- const xs = p.poly.map(function (pt) { return pt[0]; });
- const ys = p.poly.map(function (pt) { return pt[1]; });
- const x = Math.min.apply(null, xs), y = Math.min.apply(null, ys);
- return { x: x, y: y, w: Math.max.apply(null, xs) - x, h: Math.max.apply(null, ys) - y };
- }
- const ACCESSORY = { glue: 1, flap: 1, dust: 1, tuck: 1, lip: 1 };
- function isBodyKind(k) { return !ACCESSORY[k]; }
- function panelEdges(p) {
- 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]];
- const out = [];
- for (let i = 0; i < pts.length; i += 1) {
- const a = pts[i], b = pts[(i + 1) % pts.length];
- 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]) });
- 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]) });
- }
- return out;
- }
- function sharedEdge(a, b) {
- let best = null;
- panelEdges(a).forEach(function (ea) {
- panelEdges(b).forEach(function (eb) {
- if (ea.axis !== eb.axis || Math.abs(ea.c - eb.c) > 0.6) return;
- const lo = Math.max(ea.lo, eb.lo), hi = Math.min(ea.hi, eb.hi), len = hi - lo;
- if (len <= 2) return;
- if (!best || len > best.len) best = { axis: ea.axis, c: ea.c, lo: lo, hi: hi, len: len };
- });
- });
- return best;
- }
- function canHinge(a, b) {
- const ka = a.kind || 'body', kb = b.kind || 'body';
- if (isBodyKind(ka) !== isBodyKind(kb)) return true;
- if (isBodyKind(ka) && isBodyKind(kb)) return true;
- return (ka === 'tuck' && kb === 'flap') || (ka === 'flap' && kb === 'tuck');
- }
- function autoFolds(panels) {
- const out = [];
- for (let i = 0; i < panels.length; i += 1) for (let j = i + 1; j < panels.length; j += 1) {
- if (!canHinge(panels[i], panels[j])) continue;
- const e = sharedEdge(panels[i], panels[j]);
- if (!e) continue;
- if (e.axis === 'v') out.push({ x1: e.c, y1: e.lo, x2: e.c, y2: e.hi });
- else out.push({ x1: e.lo, y1: e.c, x2: e.hi, y2: e.c });
- }
- return out;
- }
- function finishDieline(panels) {
- let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
- panels.forEach(function (p) {
- const b = panelBounds(p);
- minX = Math.min(minX, b.x); minY = Math.min(minY, b.y);
- maxX = Math.max(maxX, b.x + b.w); maxY = Math.max(maxY, b.y + b.h);
- });
- 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 } };
- }
- function roundedRectPoly(x, y, w, hh, r) {
- r = Math.min(r, w / 2, hh / 2);
- if (r <= 0.05) return [[x, y], [x + w, y], [x + w, y + hh], [x, y + hh]];
- const pts = [];
- const arc = function (cx, cy, a0, a1) {
- for (let i = 0; i <= 6; i += 1) {
- const a = a0 + (a1 - a0) * i / 6;
- pts.push([cx + r * Math.cos(a), cy + r * Math.sin(a)]);
- }
- };
- arc(x + w - r, y + r, -Math.PI / 2, 0);
- arc(x + w - r, y + hh - r, 0, Math.PI / 2);
- arc(x + r, y + hh - r, Math.PI / 2, Math.PI);
- arc(x + r, y + r, Math.PI, Math.PI * 1.5);
- return pts;
- }
- function circlePoly(cx, cy, r) {
- const pts = [];
- for (let i = 0; i < 36; i += 1) {
- const a = i / 36 * Math.PI * 2;
- pts.push([cx + r * Math.cos(a), cy + r * Math.sin(a)]);
- }
- return pts;
- }
- /* 卡袋/封套等异形结构的盒号白名单(圆角卡盒) */
- const ROUND_CARD_BOXNOS = { '260040': 1, '270020': 1, '261002': 1 };
- function buildBase(type, w, d, hh, params, mat) {
- const t = mat.thickness;
- const W = w + t * 2, D2 = d + t * 2, H = hh + t * 2;
- const P = params || DEFAULT_PARAMS;
- const panels = [];
- if (type === 'card') {
- const isRound = !!ROUND_CARD_BOXNOS[P.boxNo || ''];
- const hole = P.holeR || 0;
- const p1 = isRound
- ? poly(circlePoly(W / 2, H / 2, Math.min(W, H) / 2), 'front', 'body')
- : poly(roundedRectPoly(0, 0, W, H, P.cornerR || 0), 'front', 'body');
- if (hole && !isRound) {
- p1.holes = [{ x: W / 2, y: hole + 4, r: hole }];
- p1.holeH = hole * 2 + 8;
- }
- panels.push(p1);
- const back = isRound
- ? poly(circlePoly(W / 2, H / 2, Math.min(W, H) / 2), 'back', 'body')
- : poly(roundedRectPoly(W + 10, 0, W, H, P.cornerR || 0), 'back', 'body');
- panels.push(back);
- return finishDieline(panels);
- }
- if (type === 'sleeve') {
- const glue = P.glue;
- let x = 0;
- panels.push(rect(x, 0, glue, H, null, 'glue')); x += glue;
- panels.push(rect(x, 0, W, H, 'front', 'body')); x += W;
- panels.push(rect(x, 0, D2, H, null, 'flap')); x += D2;
- panels.push(rect(x, 0, W, H, 'back', 'body')); x += W;
- panels.push(rect(x, 0, D2, H, null, 'flap'));
- return finishDieline(panels);
- }
- if (type === 'bag') {
- const tf = P.topFold || 20;
- const holeR = P.holeR || 4;
- const gus = D2 / 2;
- let x = 0;
- panels.push(rect(x, 0, W, H, 'front', 'body'));
- panels.push(rect(x, -tf, W, tf, null, 'flap'));
- const fP = panels[0], fT = panels[1];
- 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 }];
- x += W;
- panels.push(rect(x, 0, gus, H, null, 'body')); x += gus;
- panels.push(rect(x, 0, gus, H, null, 'body')); x += gus;
- panels.push(rect(x, 0, W, H, 'back', 'body'));
- panels.push(rect(x, -tf, W, tf, null, 'flap'));
- 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 }];
- x += W;
- panels.push(rect(x, 0, gus, H, null, 'body')); x += gus;
- panels.push(rect(x, 0, gus, H, null, 'body'));
- panels.push(rect(0, H, W, D2, 'bottom', 'body'));
- return finishDieline(panels);
- }
- // 管式盒族
- const glue = P.glue;
- const ch = Math.max(3, Math.min(8, D2 * 0.06)) * (1 - Math.min(1, (P.cornerR || 0) / 8) * 0.5);
- const half = D2 / 2;
- const xF = glue, xR = xF + W, xB = xR + D2, xL = xB + W;
- panels.push(rect(0, 0, glue, H, null, 'glue'));
- panels.push(rect(xF, 0, W, H, 'front', 'body'));
- panels.push(rect(xR, 0, D2, H, 'right', 'body'));
- panels.push(rect(xB, 0, W, H, 'back', 'body'));
- panels.push(rect(xL, 0, D2, H, 'left', 'body'));
- const big = function (x, y, face, dir) {
- const yy = dir * (half + 2);
- return poly([[x, y], [x + ch, y + yy], [x + W - ch, y + yy], [x + W, y]], face, 'flap');
- };
- const dust = function (x, y, dir) {
- const yy = dir * (half - 2);
- return poly([[x, y], [x + ch, y + yy], [x + D2 - ch, y + yy], [x + D2, y]], null, 'dust');
- };
- const tuckH = P.tuckH;
- if (type === 'rsc') {
- [[xF, W, 'top', true], [xR, D2, null, false], [xB, W, null, false], [xL, D2, null, false]].forEach(function (f) {
- const top = rect(f[0], -half, f[1], half, f[2], 'flap');
- if (!f[2]) top.paint = 'top';
- panels.push(top);
- const bottom = rect(f[0], H, f[1], half, f[3] ? 'bottom' : null, 'flap');
- if (!f[3]) bottom.paint = 'bottom';
- panels.push(bottom);
- });
- } else if (type === 'carton') {
- panels.push(big(xF, 0, 'top', -1));
- const topBack = big(xB, 0, null, -1);
- topBack.paint = 'top';
- panels.push(topBack);
- panels.push(dust(xR, 0, -1));
- panels.push(dust(xL, 0, -1));
- panels.push(big(xF, H, 'bottom', 1));
- const bottomBack = big(xB, H, null, 1);
- bottomBack.paint = 'bottom';
- panels.push(bottomBack);
- panels.push(dust(xR, H, 1));
- panels.push(dust(xL, H, 1));
- } else if (type === 'mailer') {
- const dustW = P.dustW;
- panels.push(poly([[xB, 0], [xB + ch, -(D2 + 2)], [xB + W - ch, -(D2 + 2)], [xB + W, 0]], 'top', 'flap'));
- const mt = rect(xB + dustW, -(D2 + 2) - tuckH, W - dustW * 2, tuckH, null, 'tuck');
- if ((P.holeR || 0) > 1 && tuckH > (P.holeR || 4) * 2 + 1) {
- mt.holes = [{ x: xB + dustW + (W - dustW * 2) / 2, y: -(D2 + 2) - tuckH / 2, r: P.holeR }];
- }
- panels.push(mt);
- panels.push(rect(xB - dustW, -(D2 + 2), dustW, D2, null, 'dust'));
- panels.push(rect(xB + W, -(D2 + 2), dustW, D2, null, 'dust'));
- panels.push(poly([[xF, H], [xF + ch, H + D2 + 2], [xF + W - ch, H + D2 + 2], [xF + W, H]], 'bottom', 'flap'));
- const mb = rect(xF + dustW, H + D2 + 2, W - dustW * 2, tuckH, null, 'tuck');
- if ((P.holeR || 0) > 1 && tuckH > (P.holeR || 4) * 2 + 1) {
- mb.holes = [{ x: xF + dustW + (W - dustW * 2) / 2, y: H + D2 + 2 + tuckH / 2, r: P.holeR }];
- }
- panels.push(mb);
- panels.push(rect(xF - dustW, H + D2 + 2, dustW, D2, null, 'dust'));
- panels.push(rect(xF + W, H + D2 + 2, dustW, D2, null, 'dust'));
- } else {
- // tuck / straight / autobottom:上盖挂后板
- const lidD = D2 + 2;
- panels.push(poly([[xB, 0], [xB + ch, -lidD], [xB + W - ch, -lidD], [xB + W, 0]], 'top', 'flap'));
- const tuckTop = rect(xB + 18, -lidD - tuckH, W - 36, tuckH, null, 'tuck');
- if ((P.holeR || 0) > 1 && tuckH > (P.holeR || 4) * 2 + 1) {
- tuckTop.holes = [{ x: xB + W / 2, y: -lidD - tuckH / 2, r: P.holeR }];
- }
- panels.push(tuckTop);
- panels.push(dust(xR, 0, -1));
- panels.push(dust(xL, 0, -1));
- if (type === 'straight') {
- panels.push(poly([[xB, H], [xB + ch, H + lidD], [xB + W - ch, H + lidD], [xB + W, H]], 'bottom', 'flap'));
- const tuckBot = rect(xB + 18, H + lidD, W - 36, tuckH, null, 'tuck');
- if ((P.holeR || 0) > 1 && tuckH > (P.holeR || 4) * 2 + 1) {
- tuckBot.holes = [{ x: xB + W / 2, y: H + lidD + tuckH / 2, r: P.holeR }];
- }
- panels.push(tuckBot);
- panels.push(dust(xR, H, 1));
- panels.push(dust(xL, H, 1));
- } else if (type === 'autobottom') {
- const d1 = half + 5, d2 = half + 2, c2 = ch * 0.6;
- panels.push(poly([
- [xF, H], [xF + c2, H + d1], [xF + W * 0.32, H + d1], [xF + W * 0.32, H + d1 - 4],
- [xF + W * 0.68, H + d1 - 4], [xF + W * 0.68, H + d1], [xF + W - c2, H + d1], [xF + W, H]
- ], 'bottom', 'flap'));
- const bottomBack = poly([[xB, H], [xB + c2, H + d2], [xB + W - c2, H + d2], [xB + W, H]], null, 'flap');
- bottomBack.paint = 'bottom';
- panels.push(bottomBack);
- const bottomR = poly([[xR, H], [xR + D2, H], [xR + D2 / 2, H + half + 3]], null, 'dust');
- bottomR.paint = 'bottom';
- panels.push(bottomR);
- const bottomL = poly([[xL, H], [xL + D2, H], [xL + D2 / 2, H + half + 3]], null, 'dust');
- bottomL.paint = 'bottom';
- panels.push(bottomL);
- } else {
- // tuck 反向插锁:下盖挂前板
- panels.push(poly([[xF, H], [xF + ch, H + lidD], [xF + W - ch, H + lidD], [xF + W, H]], 'bottom', 'flap'));
- panels.push(rect(xF + 18, H + lidD, W - 36, tuckH, null, 'tuck'));
- panels.push(dust(xR, H, 1));
- panels.push(dust(xL, H, 1));
- }
- }
- return finishDieline(panels);
- }
- function buildMailbox(type, w, d, hh, params, mat) {
- const t = mat.thickness;
- const W = w + t * 2, D2 = d + t * 2, H = hh + t * 2;
- const P = params || DEFAULT_PARAMS;
- if (type === 'mailbox') {
- const lipW = P.lipW;
- const xRw = 0, xB = D2, xSpine = xB + W, xF = xSpine + D2;
- const panels = [
- rect(xRw, 0, D2, H, 'right', 'body'),
- rect(xB, 0, W, H, 'back', 'body'),
- rect(xSpine, 0, D2, H, 'left', 'body'),
- rect(xF, 0, W, H, 'front', 'body'),
- rect(xF + W, 0, lipW, H, null, 'lip'),
- rect(xB, -D2, W, D2, 'top', 'body'),
- rect(xB, H, W, D2, 'bottom', 'body')
- ];
- return finishDieline(panels);
- }
- if (type === 'drawer') {
- const glue = P.glue;
- const xF = glue, xR = xF + W, xB = xR + D2, xL = xB + W;
- const panels = [
- rect(0, 0, glue, H, null, 'glue'),
- rect(xF, 0, W, H, 'front', 'body'),
- rect(xR, 0, D2, H, 'right', 'body'),
- rect(xB, 0, W, H, 'back', 'body'),
- rect(xL, 0, D2, H, 'left', 'body')
- ];
- const W2 = Math.max(10, W - 2), D3 = Math.max(10, D2 - 2), TH = Math.max(10, H - 8);
- const ty = H + 26, tx0 = 24, txF = tx0 + glue, txR = txF + W2, txB = txR + D3, txL = txB + W2;
- panels.push(rect(tx0, ty, glue, TH, null, 'glue'));
- panels.push(rect(txF, ty, W2, TH, 'front', 'tray'));
- panels.push(rect(txR, ty, D3, TH, null, 'tray'));
- panels.push(rect(txB, ty, W2, TH, null, 'tray'));
- panels.push(rect(txL, ty, D3, TH, null, 'tray'));
- panels.push(rect(txF, ty + TH, W2, D3, null, 'bottom'));
- return finishDieline(panels);
- }
- return null;
- }
- /**
- * @param {string} type 盒型 id(carton/rsc/tuck/straight/autobottom/mailer/mailbox/drawer/card/sleeve/bag)
- * @param {number} w 内宽 mm
- * @param {number} d 内深 mm
- * @param {number} hh 内高 mm
- * @param {object} [params] 结构参数(glue/tuckH/cornerR/dustW/lipW/topFold/holeR/boxNo)
- * @param {string|object} [material] 材质 id 或材质对象(默认白卡)
- */
- function build(type, w, d, hh, params, material) {
- const mat = typeof material === 'object' && material
- ? material
- : materialById(typeof material === 'string' ? material : 'card');
- const P = Object.assign({}, DEFAULT_PARAMS, params || {});
- return buildMailbox(type, w, d, hh, P, mat) || buildBase(type, w, d, hh, P, mat);
- }
- window.PL = window.PL || {};
- window.PL.boxDieline = {
- build: build,
- MATERIALS: MATERIALS,
- DEFAULT_PARAMS: DEFAULT_PARAMS,
- materialById: materialById,
- finishDieline: finishDieline,
- panelBounds: panelBounds
- };
- })();
|