Draw a Line with Thick Base and Thin Top -Speedometer Needle

BASEER ULHASSAN

I am having a problem: I need to draw a Needle over a Speedometer. I am using simple Line Function in JS that draws a line/Needle. I want my Needle to be thick from base and thin from tip as shown below. Please advise how to draw such needle in JavaScript. The Desired and Current Needles are below.

Code of Line:

function drawNeedle(options) {
/* Draw the needle  at the
* angle that represents the options.speed value.
*/

var iSpeedAsAngle = convertSpeedToAngle(options),
    iSpeedAsAngleRad = degToRad(iSpeedAsAngle),
    gaugeOptions = options.gaugeOptions,
    innerTickX = gaugeOptions.radius - (Math.cos(iSpeedAsAngleRad) * 10),
    innerTickY = gaugeOptions.radius - (Math.sin(iSpeedAsAngleRad) * 10),
    fromX = (options.center.X - gaugeOptions.radius) + innerTickX,//+ innerTickX ,// /2,
    fromY = (gaugeOptions.center.Y - gaugeOptions.radius) + innerTickY ,//+ innerTickY, // /2,
    endNeedleX = gaugeOptions.radius - (Math.cos(iSpeedAsAngleRad) * gaugeOptions.radius),//+40,
    endNeedleY = gaugeOptions.radius - (Math.sin(iSpeedAsAngleRad) * gaugeOptions.radius),//+60, // controlled height of nedle
    toX = (options.center.X - gaugeOptions.radius) + endNeedleX,
    toY = (gaugeOptions.center.Y - gaugeOptions.radius) + endNeedleY,

   line = createLine(options.center.X + 80, options.center.Y + 60, toX+75, toY+60, "rgb(3,2,245)", 5, 0.6);  //80.60.75.65

// line = createLine(fromX, fromY, toX, toY, "rgb(3,2,245)", 5, 0.6);

drawLine(options, line);

}

drawLine Function

function drawLine(options, line) {

// Draw a line using the line object passed in
options.ctx.beginPath();

// Set attributes of open
options.ctx.globalAlpha = line.alpha;
options.ctx.lineWidth = line.lineWidth;
options.ctx.fillStyle = line.fillStyle;
options.ctx.strokeStyle = line.fillStyle;
options.ctx.moveTo(line.from.X,
    line.from.Y);

// Plot the line
options.ctx.lineTo(
    (line.to.X),
    line.to.Y
);

options.ctx.stroke();

}

The Desired and Current Needles

markE

Here's how to draw a needle guage:

  • Translate to the center of the guage,
  • Rotate to a specified angle,
  • Draw the needle as a triangle,
  • Un-rotate by the same specified angle,
  • Un-translate by the center coordinates.

Example code and a Demo:

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;

var degrees=-90;
var radians=degrees*Math.PI/180;

$myslider=$('#myslider');
$myslider.attr({min:-180,max:0}).val(degrees);
$myslider.on('input change',function(){
    var degrees=parseInt($(this).val());
    var radians=degrees*Math.PI/180;
    drawNeedle(cw/2,ch/2,150,radians);
});

drawNeedle(cw/2,ch/2,150,radians);

function drawNeedle(cx,cy,radius,radianAngle){
    ctx.clearRect(0,0,cw,ch);
    ctx.translate(cx,cy);
    ctx.rotate(radianAngle);
    ctx.beginPath();
    ctx.moveTo(0,-5);
    ctx.lineTo(radius,0);
    ctx.lineTo(0,5);
    ctx.fillStyle='blue';
    ctx.fill();
    ctx.rotate(-radianAngle);
    ctx.translate(-cx,-cy);
    ctx.beginPath();
    ctx.arc(cx,cy,10,0,Math.PI*2);
    ctx.fill();
}
body{ background-color:white; }
#canvas{border:1px solid red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Needle angle&nbsp <input id=myslider type=range><br>
<canvas id="canvas" width=512 height=512></canvas>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

animate speedometer needle with pure css

From Dev

animate speedometer needle with pure css

From Dev

Pygame draw anti-aliased thick line

From Dev

How to draw a thick dash line using xml?

From Dev

SVG Polygon Arrow, thin line at top?

From Dev

Thin or thick REST-client?

From Dev

Draw hair thin line on iOS, using OpenGL ES

From Dev

Draw triangle at bottom or top of line

From Dev

Draw vertical line on top of image

From Dev

Swift: Creating a border or a thin line on top of the displaying Keyboard in iOS

From Dev

Thick orange ProgressBar instead of thin blue one

From Dev

Thick orange ProgressBar instead of thin blue one

From Dev

Thin vhdk considered as thick in my esxi

From Dev

When use thick client over thin client?

From Dev

How to draw a line to perfectly align top of a text?

From Dev

I want to draw a thin line on my view as shown in the images i linked

From Dev

CSS - border-bottom double 1 thick 1 thin

From Dev

Smart Client, as opposed to Thin/Thick Client- Examples

From Dev

Designing client (thick) / server + extra REST API for the thin client

From Dev

modifying the PS file to thick the line

From Dev

JavaFX : draw sharp thin lines

From Dev

Select thin line

From Dev

How to draw line chart using jfreechart from top to bottom?

From Dev

Drawing a very thin line on UIView

From Dev

Add thick vertical line to table using CSS

From Dev

Detecting thick line clusters and measuring gradient

From Dev

line vertex is too thick (no matter what the width is)

From Dev

Add thick vertical line to table using CSS

From Dev

HTML5 Canvas — Line too thick