为什么我的画布是空白的,除了在角落里
Why is my canvas blank except for in a corner?
我正在通过玩画布来学习JavaScript,我已经设法用随机生成的颜色制作了一个正方形网格。
我决定让每个方块的颜色增加和减少强度,但结果只是绘制网格的一个小角,颜色不会改变。我做错了什么?
在线代码在这里,把垃圾桶粘贴在这里。
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);
因此,在绘制每个矩形之前,您正在清除整个画布。
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 可以't让我的if语句处理js中的html表单输入
- 我的单元测试选项是什么
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 我的职位回报太快了,如何做出承诺
- 我的jQuery插件参数没有正确启动,遇到了问题
- 如何识别我的网站中的慢速设备
- 如何将JSON数据导入我的ejs模板
- JQuery添加元素需要在我的js之前再次添加JQuery脚本
- 知道为什么我的旋转木马不会自动更改图片吗
- 我的模板未被解析
- 无法将数据从firebase获取到我的html页面
- 角度图表;t显示在我的页面中
- 我的AngularJS表达式没有'不起作用
- 将电视直播频道从网站嵌入我的网站
- /undefined在我的404错误日志中多次出现
- 为什么我的d3.jsselectAll+过滤器没有过滤
- 元素在我的代码中不会.fadeTo.Don'I don’我不知道;s错误的JavaScript、JQuery、H
- 我的javascript for循环不起作用
- 为什么我的画布是空白的,除了在角落里