Canvas doesn't draw in a loop


Saba Silagadze

Why draw arent rectangles in a loop? I just started a snake game n trying to draw the snake's body through a loop, but for some reason ctx is not drawing the loop. If I write the rectangle without the loop, it works.

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

var snake = [];
snake[0] = {x:100, 100};
snake[1] = {x:90, 100};
snake[2] = {x:80, 100};

var i;

function draw() {
   for (i = 0; i > snake.length; i++) {
     ctx.fillStyle = "yellow";
     ctx.rect(snake[i].x, snake[i].y, 22, 300); 
     ctx.fill();
 }

setInterval(draw,100);
Pritam Banerjee

The for loop is wrong (you have an infinite loop and make it less than i < snake.length):

function draw() {
    for (i = 0; i < snake.length; i++) {
        ctx.fillStyle = "yellow";
        ctx.rect(snake[i].x, snake[i].y, 22, 300); 
        ctx.fill();
    }
}

Related


Canvas doesn't draw in a loop

Saba Silagadze Why draw arent rectangles in a loop? I just started a snake game n trying to draw the snake's body through a loop, but for some reason ctx doesn't draw the loop. If I write the rectangle without the loop, it works. var canv = document.getElement

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw text?

code M I want to display some text on the screen when the user clicks buttonwith canvasin android studio. I've been watching a lot of posts and videos on how to do this. The problem is, when I try their code, when I click the button, nothing is displayed. Does

PinView doesn't draw in loop

AK2016 I extended SubsamplingScaleImageView to create a PinView as suggested in the documentation. I am able to draw pints in place. But the problem I'm facing is: when I try to draw the loop, the Pin is lost. I confirmed that OnDraw is being called on the mai

Can't draw to canvas in Tkinter after() loop

discrete tomatoes I'm having a problem, when I try to create an image on the canvas, I can't generate the image. However, I can create and configure my image before starting the after() loop. Also, I am able to use my canvas to delete objects canvas.delete()in

HTML5 canvas for loop doesn't draw first object in array

Kaizokupuffball So basically. I'm trying to loop through each letter in a text (in this case, the text "marius"). The problem now is that the first letter is never drawn. If the text is "marius", "arius" is drawn. I've tried my best but I can't find the error.

HTML5 canvas for loop doesn't draw first object in array

Kaizokupuffball So basically. I'm trying to loop through each letter in a text (in this case, the text "marius"). The problem now is that the first letter is never drawn. If the text is "marius", "arius" is drawn. I've tried my best but I can't find the error.

HTML5 canvas for loop doesn't draw first object in array

Kaizokupuffball So basically. I'm trying to loop through each letter in a text (in this case, the text "marius"). The problem now is that the first letter is never drawn. If the text is "marius", "arius" is drawn. I've tried my best but I can't find the error.

HTML5 canvas for loop doesn't draw first object in array

Kaizokupuffball So basically. I'm trying to loop through each letter in a text (in this case, the text "marius"). The problem now is that the first letter is never drawn. If the text is "marius", "arius" is drawn. I've tried my best but I can't find the error.

Canvas doesn't draw anything? Kotlin

David Wenzel When I write this code it doesn't work: if(shapeSpinner?.selectedItemPosition == 0){ canvas.drawCircle((0..dimensionX).random().toFloat(), (0..dimensionY).random().toFloat(), (0..dimensionX).random().toFloat(), paint) } if(shapeSpin

Canvas doesn't draw IE 11

Barr I have a canvas code to draw the signature. The code works fine on chrome and Firefox, but doesn't draw at all on IE 11. My canvas is: <canvas id="signitureCanvas" style="border: 3px solid #000; cursor:crosshair; background-color:white;"></canvas> My cod

Canvas.drawRect() doesn't draw anything

Cody My painter in the Canvas class doesn't draw the rectangle, although I configure it to do so. I have a simple canvas layout, I have set up 6 rectangle painters that should draw a grey rectangle if the corresponding boolean flag is true. Still, for some rea

Canvas doesn't draw anything? Kotlin

David Wenzel When I write this code it doesn't work: if(shapeSpinner?.selectedItemPosition == 0){ canvas.drawCircle((0..dimensionX).random().toFloat(), (0..dimensionY).random().toFloat(), (0..dimensionX).random().toFloat(), paint) } if(shapeSpin

canvas.drawBitmap doesn't draw anything

Eye I have covered onDragShadowfor DragShadowBuilderthis @Override public void onDrawShadow(Canvas canvas) { super.onDrawShadow(canvas); Bitmap bitmap = InGameActivity.getRandomBitmap(); Rect source = new Rect(0, 0, bitmap.getWidth(

Script doesn't draw lines by coordinates on Canvas

Anton I'm trying to use Canvas to let users draw them on a PC or smartphone. I don't understand why this script doesn't work. The script doesn't give any errors. The coordinates are set correctly. I tried setting it manually, but even then nothing appears on t

Canvas drawing doesn't draw correctly despite setting properties

macaron lover I just created 2 drawings, trying to make one look the same as the other, but I have no luck. Regarding the screenshots, the graph at the top was created using canvas, while the graph at the bottom was created using XML. XML drawing works perfect

Canvas drawing doesn't draw correctly despite setting properties

macaron lover I just created 2 drawings, trying to make one look the same as the other, but I have no luck. Regarding the screenshots, the graph at the top was created using canvas, while the graph at the bottom was created using XML. XML drawing works perfect

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon

React-Native-Canvas doesn't draw whole rectangle

Brian Ton While making another app, I ran into a problem react-native-canvas, so I created this minimal project. The problem is that the WebView is much smaller than the Canvas and doesn't fill the entire canvas when appropriate. Here is the code (very similar

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon

canvas.drawRect doesn't draw a rectangle on the image

Krishna Here canvas.drawRect will be different when using different bitmaps. I want to draw a rectangle over the top image and want the part of the image outside the rectangle to be darkened or blurred. please help me. draw.setOnClickListener(new View.OnClickL

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon

Canvas drawing doesn't draw correctly despite setting properties

macaron lover I just created 2 drawings, trying to make one look the same as the other, but I have no luck. Regarding the screenshots, the graph at the top was created using canvas, while the graph at the bottom was created using XML. XML drawing works perfect

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon