forked from Enderer/sixteensegment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment.min.js
13 lines (13 loc) · 6.08 KB
/
segment.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
function ElementArray(b){this.SetCount(b||0)}ElementArray.prototype.NullMask=16;ElementArray.prototype.SetCount=function(b){var d=parseInt(b,10);if(isNaN(d))throw"Invalid element count: "+b;this.Elements=[d];for(b=0;b<d;b++)this.Elements[b]=0};
ElementArray.prototype.SetText=function(b,d){null===b&&(b="");b=b.toString();for(var a=0;a<this.Elements.length;a++)this.SetElementValue(a,0);if(0!==b.length)for(a=0;a<this.Elements.length&&a<b.length;a++){var c=d[b[a]];if(null===c||void 0===c)c=this.NullMask;this.SetElementValue(a,c)}};ElementArray.prototype.SetElementValue=function(b,d){0<=b&&b<this.Elements.length&&(this.Elements[b]=parseInt(d,10))};function SegmentCanvas(){this.SegmentWidth=0.16;this.SegmentInterval=0.05;this.BevelWidth=0.06;this.SideBevelEnabled=!1;this.FillLight="#86FD06";this.FillDark="#004400";this.StrokeLight="#007700";this.StrokeDark="#440044";this.StrokeWidth=0;this.Spacing=this.Padding=10;this.Y=this.X=0;this.Width=200;this.Height=100;this.ElementArray=new ElementArray(1)}SegmentCanvas.prototype.DispayText=function(b){this.CalcPoints();this.ElementArray.SetText(b,this.CharacterMasks);this.Draw(this.Canvas,this.ElementArray.Elements)};
SegmentCanvas.prototype.Draw=function(b,d){var a=b.getContext("2d");a.clearRect(this.X,this.Y,this.Width,this.Height);a.save();var c=this.CalcElementDimensions().Width;a.translate(this.X,this.Y);a.translate(this.Padding,this.Padding);for(var e=0;e<d.length;e++){for(var h=d[e],k=0;k<this.Points.length;k++){var g=h&1<<k?this.FillLight:this.FillDark,l=h&1<<k?this.StrokeLight:this.StrokeDark;a.lineWidth=this.StrokeWidth;a.strokeStyle=l;a.fillStyle=g;a.beginPath();a.moveTo(this.Points[k][0].x,this.Points[k][0].y);
for(g=1;g<this.Points[k].length;g++)a.lineTo(this.Points[k][g].x,this.Points[k][g].y);a.closePath();a.fill();0<this.StrokeWidth&&a.stroke()}a.translate(c+this.Spacing,0)}a.restore()};SegmentCanvas.prototype.SetCount=function(b){this.ElementArray.SetCount(b)};SegmentCanvas.prototype.GetCount=function(){return this.ElementArray.Elements.length};
SegmentCanvas.prototype.CalcElementDimensions=function(){var b=this.ElementArray.Elements.length,d=this.Height,d=d-2*this.Padding,a=this.Width,a=a-this.Spacing*(b-1),a=a-2*this.Padding;return{Width:a/b,Height:d}};SegmentCanvas.prototype.FlipVertical=function(b,d){for(var a=[],c=0;c<b.length;c++)a[c]={},a[c].x=b[c].x,a[c].y=d-b[c].y;return a};SegmentCanvas.prototype.FlipHorizontal=function(b,d){for(var a=[],c=0;c<b.length;c++)a[c]={},a[c].x=d-b[c].x,a[c].y=b[c].y;return a};
SegmentCanvas.GetRGB=function(b){/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(b);b=parseInt(b,16);return{R:b>>16&255,G:b>>8&255,B:b&255}};SegmentCanvas.Lerp=function(b,d,a){return b+(d-b)*a};SegmentCanvas.LerpColor=function(b,d,a){var c=hexToRgb(b),e=hexToRgb(d),h=c.G;d=c.B;var k=e.G;b=e.B;c=SegmentCanvas.Lerp(c.R,e.R,a);h=SegmentCanvas.Lerp(h,k,a);a=SegmentCanvas.Lerp(d,b,a);return rgbToHex(c,h,a)};
function hexToRgb(b){b=b.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i,function(b,a,c,e){return a+a+c+c+e+e});return(b=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(b))?{R:parseInt(b[1],16),G:parseInt(b[2],16),B:parseInt(b[3],16)}:null}function rgbToHex(b,d,a){return"#"+parseInt(16777216+(b<<16)+(d<<8)+a,10).toString(16).slice(1)};function SixteenSegment(b,d,a,c,e,h){this.X=e||0;this.Y=h||0;this.Width=a||d.width;this.Height=c||d.height;this.Canvas=d;this.CalcPoints();this.SetCount(b)}SixteenSegment.prototype=new SegmentCanvas;
SixteenSegment.prototype.CalcPoints=function(){var b=this.CalcElementDimensions(),d=b.Width,b=b.Height,a=this.SegmentWidth*d,c=this.SegmentInterval*d,e=this.BevelWidth*a,h=this.SideBevelEnabled?1:0,k=0.8*a,g=b/d,l=Math.SQRT2,n=Math.sqrt(3),p=d/2-a/2,q=d/2,u=a/2,s=d/2+a/2,t=d-a,m=b/2-a/2,v=d-a/2,r=b/2,w=b/2+a/2,f=[];f[0]=[{x:2*e+c/l,y:0},{x:q-c/2-a/2*h,y:0},{x:q-c/2,y:u},{x:p-c/2,y:a},{x:a+c/l,y:a},{x:e+c/l,y:0+e}];f[9]=[{x:s+c/l,y:m},{x:t-c/2*n,y:m},{x:v-c/2*n,y:r},{x:t-c/2*n,y:w},{x:s+c/l,y:w},{x:q+
c/l,y:r}];f[2]=[{x:d,y:0+2*e+c/l},{x:d,y:r-c/2-a/2*h},{x:v,y:r-c/2},{x:t,y:m-c/2},{x:t,y:a+c/l},{x:d-e,y:0+e+c/l}];f[11]=[{x:s,y:a+c/2*n},{x:s,y:m-c/l},{x:q,y:r-c/l},{x:p,y:m-c/l},{x:p,y:a+c/2*n},{x:q,y:u+c/2*n}];f[10]=[{x:(a+k)/g+c,y:a+c},{x:p-c,y:p*g-k-c},{x:p-c,y:m-c},{x:(m-k)/g-c,y:m-c},{x:a+c,y:a*g+k+c},{x:a+c,y:a+c}];f[1]=this.FlipHorizontal(f[0],d);f[3]=this.FlipVertical(f[2],b);f[4]=this.FlipVertical(f[0],b);f[5]=this.FlipHorizontal(f[4],d);f[6]=this.FlipHorizontal(f[3],d);f[7]=this.FlipHorizontal(f[2],
d);f[8]=this.FlipHorizontal(f[9],d);f[12]=this.FlipHorizontal(f[10],d);f[13]=this.FlipVertical(f[12],b);f[14]=this.FlipVertical(f[11],b);f[15]=this.FlipVertical(f[10],b);this.Points=f};
SixteenSegment.prototype.CharacterMasks=function(){return{" ":0,"":0,"0":37119,1:4108,2:887,3:575,4:908,5:955,6:1019,7:15,8:1023,9:911,A:975,B:19007,C:243,D:18495,E:1011,F:451,G:763,H:972,I:18483,J:124,K:12736,L:240,M:5324,N:9420,O:255,P:967,Q:8447,R:9159,S:955,T:18435,U:252,V:37056,W:41164,X:46080,Y:17284,Z:36915,"-":768,"?":16903,"+":19200,"*":65280}}();function SevenSegment(b,d,a,c,e,h){this.X=e||0;this.Y=h||0;this.Width=a||d.width;this.Height=c||d.height;this.Canvas=d;this.CalcPoints();this.ElementArray.SetCount(b)}SevenSegment.prototype=new SegmentCanvas;
SevenSegment.prototype.CalcPoints=function(){var b=this.CalcElementDimensions(),d=b.Width,b=b.Height,a=this.SegmentWidth*d,c=this.SegmentInterval*d,e=this.BevelWidth*a/a,h=Math.SQRT2,k=Math.sqrt(3),g=[];g[0]=[{x:2*a*e+c/h,y:0},{x:d-2*a*e-c/h,y:0},{x:d-a*e-c/h,y:a*e},{x:d-a-c/h,y:a},{x:a+c/h,y:a},{x:a*e+c/h,y:a*e}];g[1]=[{x:d,y:2*a*e+c/h},{x:d,y:b/2-0.5*c},{x:d-a/2,y:b/2-0.5*c},{x:d-a,y:b/2-a/2-0.5*c},{x:d-a,y:a+c/h},{x:d-a*e,y:a*e+c/h}];g[6]=[{x:a+c/2*k,y:b/2-a/2},{x:d-a-c/2*k,y:b/2-a/2},{x:d-a/2-
c/2*k,y:b/2},{x:d-a-c/2*k,y:b/2+a/2},{x:a+c/2*k,y:b/2+a/2},{x:a/2+c/2*k,y:b/2}];g[2]=this.FlipVertical(g[1],b);g[3]=this.FlipVertical(g[0],b);g[4]=this.FlipHorizontal(g[2],d);g[5]=this.FlipHorizontal(g[1],d);this.Points=g};SevenSegment.prototype.CharacterMasks=function(){return{" ":0,"":0,"0":63,1:6,2:91,3:79,4:102,5:109,6:125,7:7,8:127,9:103,A:119,B:127,C:57,D:63,E:121,F:113,G:125,H:118,I:6,J:30,K:112,L:56,M:55,N:55,O:63,P:115,Q:63,R:119,S:109,T:7,U:62,V:62,W:62,X:112,Y:114,Z:91,"-":64}}();