wDiy.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. var util = require('./util');
  2. var sdzAjax = require('./sdzAjax');
  3. ;(function($){
  4. var app = new Object();
  5. /*设置 当前活动元素或指定元素的样式*/
  6. app.setActiveStyle = function(styleName,value,object){
  7. var canvas = this.mainCanvas;
  8. object = object || canvas.getActiveObject();
  9. if (!object) return;
  10. if (object.setSelectionStyles && object.isEditing) {
  11. var style = {};
  12. style[styleName] = value;
  13. object.setSelectionStyles(style);
  14. object.setCoords();
  15. }
  16. else {
  17. object.set(styleName, value);
  18. }
  19. object.setCoords();
  20. canvas.renderAll();
  21. }
  22. /*调整印花控制器显示的个数*/
  23. app.ControlVisibility = function(canvasItem){
  24. var _controlsVisibility = {
  25. tl: !0,
  26. tr: !0,
  27. br: !0,
  28. bl: !0,
  29. ml: 0,
  30. mt: 0,
  31. mr: 0,
  32. mb: 0,
  33. mtr: !0
  34. }
  35. for(var item in _controlsVisibility){
  36. canvasItem['setControlVisible'](item,_controlsVisibility[item]);
  37. }
  38. }
  39. //添加图片
  40. app.addImg = function(url){
  41. var that = this;
  42. fabric.Image.fromURL(url,function(designImg){
  43. designImg.set({
  44. cornerSize:15,
  45. cornerStyle:'circle',
  46. rotatingPointOffset:30,
  47. borderDashArray:[3,6],
  48. borderColor:'#000',
  49. cornerColor: '#000'
  50. });
  51. //图片宽高比
  52. var imgRATIO = designImg.width / designImg.height;
  53. //图片最大尺寸 app.oldCutWidth
  54. that.maxWidthImg = that.oldCutWidth*0.8;
  55. that.maxHeightImg = that.oldCutHeight*0.8;
  56. var imgWidth,imgHeight
  57. //app.cutWidth
  58. if(that.maxHeightImg*imgRATIO < that.maxWidthImg ){
  59. imgWidth = that.maxHeightImg*imgRATIO;
  60. imgHeight = that.maxHeightImg;
  61. }else{
  62. imgWidth = that.maxWidthImg;
  63. imgHeight = that.maxWidthImg/imgRATIO;
  64. }
  65. designImg.setWidth(imgWidth);
  66. designImg.setHeight(imgHeight);
  67. that.centerObj(designImg);
  68. that.mainCanvas.add(designImg).setActiveObject(designImg);
  69. that.mainCanvas.renderAll();
  70. that.mainCanvas.calcOffset();
  71. })
  72. }
  73. //返回当前中心店坐标
  74. app.transformViewPoint = function(o){
  75. var that = this;
  76. var point = that.mainCanvas.getCenter();
  77. if(o){
  78. var i ={
  79. x:o.x,
  80. y:o.y
  81. }
  82. }else{
  83. var i ={
  84. x:point.left,
  85. y:point.top
  86. }
  87. }
  88. // after 计算出 变换之后 中心点坐标
  89. var after = fabric.util.transformPoint(i, fabric.util.invertTransform(that.mainCanvas.viewportTransform));
  90. return after;
  91. }
  92. //当前元素居中
  93. app.centerObj = function(obj){
  94. var that = this;
  95. console.log(obj)
  96. var afterPoint = app.transformViewPoint();
  97. obj.set('left',afterPoint.x - obj.width*obj.scaleX/2);
  98. obj.set('top',afterPoint.y - obj.height*obj.scaleY/2);
  99. obj.setCoords();
  100. }
  101. app.returnCanvasJson = function(){
  102. var that = this;
  103. return that.mainCanvas.toJSON([
  104. 'borderColor',
  105. 'cornerColor',
  106. 'cornerStyle',
  107. 'cornerSize',
  108. 'rotatingPointOffset',
  109. 'borderDashArray',
  110. 'plateRATIO',
  111. 'viewportTransform',
  112. 'oldCutWidth',
  113. 'oldCutHeight',
  114. 'width',
  115. 'height',
  116. '_controlsVisibility'
  117. ]);
  118. }
  119. //把当前画布保存成图片
  120. app.returnCanvasImg = function(width){
  121. var that= this;
  122. app.outputWidth = width || 500 ;
  123. app.outputHeight = app.outputWidth/app.plateRATIO;
  124. //新的缩放比例
  125. var afterCutRATIO = app.outputWidth/app.oldCutWidth;
  126. //中心点坐标
  127. var point = that.mainCanvas.getCenter();
  128. //之前缩放比例
  129. var zoomNum = that.mainCanvas.getZoom();
  130. //执行新的缩放
  131. that.mainCanvas.zoomToPoint(new fabric.Point(point.left,point.top),afterCutRATIO);
  132. //生成剪切图片
  133. var dataURL = that.mainCanvas.toDataURL({
  134. format: 'png',
  135. left: point.left -app.outputWidth/2 ,
  136. top: point.top - app.outputHeight/2,
  137. width: app.outputWidth,
  138. height: app.outputHeight
  139. });
  140. //缩放回原来大小
  141. that.mainCanvas.zoomToPoint(new fabric.Point(point.left,point.top),zoomNum);
  142. return dataURL;
  143. }
  144. //绘制剪切区域
  145. app.cutArea = function(width,height,win){
  146. if(app.cutCanvas){
  147. $("#cutCanvas").remove();
  148. }
  149. app.cutCanvas = document.createElement('canvas');
  150. app.cutCanvas.width = width;
  151. app.cutCanvas.height= height;
  152. app.cutCanvas.id = 'cutCanvas';
  153. $(app.cutCanvas).css({
  154. position:'absolute',
  155. top:0,
  156. left:0,
  157. width:width,
  158. height:height
  159. });
  160. var cutCtx = app.cutCanvas.getContext('2d');
  161. cutCtx.fillStyle = 'rgba(173,173,173,0.8)';
  162. cutCtx.fillRect(0,0,width,height);
  163. //剪切区域的最大高度 和宽度
  164. app.maxWidth = app.baseWidth*0.8;
  165. app.maxHeight = app.baseHeight*0.8;
  166. if(!win){
  167. if(app.maxHeight*app.plateRATIO < app.maxWidth){
  168. app.cutWidth = app.maxHeight*app.plateRATIO;
  169. app.cutHeight = app.maxHeight;
  170. }else {
  171. app.cutWidth = app.maxWidth;
  172. app.cutHeight = app.maxWidth/app.plateRATIO;
  173. }
  174. }
  175. cutCtx.clearRect((width-app.cutWidth)/2,(height-app.cutHeight)/2, app.cutWidth, app.cutHeight);
  176. $("#diy").after(app.cutCanvas);
  177. }
  178. //变换屏幕变换时,让canvas 元素居中
  179. app.resizeCanvasSize = function(){
  180. var canvas = this.mainCanvas;
  181. app.centerLeft = canvas.getWidth()/2;
  182. app.centerTop = canvas.getHeight()/2;
  183. canvas.setWidth(app.baseWidth);
  184. canvas.setHeight(app.baseHeight);
  185. var point = canvas.getCenter();
  186. //抛弃选择中的组
  187. canvas.discardActiveGroup();
  188. app.resizeLeft = point.left - app.centerLeft,
  189. app.resizeTop = point.top - app.centerTop;
  190. var zoomNum = canvas.getZoom();
  191. //console.log(zoomNum)
  192. var objects = canvas.getObjects();
  193. //console.log(objects[0])
  194. for (var i in objects) {
  195. objects[i].left = objects[i].left + app.resizeLeft/zoomNum;
  196. objects[i].top = objects[i].top + app.resizeTop/zoomNum;
  197. objects[i].setCoords();
  198. }
  199. canvas.renderAll();
  200. canvas.calcOffset();
  201. }
  202. app.cutRATIO = null;
  203. //自适应屏幕 缩放
  204. app.resizeRender = function(){
  205. var that = this;
  206. //缩放比例
  207. var afterCutRATIO = app.cutWidth/app.oldCutWidth;
  208. if(app.cutRATIO == afterCutRATIO){
  209. return false;
  210. }else{
  211. app.cutRATIO = afterCutRATIO;
  212. }
  213. var point = that.mainCanvas.getCenter();
  214. that.mainCanvas.zoomToPoint(new fabric.Point(point.left,point.top),app.cutRATIO);
  215. that.mainCanvas.renderAll();
  216. that.mainCanvas.calcOffset();
  217. }
  218. //canvas 检测按下
  219. app.dowmCanvas = function(){
  220. var canvas = this.mainCanvas;
  221. canvas.on('mouse:down',function(options){
  222. if(options.target && options.target.type=='i-text'){
  223. $(".djsy-text-edit-wrap").addClass('djsy-text-edit-ani');
  224. }else{
  225. $(".djsy-text-edit-wrap").removeClass('djsy-text-edit-ani');
  226. }
  227. if(options.target){
  228. $(".djys-align-wrap").removeClass('djys-align-ani');
  229. }else{
  230. $(".djys-align-wrap").addClass('djys-align-ani');
  231. }
  232. });
  233. canvas.on('mouse:up',function(options){
  234. if(options.target){
  235. }
  236. });
  237. canvas.on('object:selected',function(options){
  238. var group = canvas.getActiveGroup();
  239. if(group){
  240. group.set({
  241. cornerSize:15,
  242. cornerStyle:'circle',
  243. rotatingPointOffset:30,
  244. borderDashArray:[3,6],
  245. borderColor:'#000',
  246. cornerColor: '#000'
  247. })
  248. }
  249. })
  250. }
  251. //添加文字
  252. app.addText = function(texts){
  253. var that = this;
  254. var text = new fabric.IText(texts, {
  255. fontSize: 30,
  256. textAlign:'left',
  257. cornerStyle:'circle',
  258. borderColor:'#000',
  259. cornerColor: '#000',
  260. fontFamily:'Microsoft YaHei',
  261. cornerSize:15,
  262. rotatingPointOffset:30,
  263. borderDashArray:[3,6]
  264. });
  265. text.on('editing:entered',function(){
  266. app.textFlage = true;
  267. });
  268. text.on('editing:exited',function(){
  269. app.textFlage = false;
  270. });
  271. app.ControlVisibility(text);
  272. that.centerObj(text);
  273. that.mainCanvas.add(text).setActiveObject(text);
  274. that.mainCanvas.renderAll();
  275. }
  276. app.delObject = function(){
  277. var that = this;
  278. $("#diy_del_object").click(function(){
  279. if(that.mainCanvas.getActiveGroup()){
  280. alert('请选择对象删除');
  281. return false;
  282. }
  283. var obj = that.mainCanvas.getActiveObject();
  284. if(!obj||obj=='undefined'){return false;}
  285. that.mainCanvas.remove(obj);
  286. that.mainCanvas.renderAll();
  287. });
  288. }
  289. app.init = function(did,typeId){
  290. //diy 基础宽度
  291. app.baseWidth = $(".diy_canvas_wrap").width();
  292. app.baseHeight = $(".diy_canvas_wrap").height();
  293. app.mainCanvas = new fabric.Canvas('diy',{
  294. selectionColor: 'rgba(0,0,0,0.1)',
  295. controlsAboveOverlay:true,
  296. preserveObjectStacking:true,
  297. backgroundColor:'rgba(255,255,255,0)',
  298. selctionBorderColor:'#000',
  299. selectionDashArray:[3,6],
  300. selectionLineWidth: 1
  301. });
  302. if(did){
  303. app.diyId = did;
  304. if(typeId){
  305. app.typeId = typeId;
  306. }
  307. util.tipHtml('loading','正在加载');
  308. sdzAjax.getDiy({id:app.diyId,type:app.typeId},function(data){
  309. //console.log(data);
  310. util.hideTip();
  311. var json = JSON.parse(data.diy.content);
  312. app.plateRATIO = json.plateRATIO;
  313. app.oldCutWidth = json.oldCutWidth;
  314. app.oldCutHeight = json.oldCutHeight;
  315. app.cutArea(app.baseWidth,app.baseHeight);
  316. app.mainCanvas.loadFromJSON(json,function(){
  317. app.resizeCanvasSize();
  318. app.resizeRender();
  319. app.mainCanvas.renderAll.bind(app.mainCanvas);
  320. },function(o,obj){
  321. obj.hasControls = false;
  322. obj.lockMovementX = obj.lockMovementY = true;
  323. app.mainCanvas.renderAll();
  324. });
  325. app.bottle = {
  326. id:$("#bottle_id").val(),
  327. img_url:$("#bottle_img_url").val()
  328. }
  329. });
  330. }else{
  331. alert('diy获取错误')
  332. }
  333. //监控diy 事件
  334. app.dowmCanvas();
  335. //删除
  336. app.delObject();
  337. }
  338. //屏幕变化的时候
  339. function windowResize(){
  340. $(window).resize(function(e){
  341. if(app.mainCanvas){
  342. app.baseWidth = $(".diy_canvas_wrap").width();
  343. app.baseHeight = $(".diy_canvas_wrap").height();
  344. //变换剪切区域
  345. app.cutArea(app.baseWidth,app.baseHeight);
  346. app.resizeCanvasSize();
  347. //app.resizeRender();
  348. }
  349. })
  350. }
  351. //层级修改
  352. function changeHierarchy(){
  353. $("#view_ward .djys-align-item").each(function(){
  354. $(this).click(function(){
  355. var key = $(this).data('key');
  356. switch(key)
  357. {
  358. case 0://上一层
  359. var obj = app.mainCanvas.getActiveObject();
  360. if(!obj||obj=='undefined'){return false;};
  361. app.mainCanvas.bringForward(obj);
  362. app.mainCanvas.renderAll();
  363. break;
  364. case 1: //下一层
  365. var obj = app.mainCanvas.getActiveObject();
  366. if(!obj||obj=='undefined'){return false;};
  367. app.mainCanvas.sendBackwards(obj);
  368. app.mainCanvas.renderAll();
  369. break;
  370. case 2: //置顶
  371. var obj = app.mainCanvas.getActiveObject();
  372. if(!obj||obj=='undefined'){return false;};
  373. app.mainCanvas.bringToFront(obj);
  374. app.mainCanvas.renderAll();
  375. break;
  376. default: //置底
  377. var obj = app.mainCanvas.getActiveObject();
  378. if(!obj||obj=='undefined'){return false;};
  379. app.mainCanvas.sendToBack(obj);
  380. app.mainCanvas.renderAll();
  381. }
  382. })
  383. })
  384. }
  385. //颜色遍历
  386. function getColor(){
  387. sdzAjax.getColor(function(data){
  388. var html = ''
  389. var divContent = '<div class="djsy-color-panel-item">{span}</div>'
  390. var spanContent = '<span data-color="{color}" style="background-color:{color}"></span>'
  391. for( var item in data.colorPanel){
  392. var span = ''
  393. for(var i=0;i<data.colorPanel[item].length;i++){
  394. span+=spanContent.replace(/{color}/g,data.colorPanel[item][i]);
  395. }
  396. html+=divContent.replace("{span}",span);
  397. }
  398. $("#djsy_text_color_panel_content").html(html);
  399. $("#djsy_text_color_panel_content .djsy-color-panel-item > span,#djsy_color_default > span").each(function(){
  400. $(this).click(function(){
  401. var color = $(this).data('color');
  402. app.setActiveStyle('fill',color);
  403. mui('#djsy_text_color_panel').popover('hide');
  404. })
  405. })
  406. });
  407. }
  408. //预览效果
  409. function previewCanvas(){
  410. /* $("#diy_preview").click(function(){
  411. var imgUrl = app.returnCanvasImg(60);
  412. $("#previewCanvas").show();
  413. var img = '<img class="previewImg" src="'+ imgUrl +'"/>';
  414. $("#previewImgWrap").html(img);
  415. });*/
  416. $("#diy_preview").click(function(){
  417. var imgUrl = app.returnCanvasImg(200);
  418. var previewCanvas = document.createElement('canvas');
  419. var ctx = previewCanvas.getContext("2d");
  420. var img = new Image();
  421. img.src = app.bottle.img_url;
  422. console.log(app.bottle);
  423. img.onload = function(){
  424. console.log(img.onload);
  425. previewCanvas.width = img.width;
  426. previewCanvas.height = img.height;
  427. ctx.drawImage(img,0,0,img.width,img.height);
  428. var imgBotte = new Image();
  429. imgBotte.src = imgUrl;
  430. imgBotte.onload = function(){
  431. imgBotte.height = img.height*0.54;
  432. imgBotte.width = imgBotte.height*app.plateRATIO;
  433. ctx.drawImage(imgBotte,(img.width-imgBotte.width)/2,img.height*0.3,imgBotte.width,imgBotte.height);
  434. var imgBotteUrl = previewCanvas.toDataURL();
  435. $("#previewCanvas").show();
  436. var imgEle = '<img class="previewImg" src="'+ imgBotteUrl +'"/>';
  437. $("#previewImgWrap").html(imgEle);
  438. }
  439. }
  440. });
  441. $(".close_preview").click(function(){
  442. $("#previewCanvas").hide();
  443. })
  444. }
  445. //添加文字弹窗
  446. function popAddText(text){
  447. var title=prompt("请输入内容",text);
  448. if (title!=null && title!=""){
  449. app.addText(title);
  450. }
  451. }
  452. app.mtid = null;
  453. //获取分类 sdzAjax
  454. function getMaterialType(){
  455. if(!app.mtid){
  456. util.tipHtml('loading','正在加载');
  457. sdzAjax.getMaterialType(function(res){
  458. var list = res.list;
  459. var html = '';
  460. var i =0;
  461. res.list.forEach(function(item){
  462. if(i==0){
  463. html+='<span class="active" data-id="'+ item.id +'">'+ item.title +'</span>';
  464. }else{
  465. html+='<span data-id="'+ item.id +'">'+ item.title +'</span>';
  466. }
  467. i++;
  468. });
  469. $("#djsy-source-list-left").html(html);
  470. //
  471. app.mtid = list[0].id;
  472. getMaterial(app.mtid);
  473. changeSelectMaterialType();
  474. });
  475. }
  476. }
  477. function changeSelectMaterialType(){
  478. $("#djsy-source-list-left >span").each(function(){
  479. $(this).unbind('click').click(function(){
  480. $(this).addClass('active').siblings('span').removeClass('active');
  481. var id = $(this).data('id');
  482. if(id&&id != app.mtid){
  483. getMaterial(id);
  484. app.mtid = id;
  485. }
  486. });
  487. })
  488. }
  489. //获取分类下面内容
  490. function getMaterial(id){
  491. sdzAjax.getMaterial(id,function(res){
  492. var html ='';
  493. for(var i=0;i<res.list.length;i++){
  494. html+='<li class="box_item" data-imgUrl="'+ res.list[i].img_url +'">';
  495. html+='<div class="dsjy_trend_wrap">';
  496. html+='<img src="'+res.list[i].img_url+'" />';
  497. html+='<span>'+res.list[i].title+'</span>';
  498. html+='</div>';
  499. html+='</li>';
  500. }
  501. util.hideTip();
  502. $("#djsy_source_contant_ul").html(html);
  503. $(".box_item").each(function(){
  504. $(this).unbind('click').click(function(){
  505. //添加设计图
  506. var imgUrl = $(this).data('imgurl');
  507. app.addImg(imgUrl);
  508. $(".djsy-pop-wrap").hide();
  509. })
  510. })
  511. })
  512. }
  513. //保存diy
  514. function saveCanvas(){
  515. $("#diy_save_diy_btn").click(function(){
  516. var obj = app.mainCanvas._objects;
  517. if(obj.length<=0){
  518. alert("请添加内容");
  519. return false;
  520. }
  521. util.tipHtml('loading','正在提交');
  522. var data = {
  523. id :app.diyId,
  524. img_url:app.returnCanvasImg(100),
  525. content:JSON.stringify(app.returnCanvasJson()),
  526. type:app.typeId
  527. }
  528. sdzAjax.saveDiy(data,function(data){
  529. alert(data.msg + '请到个人中心-我的定制查看');
  530. window.location.href = '/wap/user/diy';
  531. });
  532. });
  533. $("#diy_cart_diy_btn").click(function(){
  534. var obj = app.mainCanvas._objects;
  535. if(obj.length<=0){
  536. alert("请添加内容");
  537. return false;
  538. }
  539. util.tipHtml('loading','正在提交');
  540. var data = {
  541. id :app.diyId,
  542. img_url:app.returnCanvasImg(100),
  543. content:JSON.stringify(app.returnCanvasJson()),
  544. type:app.typeId
  545. }
  546. sdzAjax.addCart(data,function(data){
  547. alert(data.msg);
  548. window.location.href = '/wap/shop/cart';
  549. });
  550. })
  551. }
  552. $(document).ready(function(){
  553. var did = $("#diyId").val();
  554. var typeId = $("#diytype").val();
  555. //展开功能区
  556. $(".add_icon").click(function(){
  557. $(".func_add").toggleClass("func_add_click")
  558. $(".djsy-position-view-wrap").addClass('djsy-position-view-ani')
  559. $("#djsy_full_sceen").fadeOut();
  560. });
  561. //添加图片
  562. $("#diy_bg_img_file").change(function(){
  563. var imgVal = $(this).val();
  564. if(!imgVal){
  565. return false;
  566. }
  567. var formData = new FormData($("#diy_bg_img_form")[0]);
  568. sdzAjax.uploadImg(formData,function(data){
  569. app.addImg(data.img_url);
  570. $("#diy_bg_img_file").val('');
  571. });
  572. });
  573. //添加文字
  574. $("#djsy_add_text_btn").click(function(){
  575. popAddText('输入内容');
  576. });
  577. //设置字体
  578. $("#changeFont > li").each(function(){
  579. $(this).click(function(){
  580. var fontVal = $(this).data('val');
  581. app.setActiveStyle('fontFamily',fontVal);
  582. $("#showFont").text(fontVal);
  583. var className = $(this).find('div')[0].className;
  584. $("#showFont").removeClass().addClass(className);
  585. mui('#djsy_text_edit_popover').popover('toggle');
  586. });
  587. });
  588. //选择项目图片
  589. $("#djsy_add_source_btn").click(function(){
  590. $(".djsy-pop-wrap").show();
  591. getMaterialType();
  592. });
  593. //关闭项目图片
  594. $("#close_pop").click(function(){
  595. $(".djsy-pop-wrap").hide();
  596. });
  597. app.init(did,typeId);
  598. //屏幕自适应
  599. //windowResize();
  600. //层级修改
  601. changeHierarchy();
  602. //获得颜色
  603. getColor();
  604. //预览效果
  605. previewCanvas();
  606. //保存
  607. saveCanvas();
  608. });
  609. })(jQuery);