I need assistance with incorporating arrowheads at the end of linear plots drawn on a coordinate grid using my custom function. You can view an example of the current setup in this JsFiddle: https://jsfiddle.net/zje14n92/1/
Below is the code snippet responsible for drawing the linear plots:
if (canvas1.getContext) {
canvas1.width = x_axis * 2;
canvas1.height = y_axis * 2;
var ctx1 = canvas1.getContext("2d");
ctx1.font = "10px sans-serif";
ctx1.strokeText(' ', x_axis+50, 50);
ctx1.lineWidth = 1;
ctx1.beginPath();
ctx1.strokeStyle = 'black';
x = -x_max;
y = 4*x + 5; // Modify this line to change the equation
ctx1.moveTo(offsetX(x), offsetY(y));
while (x < x_max) { // Include broken line code here
x += 0.1;
y = 4*x+5; // Another line to modify equation
ctx1.lineTo(offsetX(x), offsetY(y));
}
ctx1.stroke();
ctx1.closePath();
I've been trying to implement arrowheads at the endpoints similar to what I found in this JsFiddle (http://jsfiddle.net/m1erickson/Sg7EZ/), but I'm struggling to integrate it into my existing code. If anyone knows of a straightforward way to achieve this, I'd greatly appreciate your guidance!
var startRadians=Math.atan((this.y2-this.y1)/(this.x2-this.x1));
startRadians+=((this.x2>this.x1)?-90:90)*Math.PI/180;
this.drawArrowhead(ctx,this.x1,this.y1,startRadians);
// draw the ending arrowhead
var endRadians=Math.atan((this.y2-this.y1)/(this.x2-this.x1));
endRadians+=((this.x2>this.x1)?90:-90)*Math.PI/180;
this.drawArrowhead(ctx,this.x2,this.y2,endRadians);
}
Line.prototype.drawArrowhead=function(ctx,x,y,radians){
ctx.save();
ctx.beginPath();
ctx.translate(x, y);
ctx.rotate(radians);
ctx.moveTo(0,0);
ctx.lineTo(5,20);
ctx.lineTo(-5,20);
ctx.closePath();
ctx.restore();
ctx.fill();
}
If there is a simple method to add arrowheads to the ends of lines, please advise. Thank you for any help provided!