Javascript examples for Canvas:Example
laying out labels on a gauge
<!doctype html> <html lang="en"> <head> <style> body{ background-color: ivory; } #wrapper{ position:relative; } canvas{ position:absolute; left:40px; top:5px; border:1px solid red;} #amount{ position:absolute; left:1px; top:5px; margin-bottom:15px; width:23px; border:0; color:#f6931f; font-weight:bold; } #slider-vertical{ position:absolute; left:5px; top:40px; width:15px; height:225px; border:0px; color:#f6931f; font-weight:bold; } </style> <link rel="stylesheet" href="https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.9.1.js"></script> <script src="https://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> <script> $(function() {/*from w ww. ja v a2 s .c o m*/ var canvas=document.getElementById("canvas"); var ctx=canvas.getContext("2d"); var PI=Math.PI; var cx=200; var cy=200; var radius=100; var min=0; var max=1000; var minBound=250; var maxBound=750; var arcWidth=40; var tickWidth=4; var tickValue=400; var tickColor="black"; $( "#slider-vertical" ).slider({ orientation: "vertical", range: "min", min: min, max: max, value:tickValue, slide:function( event, ui ) { $( "#amount" ).val(ui.value); draw(ui.value); } }); $( "#amount" ).val( $( "#slider-vertical" ).slider( "value" ) ); draw(tickValue); function draw(value){ tickValue=value; tickColor="green"; if(tickValue<minBound){tickColor="blue";} if(tickValue>maxBound){tickColor="red";} // ctx.clearRect(0,0,canvas.width,canvas.height); drawArc(); drawTick(minBound,"gray"); drawTick(maxBound,"gray"); drawTick(tickValue,tickColor,10); drawLabel(minBound,"gray",20,18); drawLabel(maxBound,"gray",20,18); drawLabel(tickValue,tickColor,55,24); } function drawArc(){ ctx.beginPath(); ctx.arc(cx,cy,radius,0,PI,true); ctx.lineWidth=arcWidth; ctx.strokeStyle="lightgray"; ctx.stroke(); } function drawTick(tickValue,color,extension){ var plus=extension||0; var angle=PI+tickValue/(max-min)*PI; var x1=cx+(radius-arcWidth/2)*Math.cos(angle); var y1=cy+(radius-arcWidth/2)*Math.sin(angle); var x2=cx+(radius+arcWidth/2+plus)*Math.cos(angle); var y2=cy+(radius+arcWidth/2+plus)*Math.sin(angle); ctx.beginPath(); ctx.moveTo(x1,y1); ctx.lineTo(x2,y2); ctx.lineWidth=tickWidth; ctx.strokeStyle=color; ctx.stroke(); } function drawLabel(tickValue,color,extension,fontsize){ var angle=PI+tickValue/(max-min)*PI; var x=cx+(radius+arcWidth/2+extension)*Math.cos(angle); var y=cy+(radius+arcWidth/2+extension)*Math.sin(angle); ctx.textAlign="center"; ctx.fillStyle=color; ctx.font=fontsize+"px arial"; ctx.fillText(tickValue,x,y); } }); // end $(function(){}); </script> </head> <body> <div id="wrapper"> <input type="text" id="amount"> <div id="slider-vertical"></div> <canvas id="canvas" width="425" height="300"></canvas> </div> </body> </html>