0

When executing a python script (using Brython in a sandbox on codehs.com), I'm getting an extra white rectangle, left of the grid I'm outputting to the screen:

enter image description here

When inspecting the html, I can see where it is coming from:

enter image description here

Is there any way I can get rid of that white rectangle?

Here is my code so far:

from browser import document, html, window

# Create a canvas element and set its size
canvas = html.CANVAS(width=400, height=400)
document <= canvas

def create_grid(canvas):

    # Get the canvas context
    ctx = canvas.getContext('2d')
    
    # Define cell size and line width
    cell_size = 20
    line_width = 1
    
    # Draw grid lines
    for x in range(0, 400, cell_size):
        ctx.moveTo(x, 0)
        ctx.lineTo(x, 400)
        ctx.lineWidth = line_width
        ctx.stroke()
    
    for y in range(0, 400, cell_size):
        ctx.moveTo(0, y)
        ctx.lineTo(400, y)
        ctx.lineWidth = line_width
        ctx.stroke()
                
create_grid(canvas)

  • The browser show 2 canvas elements in your document. You have not shared your complete html page, there are another canvas previously to the one that you add? – Gonzalo Odiard Mar 16 '23 at 16:23

1 Answers1

1

there is one Canvas element hardcoded in html, and then one is created programatically. Just drop the HTML `<canvas id="brython-canvas" ...>" from there and you should be fine

jsbueno
  • 99,910
  • 10
  • 151
  • 209