I'm trying to create a function that draws a triangle in the middle of an HTML canvas based on three given lengths, I thought this would be an easy intro to HTML canvas, I was wrong.
Here's where I have so far, but the sides aren't rendering properly:
function drawTriangle(sideOne, sideTwo, sideThree) {
var canvas = document.getElementById('triangle-canvas');
var ctx = canvas.getContext('2d');
var cx = canvas.width / 2;
var cy = canvas.height / 4;
var sideOneHeight = sideOne * (Math.sqrt(3) / 2);
var sideTwoHeight = sideTwo * (Math.sqrt(3) / 2);
ctx.beginPath();
ctx.moveTo(cx, cy);
ctx.lineTo(cx + 50, cy + sideOneHeight);
ctx.lineTo(cx - 50, cy + sideTwoHeight);
ctx.lineTo(cx, cy);
ctx.fill();
ctx.closePath();
}
Any suggestions?