-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDegas.min.js
1 lines (1 loc) · 7.78 KB
/
Degas.min.js
1
var Degas=function(a){this.canvas=a,this.ctx=this.canvas.getContext("2d"),this.fullClear=!0,this.clearColor="rgba(0,0,0,0.25)",this.children=[]};Degas.prototype.resize=function(a,b){this.canvas.width=a,this.canvas.height=b},Degas.prototype.addChild=function(a){a.uid=this.children.length,this.children.push(a)},Degas.prototype.removeChild=function(a){for(var b=0;b<this.children.length;b++)if(this.children[b].uid==a.uid){this.children.splice(b,1);break}},Degas.prototype.getChildIndex=function(a){for(var b=void 0,c=0;c<this.children.length;c++)if(this.children[c].uid==a.uid){b=c;break}return b},Degas.prototype.bringToFront=function(a){for(;this.getChildIndex(a)<this.children.length-1;)this.swapChildIndex(this.getChildIndex(a),this.getChildIndex(a)+1)},Degas.prototype.bringForward=function(a){this.getChildIndex(a)<this.children.length-1&&this.swapChildIndex(this.getChildIndex(a),this.getChildIndex(a)+1)},Degas.prototype.bringBackward=function(a){this.getChildIndex(a)>0&&this.swapChildIndex(this.getChildIndex(a),this.getChildIndex(a)-1)},Degas.prototype.sendToBack=function(a){for(;this.getChildIndex(a)>0;)this.swapChildIndex(this.getChildIndex(a),this.getChildIndex(a)-1)},Degas.prototype.swapChildIndex=function(a,b){return this.children.splice(b,0,this.children.splice(a,1)[0]),this},Degas.prototype.getMetName=function(a){console.log(this.drawRectangle)},Degas.prototype.render=function(){this.fullClear?this.ctx.clearRect(0,0,window.innerWidth,window.innerHeight):(this.ctx.fillStyle=this.clearColor,this.ctx.fillRect(0,0,window.innerWidth,window.innerHeight));for(var a=0;a<this.children.length;a++){var b=this.children[a];this.ctx.save(),void 0!=b.group&&(this.ctx.translate(b.group.x,b.group.y),this.ctx.rotate(Degas.degreeToRadian(b.group.rotation)),this.ctx.scale(b.group.scaleX,b.group.scaleY),this.ctx.globalAlpha=b.group.opacity),void 0!=b.mask&&(this.ctx.translate(b.mask.x,b.mask.y),this.ctx.rotate(Degas.degreeToRadian(b.mask.rotation)),this.ctx.scale(b.mask.scaleX,b.mask.scaleY),this.ctx.globalAlpha=b.mask.opacity,this["draw"+b.mask.type](b.mask),this.ctx.translate(-b.mask.x,-b.mask.y),this.ctx.clip()),this.ctx.translate(b.x,b.y),this.ctx.rotate(Degas.degreeToRadian(b.rotation)),this.ctx.scale(b.scaleX,b.scaleY),this.ctx.globalAlpha=b.opacity,this["draw"+b.type](b),""!=b.fill&&(this.ctx.fillStyle=b.fill,this.ctx.fill()),""!=b.stroke&&(this.ctx.strokeStyle=b.stroke,this.ctx.lineWidth=b.strokeWidth,this.ctx.lineCap=b.lineCap,this.ctx.lineJoin=b.lineJoin,this.ctx.stroke()),this.ctx.translate(-b.x,-b.y),this.ctx.scale(1,1),this.ctx.rotate(-1*Degas.degreeToRadian(b.rotation)),this.ctx.restore()}},Degas.prototype.drawCube=function(a){this.ctx.beginPath(),this.ctx.rect(-a.width/2,-a.height/2,a.width,a.height),this.ctx.closePath()},Degas.prototype.drawRectangle=function(a){this.ctx.beginPath(),this.ctx.rect(-a.width/2,-a.height/2,a.width,a.height),this.ctx.closePath()},Degas.prototype.drawCircle=function(a){this.ctx.beginPath(),this.ctx.arc(0,0,a.radius,0,2*Math.PI,!1),this.ctx.closePath()},Degas.prototype.drawPath=function(a){if(this.ctx.beginPath(),a.smoothed){if(a.smoothed)if(a.smooth(),a.closed){if(a.closed){this.ctx.moveTo(a.smoothedPoints[a.smoothPointsNumber].x,a.smoothedPoints[a.smoothPointsNumber].y);for(var b=a.smoothPointsNumber+1;b<a.smoothedPoints.length-a.smoothPointsNumber;b++)this.ctx.lineTo(a.smoothedPoints[b].x,a.smoothedPoints[b].y)}}else{this.ctx.moveTo(a.smoothedPoints[0].x,a.smoothedPoints[0].y);for(var b=1;b<a.smoothedPoints.length;b++)this.ctx.lineTo(a.smoothedPoints[b].x,a.smoothedPoints[b].y)}}else{this.ctx.moveTo(a.points[0].x,a.points[0].y);for(var b=1;b<a.points.length;b++)this.ctx.lineTo(a.points[b].x,a.points[b].y);a.closed&&this.ctx.closePath()}},Degas.prototype.drawRegularPolygon=function(a){this.drawPath(a.path)},Degas.prototype.drawArc=function(a){this.ctx.beginPath(),this.ctx.arc(0,0,a.radius,Degas.degreeToRadian(a.startAngle),Degas.degreeToRadian(a.endAngle),a.antiClockwise),this.ctx.closePath()},Degas.prototype.drawImage=function(a){this.ctx.drawImage(a.img,-a.width/2,-a.height/2)},Degas.prototype.drawText=function(a){this.ctx.fillStyle=a.fill,this.ctx.font=a.fontStyle+" "+a.fontSize+" "+a.fontFamily,this.ctx.textAlign=a.textAlign,this.ctx.fillText(a.txt,0,0)},Degas.BaseShape=function(){this.uid=0,this.x=0,this.y=0,this.rotation=0,this.scaleX=1,this.scaleY=1,this.opacity=1,this.fill="",this.stroke="",this.strokeWidth=1,this.lineCap="butt",this.lineJoin="miter",this.mask,this.group},Degas.Cube=function(a){this.type="Cube",this.width=a||50,this.height=a||50},Degas.Cube.prototype=new Degas.BaseShape,Degas.Rectangle=function(a,b){this.type="Rectangle",this.width=a,this.height=b},Degas.Rectangle.prototype=new Degas.BaseShape,Degas.Circle=function(a){this.type="Circle",this.radius=a||50},Degas.Circle.prototype=new Degas.BaseShape,Degas.RegularPolygon=function(a,b){this.type="RegularPolygon",this.radius=a||50;for(var c=[],d=2*Math.PI/b,e=0;b>e;e++)c.push(new Degas.Point(this.radius*Math.sin(d*e),this.radius*Math.cos(d*e)));this.path=new Degas.Path(c)},Degas.RegularPolygon.prototype=new Degas.BaseShape,Degas.Image=function(a){var b=this;this.type="Image",this.src=a||"",this.loaded=!1,this.img=new Image,this.img.onload=function(){b.loaded=!0,b.width=b.img.width,b.height=b.img.height},""!=this.src&&(this.img.src=this.src)},Degas.Image.prototype=new Degas.BaseShape,Degas.Text=function(a,b){this.type="Text",this.txt=a,this.fontFamily=b.fontFamily||"Arial",this.fontSize=b.fontSize||"16px",this.fontStyle=b.fontStyle||"normal",this.textAlign=b.textAlign||"center"},Degas.Text.prototype=new Degas.BaseShape,Degas.Path=function(a){this.type="Path",this.points=a,this.smoothedPoints,this.smoothed=!1,this.closed=!1,this.tension=.5,this.smoothPointsNumber=16},Degas.Path.prototype=new Degas.BaseShape,Degas.Path.prototype.smooth=function(){var a=[];this.closed&&(a.push(this.points[this.points.length-1].x),a.push(this.points[this.points.length-1].y));for(var b=0;b<this.points.length;b++)a.push(this.points[b].x),a.push(this.points[b].y);this.closed&&(a.push(this.points[0].x),a.push(this.points[0].y),a.push(this.points[1].x),a.push(this.points[1].y));var c=Degas.smoothPath(a,this.tension,!1,this.smoothPointsNumber);this.smoothedPoints=[];for(var b=0;b<c.length;b+=2)this.smoothedPoints.push(new Degas.Point(c[b],c[b+1]));this.smoothed=!0},Degas.Path.prototype.unSmooth=function(){this.smoothed=!1},Degas.Path.prototype.add=function(a){this.points.push(a)},Degas.Path.prototype.shuffle=function(a){for(var b=0;b<this.points.length;b++)this.points[b].moveRandom(a)},Degas.Arc=function(a,b,c,d){this.type="Arc",this.startAngle=a||0,this.endAngle=b||45,this.radius=c||50,this.antiClockwise=d||!1},Degas.Arc.prototype=new Degas.BaseShape,Degas.Point=function(a,b){this.type="Point",this.x=a||0,this.y=b||0},Degas.Point.prototype.moveRandom=function(a){this.x+=Math.round(2*Math.random()*a)-a,this.y+=Math.round(2*Math.random()*a)-a},Degas.Group=function(){this.uid=0,this.x=0,this.y=0,this.rotation=0,this.scaleX=1,this.scaleY=1,this.opacity=1},Degas.Group.prototype.add=function(a){a.group=this},Degas.Group.prototype.remove=function(a){a.group=void 0},Degas.smoothPath=function(a,b,c,d){b="undefined"!=typeof b?b:.5,c=c?c:!1,d=d?d:16;var e,f,g,h,i,j,k,l,m,n,o,p,q,r=[],s=[];for(r=a.slice(0),c?(r.unshift(r[r.length-1]),r.unshift(r[r.length-2]),r.unshift(r[r.length-1]),r.unshift(r[r.length-2]),r.push(r[0]),r.push(r[1])):(r.unshift(r[1]),r.unshift(r[0]),r.push(r[r.length-2]),r.push(r[r.length-1])),q=2;q<r.length-4;q+=2)for(p=0;d>=p;p++)g=(r[q+2]-r[q-2])*b,h=(r[q+4]-r[q])*b,i=(r[q+3]-r[q-1])*b,j=(r[q+5]-r[q+1])*b,o=p/d,k=2*Math.pow(o,3)-3*Math.pow(o,2)+1,l=-(2*Math.pow(o,3))+3*Math.pow(o,2),m=Math.pow(o,3)-2*Math.pow(o,2)+o,n=Math.pow(o,3)-Math.pow(o,2),e=k*r[q]+l*r[q+2]+m*g+n*h,f=k*r[q+1]+l*r[q+3]+m*i+n*j,s.push(e),s.push(f);return s},Degas.degreeToRadian=function(a){return a*Math.PI/180},Degas.radianToDegree=function(a){return 180*a/Math.PI};