簡體   English   中英

在HTML5畫布上繪制形狀

[英]Getting a shape to draw on the html5 canvas

我有一個函數,該函數基本上使用線條繪制特定大小的偽圓並將其填充為顏色。

但是,當我嘗試調用我的方法時,它不會繪制,並且在調試控制台上沒有發現錯誤(我正在使用chrome),而jsfiddle卻沒有為我拋出任何錯誤。

這是代碼:

function drawLineCircle (x, y, size, colour, scale, segments) {
    context.save();

    context.strokestyle = '#000000';
    context.fillStyle   = colour;
    context.lineWidth   = 3;

    context.beginPath();

    var anglePerSegment = Math.PI *2 / segments;

    for (var i = 0; i <= segments; i = i + 1){
        var angle  = anglePerSegment * i;
        var radius = size * scale;
        var a      = x + radius * cos (angle);
        var b      = y + radius * sin (angle);
        if (i == 0)
            context.moveTo (a, b);
        else
            context.lineTo (a, b);

    }

    context.stroke();
    context.fill();

    context.closePath();
    context.restore();
}

任何幫助將不勝感激!

您的代碼有兩個問題:

  1. 您沒有定義context
  2. cossinMath屬性

這是一個更正的示例: http : //jsfiddle.net/REw7j/1/

function drawLineCircle (x, y, size, colour, scale, segments) {
    var canvas = document.getElementById('example');
    var context = canvas.getContext('2d');
    context.save();

    context.strokestyle = '#000000';
    context.fillStyle   = colour;
    context.lineWidth   = 3;

    context.beginPath();

    var anglePerSegment = Math.PI *2 / segments;

    for (var i = 0; i <= segments; i = i + 1){
        var angle  = anglePerSegment * i;
        var radius = size * scale;
        var a      = x + radius * Math.cos (angle);
        var b      = y + radius * Math.sin (angle);
        if (i == 0)
            context.moveTo (a, b);
        else
            context.lineTo (a, b);

    }

    context.stroke();
    context.fill();

    context.closePath();
    context.restore();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM