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 (containing the pixel coordinates of each rectangle). The code can be viewed here : https://jsfiddle.net/9u3ez6gu/2/

The letters are rendered correctly, but after the for loop (see code sample below) I am trying to create a lime white space between each letter. No matter what I do, this lime space never gets rendered. Here is the for loop and the code I am using to render the space. Does anyone know why canvas won't let me draw lime rectangles?

for (i = 0; i <= inv[letter].length; i++) {
    var x = inv[letter][i][1] * full;
    var y = inv[letter][i][0] * full;
    context.beginPath();
    context.rect(x, y, unit, unit);
    context.fillStyle = 'black';
    context.closePath();
    context.fill();
}// End for loop

//Add a gap between letters
var gapx = full * 5;
context.beginPath();
context.rect(gapx, 0, full, full);
context.fillStyle = 'lime';
context.closePath();
context.fill();
}// End function
pointy

Your forloop makes one iteration:

for (i = 0; i <= inv[letter].length; i++) { 

Should be <, no <=. Keep your developer console open!

Also, the declaration ishould be varused at the top of the function or in the forloop header itself. It's (probably) okay in this case, but it's good to get into the habit. If you don't declare the variable, it will be global. If your other function also fails to declare another function i, they will be the same thing and will cause weird errors.

if you put

"use strict";

At the top of a block of code , or at the very top of each function, the parser (in modern browsers) will flag assignments marked as implicit globals as errors.<script>

Related


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 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.getEleme

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

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

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

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

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

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

Tkinter won't draw rectangle on Canvas

Cadel Watson I'm trying to initialize a grid of blue rectangles with a user-specified size. However, the rectangle is not drawn on the initialized canvas. I am trying to store them in a matrix for later use. My code is as follows: import Tkinter import sys fro

Tkinter won't draw rectangle on Canvas

Cadel Watson I'm trying to initialize a grid of blue rectangles with a user-specified size. However, the rectangle is not drawn on the initialized canvas. I am trying to store them in a matrix for later use. My code is as follows: import Tkinter import sys fro

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

draw rectangle in canvas

125fur I can draw rectangle on video on canvas. But it's hidden behind the video but can't suggest how I should draw the rectangle on the video through the side.Pls like I can draw the code on the image. : <p>Video to use:</p> <video id="video1" controls wi

draw rectangle in canvas

125fur I can draw rectangle on video on canvas. But it's hidden behind the video but can't suggest how I should draw the rectangle on the video through the side.Pls like I can draw the code on the image. : <p>Video to use:</p> <video id="video1" controls wi

Draw resizable rectangle on canvas

Inde · 穆里罗 (Masinde Muliro) I am trying to draw a resizable rectangle on a canvas element. I check the position of the mouse relative to the rectangle on the screen and then update the width/height of the triangle the mouse falls into. So far I've managed to s

draw rectangle on canvas

Avsana Khan I'm trying to develop a ping pong game in Android where I will have two bats to hit the ball. I drew a bat on the canvas and it was placed at the far left of the screen. I need another bat on the far right of the screen, but every time the paddle i

draw rectangle on canvas

Avsana Khan I'm trying to develop a ping pong game in Android where I will have two bats to hit the ball. I drew a bat on the canvas and it was placed at the far left of the screen. I need another bat on the far right of the screen, but every time the paddle i

Draw resizable rectangle on canvas

Inde · 穆里罗 (Masinde Muliro) I am trying to draw a resizable rectangle on a canvas element. I check the position of the mouse relative to the rectangle on the screen and then update the width/height of the triangle the mouse falls into. So far I've managed to s

draw rectangle in canvas

125fur I can draw rectangle on video on canvas. But it's hidden behind the video but can't suggest how I should draw the rectangle on the video through the side.Pls like I can draw the code on the image. : <p>Video to use:</p> <video id="video1" controls wi

Canvas rectangle doesn't move with label, why?

companion I'm making a gui application and for aesthetic reasons I want to randomly generate labels with colored backgrounds. But whenever the label is generated, the canvas rectangle stays at the bottom left corner. pos_hint does not affect the rectangle, onl

Canvas rectangle doesn't move with label, why?

companion I'm making a gui application and for aesthetic reasons I want to randomly generate labels with colored backgrounds. But whenever the label is generated, the canvas rectangle stays at the bottom left corner. pos_hint does not affect the rectangle, onl