设置HTML画布弧的属性

时间:2010-12-26 19:36:15

标签: javascript html5 canvas

我还是HTML5和canvas的新手。我在画布上设置点是这样的:

var ctx = canvas.getContext("2d");
        for (var i = 0; i < 500; i++) {
            ctx.fillStyle = 'rgba(255,255,255,0.2)';
            ctx.beginPath();
            ctx.arc(points[i].x, points[i].y, radius, 0, Math.PI * 2, true);
            ctx.fill();
        }

有没有这样的方式,一旦绘制画布,当我寻找说“200”时,我可以识别特定的点并改变它的颜色?或者重绘整个画布会更好吗?

1 个答案:

答案 0 :(得分:2)

var canvas, ctx, points;
var radius = 10;
var num = 20;
$(function () {
    points = [];
    for (var i = 0; i < num; i++) {
        points.push({
            x: Math.random() * 300 >> 0,
            y: Math.random() * 200 >> 0
        });
    }
    canvas = document.getElementById('canvas');
    ctx = canvas.getContext("2d");
    ctx.fillStyle = '#000';
    ctx.fillRect(0, 0, 300, 200);

    for (var i in points) {
        ctx.fillStyle = 'rgba(255,255,255,0.8)';
        ctx.beginPath();
        ctx.arc(points[i].x, points[i].y, radius, 0, Math.PI * 2, true);
        ctx.fill();
    }
});

var initrand = Math.random() * num >> 0;

function change() {
    var random = initrand;
    ctx.fillStyle = '#234263';
    ctx.beginPath();
    ctx.arc(points[random].x, points[random].y, radius, 0, Math.PI * 2, true);
    ctx.fill();
    initrand = Math.random() * num >> 0;
}

Demo for above Code