Drawing Polygons 绘制多边形 - caleb531/jcanvas-docs GitHub Wiki

Polygons 多边形

The drawPolygon() method creates a regular (equal-angled) polygon.
使用drawPolygon()方法来创建一个规则的(等角)多边形。

// Draw a triangle 画三角形
$('canvas').drawPolygon({
  strokeStyle: 'black',
  strokeWidth: 4,
  x: 200, y: 100,
  radius: 50,
  sides: 3
});
// Draw a polygon 画多边形
$('canvas').drawPolygon({
  fillStyle: '#589',
  strokeStyle: '#000',
  x: 100, y: 100,
  radius: 50,
  sides: 5,
  rotate: 25
});

Concave Polygons 凹多边形

To create concave polygons (polygons that point inward), include the concavity property.
要创建凹多边形(向里凹的多边形),要包含concavity属性:

  • A value greater than 0 will cause the sides to point inward.
    比0大的值会使边向里凹。
  • A value less than 0 will cause the sides to point outward.
    比0小的值会使边向外凸。
  • A value equal to 0 does not project at all.
    等于0的值完全不会突出。
  • A value of 1 will render the polygon invisible, because the sides will point in completely.
    值1会使多边形不可见,因为边完全凹进去了。
// Draw a star 画星形
$('canvas').drawPolygon({
  fillStyle: '#36c',
  x: 100, y: 100,
  radius: 50,
  sides: 5,
  concavity: 0.5
});
// Draw a badge-like shape 画徽章形
$('canvas').drawPolygon({
  fillStyle: '#3c6',
  strokeStyle: '#083',
  x: 100, y: 100,
  radius: 50,
  sides: 50,
  concavity: 0.1
});
// Draw a shield-like shape 画盾牌形
$('canvas').drawPolygon({
  fillStyle: '#36c',
  strokeStyle: '#f60',
  strokeWidth: 5,
  x: 100, y: 100,
  radius: 50,
  sides: 3,
  concavity: -0.5,
  rotate: 180
});