HTML5 Game - Shape from curve

Description

Shape from curve

Demo

ResultView the demo in separate window

<!doctype html>
<html>
  <head>
    <title>Drawing a spaceship</title>
  </head>/*w  w  w  . jav  a 2 s . c  o  m*/
  <body>
    <h1>Drawing a spaceship</h1>
    <canvas id="balls" width="400" height="400"></canvas>
    <script>
let context = document.getElementById("balls").getContext("2d");

function draw_ship(ctx, radius, options) {
  options = options || {};
  ctx.save();
  ctx.lineWidth = options.lineWidth || 2;
  ctx.strokeStyle = options.stroke || "white";
  ctx.fillStyle = options.fill || "black";
  let angle = (options.angle || 0.5 * Math.PI) / 2;
  let curve = options.curve || 0.5;
  ctx.beginPath();
  ctx.moveTo(radius, 0);
  ctx.lineTo(
    Math.cos(Math.PI - angle) * radius,
    Math.sin(Math.PI - angle) * radius
  );
  ctx.quadraticCurveTo(radius * curve - radius, 0,
    Math.cos(Math.PI + angle) * radius,
    Math.sin(Math.PI + angle) * radius
  );
  ctx.lineTo(radius, 0);
  ctx.fill();
  ctx.stroke();
 
    ctx.fillStyle = "white";
    ctx.lineWidth = 1;
    ctx.beginPath();
    ctx.moveTo(-radius, 0);
    ctx.lineTo(0, 0);
    ctx.stroke();
    ctx.beginPath();
    ctx.arc(radius * curve - radius, 0, radius/50, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.beginPath();
    ctx.arc(0, 0, radius, 0, 2 * Math.PI);
    ctx.stroke();

  ctx.restore();
}

context.translate(300, 300);
draw_ship(context, 100);
    </script>
  </body>
</html>

Related Topics