我试图表现出同样高度的画布多个管道,但它呈现出单管,而不是它的很多,甚至使用一个循环后如何让多个元素在画布
<script>
var tryCanvas = document.getElementById("myCanvas");
var c = tryCanvas.getContext("2d");
var myCall = [];
function Squares() {
for(var i =0; i < 10; i++){
this.x = Math.random()* tryCanvas.clientWidth;
this.y = 0;
this.w = 20;
this.h = 60;
this.counter = 0;
this.draw = function() {
c.beginPath();
c.rect(this.x, this.y, this.w, this.h)
c.fill();
}
}
this.update = function() {
if(this.x < 0){
this.x = 0;
}
this.x -= 1;
this.draw();
}
}
var holder = new Squares;
setInterval(callFun, 10);
function callFun() {
c.clearRect(0,0,tryCanvas.clientWidth, tryCanvas.clientHeight);
holder.update();
}
</script>
如果我在数组中推入构造函数,它不会在画布中显示任何内容,并且在控制台中显示未定义或NaN。 但是,如果我没有这个“它”它产生rects的数量。 我在这里做错了什么?
'c.clearRect(0,0,tryCanvas.clientWidth,tryCanvas.clientHeight);'做什么? – Teemu
它基本上清除画布 – tankit88
不仅基本上,它几乎清除... – Teemu