var util = require('./util'); var sdzAjax = require('./sdzAjax'); ;(function($){ var app = new Object(); /*设置 当前活动元素或指定元素的样式*/ app.setActiveStyle = function(styleName,value,object){ var canvas = this.mainCanvas; object = object || canvas.getActiveObject(); if (!object) return; if (object.setSelectionStyles && object.isEditing) { var style = {}; style[styleName] = value; object.setSelectionStyles(style); object.setCoords(); } else { object.set(styleName, value); } object.setCoords(); canvas.renderAll(); } /*调整印花控制器显示的个数*/ app.ControlVisibility = function(canvasItem){ var _controlsVisibility = { tl: !0, tr: !0, br: !0, bl: !0, ml: 0, mt: 0, mr: 0, mb: 0, mtr: !0 } for(var item in _controlsVisibility){ canvasItem['setControlVisible'](item,_controlsVisibility[item]); } } //添加图片 app.addImg = function(url){ var that = this; fabric.Image.fromURL(url,function(designImg){ designImg.set({ cornerSize:15, cornerStyle:'circle', rotatingPointOffset:30, borderDashArray:[3,6], borderColor:'#000', cornerColor: '#000' }); //图片宽高比 var imgRATIO = designImg.width / designImg.height; //图片最大尺寸 app.oldCutWidth that.maxWidthImg = that.oldCutWidth*0.8; that.maxHeightImg = that.oldCutHeight*0.8; var imgWidth,imgHeight //app.cutWidth if(that.maxHeightImg*imgRATIO < that.maxWidthImg ){ imgWidth = that.maxHeightImg*imgRATIO; imgHeight = that.maxHeightImg; }else{ imgWidth = that.maxWidthImg; imgHeight = that.maxWidthImg/imgRATIO; } designImg.setWidth(imgWidth); designImg.setHeight(imgHeight); that.centerObj(designImg); that.mainCanvas.add(designImg).setActiveObject(designImg); that.mainCanvas.renderAll(); that.mainCanvas.calcOffset(); }) } //返回当前中心店坐标 app.transformViewPoint = function(o){ var that = this; var point = that.mainCanvas.getCenter(); if(o){ var i ={ x:o.x, y:o.y } }else{ var i ={ x:point.left, y:point.top } } // after 计算出 变换之后 中心点坐标 var after = fabric.util.transformPoint(i, fabric.util.invertTransform(that.mainCanvas.viewportTransform)); return after; } //当前元素居中 app.centerObj = function(obj){ var that = this; console.log(obj) var afterPoint = app.transformViewPoint(); obj.set('left',afterPoint.x - obj.width*obj.scaleX/2); obj.set('top',afterPoint.y - obj.height*obj.scaleY/2); obj.setCoords(); } app.returnCanvasJson = function(){ var that = this; return that.mainCanvas.toJSON([ 'borderColor', 'cornerColor', 'cornerStyle', 'cornerSize', 'rotatingPointOffset', 'borderDashArray', 'plateRATIO', 'viewportTransform', 'oldCutWidth', 'oldCutHeight', 'width', 'height', '_controlsVisibility' ]); } //把当前画布保存成图片 app.returnCanvasImg = function(width){ var that= this; app.outputWidth = width || 500 ; app.outputHeight = app.outputWidth/app.plateRATIO; //新的缩放比例 var afterCutRATIO = app.outputWidth/app.oldCutWidth; //中心点坐标 var point = that.mainCanvas.getCenter(); //之前缩放比例 var zoomNum = that.mainCanvas.getZoom(); //执行新的缩放 that.mainCanvas.zoomToPoint(new fabric.Point(point.left,point.top),afterCutRATIO); //生成剪切图片 var dataURL = that.mainCanvas.toDataURL({ format: 'png', left: point.left -app.outputWidth/2 , top: point.top - app.outputHeight/2, width: app.outputWidth, height: app.outputHeight }); //缩放回原来大小 that.mainCanvas.zoomToPoint(new fabric.Point(point.left,point.top),zoomNum); return dataURL; } //绘制剪切区域 app.cutArea = function(width,height,win){ if(app.cutCanvas){ $("#cutCanvas").remove(); } app.cutCanvas = document.createElement('canvas'); app.cutCanvas.width = width; app.cutCanvas.height= height; app.cutCanvas.id = 'cutCanvas'; $(app.cutCanvas).css({ position:'absolute', top:0, left:0, width:width, height:height }); var cutCtx = app.cutCanvas.getContext('2d'); cutCtx.fillStyle = 'rgba(173,173,173,0.8)'; cutCtx.fillRect(0,0,width,height); //剪切区域的最大高度 和宽度 app.maxWidth = app.baseWidth*0.8; app.maxHeight = app.baseHeight*0.8; if(!win){ if(app.maxHeight*app.plateRATIO < app.maxWidth){ app.cutWidth = app.maxHeight*app.plateRATIO; app.cutHeight = app.maxHeight; }else { app.cutWidth = app.maxWidth; app.cutHeight = app.maxWidth/app.plateRATIO; } } cutCtx.clearRect((width-app.cutWidth)/2,(height-app.cutHeight)/2, app.cutWidth, app.cutHeight); $("#diy").after(app.cutCanvas); } //变换屏幕变换时,让canvas 元素居中 app.resizeCanvasSize = function(){ var canvas = this.mainCanvas; app.centerLeft = canvas.getWidth()/2; app.centerTop = canvas.getHeight()/2; canvas.setWidth(app.baseWidth); canvas.setHeight(app.baseHeight); var point = canvas.getCenter(); //抛弃选择中的组 canvas.discardActiveGroup(); app.resizeLeft = point.left - app.centerLeft, app.resizeTop = point.top - app.centerTop; var zoomNum = canvas.getZoom(); //console.log(zoomNum) var objects = canvas.getObjects(); //console.log(objects[0]) for (var i in objects) { objects[i].left = objects[i].left + app.resizeLeft/zoomNum; objects[i].top = objects[i].top + app.resizeTop/zoomNum; objects[i].setCoords(); } canvas.renderAll(); canvas.calcOffset(); } app.cutRATIO = null; //自适应屏幕 缩放 app.resizeRender = function(){ var that = this; //缩放比例 var afterCutRATIO = app.cutWidth/app.oldCutWidth; if(app.cutRATIO == afterCutRATIO){ return false; }else{ app.cutRATIO = afterCutRATIO; } var point = that.mainCanvas.getCenter(); that.mainCanvas.zoomToPoint(new fabric.Point(point.left,point.top),app.cutRATIO); that.mainCanvas.renderAll(); that.mainCanvas.calcOffset(); } //canvas 检测按下 app.dowmCanvas = function(){ var canvas = this.mainCanvas; canvas.on('mouse:down',function(options){ if(options.target && options.target.type=='i-text'){ $(".djsy-text-edit-wrap").addClass('djsy-text-edit-ani'); }else{ $(".djsy-text-edit-wrap").removeClass('djsy-text-edit-ani'); } if(options.target){ $(".djys-align-wrap").removeClass('djys-align-ani'); }else{ $(".djys-align-wrap").addClass('djys-align-ani'); } }); canvas.on('mouse:up',function(options){ if(options.target){ } }); canvas.on('object:selected',function(options){ var group = canvas.getActiveGroup(); if(group){ group.set({ cornerSize:15, cornerStyle:'circle', rotatingPointOffset:30, borderDashArray:[3,6], borderColor:'#000', cornerColor: '#000' }) } }) } //添加文字 app.addText = function(texts){ var that = this; var text = new fabric.IText(texts, { fontSize: 30, textAlign:'left', cornerStyle:'circle', borderColor:'#000', cornerColor: '#000', fontFamily:'Microsoft YaHei', cornerSize:15, rotatingPointOffset:30, borderDashArray:[3,6] }); text.on('editing:entered',function(){ app.textFlage = true; }); text.on('editing:exited',function(){ app.textFlage = false; }); app.ControlVisibility(text); that.centerObj(text); that.mainCanvas.add(text).setActiveObject(text); that.mainCanvas.renderAll(); } app.delObject = function(){ var that = this; $("#diy_del_object").click(function(){ if(that.mainCanvas.getActiveGroup()){ alert('请选择对象删除'); return false; } var obj = that.mainCanvas.getActiveObject(); if(!obj||obj=='undefined'){return false;} that.mainCanvas.remove(obj); that.mainCanvas.renderAll(); }); } app.init = function(did,typeId){ //diy 基础宽度 app.baseWidth = $(".diy_canvas_wrap").width(); app.baseHeight = $(".diy_canvas_wrap").height(); app.mainCanvas = new fabric.Canvas('diy',{ selectionColor: 'rgba(0,0,0,0.1)', controlsAboveOverlay:true, preserveObjectStacking:true, backgroundColor:'rgba(255,255,255,0)', selctionBorderColor:'#000', selectionDashArray:[3,6], selectionLineWidth: 1 }); if(did){ app.diyId = did; if(typeId){ app.typeId = typeId; } util.tipHtml('loading','正在加载'); sdzAjax.getDiy({id:app.diyId,type:app.typeId},function(data){ //console.log(data); util.hideTip(); var json = JSON.parse(data.diy.content); app.plateRATIO = json.plateRATIO; app.oldCutWidth = json.oldCutWidth; app.oldCutHeight = json.oldCutHeight; app.cutArea(app.baseWidth,app.baseHeight); app.mainCanvas.loadFromJSON(json,function(){ app.resizeCanvasSize(); app.resizeRender(); app.mainCanvas.renderAll.bind(app.mainCanvas); },function(o,obj){ obj.hasControls = false; obj.lockMovementX = obj.lockMovementY = true; app.mainCanvas.renderAll(); }); app.bottle = { id:$("#bottle_id").val(), img_url:$("#bottle_img_url").val() } }); }else{ alert('diy获取错误') } //监控diy 事件 app.dowmCanvas(); //删除 app.delObject(); } //屏幕变化的时候 function windowResize(){ $(window).resize(function(e){ if(app.mainCanvas){ app.baseWidth = $(".diy_canvas_wrap").width(); app.baseHeight = $(".diy_canvas_wrap").height(); //变换剪切区域 app.cutArea(app.baseWidth,app.baseHeight); app.resizeCanvasSize(); //app.resizeRender(); } }) } //层级修改 function changeHierarchy(){ $("#view_ward .djys-align-item").each(function(){ $(this).click(function(){ var key = $(this).data('key'); switch(key) { case 0://上一层 var obj = app.mainCanvas.getActiveObject(); if(!obj||obj=='undefined'){return false;}; app.mainCanvas.bringForward(obj); app.mainCanvas.renderAll(); break; case 1: //下一层 var obj = app.mainCanvas.getActiveObject(); if(!obj||obj=='undefined'){return false;}; app.mainCanvas.sendBackwards(obj); app.mainCanvas.renderAll(); break; case 2: //置顶 var obj = app.mainCanvas.getActiveObject(); if(!obj||obj=='undefined'){return false;}; app.mainCanvas.bringToFront(obj); app.mainCanvas.renderAll(); break; default: //置底 var obj = app.mainCanvas.getActiveObject(); if(!obj||obj=='undefined'){return false;}; app.mainCanvas.sendToBack(obj); app.mainCanvas.renderAll(); } }) }) } //颜色遍历 function getColor(){ sdzAjax.getColor(function(data){ var html = '' var divContent = '