/***************************************************************** CanvasLib Object: This library allow us to use Canvas in a "browser-agnostic" way @Author: Stefano Ceschi Berrini - Zucchetti Padova *****************************************************************/ if (typeof(CanvasLib)=='undefined') { CanvasLib=new function(){ //Content name this.container; this.canvas; this.scale = 100; this.scaleincrement = 20; this.rotation = 0; //Browser Name this.browserName=navigator.appName; //I hope we are in a Netscape or Opera Browser this.isIE=false; //Namespace for IE is not active atm. Read above this.namespaceactivated=false; //Fill color this.fillColor = [0,0,0]; //Alpha transparency this.alphaChannel = 0.5; //Environment Initialization this.id,this.w,this.h; this.lineWidth = 0.5; this.Init=function(id, w, h){ this.id=id; this.w=w; this.h=h; this.rotation=0; if(!document.createElement('canvas').getContext) this.isIE=true; //if(this.browserName == "Microsoft Internet Explorer"){ //IE Check //this.isIE=true; if(this.isIE){ // setup namespace (WORKING!) if (!document.namespaces["g_vml_"]) { document.namespaces.add('g_vml_', 'urn:schemas-microsoft-com:vml', "#default#VML"); } // setup default css (WORKING!) try{ var ss = document.createStyleSheet(); ss.cssText = "g_vml_\:*{behavior:url(#default#VML)}"; }catch(e){} } //Nothing, we're using a !MS browser var tmpObj = document.getElementById(id); tmpObj.style.width = w + "px"; tmpObj.style.height = h + "px"; tmpObj.style.overflow = "hidden"; this.container = tmpObj; this.canvas = tmpObj; if(!this.isIE){ var UID=AlfaKeyGen(10); tmpObj.innerHTML = ''; tmpObj = document.getElementById(UID); if(tmpObj.getContext){ this.container = tmpObj.getContext("2d"); this.canvas = tmpObj; } } return this; } this.Clear=function(){ if(!this.isIE){ var tmpObj = document.getElementById(this.id); var UID=AlfaKeyGen(10); tmpObj.innerHTML = ''; tmpObj = document.getElementById(UID); if(tmpObj.getContext){ this.container = tmpObj.getContext("2d"); this.canvas = tmpObj; } } else this.container.innerHTML = ""; } /* ******************************************* All the CanvasLib public functions (methods) ******************************************* */ /* @Name: DrawText @Param: */ this.DrawText=function(font,fontsize,x,y,w,h,text){ if(this.isIE){ this.container.innerHTML += '
' + text +'
'; } else{ this.container.font = fontsize + "px " + font; this.container.fillText(text, x, y); } } /* @Name: DrawRect @Param: */ this.DrawRect=function(x,y,w,h,filled){ if(this.isIE){ var col = "rgb("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+")"; if(filled) this.container.innerHTML += ''; else this.container.innerHTML += ''; }else{ if(filled){ this.container.fillStyle = "rgba("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+","+this.alphaChannel+")"; this.container.fillRect(x,y,w,h); this.container.strokeRect(x,y,w,h); }else{ this.container.strokeRect(x,y,w,h); } } } /* @Name: DrawLine @Param: */ this.DrawLine=function(x1,y1,x2,y2,colorline,text,font,fontsize){ if(this.isIE){ var line = document.createElement("g_vml_:line"); var col = "rgb("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+")"; line.style.position = "absolute"; line.style.left = x1; line.style.top = y1; line.from = x1+','+y1; line.to=(x2)+","+(y2); if(colorline){ line.strokecolor = col; } var lineOp = document.createElement("g_vml_:stroke"); lineOp.opacity = this.alphaChannel; line.appendChild(lineOp); if(text){ if(font && fontsize){ var fontObj = document.createElement("div"); fontObj.style.fontFamily = font; fontObj.style.fontSize = fontsize+"px"; fontObj.style.lineHeight = fontsize+"px"; fontObj.innerHTML=text line.appendChild(fontObj); } } this.container.appendChild(line); //this.container.innerHTML += ''; } else{ this.container.strokeStyle = "rgba("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+","+this.alphaChannel+")"; this.container.beginPath(); this.container.moveTo(x1,y1); this.container.lineTo(x2,y2); this.container.lineWidth=this.lineWidth; this.container.stroke(); } } this.DrawDashedLine=function(x1,y1,x2,y2,stepDraw,stepPause){ // manca x IE 9- this.container.strokeStyle = "rgba("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+","+this.alphaChannel+")"; this.container.beginPath(); var i; if(x1==x2){ //verticale for(i=y1; i<=y2; i=i+(stepPause+stepDraw)){ this.container.moveTo(x1,i); this.container.lineTo(x1,i+stepDraw); } } else if(y1==y2){ //orizzontale for(i=x1; i<=x2; i=i+(stepPause+stepDraw)){ this.container.moveTo(i,y1); this.container.lineTo(i+stepDraw,y1); } } this.container.lineWidth=this.lineWidth; this.container.stroke(); } this.DrawRhombus=function(x,y,w,h){ if(this.isIE){ var col = "rgb("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+")"; var sel = document.createElement("g_vml_:shape"); sel.style.position = "absolute"; sel.style.left = x; sel.style.top = y; sel.style.width = w; sel.style.height = h; sel.coordsize=w+ "," +h; sel.fillcolor = col; var interMx = parseInt(x+(w/2)); var interMy = parseInt(y+(h/2)); var path = document.createElement("g_vml_:path"); path.v = "m "+ interMx +","+ y; path.v += " l" +" " + (x+w) +","+ interMy + " "; path.v += interMx + "," + (y+h) + " "; path.v += x +","+ interMy + " x e"; sel.appendChild(path); var selOp = document.createElement("g_vml_:fill"); selOp.opacity = this.alphaChannel; sel.appendChild(selOp); this.container.appendChild(sel); //this.container.innerHTML += ''; }else{ this.container.fillStyle = "rgba("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+","+this.alphaChannel+")"; this.container.beginPath(); this.container.moveTo(x+(w/2),y); this.container.lineTo(x+w,y+(h/2)); this.container.lineTo(x+(w/2),y+h); this.container.lineTo(x,y+(h/2)); this.container.lineTo(x+(w/2),y); this.container.fill(); this.container.stroke(); } } this.DrawSelector=function(x,y,w,h){ var pFirst, pSecond; if(this.isIE){ var col = "rgb("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+")"; var sel = document.createElement("g_vml_:shape"); sel.style.position = "absolute"; sel.style.left = x; sel.style.top = y; sel.style.width = w; sel.style.height = h; sel.coordsize=w+ "," +h; sel.fillcolor = col; pFirst = parseInt((w*15)/100); pSecond = w-pFirst; var interM = parseInt(y+(h/2)); var path = document.createElement("g_vml_:path"); path.v = "m "+ parseInt(x+pFirst) +","+ y; path.v += " l" +" " + parseInt(x+pSecond) +","+ y + " "; path.v += (x+w)+ "," + interM + " "; path.v += parseInt(x+pSecond)+","+ (y+h) + " " ; path.v += parseInt(x+pFirst)+","+ (y+h) + " " + x +","+ interM + " x e"; sel.appendChild(path); var selOp = document.createElement("g_vml_:fill"); selOp.opacity = this.alphaChannel; sel.appendChild(selOp); this.container.appendChild(sel); /* m 0,0: Move to the point (0, 0). l 200,200: Draw a line from the current position to (200, 200). (That's an L, not a one.) e: End drawing. */ }else{ this.container.fillStyle = "rgba("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+","+this.alphaChannel+")"; pFirst = parseInt((w*15)/100); pSecond = w-pFirst; this.container.beginPath(); this.container.moveTo(x+pFirst,y); this.container.lineTo(x+pSecond,y); this.container.lineTo(x+w,y+(h/2)); this.container.lineTo(x+pSecond,y+h); this.container.lineTo(x+pFirst,y+h); this.container.lineTo(x,y+(h/2)); this.container.lineTo(x+pFirst,y); this.container.fill(); this.container.stroke(); } } /* @Name: DrawArc @Param: */ this.DrawArc=function(x,y,radius,filled){ if(this.isIE){ var col, arc, arcOp; if(filled){ col = "rgb("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+")"; arc = document.createElement("g_vml_:oval"); arc.style.position = "absolute"; arc.style.left = x-radius; arc.style.top = y-radius; arc.style.width = radius*2; arc.style.height = radius*2; arc.strokeweight="1pt"; arc.fillcolor=col; arcOp = document.createElement("g_vml_:fill"); arcOp.opacity = this.alphaChannel; arc.appendChild(arcOp); this.container.appendChild(arc); /*this.container.innerHTML += '' + '' */ }else{ col = "rgb("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+")"; arc = document.createElement("g_vml_:oval"); arc.style.position = "absolute"; arc.style.left = x-radius; arc.style.top = y-radius; arc.style.width = radius*2; arc.style.height = radius*2; arc.strokeweight="1pt"; arc.filled="false"; arcOp = document.createElement("g_vml_:stroke"); arcOp.opacity = this.alphaChannel; arc.appendChild(arcOp); this.container.appendChild(arc); /*this.container.innerHTML += '' */ } //obj.innerHTML += ''; }else{ this.container.fillStyle = "rgba("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+","+this.alphaChannel+")"; this.container.beginPath(); this.container.arc(x,y,radius,0,Math.PI*2,true); if(filled){ this.container.fill(); this.container.stroke(); }else{ this.container.stroke(); } } } this.RoundRect = function(x,y,width,height,radius,filled){ if(this.isIE){ //obj.innerHTML += '' if(filled){ var col = "rgb("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+")"; this.container.innerHTML += ''; }else{ this.container.innerHTML += ''; } //obj.innerHTML += ''; }else{ this.container.fillStyle = "rgba("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+","+this.alphaChannel+")"; this.container.beginPath(); this.container.moveTo(x,y+radius); this.container.lineTo(x,y+height-radius); this.container.quadraticCurveTo(x,y+height,x+radius,y+height); this.container.lineTo(x+width-radius,y+height); this.container.quadraticCurveTo(x+width,y+height,x+width,y+height-radius); this.container.lineTo(x+width,y+radius); this.container.quadraticCurveTo(x+width,y,x+width-radius,y); this.container.lineTo(x+radius,y); this.container.quadraticCurveTo(x,y,x,y+radius); if(filled){ this.container.fill(); this.container.stroke(); }else{ this.container.stroke(); } } } /* @Name: DrawFillkeRect @Param: */ this.qCurve=function(mx,my,cp1x,cp1y,x,y){ if(this.isIE){ var col = "rgb("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+")"; this.container.innerHTML += '' //obj.innerHTML += ''; }else{ this.container.strokeStyle = "rgba("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+","+this.alphaChannel+")"; this.container.beginPath(); this.container.moveTo(mx,my); this.container.quadraticCurveTo(cp1x,cp1y,x,y); this.container.stroke(); } } this.qCurves=function(mx,my,cPoints,x,y){ var i, line, lineOp, cFrmX, cFrmY, cToX, cToY; if(this.isIE){ var col = "rgb("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+")"; var pX = 0; var pY = 0; var fX = 0; var fY = 0; for(i=0;i' break; }else{ cFrmX = cPoints[i].x+8; cFrmY = cPoints[i].y+8; cToX = 0; cToY = 0; pX = 0; pY = 0; if(cPoints.length != 1){ cToX = cPoints[i+1].x+8; cToY = cPoints[i+1].y+8; pX = cToX - ((cToX-cFrmX)/2); pY = cFrmY - ((cFrmY-cToY)/2); }else{ pX = x; pY = y; } if(i == 0){ line = document.createElement("g_vml_:curve"); line.from = mx+","+my; line.control1 = cFrmX+","+cFrmY; line.to=pX+","+pY; line.filled="false"; line.strokeweight="1pt"; line.strokecolor=col; lineOp = document.createElement("g_vml_:stroke"); lineOp.opacity = this.alphaChannel; line.appendChild(lineOp); this.container.appendChild(line); //this.container.innerHTML += '' }else{ line = document.createElement("g_vml_:curve"); line.from = fX+","+fY; line.control1 = cFrmX+","+cFrmY; line.to=pX+","+pY; line.filled="false"; line.strokeweight="1pt"; line.strokecolor=col; lineOp = document.createElement("g_vml_:stroke"); lineOp.opacity = this.alphaChannel; line.appendChild(lineOp); this.container.appendChild(line); //this.container.innerHTML += '' } } } }else { this.container.strokeStyle = "rgba("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+","+this.alphaChannel+")"; this.container.beginPath(); this.container.moveTo(mx,my); pX = 0; pY = 0; for(i=0;i'; this.container.innerHTML += str; return; } this.container.save(); this.container.beginPath(); this.container.fillStyle = "rgba("+this.fillColor[0]+","+this.fillColor[1]+","+this.fillColor[2]+","+this.alphaChannel+")"; dimensionX/=2; dimensionY/=2; if(dimensionX40){ this.scale = this.scale - this.scaleincrement; this.refresh(); } } // ZoomIn this.zoomIn = function() { if(this.scale<801){ this.scale = this.scale + this.scaleincrement; this.refresh(); } } // Zoom this.zoom = function(scale) { if(scale>24 && scale<801){ this.scale = scale; this.refresh(); } } /* @Name: SetAttrib @Param: */ this.SetAttrib=function(col,alpha){ var color = HexToRGB(col); var alphac = parseFloat(alpha); this.fillColor = color; this.alphaChannel = alphac; } // Scale canvas this.canvasScale=function() { } this.refresh=function(){ this.canvasScale(); } /*Image*/ this.DrawImage=function(src, x, y, w, h, r){ var img; if(this.isIE){ img = document.createElement("g_vml_:image"); img.style.position = "absolute"; var imgx = new Image(); imgx.onload = function(){ } switch(r) { default : case '0': case '360': img.style.left = x; img.style.top = y; img.style.width = w + "px"; img.style.height = h + "px"; img.src = src; this.container.appendChild(img); break; case '90' : img.style.rotation='90deg'; img.style.top = (h-w)/2; img.style.left = -(h-w)/2; img.style.width = h + "px"; img.style.height = w + "px"; img.src = src; this.container.appendChild(img); break; case '180' : img.style.rotation='180deg'; img.style.left = x; img.style.top = y; img.style.width = w + "px"; img.style.height = h + "px"; img.src = src; this.container.appendChild(img); break; case '270' : case '-90' : img.style.rotation='270deg'; img.style.left = -(h-w)/2; img.style.top = (h-w)/2; img.style.width = h + "px"; img.style.height = w + "px"; img.src = src; this.container.appendChild(img); break; }; imgx.src = src; return imgx; }else{ var ctx = this.container; img = new Image(); img.onload = function(){ switch(r) { default : case '0': case '360': ctx.rotate(r * Math.PI / 180); ctx.drawImage(img,x, y,w,h); break; case '90' : ctx.rotate(r * Math.PI / 180); ctx.drawImage(img, x, -w,h,w); break; case '180' : ctx.rotate(r * Math.PI / 180); ctx.drawImage(img,-w,-h,w,h); break; case '270' : case '-90' : ctx.rotate(r * Math.PI / 180); ctx.drawImage(img, -h, y,h,w); break; }; } img.src = src; return img; } } this.DrawRectImage=function(src, x, y, w, h, r){ if(this.isIE){ var imgx = new Image(); imgx.onload = function(){ } switch(r) { default : case '0': case '360': this.container.innerHTML += ''; break; case '90' : this.container.innerHTML += ''; break; case '180' : this.container.innerHTML += ''; break; case '270' : case '-90' : this.container.innerHTML += ''; break; }; imgx.src = src; return imgx; }else{ var ctx = this.container; var img = new Image(); img.onload = function(){ switch(r) { default : case '0': case '360': if(this.rotation != r ) ctx.rotate(r * Math.PI / 180); ctx.drawImage(img,x, y,w,h); break; case '90' : if(this.rotation != r ) ctx.rotate(r * Math.PI / 180); ctx.drawImage(img, y, -w-x,h,w); break; case '180' : if(this.rotation != r ) ctx.rotate(r * Math.PI / 180); ctx.drawImage(img,-w-x,-y-h,w,h); break; case '270' : case '-90' : if(this.rotation != r ) ctx.rotate(r * Math.PI / 180); ctx.drawImage(img, -h-y, x,h,w); break; }; this.rotation = r; } img.src = src; return img; } } }//endCL }//endIF /* SUPPORT FUNCTIONS */ function HexToRGB(hex){ var R = HexToR(hex); var G = HexToG(hex); var B = HexToB(hex); var tmp = [R,G,B]; return tmp; } function HexToR(h){ return parseInt((cutHex(h)).substring(0,2),16) } function HexToG(h){ return parseInt((cutHex(h)).substring(2,4),16) } function HexToB(h){ return parseInt((cutHex(h)).substring(4,6),16) } function cutHex(h){ return (h.charAt(0)=="#") ? h.substring(1,7):h } function AlfaKeyGen(keyLen){ var res=""; for(var i=0; i++