我已经定义了两个函数来渲染一个圆形和一个三角形。非常直接的东西。
function circle(offset, size){
  var color = $("#color option:selected").val();
  var canvas = document.getElementById("canvas");
  var context = canvas.getContext("2d");
  radius = size * 1;
  context.beginPath();
  context.arc(offset, 2, radius, 0, 2 * Math.PI, false);
  context.fillStyle = color;
  context.fill();
 }
 function triangle(offset, size){
  var color = $("#color option:selected").val();
  var canvas = document.getElementById("canvas");
  var context = canvas.getContext("2d");
  var width = size * 6;
  var height = size * 5;
  var padding = 0;
  // Draw a path
  context.beginPath();
  context.moveTo(offset + width/2, padding);
  context.lineTo(offset + width, height + padding);
  context.lineTo(offset, height + padding);
  context.closePath();
  // Fill the path
  context.fillStyle = color;
  context.fill();
}
我已将画布添加到我的页面中:
<canvas id="canvas"></canvas>
出于某种原因,我可以看到圆形和方形没有正确渲染。请参阅随附的屏幕截图。

