加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
YYGraft.min.js 3.02 KB
一键复制 编辑 原始数据 按行查看 历史
/* YYGraft 1.0.1 (2017-09-20), Copyright (C) r9it.com,*/
!function(a){a.fn.enable=function(){a(this).addClass("active"),a(this).removeAttr("disabled")},a.fn.disable=function(){a(this).removeClass("active"),a(this).attr("disabled",!0)};var b=function(b){function c(b){q=!0,s.push(m.getImageData(0,0,k.width,k.height)),m.beginPath();var c=a(l).offset();m.moveTo(b.pageX-c.left,b.pageY-c.top)}function d(){q=!1,t=[],o.disable(),1==s.length&&(n.enable(),p.enable())}function e(b){if(q){var c=a(l).offset(),d=b.pageX-c.left,e=b.pageY-c.top;m.lineTo(d,e),m.stroke()}}function f(){if(s.length>0){t.push(m.getImageData(0,0,k.width,k.height));var a=s.pop();m.putImageData(a,0,0),o.enable(),0==s.length&&n.disable()}}function g(){if(t.length>0){s.push(m.getImageData(0,0,k.width,k.height));var a=t.pop();m.putImageData(a,0,0),n.enable(),0==t.length&&o.disable()}}function h(){m.clearRect(0,0,m.canvas.width,m.canvas.height),s=[],t=[],n.disable(),o.disable(),p.disable()}function i(b){b||(b=a(".colorBar .active:eq(0)").data("color")),m.strokeStyle=b,m.shadowColor=b}function j(){if(a("#picBoard img").length>0){var b=new Image;b.src=a("#picBoard img:eq(0)").attr("src"),b.onload=function(){m.save(),m.shadowBlur=0,m.shadowColor="#FFF",m.globalCompositeOperation="destination-atop",m.drawImage(this,0,0,k.width,k.height),m.restore(),k.save(l.toDataURL("image/png"))}}else k.save(l.toDataURL("image/png"))}var k={width:360,height:300,save:function(a){}};b=b||{},a.extend(k,b);var l=a("#"+k.canvasId)[0];l.width=k.width,l.height=k.height;var m=l.getContext("2d");m.lineCap="round",m.lineJoin="round",i();var n=a("#J_prevStep"),o=a("#J_nextStep"),p=a("#J_clearBoard"),q=!1,r=!1,s=[],t=[],u={};return a("#picBoard").css({width:k.width+"px",height:k.height+"px"}),l.onmousedown=c,l.onmouseup=d,l.onmouseout=d,l.onmousemove=e,n.on("click",f),o.on("click",g),p.on("click",h),a("#clearSetting").on("click",function(){m.lineWidth=1,i(a(".colorBar span:first").data("color")),m.shadowBlur=0,alert("画笔已重新初始化,请重新配置画笔。")}),a("#J_canvas_bg").on("change",function(){if(a("#picBoard img").length>0)return void a("#picBoard img:eq(0)").attr("src",window.URL.createObjectURL(this.files[0]));var b='<img src="'+window.URL.createObjectURL(this.files[0])+'" width="'+k.width+'" height="'+k.height+'" />';a("#picBoard").append(b),a("#J_removeImg").enable()}),a("#J_removeImg").on("click",function(){a("#picBoard").empty(),a(this).disable()}),a("#J_saveImg").on("click",j),a("#scrawl-main .brush-size").on("click",function(){m.restore(),m.lineWidth=parseInt(a(this).text()),r=!1}),a("#scrawl-main .blur-size").on("click",function(){m.shadowBlur=parseInt(a(this).text())}),a("#scrawl-main .eraser-size").on("click",function(){1!=r&&(r=!0,m.save(),m.lineCap="round",m.lineJoin="round",m.lineWidth=10,m.globalCompositeOperation="destination-out")}),a("#scrawl-main .colorBar span").on("click",function(){a("#scrawl-main .colorBar .active").removeClass("active"),a(this).addClass("active"),i(a(this).data("color"))}),u.nextStep=g,u.prevStep=g,u.setColor=i,u.save=j,u};window.Canvas=b}(jQuery);
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化