如何在HTML5画布上绘制多边形?

Cya*_*ime 85 html javascript css html5 canvas

我需要知道如何在画布上绘制多边形.不使用jQuery或类似的东西.

phi*_*hag 153

使用moveTolineTo(实时演示)创建路径:

var ctx = canvas.getContext('2d');
ctx.fillStyle = '#f00';
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(100,50);
ctx.lineTo(50, 100);
ctx.lineTo(0, 90);
ctx.closePath();
ctx.fill();
Run Code Online (Sandbox Code Playgroud)

  • @Gio Borje:AFAIK,jsFiddle不关心画布,那是你的浏览器.jsFiddle只会将您的HTML/CSS/JS反馈给您. (100认同)
  • +1,我不知道jsFiddle支持的HTML5画布. (2认同)
  • 优秀的解决方案。非常简洁的代码。谢谢你@phihag 我能理解的东西! (2认同)

小智 32

//poly [x,y, x,y, x,y.....];
var poly=[ 5,5, 100,50, 50,100, 10,90 ];
var canvas=document.getElementById("canvas")
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#f00';

ctx.beginPath();
ctx.moveTo(poly[0], poly[1]);
for( item=2 ; item < poly.length-1 ; item+=2 ){ctx.lineTo( poly[item] , poly[item+1] )}

ctx.closePath();
ctx.fill();
Run Code Online (Sandbox Code Playgroud)

  • @AlexanderDixon上面的javascript确实不是一个好例子.*所有*变量都需要`var`,在上面的代码中``item`是一个污染全局命名空间.一切都在一条线上,这降低了可读性.如果您不关心可读性,那么您也可以删除大括号. (2认同)

And*_*cik 31

来自http://www.scienceprimer.com/drawing-regular-polygons-javascript-canvas:

以下代码将绘制六边形.更改边数以创建不同的正多边形.

var ctx = document.getElementById('hexagon').getContext('2d');

// hexagon
var numberOfSides = 6,
    size = 20,
    Xcenter = 25,
    Ycenter = 25;

ctx.beginPath();
ctx.moveTo (Xcenter +  size * Math.cos(0), Ycenter +  size *  Math.sin(0));          

for (var i = 1; i <= numberOfSides;i += 1) {
  ctx.lineTo (Xcenter + size * Math.cos(i * 2 * Math.PI / numberOfSides), Ycenter + size * Math.sin(i * 2 * Math.PI / numberOfSides));
}

ctx.strokeStyle = "#000000";
ctx.lineWidth = 1;
ctx.stroke();
Run Code Online (Sandbox Code Playgroud)

  • 这很棒,非常优雅,如果你添加:`cxt.save();``cxt.fillStyle ="#FF000";``cxt.fill();``cxt.restore();`你可以填充形状. (3认同)

Jig*_*iya 9

//create and fill polygon
CanvasRenderingContext2D.prototype.fillPolygon = function (pointsArray, fillColor,     strokeColor) {
    if (pointsArray.length <= 0) return;
    this.moveTo(pointsArray[0][0], pointsArray[0][1]);
    for (var i = 0; i < pointsArray.length; i++) {
        this.lineTo(pointsArray[i][0], pointsArray[i][1]);
    }
    if (strokeColor != null && strokeColor != undefined)
        this.strokeStyle = strokeColor;

    if (fillColor != null && fillColor != undefined) {
        this.fillStyle = fillColor;
        this.fill();
    }
}
//And you can use this method as 
var polygonPoints = [[10,100],[20,75],[50,100],[100,100],[10,100]];
context.fillPolygon(polygonPoints, '#F00','#000');
Run Code Online (Sandbox Code Playgroud)


小智 5

这是一个甚至支持顺时针/逆时针绘图的函数,您可以使用非零缠绕规则控制填充。

这是一篇关于它如何工作以及更多内容的完整文章。

// Defines a path for any regular polygon with the specified number of sides and radius, 
// centered on the provide x and y coordinates.
// optional parameters: startAngle and anticlockwise

function polygon(ctx, x, y, radius, sides, startAngle, anticlockwise) {
  if (sides < 3) return;
  var a = (Math.PI * 2)/sides;
  a = anticlockwise?-a:a;
  ctx.save();
  ctx.translate(x,y);
  ctx.rotate(startAngle);
  ctx.moveTo(radius,0);
  for (var i = 1; i < sides; i++) {
    ctx.lineTo(radius*Math.cos(a*i),radius*Math.sin(a*i));
  }
  ctx.closePath();
  ctx.restore();
}

// Example using the function.
// Define a path in the shape of a pentagon and then fill and stroke it.
context.beginPath();
polygon(context,125,125,100,5,-Math.PI/2);
context.fillStyle="rgba(227,11,93,0.75)";
context.fill();
context.stroke();
Run Code Online (Sandbox Code Playgroud)