// design1核心功能模块 // 注意:此代码在layui.use回调函数内部执行,不需要$(function())包装 // 全局变量已在主入口文件中定义,这里只需要初始化 $("#object_attribute").css("height", "470px"); $("#obj_list").css("height", document.body.clientHeight - 520); $(".obj_list").css("height", $("#obj_list").height() - 32); $(".main").show(); $(".canvas_box").css("height", document.body.clientHeight - 100); zoom = $(".canvas_bg_img").width() / 2787 $("#canvas1").attr("width", $(".canvas_box").width() / zoom); $("#canvas1").attr("height", $(".canvas_box").height() / zoom); $("#canvas2").attr("width", $(".canvas_box").width() / zoom); $("#canvas2").attr("height", $(".canvas_box").height() / zoom); // 优化性能:在创建 Fabric Canvas 之前,先设置 canvas context 的 willReadFrequently // 注意:这需要在 Fabric.js 创建 canvas 之前执行 try { const canvas1El = document.getElementById('canvas1'); const canvas2El = document.getElementById('canvas2'); if (canvas1El) { // 先获取 context 并设置 willReadFrequently,这样 Fabric.js 创建时会复用这个 context canvas1El.getContext('2d', { willReadFrequently: true }); } if (canvas2El) { canvas2El.getContext('2d', { willReadFrequently: true }); } } catch (e) { // 如果浏览器不支持 willReadFrequently,忽略错误 } // 初始化Canvas对象 canvas1 = new fabric.Canvas('canvas1'); canvas2 = new fabric.Canvas('canvas2'); fabric.Object.prototype.objectCaching = false; ctx1 = canvas1.getSelectionContext(); ctx2 = canvas2.getSelectionContext(); is_bgi_add = false; canvas1.zoomToPoint(new fabric.Point(0, 0), 1); canvas2.zoomToPoint(new fabric.Point(0, 0), 1); // 初始化标尺 initRulers(); $("#source_front").width(zoom * $("#source_front").width()); $("#source_back").width(zoom * $("#source_back").width()); $(".canvas-container:eq(0)").css('transform', 'scale(' + zoom + ')') $(".canvas-container:eq(1)").css('transform', 'scale(' + zoom + ')') $(".canvas-container:eq(0)").css('width', 0) $(".canvas-container:eq(1)").hide(); canvas = canvas1; fabric.Object.prototype.set({ borderColor: "#9013FE", cornerColor: "#9013FE", //激活状态角落图标的填充颜色 cornerStrokeColor: "#9013FE", //激活状态角落图标的边框颜色 borderOpacityWhenMoving: 1, borderScaleFactor: 6, cornerSize: 24, cornerStyle: "circle", //rect,circle centeredScaling: false, //角落放大缩小是否是以图形中心为放大原点 centeredRotation: true, //旋转按钮旋转是否是左上角为圆心旋转 transparentCorners: false, //激活状态角落的图标是否透明 rotatingPointOffset: 20, //旋转距旋转体的距离 padding: 0, }); canvas1.preserveObjectStacking = true; canvas2.preserveObjectStacking = true; var front_list = []; ipcRenderer.on('font-list', (event, data) => { const newFontList = []; data.map(item => { if (item.indexOf('"') === 0) { newFontList.push(item.replace(/^"|"$/g, '')); } else { newFontList.push(item); } }) front_list = newFontList; for (let item of front_list) { $("#text_font_family").append(""); $("#circle_text_font_family").append(""); $("#out_text_font_family").append(""); $("#barcode_font_family").append(""); $("#count_font_family").append(""); } }); ipcRenderer.on('sys-lan', (event, data) => { let lang = localStorage.getItem("lang"); if (lang) { s_lan = lang; langua_ge(lang); $("#language_select").val(lang); return; } switch (data) { case 'zh-CN': s_lan = "zh"; langua_ge("zh"); $("#language_select").val("zh"); localStorage.setItem("lang", "zh"); break; case 'zh': s_lan = "zh"; langua_ge("zh"); $("#language_select").val("zh"); localStorage.setItem("lang", "zh"); break; case 'zh-TW': s_lan = "ozh"; langua_ge("ozh"); $("#language_select").val("ozh"); localStorage.setItem("lang", "ozh"); break; case 'en': s_lan = "en"; langua_ge("en"); $("#language_select").val("en"); localStorage.setItem("lang", "en"); break; case 'en-AU': s_lan = "en"; langua_ge("en"); $("#language_select").val("en"); localStorage.setItem("lang", "en"); break; case 'en-CA': s_lan = "en"; langua_ge("en"); $("#language_select").val("en"); localStorage.setItem("lang", "en"); break; case 'en-GB': s_lan = "en"; langua_ge("en"); $("#language_select").val("en"); localStorage.setItem("lang", "en"); break; case 'en-NZ': s_lan = "en"; langua_ge("en"); $("#language_select").val("en"); localStorage.setItem("lang", "en"); break; case 'en-US': s_lan = "en"; langua_ge("en"); $("#language_select").val("en"); localStorage.setItem("lang", "en"); break; case 'en-ZA': s_lan = "en"; langua_ge("en"); $("#language_select").val("en"); localStorage.setItem("lang", "en"); break; } }); ipcRenderer.on('close', (event, message) => { layer.confirm('' + language_str("whetherSave") + '', {//是否保存当前文件? btn: [language_str("save"), language_str("noSave"), language_str("cancel")] //['保存', '不保存', '取消'] , btn3: function (index, layero) { } }, function (index, layero) { output(() => ipcRenderer.send('run-close')); }, function (index) { ipcRenderer.send('run-close'); }); }) addBackground();//添加背景 白色卡片 initAligningGuidelines(canvas1); initAligningGuidelines(canvas2); //canvas.setZoom(0.5) //recordObjs1.push(JSON.stringify(objs1)); //recordJson1.push(canvas1.toJSON(["selectable","hoverable","hoverCursor","text","fontStyle","fontWeight","underline"])); //recordObjs2.push(JSON.stringify(objs2)); //recordJson2.push(canvas2.toJSON(["selectable","hoverable","hoverCursor","text","fontStyle","fontWeight","underline"])); canvas.renderAll();//重新渲染画布 // 立即启动事件初始化(必须在 slider 初始化之前) initCanvasEvents(); var zoomSlider = slider.render({ elem: '#zoomSlide', //绑定元素 min: 25, max: 500, value: 100, change(val) { console.log(val, zoomSlider) canvas.zoomToPoint( new fabric.Point(0, 0), val / 100 ); // zoom = val / 100; //$(".canvas-container").css("transform","scale(" + val / 100 + ")") } }); // =========================================================== // Slider 滑块初始化 // =========================================================== var circle_text_size = slider.render({ elem: '#circle_text_size', min: 10, max: 120, change: function (value) { if (canvas.getActiveObject()) { canvas.getActiveObject().set("fontSize", value); canvas.renderAll(); } } }); var circle_text_diameter = slider.render({ elem: '#circle_text_diameter', min: 500, max: 4000, change: function (value) { if (canvas.getActiveObject()) { canvas.getActiveObject().set("diameter", value); canvas.renderAll(); } } }); var out_pic_op = slider.render({ elem: '#out_pic_op', change: function (value) { if (canvas.getActiveObject()) { canvas.getActiveObject().set('opacity', (100 - value) / 100); canvas.renderAll(); } } }); var pic_op = slider.render({ elem: '#pic_op', change: function (value) { if (canvas.getActiveObject()) { canvas.getActiveObject().set("opacity", (100 - value) / 100); canvas.renderAll(); } } }); var rect_op = slider.render({ elem: '#rect_op', change: function (value) { if (canvas.getActiveObject()) { canvas.getActiveObject().set("opacity", (100 - value) / 100); canvas.renderAll(); } } }); var circle_op = slider.render({ elem: '#circle_op', change: function (value) { if (canvas.getActiveObject()) { canvas.getActiveObject().set("opacity", (100 - value) / 100); canvas.renderAll(); } } }); window.rotate = function (id) { let angle = $(`#${id}`).attr("angle") - 0; angle += 90; $(`#${id}`).css("transform", `rotate(${angle}deg)`); $(`#${id}`).attr("angle", angle); } $('#display').on('click', function () { // 类型改变 fabric.Image.fromURL('./public/images/front_bg' + bg_version + '_1.png', function (i1) { i1.left = background_image.left; i1.top = background_image.top; let img1 = i1; fabric.Image.fromURL('./public/images/back_bg_1.png', function (i2) { i2.left = background_image.left; i2.top = background_image.top; let img2 = i2; fabric.Image.fromURL('./public/images/op_1.png', function (i3) { i2.left = background_image.left; i2.top = background_image.top; let img3 = i3; display_func(img1, img2, img3); }) }) }) }); // 导出文件 async function saveImageAsPNG(buffer) { let fs = require('fs'); // 弹出保存对话框 const { canceled, filePath } = await dialog.showSaveDialog({ title: '保存图片', defaultPath: path.join(exePath, 'output.png'), // 默认路径 filters: [{ name: 'PNG Images', extensions: ['png'] }] // 文件过滤器 }); if (canceled) { console.log('用户取消了保存操作'); return; } // 将Buffer写入PNG文件到用户选择的路径 fs.writeFile(filePath, buffer, (err) => { if (err) { console.error('写入文件失败:', err); } else { layer.msg(language_str("saveSucc") + filePath);//'保存成功至' console.log('PNG文件已保存至:', filePath); } }); } function display_func(img1, img2, img3) { $("#base_control").hide(); $("#line_control").hide(); $("#pic_control").hide(); $("#out_pic_control").hide(); $("#text_control").hide(); $("#circle_text_control").hide(); $("#out_text_control").hide(); $("#rect_control").hide(); $("#circle_control").hide(); $("#counter_control").hide(); $("#qrcode_Controll").hide(); $("#barcode_controll").hide(); $("#component_type").text(language_str("bg"));//"背景" canvas1.discardActiveObject().renderAll(); canvas2.discardActiveObject().renderAll(); let _objs1 = canvas1.getObjects(); let g1 = []; let g3 = []; let g5 = []; let left = 0;//左裁剪 let top = 0;//顶裁剪 let black_left = 0;//黑色左裁剪 let black_top = 0;//黑色顶裁剪 let all_left = 0;//预览图左裁剪 let all_top = 0;//预览图顶裁剪 let list1 = [..._objs1] for (let item of list1) { let idx = getIndex(item, canvas1); //console.log("------"); //console.log(item.getCoords()); //console.log(getCoordsMinX(background_image.getCoords())); //console.log(getCoordsMinX(item.getCoords())); //console.log(getCoordsMinX(background_image.getCoords()) - getCoordsMinX(item.getCoords())); //console.log("------"); if (getCoordsMinX(background_image.getCoords()) - getCoordsMinX(item.getCoords()) > all_left) { all_left = -(getCoordsMinX(item.getCoords()) - getCoordsMinX(background_image.getCoords())); } if (getCoordsMinY(background_image.getCoords()) - getCoordsMinY(item.getCoords()) > all_top) { all_top = -(getCoordsMinY(item.getCoords()) - getCoordsMinY(background_image.getCoords())); } g3.push(fabric.util.object.clone(item)); if (idx != 0 && (objs1[idx - 1].type == 10 || objs1[idx - 1].type == 4)) { continue;//计数器 或 合成文字 } if (idx != 0 && (objs1[idx - 1].type == 8 || objs1[idx - 1].type == 9)) { continue;//二维码 条形码 } if (idx != 0 && (objs1[idx - 1].type == 2)) { continue;//合成图片 } if (idx != 0 && ( ( objs1[idx - 1].type == 3 && (item.get("fill") == "#000000" || (item.get("fill") == "#ffffff" && false)) //文字 && 黑色 ) || ( objs1[idx - 1].type == 11 && (item.get("fill") == "#000000" || (item.get("fill") == "#ffffff" && false)) //圆形文字 && 黑色 ) || ( objs1[idx - 1].type == 5//矩形 && ( ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && item.get("stroke") == "") //黑填充 透明边框 || (item.get("fill") == "" && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff")) //透明填充 黑边框 || ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff")) //黑填充 黑边框 || ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && item.get("strokeWidth") == 0) //黑填充 0边框 ) ) || ( objs1[idx - 1].type == 6//圆形 && ( ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && item.get("stroke") == "") //黑填充 透明边框 || (item.get("fill") == "" && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff")) //透明填充 黑边框 || ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff")) //黑填充 黑边框 || ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && item.get("strokeWidth") == 0) //黑填充 0边框 ) ) || ( objs1[idx - 1].type == 7//直线 && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff") ) || objs1[idx - 1].black == true //勾选并入黑色图层 // ( // objs1[idx-1].type == 1//固定图片 // && // objs1[idx-1].black == true //勾选并入黑色图层 // ) ) ) { if (getCoordsMinX(background_image.getCoords()) - getCoordsMinX(item.getCoords()) > black_left) { black_left = -(getCoordsMinX(item.getCoords()) - getCoordsMinX(background_image.getCoords())); } if (getCoordsMinY(background_image.getCoords()) - getCoordsMinY(item.getCoords()) > black_top) { black_top = -(getCoordsMinY(item.getCoords()) - getCoordsMinY(background_image.getCoords())); } g1.push(fabric.util.object.clone(item)); continue;//黑色文字 黑色矩形 黑色线条 黑色圆形 } else if (idx == 0) { g1.push(fabric.util.object.clone(item)); } if (getCoordsMinX(background_image.getCoords()) - getCoordsMinX(item.getCoords()) > left) { left = -(getCoordsMinX(item.getCoords()) - getCoordsMinX(background_image.getCoords())); } if (getCoordsMinY(background_image.getCoords()) - getCoordsMinY(item.getCoords()) > top) { top = -(getCoordsMinY(item.getCoords()) - getCoordsMinY(background_image.getCoords())); } g1.push(fabric.util.object.clone(item)); } let url1 = new fabric.Group(g1).toDataURL({ height: 2787, width: 2787, top: all_top, left: all_left }); g3.push(img1); let url3 = new fabric.Group(g3).toDataURL({ height: 2787, width: 2787, top: all_top, left: all_left }); let url5 = new fabric.Group(g5).toDataURL({ height: 2787, width: 2787, top: black_top, left: black_left }); let _objs2 = canvas2.getObjects(); let g2 = []; let g4 = []; let g6 = []; left = 0; top = 0; black_top = 0; black_left = 0; all_top = 0; all_left = 0; for (let item of _objs2) { let idx = getIndex(item, canvas2); if (getCoordsMinX(background_image.getCoords()) - getCoordsMinX(item.getCoords()) > all_left) { all_left = -(getCoordsMinX(item.getCoords()) - getCoordsMinX(background_image.getCoords())); } if (getCoordsMinY(background_image.getCoords()) - getCoordsMinY(item.getCoords()) > all_top) { all_top = -(getCoordsMinY(item.getCoords()) - getCoordsMinY(background_image.getCoords())); } g4.push(fabric.util.object.clone(item)); if (idx != 0 && (objs2[idx - 1].type == 10 || objs2[idx - 1].type == 4)) { continue;//黑色字或计数器 或 合成文字 } if (idx != 0 && (objs2[idx - 1].type == 8 || objs2[idx - 1].type == 9)) { continue;//二维码 条形码 } if (idx != 0 && (objs2[idx - 1].type == 2)) { continue;//合成图片 } if (idx != 0 && (objs2[idx - 1].type == 10)) { continue;//合成图片 } if (idx != 0 && ( ( objs2[idx - 1].type == 3 && (item.get("fill") == "#000000" || (item.get("fill") == "#ffffff" && false)) //文字 && 黑色 ) || ( objs2[idx - 1].type == 11 && (item.get("fill") == "#000000" || (item.get("fill") == "#ffffff" && false)) //圆形文字 && 黑色 ) || ( objs2[idx - 1].type == 5//矩形 && ( ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && item.get("stroke") == "") //黑填充 透明边框 || (item.get("fill") == "" && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff")) //透明填充 黑边框 || ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff")) //黑填充 黑边框 || ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && item.get("strokeWidth") == 0) //黑填充 0边框 ) ) || ( objs2[idx - 1].type == 6//圆形 && ( ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && item.get("stroke") == "") //黑填充 透明边框 || (item.get("fill") == "" && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff")) //透明填充 黑边框 || ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff")) //黑填充 黑边框 || ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && item.get("strokeWidth") == 0) //黑填充 0边框 ) ) || ( objs2[idx - 1].type == 7//直线 && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff") ) || objs2[idx - 1].black == true //勾选并入黑色图层 // ( // objs2[idx-1].type == 1//固定图片 // && // objs2[idx-1].black == true //勾选并入黑色图层 // ) ) ) { if (getCoordsMinX(background_image.getCoords()) - getCoordsMinX(item.getCoords()) > black_left) { black_left = -(getCoordsMinX(item.getCoords()) - getCoordsMinX(background_image.getCoords())); } if (getCoordsMinY(background_image.getCoords()) - getCoordsMinY(item.getCoords()) > black_top) { black_top = -(getCoordsMinY(item.getCoords()) - getCoordsMinY(background_image.getCoords())); } g2.push(fabric.util.object.clone(item)); continue;//黑色文字 黑色矩形 黑色线条 黑色圆形 } else if (idx == 0) { g2.push(fabric.util.object.clone(item)); } if (getCoordsMinX(background_image.getCoords()) - getCoordsMinX(item.getCoords()) > left) { left = -(getCoordsMinX(item.getCoords()) - getCoordsMinX(background_image.getCoords())); } if (getCoordsMinY(background_image.getCoords()) - getCoordsMinY(item.getCoords()) > top) { top = -(getCoordsMinY(item.getCoords()) - getCoordsMinY(background_image.getCoords())); } g2.push(fabric.util.object.clone(item)); } let url2 = new fabric.Group(g2).toDataURL({ height: 2787, width: 2787, top: top, left: left }); let white_url1 = canvas1.getObjects()[0].toDataURL({ height: 2787, width: 2787, }); g4.push(img2); let url4 = new fabric.Group(g4).toDataURL({ height: 2787, width: 2787, top: all_top, left: all_left }); let url6 = new fabric.Group(g6).toDataURL({ height: 2787, width: 2787, top: black_top, left: black_left }); let w = document.body.clientWidth * 0.7 + "px"; let h = (document.body.clientWidth * 0.7 - 40 - 0.06 * document.body.clientWidth * 0.7) * 0.62548 + 150 + "px"; let h1 = (document.body.clientWidth * 0.7 - 40 - 0.06 * document.body.clientWidth * 0.7) * 0.62548 + "px"; if ((document.body.clientWidth * 0.7 - 40 - 0.06 * document.body.clientWidth * 0.7) * 0.62548 > document.body.clientHeight) { //高度超出,则按高度计算 h = document.body.clientHeight * 0.8 + "px"; h1 = document.body.clientHeight * 0.8 - 150 + "px"; w = ((document.body.clientHeight * 0.8 - 150) / 0.62548 + 40) / 0.94 } let fs = require('fs'); const Buffer = require('buffer').Buffer; const buffer = Buffer.from(url3.replace(/^data:image\/\w+;base64,/, ""), 'base64'); const printPath = path.join(exePath, 'print.png'); fs.writeFileSync(printPath, buffer); layer.open({ type: 1, area: [w, h], title: language_str("display"), //不显示标题栏"预览" shadeClose: true, //点击遮罩关闭 content: `
`, btn: [language_str("output"), "打印"],//'导出' btn1: function (index, layero) { saveImageAsPNG(buffer) printJS({ printable: printPath, type: 'image', style: 'img { width: 100%; height: auto; }' }) } }); } function output(callback = null, _save = save) { // 类型改变 fabric.Image.fromURL('./public/images/op_1.png', function (i1) { i1.left = background_image.left; i1.top = background_image.top; let img1 = i1; fabric.Image.fromURL('./public/images/op_1.png', function (i2) { i2.left = background_image.left; i2.top = background_image.top; let img2 = i2; $("#base_control").hide(); $("#line_control").hide(); $("#pic_control").hide(); $("#out_pic_control").hide(); $("#text_control").hide(); $("#circle_text_control").hide(); $("#out_text_control").hide(); $("#rect_control").hide(); $("#circle_control").hide(); $("#counter_control").hide(); $("#qrcode_Controll").hide(); $("#barcode_controll").hide(); $("#component_type").text(language_str("bg"));//"背景" canvas1.discardActiveObject().renderAll(); canvas2.discardActiveObject().renderAll(); let _objs1 = canvas1.getObjects(); let g1 = []; let g3 = []; let g5 = [];//黑色图层 let left = 0;//左裁剪 let top = 0;//顶裁剪 let black_left = 0;//黑色左裁剪 let black_top = 0;//黑色顶裁剪 let all_left = 0;//预览图左裁剪 let all_top = 0;//预览图顶裁剪 for (let item of _objs1) { let idx = getIndex(item, canvas1); if (getCoordsMinX(background_image.getCoords()) - getCoordsMinX(item.getCoords()) > all_left) { all_left = -(getCoordsMinX(item.getCoords()) - getCoordsMinX(background_image.getCoords())); } if (getCoordsMinY(background_image.getCoords()) - getCoordsMinY(item.getCoords()) > all_top) { all_top = -(getCoordsMinY(item.getCoords()) - getCoordsMinY(background_image.getCoords())); } g3.push(fabric.util.object.clone(item)); if (idx != 0 && (objs1[idx - 1].type == 10 || objs1[idx - 1].type == 4)) { continue;//计数器 或 合成文字 } if (idx != 0 && (objs1[idx - 1].type == 8 || objs1[idx - 1].type == 9)) { continue;//二维码 条形码 } if (idx != 0 && (objs1[idx - 1].type == 2)) { continue;//合成图片 } if (idx != 0 && ( ( objs1[idx - 1].type == 3 && item.get("fill") == "#000000" //文字 && 黑色 ) || ( objs1[idx - 1].type == 11 && item.get("fill") == "#000000" //圆形文字 && 黑色 ) || ( objs1[idx - 1].type == 5//矩形 && ( ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && item.get("stroke") == "") //黑填充 透明边框 || (item.get("fill") == "" && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff")) //透明填充 黑边框 || ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff")) //黑填充 黑边框 || ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && item.get("strokeWidth") == 0) //黑填充 0边框 ) ) || ( objs1[idx - 1].type == 6//圆形 && ( ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && item.get("stroke") == "") //黑填充 透明边框 || (item.get("fill") == "" && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff")) //透明填充 黑边框 || ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff")) //黑填充 黑边框 || ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && item.get("strokeWidth") == 0) //黑填充 0边框 ) ) || ( objs1[idx - 1].type == 7//直线 && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff") ) || objs1[idx - 1].black == true //勾选并入黑色图层 ) ) { if (getCoordsMinX(background_image.getCoords()) - getCoordsMinX(item.getCoords()) > black_left) { black_left = -(getCoordsMinX(item.getCoords()) - getCoordsMinX(background_image.getCoords())); } if (getCoordsMinY(background_image.getCoords()) - getCoordsMinY(item.getCoords()) > black_top) { black_top = -(getCoordsMinY(item.getCoords()) - getCoordsMinY(background_image.getCoords())); } g1.push(fabric.util.object.clone(item)); continue;//黑色文字 黑色矩形 黑色线条 黑色圆形 } else if (idx == 0) { g1.push(fabric.util.object.clone(item)); } if (getCoordsMinX(background_image.getCoords()) - getCoordsMinX(item.getCoords()) > left) { left = -(getCoordsMinX(item.getCoords()) - getCoordsMinX(background_image.getCoords())); } if (getCoordsMinY(background_image.getCoords()) - getCoordsMinY(item.getCoords()) > top) { top = -(getCoordsMinY(item.getCoords()) - getCoordsMinY(background_image.getCoords())); } g1.push(fabric.util.object.clone(item)); } let url1 = new fabric.Group(g1).toDataURL({ height: 2787, width: 2787, top: all_top, left: all_left }); g3.push(img1); let url3 = new fabric.Group(g3).toDataURL({ height: 2787, width: 2787, top: all_top, left: all_left }); let url5 = new fabric.Group(g5).toDataURL({ height: 2787, width: 2787, top: black_top, left: black_left }); let _objs2 = canvas2.getObjects(); let g2 = []; let g4 = []; let g6 = [];//黑色图层 left = 0; top = 0; black_left = 0;//黑色左裁剪 black_top = 0;//黑色顶裁剪 all_left = 0;//黑色左裁剪 all_top = 0;//黑色顶裁剪 for (let item of _objs2) { let idx = getIndex(item, canvas2); if (getCoordsMinX(background_image.getCoords()) - getCoordsMinX(item.getCoords()) > all_left) { all_left = -(getCoordsMinX(item.getCoords()) - getCoordsMinX(background_image.getCoords())); } if (getCoordsMinY(background_image.getCoords()) - getCoordsMinY(item.getCoords()) > all_top) { all_top = -(getCoordsMinY(item.getCoords()) - getCoordsMinY(background_image.getCoords())); } g4.push(fabric.util.object.clone(item)); if (idx != 0 && (objs2[idx - 1].type == 10 || objs2[idx - 1].type == 4)) { continue;//计数器 或 合成文字 } if (idx != 0 && (objs2[idx - 1].type == 8 || objs2[idx - 1].type == 9)) { continue;//二维码 条形码 } if (idx != 0 && (objs2[idx - 1].type == 2)) { continue;//合成图片 } if (idx != 0 && ( ( objs2[idx - 1].type == 3 && (item.get("fill") == "#000000" || (item.get("fill") == "#ffffff" && false)) //文字 && 黑色 ) || ( objs2[idx - 1].type == 11 && (item.get("fill") == "#000000" || (item.get("fill") == "#ffffff" && false)) //圆形文字 && 黑色 ) || ( objs2[idx - 1].type == 5//矩形 && ( ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && item.get("stroke") == "") //黑填充 透明边框 || (item.get("fill") == "" && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff")) //透明填充 黑边框 || ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff")) //黑填充 黑边框 || ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && item.get("strokeWidth") == 0) //黑填充 0边框 ) ) || ( objs2[idx - 1].type == 6//圆形 && ( ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && item.get("stroke") == "") //黑填充 透明边框 || (item.get("fill") == "" && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff")) //透明填充 黑边框 || ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff")) //黑填充 黑边框 || ((item.get("fill") == "#000000" || item.get("fill") == "#ffffff") && item.get("strokeWidth") == 0) //黑填充 0边框 ) ) || ( objs2[idx - 1].type == 7//直线 && (item.get("stroke") == "#000000" || item.get("stroke") == "#ffffff") ) || objs2[idx - 1].black == true //勾选并入黑色图层 // ( // objs2[idx-1].type == 1//固定图片 // && // objs2[idx-1].black == true //勾选并入黑色图层 // ) ) ) { if (getCoordsMinX(background_image.getCoords()) - getCoordsMinX(item.getCoords()) > black_left) { black_left = -(getCoordsMinX(item.getCoords()) - getCoordsMinX(background_image.getCoords())); } if (getCoordsMinY(background_image.getCoords()) - getCoordsMinY(item.getCoords()) > black_top) { black_top = -(getCoordsMinY(item.getCoords()) - getCoordsMinY(background_image.getCoords())); } g2.push(fabric.util.object.clone(item)); continue;//黑色文字 黑色矩形 黑色线条 黑色圆形 } else if (idx == 0) { g2.push(fabric.util.object.clone(item)); } if (getCoordsMinX(background_image.getCoords()) - getCoordsMinX(item.getCoords()) > left) { left = -(getCoordsMinX(item.getCoords()) - getCoordsMinX(background_image.getCoords())); } if (getCoordsMinY(background_image.getCoords()) - getCoordsMinY(item.getCoords()) > top) { top = -(getCoordsMinY(item.getCoords()) - getCoordsMinY(background_image.getCoords())); } g2.push(fabric.util.object.clone(item)); } let url2 = new fabric.Group(g2).toDataURL({ height: 2787, width: 2787, top: top, left: left }); let white_url1 = canvas1.getObjects()[0].toDataURL({ height: 2787, width: 2787, }); g4.push(img2); let url4 = new fabric.Group(g4).toDataURL({ height: 2787, width: 2787, top: all_top, left: all_left }); let url6 = new fabric.Group(g6).toDataURL({ height: 2787, width: 2787, top: black_top, left: black_left }); // let white_url2 = canvas2.getObjects()[0].toDataURL({ // height: 2787, // width: 2787, // }); //console.log(canvas.getObjects()) let w = document.body.clientWidth * 0.7 + "px"; let h = (document.body.clientWidth * 0.7 - 40 - 0.06 * document.body.clientWidth * 0.7) * 0.62548 + 150 + "px"; let h1 = (document.body.clientWidth * 0.7 - 40 - 0.06 * document.body.clientWidth * 0.7) * 0.62548 + "px"; if ((document.body.clientWidth * 0.7 - 40 - 0.06 * document.body.clientWidth * 0.7) * 0.62548 > document.body.clientHeight) { //高度超出,则按高度计算 h = document.body.clientHeight * 0.8 + "px"; h1 = document.body.clientHeight * 0.8 - 150 + "px"; w = ((document.body.clientHeight * 0.8 - 150) / 0.62548 + 40) / 0.94 } let op = { frontColorPic: "", backColorPic: "", frontBlackPic: "", backBlackPic: "", frontData: [], backData: [], flag: 1, frontDisplayPic: url3, backDisplayPic: '' }; if (objs1.length != 0 & objs2.length == 0) { op.flag = 2; } else if (objs1.length == 0 & objs2.length != 0) { op.flag = 3; } op.frontColorPic = desEncrypt(url1); op.backColorPic = ''; //op.frontBlPic = url5; //op.backBlPic = url6; op.frontBlackPic = ''; op.backBlackPic = ''; for (let item of _objs1) { let idx = getIndex(item, canvas1); if (idx == 0) continue;//background-image let type = objs1[idx - 1].type; if (type == 2) { //out_pic let xy = getAbsoluteXY(item); op.frontData.push({ "type": 1, "name": objs1[idx - 1].name, "FontColor": objs1[idx - 1].black ? "#000000" : "@", "transparency": item.get("opacity"), "x": parseInt((xy[0] - background_image1.left).toFixed(0)), "y": parseInt((xy[1] - background_image1.top).toFixed(0)), "h": parseInt((item.get("scaleY") * item.get("height")).toFixed(0)), "w": parseInt((item.get("scaleX") * item.get("width")).toFixed(0)), "rotate": parseInt(item.get("angle").toFixed(0)) }); } else if (type == 3) { } else if (type == 11) { op.frontData.push({ "type": 2, "Text": item.get("text"), "FontFamily": item.get("fontFamily"), "FontSize": parseInt(item.get("fontSize")), "FontColor": "#000000", "FontBackgroudColor": item.get("textBackgroundColor"), "Bold": item.get("fontWeight") == "bold", "Underline": item.get("underline"), "Italic": item.get("fontStyle") == "italic", "Strikeout": item.get("linethrough"), "x": parseInt((item.get("left") - background_image1.left).toFixed(0)), "y": parseInt((item.get("top") - background_image1.top).toFixed(0)), "h": parseInt((item.get("scaleY") * item.get("height")).toFixed(0)), "w": parseInt((item.get("scaleX") * item.get("width")).toFixed(0)), "rotate": parseInt(item.get("angle").toFixed(0)), "circleCenter": objs1[idx - 1].circleCenter }); } else if (type == 4) { //out_text let xy = getAbsoluteXY(item); op.frontData.push({ "type": 3, "name": objs1[idx - 1].name, "FontFamily": item.get("fontFamily"), "DefaultText": item.get("text"), "FontBackgroudColor": item.get("textBackgroundColor"), "FontSize": parseInt(item.get("fontSize")), "FontColor": item.get("fill"), "Exceed": objs1[idx - 1].exceed, "AutoWrap": objs1[idx - 1].autoWrap, "FixSize": objs1[idx - 1].fixSize, "Bold": item.get("fontWeight") == "bold", "Underline": item.get("underline"), "Italic": item.get("fontStyle") == "italic", "Strikeout": item.get("linethrough"), "x": parseInt((xy[0] - background_image1.left).toFixed(0)), "y": parseInt((xy[1] - background_image1.top).toFixed(0)), "h": parseInt((item.get("scaleY") * item.get("height")).toFixed(0)), "w": parseInt((item.get("scaleX") * item.get("width")).toFixed(0)), "rotate": parseInt(item.get("angle").toFixed(0)), align: objs1[idx - 1].align }); } else if (type == 8) { //二维码 let xy = getAbsoluteXY(item); op.frontData.push({ "type": 4, "name": objs1[idx - 1].name, "FontColor": objs1[idx - 1].color, "DefaultText": objs1[idx - 1].val, "x": parseInt((xy[0] - background_image1.left).toFixed(0)), "y": parseInt((xy[1] - background_image1.top).toFixed(0)), "h": parseInt((item.get("scaleY") * item.get("height")).toFixed(0)), "w": parseInt((item.get("scaleX") * item.get("width")).toFixed(0)), "rotate": parseInt(item.get("angle").toFixed(0)) }); } else if (type == 9) { //条形码 let xy = getAbsoluteXY(item); op.frontData.push({ "type": 5, "name": objs1[idx - 1].name, "FontSize": parseInt(objs1[idx - 1].fontSize), "DefaultText": objs1[idx - 1].val, "HideText": objs1[idx - 1].show, "FontFamily": objs1[idx - 1].font, "FontColor": objs1[idx - 1].color, "x": parseInt((xy[0] - background_image1.left).toFixed(0)), "y": parseInt((xy[1] - background_image1.top).toFixed(0)), "h": parseInt((item.get("scaleY") * item.get("height")).toFixed(0)), "w": parseInt((item.get("scaleX") * item.get("width")).toFixed(0)), "rotate": parseInt(item.get("angle").toFixed(0)) }); } else if (type == 10) { let xy = getAbsoluteXY(item); op.frontData.push({ "type": 6, "name": objs1[idx - 1].name, "FontFamily": item.get("fontFamily"), "FontSize": parseInt(item.get("fontSize")), "FontColor": item.get("fill"), "start": objs1[idx - 1].start, "add": parseInt(objs1[idx - 1].add), "x": parseInt((xy[0] - background_image1.left).toFixed(0)), "y": parseInt((xy[1] - background_image1.top).toFixed(0)), "h": parseInt((item.get("scaleY") * item.get("height")).toFixed(0)), "w": parseInt((item.get("scaleX") * item.get("width")).toFixed(0)), "rotate": parseInt(item.get("angle").toFixed(0)) }); } } //obj2 for (let item of _objs2) { let idx = getIndex(item, canvas2); if (idx == 0) continue;//background-image let type = objs2[idx - 1].type; if (type == 2) { //out_pic let xy = getAbsoluteXY(item); op.backData.push({ "type": 1, "name": objs2[idx - 1].name, "FontColor": objs2[idx - 1].black ? "#000000" : "@", "transparency": item.get("opacity"), "x": parseInt((xy[0] - background_image2.left).toFixed(0)), "y": parseInt((xy[1] - background_image2.top).toFixed(0)), "h": parseInt((item.get("scaleY") * item.get("height")).toFixed(0)), "w": parseInt((item.get("scaleX") * item.get("width")).toFixed(0)), "rotate": parseInt(item.get("angle").toFixed(0)) }); } else if (type == 3) { } else if (type == 11) { op.frontData.push({ "type": 2, "Text": item.get("text"), "FontFamily": item.get("fontFamily"), "FontSize": parseInt(item.get("fontSize")), "FontColor": "#000000", "FontBackgroudColor": item.get("textBackgroundColor"), "Bold": item.get("fontWeight") == "bold", "Underline": item.get("underline"), "Italic": item.get("fontStyle") == "italic", "Strikeout": item.get("linethrough"), "x": parseInt((item.get("left") - background_image1.left).toFixed(0)), "y": parseInt((item.get("top") - background_image1.top).toFixed(0)), "h": parseInt((item.get("scaleY") * item.get("height")).toFixed(0)), "w": parseInt((item.get("scaleX") * item.get("width")).toFixed(0)), "rotate": parseInt(item.get("angle").toFixed(0)), "circleCenter": objs2[idx - 1].circleCenter }); } else if (type == 4) { let xy = getAbsoluteXY(item); op.backData.push({ "type": 3, "name": objs2[idx - 1].name, "FontFamily": item.get("fontFamily"), "DefaultText": item.get("text"), "FontSize": parseInt(item.get("fontSize")), "FontBackgroudColor": item.get("textBackgroundColor"), "FontColor": item.get("fill"), "Exceed": objs2[idx - 1].exceed, "AutoWrap": objs2[idx - 1].autoWrap, "FixSize": objs2[idx - 1].fixSize, "Bold": item.get("fontWeight") == "bold", "Underline": item.get("underline"), "Italic": item.get("fontStyle") == "italic", "Strikeout": item.get("linethrough"), "x": parseInt((xy[0] - background_image2.left).toFixed(0)), "y": parseInt((xy[1] - background_image2.top).toFixed(0)), "h": parseInt((item.get("scaleY") * item.get("height")).toFixed(0)), "w": parseInt((item.get("scaleX") * item.get("width")).toFixed(0)), "rotate": parseInt(item.get("angle").toFixed(0)), align: objs2[idx - 1].align }); } else if (type == 8) { //二维码 let xy = getAbsoluteXY(item); op.backData.push({ "type": 4, "name": objs2[idx - 1].name, "FontColor": objs2[idx - 1].color, "DefaultText": objs2[idx - 1].val, "x": parseInt((xy[0] - background_image2.left).toFixed(0)), "y": parseInt((xy[1] - background_image2.top).toFixed(0)), "h": parseInt((item.get("scaleY") * item.get("height")).toFixed(0)), "w": parseInt((item.get("scaleX") * item.get("width")).toFixed(0)), "rotate": parseInt(item.get("angle").toFixed(0)) }); } else if (type == 9) { let xy = getAbsoluteXY(item); op.backData.push({ "type": 5, "name": objs2[idx - 1].name, "DefaultText": objs2[idx - 1].val, "HideText": objs1[idx - 1].show, "FontSize": parseInt(objs2[idx - 1].fontSize), "FontFamily": objs2[idx - 1].font, "FontColor": objs2[idx - 1].color, "x": parseInt((xy[0] - background_image2.left).toFixed(0)), "y": parseInt((xy[1] - background_image2.top).toFixed(0)), "h": parseInt((item.get("scaleY") * item.get("height")).toFixed(0)), "w": parseInt((item.get("scaleX") * item.get("width")).toFixed(0)), "rotate": parseInt(item.get("angle").toFixed(0)) }); } else if (type == 10) { let xy = getAbsoluteXY(item); op.backData.push({ "type": 6, "name": objs2[idx - 1].name, "FontFamily": item.get("fontFamily"), "FontSize": parseInt(item.get("fontSize")), "FontColor": item.get("fill"), "start": parseInt(objs2[idx - 1].start), "add": parseInt(objs2[idx - 1].add), "x": parseInt((xy[0] - background_image2.left).toFixed(0)), "y": parseInt((xy[1] - background_image2.top).toFixed(0)), "h": parseInt((item.get("scaleY") * item.get("height")).toFixed(0)), "w": parseInt((item.get("scaleX") * item.get("width")).toFixed(0)), "rotate": parseInt(item.get("angle").toFixed(0)) }); } } _save(op, callback); //return false 开启该代码可禁止点击该按钮关闭 }) }) } $('#output').on('click', function () { output(null, saveAs); }); function addBackground() { // 类型改变 fabric.Image.fromURL('./public/images/bg_front_1.png', function (image) { let width = $("#canvas1").width() image.left = ($("#canvas1").width() - image.width * image.get("scaleX")) / 2; image.top = ($("#canvas1").height() - image.height * image.get("scaleY")) / 2; image.selectable = false; image.hoverable = false; image.hoverCursor = "default" image.setSrc(image.toDataURL(), function (image) { image.scale(1, 1); background_image1 = image; background_image = background_image1; canvas.add(image); canvas.renderAll(); //pre_objs1.push(JSON.stringify(objs1)); //pre_json1.push(canvas1.toJSON(["selectable","hoverable","hoverCursor"])); recordObjs1.push(JSON.stringify(objs1)); recordJson1.push(canvas1.toJSON(["selectable", "hoverable", "hoverCursor", "text", "fontStyle", "fontWeight", "underline"])); }); // 类型改变 fabric.Image.fromURL('./public/images/bg_back.png', function (image) { image.left = ($("#canvas2").width() - image.width) / 2; image.top = ($("#canvas2").height() - image.height) / 2; image.selectable = false; image.hoverable = false; image.hoverCursor = "default" image.setSrc(image.toDataURL(), function (image) { background_image2 = image; canvas2.add(image); canvas2.renderAll(); //pre_objs2.push(JSON.stringify(objs2)); //pre_json2.push(canvas2.toJSON(["selectable","hoverable","hoverCursor"])); recordObjs2.push(JSON.stringify(objs2)); recordJson2.push(canvas2.toJSON(["selectable", "hoverable", "hoverCursor", "text", "fontStyle", "fontWeight", "underline"])); let file = GetFile().get('file') console.log(file) if (file && file != 'empty') { open(file) } }); }); is_bgi_add = true; }); } function updateControls() { if (!canvas.getActiveObject()) return; $("#text_r").val(canvas.getActiveObject().get("angle").toFixed(0)); $("#text_x").val((canvas.getActiveObject().get("left") - background_image.left).toFixed(0)); $("#text_y").val((canvas.getActiveObject().get("top") - background_image.top).toFixed(0)); $("#text_h").val((canvas.getActiveObject().get("scaleY") * canvas.getActiveObject().get("height")).toFixed(0)); $("#text_w").val((canvas.getActiveObject().get("scaleX") * canvas.getActiveObject().get("width")).toFixed(0)); if (canvas.getActiveObject().get("selectable")) { $("#lock_img").attr("src", "public/images/unlock20.png")//已经上锁,需要解锁 } else { $("#lock_img").attr("src", "public/images/lock20.png")//没上锁,可以上锁 } } function selectObject(target, index) { updateControls(); $("#base_control").show(); //$("#text_x").val((target.left - background_image.left).toFixed(1)); //$("#text_y").val((target.top - background_image.top).toFixed(1)); //$("#text_w").val(parseFloat(target.get("scaleX") * target.get("width")).toFixed(1)); //$("#text_h").val(parseFloat(target.get("scaleY") * target.get("height")).toFixed(1)); //$("#text_r").val(target.get("angle").toFixed(0)); } function addPic(pointer) { pre_add_image.left = pointer.x; pre_add_image.top = pointer.y; canvas.add(pre_add_image); objs.push({ type: 1, black: false }); canvas.renderAll(); updateList(); recordState(); //OpenDialog(); //function OpenDialog() //{ // dialog.showOpenDialog({ // title: "请选择图片", // buttonLabel: "确认", // filters: [ // { name: 'Image File Type', extensions: ['png', 'jpg'] }, // ] // }).then(result => { // if(result.canceled)return; // //console.log(result.filePaths) // fabric.Image.fromURL(result.filePaths[0], function(image) { // image.left = pointer.x; // image.top = pointer.y; // // canvas.add(image); // objs.push({type: 1}); // canvas.renderAll(); // updateList(); // recordState(); // } ); // }).catch(err => { // console.log(err) // }) //} } function selectPic(target, index) { //透明度 $("#pic_control").show(); $("#set_bg").show(); let activeObj = canvas.getActiveObject(); if (typeof pic_op !== 'undefined' && pic_op.setValue) { pic_op.setValue(100 - (activeObj.get("opacity") * 100)); } else { $("#pic_op").next().children(".layui-slider-bar").css("width", (1 - activeObj.get("opacity")) * 100 + "%"); $("#pic_op").next().children(".layui-slider-handle").css("left", (1 - activeObj.get("opacity")) * 100 + "%"); } let idx = getIndex(canvas.getActiveObject()); // $("#pic_black").prop("checked", objs[idx-1].black ? 'checked' : ''); $("#out_pic_control").hide(); $("#text_control").hide(); $("#circle_text_control").hide(); $("#out_text_control").hide(); $("#rect_control").hide(); $("#circle_control").hide(); $("#line_control").hide(); $("#counter_control").hide(); $("#qrcode_Controll").hide(); $("#barcode_controll").hide(); } function addOutPic(pointer) { fabric.Image.fromURL('./public/images/outpic_extra.png', function (image) { let width = canvas.getWidth(); let height = canvas.getHeight(); image.left = pointer.x; image.top = pointer.y; image.opacity = 1; //image.on("selected",function(){//test well // console.log(222); //}); image.setSrc(image.toDataURL(), function (image) { canvas.add(image); objs.push({ type: 2, name: resName("OUT_PIC_"), black: false }); canvas.renderAll(); updateList(); recordState(); }); }); } function selectOutPic(target, index) { //字段名 $("#out_pic_control").show(); if (typeof out_pic_op !== 'undefined' && out_pic_op.setValue) { out_pic_op.setValue(100 - (canvas.getActiveObject().get("opacity") * 100)); } else { $("#out_pic_op").next().children(".layui-slider-bar").css("width", (1 - canvas.getActiveObject().get("opacity")) * 100 + "%"); $("#out_pic_op").next().children(".layui-slider-handle").css("left", (1 - canvas.getActiveObject().get("opacity")) * 100 + "%"); } let idx = getIndex(canvas.getActiveObject()); $("#out_pic_field_name").val(objs[idx - 1].name); // $("#out_pic_black").prop("checked", objs[idx-1].black ? 'checked' : ''); $("#pic_control").hide(); $("#set_bg").hide(); $("#text_control").hide(); $("#circle_text_control").hide(); $("#out_text_control").hide(); $("#rect_control").hide(); $("#circle_control").hide(); $("#line_control").hide(); $("#counter_control").hide(); $("#qrcode_Controll").hide(); $("#barcode_controll").hide(); } function addText(pointer) { let text = new fabric.Text('TEXT', {//源:文本 left: pointer.x, top: pointer.y, fill: "#000000", textBackgroundColor: "", fontStyle: "normal", fontWeight: "normal", fontSize: 100, underline: false, linethrough: false, fontFamily: 'Arial' }); text.setControlVisible("ml", false); text.setControlVisible("mt", false); text.setControlVisible("mr", false); text.setControlVisible("mb", false); canvas.add(text); objs.push({ type: 3, black: false }); canvas.renderAll(); updateList(); recordState(); } function selectText(target, index) { //console.log(canvas.getActiveObject().get("fontFamily")); $("#text_control").show(); $("#text_color").val(canvas.getActiveObject().get("fill")); $("#text_back_color").val(canvas.getActiveObject().get("textBackgroundColor") == "" ? '#ffffff' : canvas.getActiveObject().get("textBackgroundColor")); let val = parseFloat(canvas.getActiveObject().get("fontSize")) * 72 / dpi if (val % 0.5 !== 0) val = Math.round(val * 2) / 2 $("#text_size").val(val); $("#text_text").val(canvas.getActiveObject().get("text")); $("#text_font_family").val(canvas.getActiveObject().get("fontFamily")); if (canvas.getActiveObject().get("underline") == true) { $("#text_underline").addClass("ui-radio-acitve") } else { $("#text_underline").removeClass("ui-radio-acitve") } if (canvas.getActiveObject().get("fontWeight") == "bold") { $("#text_bold").addClass("ui-radio-acitve") } else { $("#text_bold").removeClass("ui-radio-acitve") } if (canvas.getActiveObject().get("fontStyle") == "italic") { $("#text_italics").addClass("ui-radio-acitve") } else { $("#text_italics").removeClass("ui-radio-acitve") } if (canvas.getActiveObject().get("linethrough") == true) { $("#text_linethrough").addClass("ui-radio-acitve") } else { $("#text_linethrough").removeClass("ui-radio-acitve") } $("#circle_text_control").hide(); $("#pic_control").hide(); $("#set_bg").hide(); $("#out_pic_control").hide(); $("#out_text_control").hide(); $("#rect_control").hide(); $("#circle_control").hide(); $("#line_control").hide(); $("#counter_control").hide(); $("#qrcode_Controll").hide(); $("#barcode_controll").hide(); } function addCircleText(pointer) { let distance = getDisdance(pointer.x, pointer.y, canvas.width / 2, canvas.height / 2) let angleDeg = getDeg(pointer) let text = new fabric.CurvedText('Circle TEXT', {//源:圆形文本 left: pointer.x, top: pointer.y, fill: "#000000", textBackgroundColor: "", fontStyle: "normal", fontWeight: "normal", diameter: distance * 2, fontSize: 100, underline: false, linethrough: false, lockScalingX: true, lockScalingY: true, fontFamily: 'Arial', originX: 'center', // originY: 'center', angle: angleDeg, flipped: pointer.y > canvas.height / 2 ? true : false }); // distance = getDisdance(pointer.x+text.width/2, pointer.y, canvas.width/2,canvas.height/2) // text.set({diameter: distance*2}) text.setControlVisible("ml", false); text.setControlVisible("mt", false); text.setControlVisible("mr", false); text.setControlVisible("mb", false); canvas.add(text); objs.push({ type: 11, black: false }); canvas.renderAll(); updateList(); recordState(); } function selectCircleText(target, index) { let idx = getIndex(canvas.getActiveObject()); $("#circle_text_control").show(); $("#circle_text_color").val(canvas.getActiveObject().get("fill")); $("#circle_text_back_color").val(canvas.getActiveObject().get("textBackgroundColor") == "" ? '#ffffff' : canvas.getActiveObject().get("textBackgroundColor")); if (typeof circle_text_size !== 'undefined' && circle_text_size.setValue) { circle_text_size.setValue(canvas.getActiveObject().get("fontSize") - 10); } else { $("#circle_text_size").next().children(".layui-slider-bar").css("width", ((canvas.getActiveObject().get("fontSize") - 10 - 10) / (120 - 10)) * 100 + "%"); $("#circle_text_size").next().children(".layui-slider-handle").css("left", ((canvas.getActiveObject().get("fontSize") - 10 - 10) / (120 - 10)) * 100 + "%"); } if (typeof circle_text_diameter !== 'undefined' && circle_text_diameter.setValue) { circle_text_diameter.setValue(canvas.getActiveObject().get("diameter") - 500); } else { $("#circle_text_diameter").next().children(".layui-slider-bar").css("width", ((canvas.getActiveObject().get("diameter") - 500 - 500) / (4000 - 500)) * 100 + "%"); $("#circle_text_diameter").next().children(".layui-slider-handle").css("left", ((canvas.getActiveObject().get("diameter") - 500 - 500) / (4000 - 500)) * 100 + "%"); } $("#circle_text_center").prop("checked", objs[idx - 1].circleCenter ? 'checked' : ''); $("#circle_text_text").val(canvas.getActiveObject().get("text")); $("#circle_text_font_family").val(canvas.getActiveObject().get("fontFamily")); if (canvas.getActiveObject().get("underline") == true) { $("#circle_text_underline").addClass("ui-radio-acitve") } else { $("#circle_text_underline").removeClass("ui-radio-acitve") } if (canvas.getActiveObject().get("fontWeight") == "bold") { $("#circle_text_bold").addClass("ui-radio-acitve") } else { $("#circle_text_bold").removeClass("ui-radio-acitve") } if (canvas.getActiveObject().get("fontStyle") == "italic") { $("#circle_text_italics").addClass("ui-radio-acitve") } else { $("#circle_text_italics").removeClass("ui-radio-acitve") } if (canvas.getActiveObject().get("linethrough") == true) { $("#circle_text_linethrough").addClass("ui-radio-acitve") } else { $("#circle_text_linethrough").removeClass("ui-radio-acitve") } $("#pic_control").hide(); $("#set_bg").hide(); $("#rotate").hide(); $("#out_pic_control").hide(); $("#text_control").hide(); $("#out_text_control").hide(); $("#rect_control").hide(); $("#circle_control").hide(); $("#line_control").hide(); $("#counter_control").hide(); $("#qrcode_Controll").hide(); $("#barcode_controll").hide(); } function addOutText(pointer) { //MERGE_TEXT_1 let text = new fabric.Text("MERGE TEXT", {//源:合成文本 left: pointer.x, top: pointer.y, fill: "#000000", fontStyle: "normal", fontWeight: "normal", fontSize: 100, textBackgroundColor: "", underline: false, linethrough: false, fontFamily: 'Arial', textAlign: 'center' }); //text.on({ // 'scaling': function(e) { // var obj = this, // w = obj.width * obj.scaleX, // h = obj.height * obj.scaleY; // // obj.set({ // 'height' : h, // 'width' : w, // 'scaleX' : 1, // 'scaleY' : 1 // }); // } //}); text.setControlVisible("ml", false); text.setControlVisible("mt", false); text.setControlVisible("mr", false); text.setControlVisible("mb", false); canvas.add(text); objs.push({ type: 4, name: resName("OUT_TEXT_"), exceed: false, autoWrap: false, fixSize: false, black: false, align: 'center' }); canvas.renderAll(); updateList(); recordState(); } function selectOutText(target, index) { //color //size const align = canvas.getActiveObject().get('textAlign') if (align === 'left') { $('#out_left_alignment').parent().addClass('tool1') } else if (align === 'right') { $('#out_right_alignment').parent().addClass('tool1') } else { $('#out_center_alignment').parent().addClass('tool1') } $("#out_text_control").show(); $("#out_text_color").val(canvas.getActiveObject().get("fill")); $("#out_text_text").val(canvas.getActiveObject().get("text")); let val = parseFloat(canvas.getActiveObject().get("fontSize")) * 72 / dpi if (val % 0.5 !== 0) val = Math.round(val * 2) / 2 $("#out_text_size").val(val); $("#out_text_back_color").val(canvas.getActiveObject().get("textBackgroundColor") == "" ? '#ffffff' : canvas.getActiveObject().get("textBackgroundColor")); let idx = getIndex(canvas.getActiveObject()); $("#out_text_field_name").val(objs[idx - 1].name); $("#out_text_font_family").val(canvas.getActiveObject().get("fontFamily")); $("#exceed").prop("checked", objs[idx - 1].exceed ? 'checked' : ''); $("#autoWrap").prop("checked", objs[idx - 1].autoWrap ? 'checked' : ''); $("#fixSize").prop("checked", objs[idx - 1].fixSize ? 'checked' : ''); if (canvas.getActiveObject().get("underline") == true) { $("#out_text_underline").addClass("ui-radio-acitve") } else { $("#out_text_underline").removeClass("ui-radio-acitve") } if (canvas.getActiveObject().get("fontWeight") == "bold") { $("#out_text_bold").addClass("ui-radio-acitve") } else { $("#out_text_bold").removeClass("ui-radio-acitve") } if (canvas.getActiveObject().get("fontStyle") == "italic") { $("#out_text_italics").addClass("ui-radio-acitve") } else { $("#out_text_italics").removeClass("ui-radio-acitve") } if (canvas.getActiveObject().get("linethrough") == true) { $("#out_text_linethrough").addClass("ui-radio-acitve") } else { $("#out_text_linethrough").removeClass("ui-radio-acitve") } $("#pic_control").hide(); $("#set_bg").hide(); $("#out_pic_control").hide(); $("#text_control").hide(); $("#circle_text_control").hide(); $("#rect_control").hide(); $("#circle_control").hide(); $("#line_control").hide(); $("#counter_control").hide(); $("#qrcode_Controll").hide(); $("#barcode_controll").hide(); } function addRect(pointer) { let rect = new fabric.Rect({ left: pointer.x, top: pointer.y, fill: '', stroke: "#ff0000", strokeWidth: 10, width: 300, height: 300 }); canvas.add(rect); objs.push({ type: 5, black: false }); canvas.renderAll(); updateList(); recordState(); } function selectRect(target, index) { //color $("#rect_control").show(); $("#rect_color").val(canvas.getActiveObject().get("fill") == "" ? "#ffffff" : canvas.getActiveObject().get("fill")); $("#rect_stroke").val(canvas.getActiveObject().get("stroke") == "" ? "#ffffff" : canvas.getActiveObject().get("stroke")); $("#rect_stroke_width").val(canvas.getActiveObject().get("strokeWidth")); $("#rect_radius").val(canvas.getActiveObject().get("rx")); if (typeof rect_op !== 'undefined' && rect_op.setValue) { rect_op.setValue(100 - (canvas.getActiveObject().get("opacity") * 100)); } else { $("#rect_op").next().children(".layui-slider-bar").css("width", (1 - canvas.getActiveObject().get("opacity")) * 100 + "%"); $("#rect_op").next().children(".layui-slider-handle").css("left", (1 - canvas.getActiveObject().get("opacity")) * 100 + "%"); } let a = canvas.getActiveObject().getCoords(); console.log(getCoordsMinX(a)); $("#pic_control").hide(); $("#set_bg").hide(); $("#out_pic_control").hide(); $("#text_control").hide(); $("#circle_text_control").hide(); $("#out_text_control").hide(); $("#circle_control").hide(); $("#line_control").hide(); $("#counter_control").hide(); $("#qrcode_Controll").hide(); $("#barcode_controll").hide(); } function addCircle(pointer) { let circle = new fabric.Circle({ left: pointer.x, top: pointer.y, fill: '', stroke: '#ff0000', strokeWidth: 10, radius: 150, strokeUniform: true, }); canvas.add(circle); objs.push({ type: 6, black: false }); canvas.renderAll(); updateList(); recordState(); } function selectCircle(target, index) { //border-color //color $("#circle_control").show(); $("#circle_color").val(canvas.getActiveObject().get("fill") == "" ? "#ffffff" : canvas.getActiveObject().get("fill")); $("#circle_stroke").val(canvas.getActiveObject().get("stroke") == "" ? "#ffffff" : canvas.getActiveObject().get("stroke")); $("#circle_stroke_width").val(canvas.getActiveObject().get("strokeWidth")); if (typeof circle_op !== 'undefined' && circle_op.setValue) { circle_op.setValue(100 - (canvas.getActiveObject().get("opacity") * 100)); } else { $("#circle_op").next().children(".layui-slider-bar").css("width", (1 - canvas.getActiveObject().get("opacity")) * 100 + "%"); $("#circle_op").next().children(".layui-slider-handle").css("left", (1 - canvas.getActiveObject().get("opacity")) * 100 + "%"); } $("#pic_control").hide(); $("#set_bg").hide(); $("#out_pic_control").hide(); $("#text_control").hide(); $("#circle_text_control").hide(); $("#out_text_control").hide(); $("#rect_control").hide(); $("#line_control").hide(); $("#counter_control").hide(); $("#qrcode_Controll").hide(); $("#barcode_controll").hide(); } function addLine(pointer) { let line = new fabric.Line([100, 200, 400, 200], { left: pointer.x, top: pointer.y, stroke: '#ff0000', strokeWidth: 10, strokeDashArray: [0, 0], }); line.setControlVisible("tl", false); line.setControlVisible("tr", false); line.setControlVisible("br", false); line.setControlVisible("bl", false); line.setControlVisible("mt", false); line.setControlVisible("mb", false); canvas.add(line); objs.push({ type: 7, black: false }); canvas.renderAll(); updateList(); recordState(); } function selectLine(target, index) { //color //weight $("#line_control").show(); let idx = getIndex(canvas.getActiveObject()); $("#line_color").val(canvas.getActiveObject().get("stroke")); $("#line_dist").val(canvas.getActiveObject().get("strokeDashArray")[0]); $("#text_h").val(canvas.getActiveObject().get("strokeWidth")) $("#pic_control").hide(); $("#set_bg").hide(); $("#out_pic_control").hide(); $("#text_control").hide(); $("#circle_text_control").hide(); $("#out_text_control").hide(); $("#rect_control").hide(); $("#circle_control").hide(); $("#counter_control").hide(); $("#qrcode_Controll").hide(); $("#barcode_controll").hide(); } function addQrCode(pointer) { let qr = jrQrcode.getQrBase64('https://www.cardsoon.com', { padding: 0 }); fabric.Image.fromURL(qr, function (image) { image.left = pointer.x; image.top = pointer.y; image.scale(2.4, 2.4) canvas.add(image); objs.push({ type: 8, name: resName("QR_CODE_"), color: "#000000", val: "https://www.cardsoon.com", black: false }); canvas.renderAll(); updateList(); recordState(); }); } function selectQrCode(target, index) { //字段名 //background-color //color $("#qrcode_Controll").show(); let idx = getIndex(canvas.getActiveObject()); $("#qrcode_field_name").val(objs[idx - 1].name); $("#qrcode_color").val(objs[idx - 1].color); $("#qrcode_val").val(objs[idx - 1].val); $("#line_control").hide(); $("#pic_control").hide(); $("#set_bg").hide(); $("#out_pic_control").hide(); $("#text_control").hide(); $("#circle_text_control").hide(); $("#out_text_control").hide(); $("#rect_control").hide(); $("#circle_control").hide(); $("#counter_control").hide(); $("#barcode_controll").hide(); } function addBarCode(pointer) { JsBarcode("#barcode", "123456789", { margin: 0, lineColor: "#000000", fontSize: 18, font: "Arial" }); let barcode = document.getElementById('barcode'); let bar = barcode.toDataURL("image/png"); fabric.Image.fromURL(bar, function (image) { image.left = pointer.x; image.top = pointer.y; image.scale(2.4, 2.4) canvas.add(image); objs.push({ type: 9, name: resName("BAR_CODE_"), val: "123456789", color: "#000000", fontSize: 18, font: "Arial", show: false, black: false });//show是取消文字显示 canvas.renderAll(); updateList(); recordState(); }); } function selectBarCode(target, index) { //字段名 //background-color //color $("#barcode_controll").show(); let idx = getIndex(canvas.getActiveObject()); $("#barcode_field_name").val(objs[idx - 1].name); $("#barcode_font_family").val(objs[idx - 1].font); let val = parseFloat(objs[idx - 1].fontSize) * 72 / dpi if (val % 0.5 !== 0) val = Math.round(val * 2) / 2 $("#barcode_size").val(val); $("#barcode_val").val(objs[idx - 1].val); $("#barcode_show").prop("checked", objs[idx - 1].show ? 'checked' : ''); $("#line_control").hide(); $("#pic_control").hide(); $("#set_bg").hide(); $("#out_pic_control").hide(); $("#text_control").hide(); $("#circle_text_control").hide(); $("#out_text_control").hide(); $("#rect_control").hide(); $("#circle_control").hide(); $("#counter_control").hide(); $("#qrcode_Controll").hide(); } function addCounter(pointer) { let text = new fabric.Text('0', { left: pointer.x, top: pointer.y, fontSize: 100, fill: "#000000", }); canvas.add(text); objs.push({ type: 10, name: resName("COUNTER_"), start: 0, add: 1, black: false }); canvas.renderAll(); updateList(); recordState(); console.log(canvas.getObjects()) } function selectCounter(target, index) { //起始 //终止 //增量 $("#counter_control").show(); let idx = getIndex(canvas.getActiveObject()); $("#counter_start").val(objs[idx - 1].start); $("#counter_add").val(objs[idx - 1].add); $("#counter_color").val(canvas.getActiveObject().get("fill")); $("#count_font_family").val(canvas.getActiveObject().get("fontFamily")); let val = parseFloat(canvas.getActiveObject().get("fontSize")) * 72 / dpi if (val % 0.5 !== 0) val = Math.round(val * 2) / 2 $("#count_size").val(val); $("#line_control").hide(); $("#pic_control").hide(); $("#set_bg").hide(); $("#out_pic_control").hide(); $("#text_control").hide(); $("#circle_text_control").hide(); $("#out_text_control").hide(); $("#rect_control").hide(); $("#circle_control").hide(); $("#qrcode_Controll").hide(); $("#barcode_controll").hide(); } function updateList() { let str = ""; let i = 0; // 定义行样式:Flex布局,两端对齐,相对定位防止溢出 const rowStyle = "display:flex; justify-content:space-between; align-items:center; height:32px; padding:0 5px; cursor:pointer; border-bottom:1px solid #444; color:#ccc; font-size:12px; position:relative;"; // 定义左侧内容样式 const leftStyle = "display:flex; align-items:center; overflow:hidden; white-space:nowrap; max-width:180px;"; // 定义序号样式:靠右,有左边框 const indexStyle = "min-width:24px; text-align:center; padding-left:5px; color:#666; border-left:1px solid #555;"; for (let item of objs) { let displayIndex = i + 1; // 序号底层为1 // 图标逻辑 let iconName = "rect.svg"; switch (item.type) { case 1: iconName = "images.svg"; break; case 2: iconName = "images_add.svg"; break; case 3: iconName = "text.svg"; break; case 4: iconName = "text2.svg"; break; case 5: iconName = "rect.svg"; break; case 6: iconName = "round.svg"; break; case 7: iconName = "line.svg"; break; case 8: iconName = "qrcode.svg"; break; case 9: iconName = "barcode.svg"; break; case 10: iconName = "counter.svg"; break; case 11: iconName = "text3.svg"; break; } // 名称逻辑 let nameText = item.name ? item.name : (item.type === 3 ? "TEXT_" + displayIndex : (item.type === 1 ? "IMG_" + displayIndex : "OBJ_" + displayIndex)); // 组装 HTML str = `