question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Responsive canvas size

See original GitHub issue

the <canvas> element will only stay at initial width and height and not able to change them afterward.

I am trying to set the canvas width and height dynamically when browser window size changes: <Unity unityContent={this.unityContent} className="unityApp" height={this.props.browserWindow.height} width={this.props.browserWindow.width} />

Describe the solution you’d like Make it listen to the height and width props, and change canvas size accordingly

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:29 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
jeffreylanterscommented, Aug 24, 2020

When using ReactUnityWebGL 8.0.0 for Unity 2020 resizing should work without any problem because Unity no longer creates the canvas by itself.

2reactions
sg3scommented, Jun 4, 2019

On Unity version 2018.3.11f1, without enabling the resizing functionality as provided by the plugin, we were able to get the resolution to adjust by setting the canvas element to 100% dimensions in css.

The canvas width/height are set to exact pixel values on every frame by UnityLoader, after getting the width/height needed based on its parent. You cannot directly set css width/height on the canvas, but an indirect css declaration targeting the canvas does work;

#game-container canvas {
    width: 100% !important;
    height: 100% !important;
}

Assuming #game-container is a position relative or absolute block level element and resizes with content on the page or the window itself the canvas should resize with it.

This also prevents issues when you take into account values such as window.devicePixelRatio to adjust to high DPI devices during frame resolution calculation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make canvas responsive - css - Stack Overflow
Remove the width and height attributes from your <canvas> . <canvas id="responsive-canvas"></canvas> · Using CSS, set the width of your canvas to ...
Read more >
A pattern for responsive canvas illustrations - Joel Gibson
A pattern allowing the dimensions of canvas or WebGL drawings to be set dynamically and responsively via CSS.
Read more >
How to Make a Dynamic, Animated, and Responsive Canvas ...
The Responsive Canvas. First, we need a canvas element. · Draw on the Canvas in Ratios. Whenever making a responsive canvas drawing, everything ......
Read more >
HTML5 canvas... responsive! - DEV Community ‍ ‍
Here is a simple example in which the canvas is always scaled to fit it's container. Much like background-size: contain; in CSS or...
Read more >
Responsive Canvas - CodePen
An experiment in responsive canvas design.... ... <div class="canvas-container">. 4. <canvas id="terrain" width="1024" height="640"></canvas>.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found