How to draw a star by using canvas HTML5?

前端 未结 5 2169
伪装坚强ぢ
伪装坚强ぢ 2021-01-05 09:04

Am trying to draw a star using canvas, but the code is not running. I want to understand: what are the steps to measure the Y and X coordinate? How to find them? to draw any

5条回答
  •  既然无缘
    2021-01-05 09:10

    The function can be shorter with coordinate translate:

    function strokeStar(x, y, r, n, inset) {
        ctx.save();
        ctx.beginPath();
        ctx.translate(x, y);
        ctx.moveTo(0,0-r);
        for (var i = 0; i < n; i++) {
            ctx.rotate(Math.PI / n);
            ctx.lineTo(0, 0 - (r*inset));
            ctx.rotate(Math.PI / n);
            ctx.lineTo(0, 0 - r);
        }
        ctx.closePath();
        ctx.fill();
        ctx.restore();
    }
    

提交回复
热议问题