8

i want to make the p5 canvas fit perfectly to the window size, but whenever I use windowWidth and windowHeight , the canvas seems to be bigger:

The canvas stretches outside

is there any way to fix this?

PineInDisguise
  • 103
  • 1
  • 7

1 Answers1

10

You need to turn off margin and padding for the <html> and <body> tags with CSS (this removes the white border around the canvas), and turn off the vertical scrollbar.

Also, if someone changes the size of the window, you'll want to handle that, which is done easily with the windowResized event.

function setup() {
  createCanvas(windowWidth, windowHeight);
}

function draw() {
  background(200);
}

function windowResized() {
  resizeCanvas(windowWidth, windowHeight);
}
html,
body {
  margin: 0;
  padding: 0;
  overflow: hidden;
}
<script src="https://cdn.jsdelivr.net/npm/p5@1.3.1/lib/p5.min.js"></script>
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34