javascript - 为什么我的 Canvas 除了 Angular 落外都是空白的?

标签 javascript html canvas

我正在通过玩 Canvas 来学习 JavaScript,我设法制作了一个随机生成的颜色的正方形网格。

我决定让每个方 block 的颜色强度增加和减少,但结果只是绘制了网格的一个小 Angular ,颜色没有改变。我做错了什么?

Online code herepaste bin here .

var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext("2d");

ctx.clearRect(0, 0, canvas.width, canvas.height);

function square(x, y) {
    this.x = x;
    this.y = y;
    this.sizex = 10;
    this.sizey = 10;
    this.Colo = Colo;

    function Colo() {
        var r = Math.floor(Math.random() * (255 - 1) + 1);
        var g = Math.floor(Math.random() * (255 - 1) + 1);
        var b = Math.floor(Math.random() * (255 - 1) + 1);
        var run = true;
        var rup, gup, bup = true;
        while (run) {

            if (rup) {
                if (r == 255) {
                    rup = false;
                } else if (r == 0) {
                    rup = true;
                }

                r += 1;
                r.toString();

            } else if (!gup) {
                r -= 1;
                r.toString();
            }

            if (gup) {
                if (g == 255) {
                    gup = false;
                } else if (g == 0) {
                    gup = true;
                }
                g += 1;
                g.toString();

            } else if (!gup) {
                g -= 1;
                g.toString();
            }

            if (bup = true) {
                if (b == 255) {
                    bup = false;
                } else if (b == 0) {
                    bup = true;
                }

                b += 1;
                b.toString();

            } else if (!gup) {
                b -= 1;
                b.toString();
            }

            ctx.clearRect(0, 0, canvas.width, canvas.height);
            var col = "rgb(" + r + "," + g + "," + b + ")";

            return col;
            window.onfocus = function () {
                rup = true;
            };
            window.onblur = function () {
                isActive = false;
            };
            console.log("Ran loop.")
        }
    }

    ctx.fillStyle = Colo();
    ctx.fillRect(this.x, this.y, this.sizex, this.sizey);
}

var squares = new Array();
var p = 0;
for (var i = 0; i < 128; i++) {
    for (var j = 0; j < 72; j++) {
        p = i + j;
        squares[p] = new square(i * 10, j * 10);
    }
}

最佳答案

您的 square() 构造函数执行此操作:

ctx.fillStyle = Colo();
ctx.fillRect(this.x, this.y, this.sizex, this.sizey);

... 其中,Colo() 执行以下操作:

ctx.clearRect(0, 0, canvas.width, canvas.height);

因此,在绘制每个矩形之前,您正在清除整个 Canvas 。

关于javascript - 为什么我的 Canvas 除了 Angular 落外都是空白的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18989112/

相关文章:

Python Tkinter Canvas 的边框与 `create_window' 创建的窗口重叠

javascript - 当从外部分配值时,更改事件不适用于文本框

javascript - 表单提交前的 jQuery 验证

javascript - ajax 加载的 javascript 上的文档就绪事件

html - div A固定高度,div B下方调整到窗口

javascript - 如何在使用 JavaScript 单击按钮时更改文件名并下载它

javascript - react : How to create a form which gets select data from an API and posts to another API then clears fields on submit?

javascript - 如何将 li 文本添加到悬停 jQuery 上的元素

html - CreateJs Canvas 形状在 Windows Phone 上丢失坐标

javascript - HTML Canvas 图像不显示?