LoginSignup
1
1

More than 5 years have passed since last update.

CanvasRenderingContext2Dで三角形を描画できるようにする

Last updated at Posted at 2013-05-18

strokeRectとかfillRectと同じように三角形を使いたいので・・・。

CanvasRenderingContext2D.prototype.strokeTriangle = function(x,y,w,h){
    this.beginPath();
    this.moveTo(x+(w/2) , y);
    this.lineTo(x+w     , y+h);
    this.lineTo(x       , y+h);
    this.closePath();
    this.stroke();
}
CanvasRenderingContext2D.prototype.fillTriangle = function(x,y,w,h) {
    this.strokeTriangle(x,y,w,h);
    this.fill();
};
// ctxにはコンテキストをセット。三角形を描画する場合は辺の比率に気をつける。
// 正三角形を描画するのであれば以下の様な感じ
ctx.fillTriangle(30,30,15*2,15*Math.sqrt(3));
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1