We would like to know how to draw equilateral triangle.
<!--from www . j a v a2 s . c om-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>//<![CDATA[
window.onload=function(){
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
drawEqTriangle(ctx, 100, canvas.width/2, canvas.height/2);
function drawEqTriangle(ctx, side, cx, cy){
var h = side * (Math.sqrt(3)/2);
ctx.strokeStyle = "#ff0000";
ctx.save();
ctx.translate(cx, cy);
ctx.beginPath();
ctx.moveTo(0, -h / 2);
ctx.lineTo( -side / 2, h / 2);
ctx.lineTo(side / 2, h / 2);
ctx.lineTo(0, -h / 2);
ctx.stroke();
ctx.fill();
ctx.closePath();
ctx.save();
}
}//]]>
</script>
</head>
<body>
<canvas id="canvas" width="300" height="300"></canvas>
</body>
</html>
The code above is rendered as follows: