Skip to content

Instantly share code, notes, and snippets.

@ZachSaucier
Created December 29, 2024 19:40
Show Gist options
  • Save ZachSaucier/f6886eed8db2e3de4ba1ec0b61da7569 to your computer and use it in GitHub Desktop.
Save ZachSaucier/f6886eed8db2e3de4ba1ec0b61da7569 to your computer and use it in GitHub Desktop.
function draw_arrow(context, x, y, angle, magnitude) {
const head_length = 10;
const dx = Math.cos(angle) * magnitude;
const dy = Math.sin(angle) * magnitude;
const end_x = x + dx;
const end_y = y + dy;
context.beginPath();
context.moveTo(x, y);
context.lineTo(end_x, end_y);
context.lineTo(end_x - head_length * Math.cos(angle - Math.PI / 6), end_y - head_length * Math.sin(angle - Math.PI / 6));
context.moveTo(end_x, end_y);
context.lineTo(end_x - head_length * Math.cos(angle + Math.PI / 6), end_y - head_length * Math.sin(angle + Math.PI / 6));
context.stroke();
context.closePath();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment