Responsive canvas size
See original GitHub issuethe <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:
- Created 5 years ago
- Comments:29 (13 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
When using ReactUnityWebGL 8.0.0 for Unity 2020 resizing should work without any problem because Unity no longer creates the canvas by itself.
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;
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.