forked from caleb531/jcanvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jcanvas.min.js
8 lines (7 loc) · 8.91 KB
/
jcanvas.min.js
1
2
3
4
5
6
7
8
/*
jCanvas v3.6
Copyright 2011, Caleb Evans
Licensed under the MIT license
http://calebevans.me/projects/jcanvas/license.html
*/
(function(f,b,e,d,g){function c(h,i){if(h===g){c.prefs=f.extend({},c.defaults)}else{if(i===true){c.defaults=f.extend({},c.defaults,h);c.prefs=f.extend({},c.defaults)}else{c.prefs=f.extend({},c.prefs,h)}}return this}c.defaults={width:0,height:0,cornerRadius:0,fillStyle:"transparent",strokeStyle:"transparent",strokeWidth:1,strokeCap:"butt",strokeJoin:"miter",rounded:false,shadowX:0,shadowY:0,shadowBlur:3,shadowColor:"transparent",opacity:1,compositing:"source-over",mask:false,x:0,y:0,x1:0,y1:0,x2:0,y2:0,radius:0,start:0,end:360,ccw:false,inDegrees:true,fromCenter:true,closed:false,sides:3,points:5,angle:0,text:"",font:"normal 12pt sans-serif",align:"center",baseline:"middle",source:"",repeat:"repeat"};c.prefs=f.extend({},c.defaults);c.retro=false;c.setGlobals=function(h,i){h.fillStyle=i.fillColor||i.fillStyle;h.strokeStyle=i.strokeColor||i.strokeStyle;h.lineWidth=i.strokeWidth;h.lineCap=i.strokeCap;h.lineJoin=i.strokeJoin;if(i.rounded===true){h.lineCap="round";h.lineJoin="round"}h.shadowOffsetX=i.shadowX;h.shadowOffsetY=i.shadowY;h.shadowBlur=i.shadowBlur;h.shadowColor=i.shadowColor;h.globalAlpha=i.globalAlpha||i.opacity;h.globalCompositeOperation=i.compositing};c.closePath=function(h,i){if(i.mask===true){h.save();h.clip()}if(i.closed===true){h.closePath();h.fill();h.stroke()}else{h.fill();h.stroke();h.closePath()}};c.checkUnits=function(h){if(h.inDegrees===true){return e.PI/180}else{return 1}};c.rotate=function(i,k,j,h){if(k.fromCenter===false){k.x+=j/2;k.y+=h/2}k.toRad=c.checkUnits(k);i.save();i.translate(k.x,k.y);i.rotate(k.angle*k.toRad);i.translate(-k.x,-k.y)};f.fn.loadCanvas=function(h){return this[0].getContext(h||"2d")};f.fn.draw=function(j){var h,i;for(i=0;i<this.length;i+=1){h=this[i].getContext("2d");j.call(this[i],h)}};f.fn.gradient=function(j){var h=this.loadCanvas(),o=f.extend({},c.prefs,j),n,m,l=0,k=1;if(o.r1!==g&&o.r2!==g){n=h.createRadialGradient(o.x1,o.y1,o.r1,o.x2,o.y2,o.r2)}else{n=h.createLinearGradient(o.x1,o.y1,o.x2,o.y2)}while(o["c"+k]!==g){l+=1;k+=1}for(k=1;k<=l;k+=1){m=e.round((100/(l-1))*(k-1))/100;if(o["s"+k]===g){o["s"+k]=m}n.addColorStop(o["s"+k],o["c"+k])}return n};f.fn.pattern=function(j){var h=this.loadCanvas(),n=f.extend({},c.prefs,j),i=new d(),l;i.src=n.source;function k(){if(i.complete===true){l=h.createPattern(i,n.repeat);return true}else{return false}}function m(){k();if(n.load){n.load.call(this[0],l)}}if(n.load){i.onload=m}else{if(k()===false){i.onload=m}}return l};f.fn.clearCanvas=function(i){var h,j,k=f.extend({},c.prefs,i);for(j=0;j<this.length;j+=1){h=this[j].getContext("2d");c.rotate(h,k,k.width,k.height);if(i===g){h.clearRect(0,0,this[j].width,this[j].height)}else{h.clearRect(k.x-k.width/2,k.y-k.height/2,k.width,k.height)}}return this};f.fn.saveCanvas=function(){var h,i;for(i=0;i<this.length;i+=1){h=this[i].getContext("2d");h.save()}return this};f.fn.restoreCanvas=function(){var h,i;for(i=0;i<this.length;i+=1){h=this[i].getContext("2d");h.restore()}return this};f.fn.scaleCanvas=function(i){var h,j,k=f.extend({},c.prefs,i);for(j=0;j<this.length;j+=1){h=this[j].getContext("2d");h.save();h.translate(k.x,k.y);h.scale(k.width,k.height);h.translate(-k.x,-k.y)}return this};f.fn.translateCanvas=function(i){var h,j,k=f.extend({},c.prefs,i);for(j=0;j<this.length;j+=1){h=this[j].getContext("2d");h.save();h.translate(k.x,k.y)}return this};f.fn.rotateCanvas=function(i){var h,j,k=f.extend({},c.prefs,i);k.toRad=c.checkUnits(k);for(j=0;j<this.length;j+=1){h=this[j].getContext("2d");h.save();h.translate(k.x,k.y);h.rotate(k.angle*k.toRad);h.translate(-k.x,-k.y)}return this};f.fn.drawRect=function(m){var p,l,k=f.extend({},c.prefs,m),j,o,i,n,h;for(l=0;l<this.length;l+=1){p=this[l].getContext("2d");c.setGlobals(p,k);c.rotate(p,k,k.width,k.height);if(k.cornerRadius>0){j=k.x-k.width/2;o=k.y-k.height/2;i=k.x+k.width/2;n=k.y+k.height/2;h=k.cornerRadius;if((i-j)-(2*h)<0){h=(i-j)/2}if((n-o)-(2*h)<0){h=(n-o)/2}p.beginPath();p.moveTo(j+h,o);p.lineTo(i-h,o);p.arc(i-h,o+h,h,3*e.PI/2,e.PI*2,false);p.lineTo(i,n-h);p.arc(i-h,n-h,h,0,e.PI/2,false);p.lineTo(j+h,n);p.arc(j+h,n-h,h,e.PI/2,e.PI,false);p.lineTo(j,o+h);p.arc(j+h,o+h,h,e.PI,3*e.PI/2,false);p.fill();p.stroke();p.closePath()}else{p.beginPath();p.rect(k.x-k.width/2,k.y-k.height/2,k.width,k.height);p.restore();c.closePath(p,k)}}return this};f.fn.drawArc=function(i){var h,j,k=f.extend({},c.prefs,i);if(k.inDegrees===false&&k.end===360){k.end=e.PI*2}for(j=0;j<this.length;j+=1){h=this[j].getContext("2d");c.setGlobals(h,k);c.rotate(h,k,k.radius,k.radius);h.beginPath();h.arc(k.x,k.y,k.radius,(k.start*k.toRad)-(e.PI/2),(k.end*k.toRad)-(e.PI/2),k.ccw);h.restore();c.closePath(h,k)}return this};f.fn.drawEllipse=function(i){var h,k,l=f.extend({},c.prefs,i),j=l.width*(4/3);for(k=0;k<this.length;k+=1){h=this[k].getContext("2d");c.setGlobals(h,l);c.rotate(h,l,l.width,l.height);h.beginPath();h.moveTo(l.x,l.y-l.height/2);h.bezierCurveTo(l.x-j/2,l.y-l.height/2,l.x-j/2,l.y+l.height/2,l.x,l.y+l.height/2);h.bezierCurveTo(l.x+j/2,l.y+l.height/2,l.x+j/2,l.y-l.height/2,l.x,l.y-l.height/2);h.restore();c.closePath(h,l)}return this};f.fn.drawLine=function(j){var i,n,o=f.extend({},c.prefs,j),h=2,m=0,k=0;for(n=0;n<this.length;n+=1){i=this[n].getContext("2d");c.setGlobals(i,o);i.beginPath();i.moveTo(o.x1,o.y1);while(1){m=o["x"+h];k=o["y"+h];if(m!==g&&k!==g){i.lineTo(m,k)}else{break}h+=1}c.closePath(i,o)}return this};f.fn.drawQuad=function(n){var q,m,k=f.extend({},c.prefs,n),j=2,i=0,h=0,p=0,o=0;for(m=0;m<this.length;m+=1){q=this[m].getContext("2d");c.setGlobals(q,k);q.beginPath();q.moveTo(k.x1,k.y1);while(1){i=k["x"+j];h=k["y"+j];p=k["cx"+(j-1)];o=k["cy"+(j-1)];if(i!==g&&h!==g&&p!==g&&o!==g){q.quadraticCurveTo(p,o,i,h)}else{break}j+=1}c.closePath(q,k)}return this};f.fn.drawBezier=function(s){var t,r,o=f.extend({},c.prefs,s),n=2,j=1,m=0,k=0,q=0,i=0,p=0,h=0;for(r=0;r<this.length;r+=1){t=this[r].getContext("2d");c.setGlobals(t,o);t.beginPath();t.moveTo(o.x1,o.y1);while(1){m=o["x"+n];k=o["y"+n];q=o["cx"+j];i=o["cy"+j];p=o["cx"+(j+1)];h=o["cy"+(j+1)];if(m!==g&&k!==g&&q!==g&&i!==g&&p!==g&&h!==g){t.bezierCurveTo(q,i,p,h,m,k)}else{break}n+=1;j+=2}c.closePath(t,o)}return this};f.fn.drawText=function(i){var h,j,k=f.extend({},c.prefs,i);for(j=0;j<this.length;j+=1){h=this[j].getContext("2d");c.setGlobals(h,k);h.textBaseline=k.baseline;h.textAlign=k.align;h.font=k.font;h.strokeText(k.text,k.x,k.y);h.fillText(k.text,k.x,k.y);h.restore()}return this};f.fn.drawImage=function(n){var p,i,m,k=f.extend({},c.prefs,n),l=new d(),h;l.src=k.source;function o(q){if(l.complete===true){h=l.width/l.height;if(k.width&&k.height){l.width=k.width;l.height=k.height}else{if(k.width&&!k.height){l.width=k.width;l.height=l.width/h}else{if(!k.width&&k.height){l.height=k.height;l.width=l.height*h}}}c.rotate(q,k,l.width,l.height);q.drawImage(l,k.x-l.width/2,k.y-l.height/2,l.width,l.height);q.restore();return true}else{return false}}function j(){o(p);if(k.load){k.load.call(i)}}for(m=0;m<this.length;m+=1){i=this[m];p=i.getContext("2d");c.setGlobals(p,k);if(k.load){l.onload=j}else{if(o(p)===false){l.onload=j}}}return this};f.fn.drawPolygon=function(o){var s,n,l=f.extend({},c.prefs,o),t=e.PI/l.sides,k=(e.PI/2)+t,r=(e.PI*2)/l.sides,j,q,h,p,m;l.apothem=e.cos(r/2)*l.radius;l.closed=true;if(l.sides>=3){for(n=0;n<this.length;n+=1){s=this[n].getContext("2d");c.setGlobals(s,l);c.rotate(s,l,l.radius,l.radius);s.beginPath();for(m=0;m<l.sides;m+=1){j=e.round(l.x+(l.radius*e.cos(k)));q=e.round(l.y+(l.radius*e.sin(k)));h=e.round(l.y+((l.apothem+l.apothem*l.projection)*e.cos(k+t)));p=e.round(l.y+((l.apothem+l.apothem*l.projection)*e.sin(k+t)));if(m===0){s.moveTo(j,q)}else{s.lineTo(j,q)}if(l.projection!==g){s.lineTo(h,p)}k+=r}s.restore();c.closePath(s,l)}}return this};f.fn.setPixels=function(p){var r,j,o,m,k=f.extend({},c.prefs,p),h,l,n,q;for(o=0;o<this.length;o+=1){j=this[o];r=j.getContext("2d");if(!k.x&&!k.y&&!k.width&&!k.height){k.width=j.width;k.height=j.height;k.x=k.width/2;k.y=k.height/2}c.rotate(r,k,k.width,k.height);h=r.getImageData(k.x-k.width/2,k.y-k.height/2,k.width,k.height);l=h.data;n=l.length;q=[];if(k.each!==g){for(m=0;m<n;m+=4){q=k.each.call(j,l[m],l[m+1],l[m+2],l[m+3]);l[m]=q[0];l[m+1]=q[1];l[m+2]=q[2];l[m+3]=q[3]}}r.putImageData(h,k.x-k.width/2,k.y-k.height/2);r.restore()}return this};c.layers=[];c.addLayer=function(h){var i=f.extend({},c.prefs,h);c.layers.push(i);return i};f.fn.drawLayers=function(h){var j,k=c.layers.length,n,m,l;for(m=0;m<this.length;m+=1){j=this[m].getContext("2d");if(h===true){j.clearRect(0,0,this[m].width,this[m].height)}for(l=0;l<k;l+=1){n=c.layers[l];if(n.fn){f.fn[n.fn].call(this.eq(m),n)}}}return this};var a=f.event.fix;f.event.fix=function(h){h=a.call(f.event,h);if(h.layerX===g&&h.layerY===g){h.layerX=h.offsetX;h.layerY=h.offsetY}return h};c.retrofit=function(){c.retro=true;f.fn.drawQuadCurve=f.fn.drawQuad;f.fn.drawBezierCurve=f.fn.drawBezier;f.fn.canvasDefaults=c;f.fn.canvas=c;c.queue=c.layers;c.create=c.addLayer;f.fn.drawQueue=f.fn.drawLayers;return f};return(f.jCanvas=c)}(jQuery,document,Math,Image));