0

Is there a way that I can use relative units like vh or vw to draw on the canvas? I tried this:

ctx.fillRect('10vh', '10vh', '10vh', '10vh');

and even without the quotations but it only works if I use pixels. I need the shape to resize according to the viewport. Thanks in advance.

  • 1
    https://stackoverflow.com/questions/33191909/can-i-specify-canvas-dimensions-using-vh-and-vw – Ken Lee Dec 25 '20 at 23:35
  • In addition to Ken Lee's answer, you would need to register an event handler for resize event - https://developer.mozilla.org/en-US/docs/Web/API/Window/resize_event . Canvas will not resize automatically, you will have to redraw it with newly acquired viewport size information everytime window resize occurs. – Simas Butavičius Dec 26 '20 at 00:16

0 Answers0