Reputation: 11
I'm trying to set an aspect ratio to the Stage element,but without any luck.
For example, with a canvas, I would write the following for landscape:
const dpr = window.devicePixelRatio || 1
const aspectRatio = 16 / 9
const canvasHeight = 1080 * dpr;
const canvasWidth = 1920 * dpr;
<canvas
height={canvasHeight}
width={canvasHeight * aspectRatio}
style={{
maxWidth: '100%',
maxHeight: '100%
}}
/>
This would result in a canvas with the required aspect ratio contained within the current div. When I need landscape I would change the aspect ratio to 6 / 19;
Upvotes: 1
Views: 555
Reputation: 20288
You can do the same for stage container:
const aspectRatio = 16 / 9
const canvasHeight = 1080;
const canvasWidth = 1920;
<Stage
height={canvasHeight}
width={canvasHeight * aspectRatio}
/>
If you want to fit the stage container into parent container, so it takes 100% of parent element you can do this:
const parentWidth = parentContainer.offsetWidht;
const aspectRatio = 16 / 9
const canvasHeight = 1080;
const canvasWidth = 1920;
const scale = parentWidth / canvasWidth;
<Stage
height={canvasHeight / aspectRatio}
width={parentWidth}
scaleX={scale}
scaleY={scale}
/>
Upvotes: 1