Установите flex: 1
в .canvasarea
, чтобы вместить пространство, доступное в гибком контейнере, и max-width: 100%
в элементе canvas, чтобы предотвратить переполнение:
var canvas1b = document.getElementById("cvs1back");
canvas1b.height = 400;
canvas1b.width = 400;
var ctx1b = canvas1b.getContext("2d");
ctx1b.fillStyle = "red";
ctx1b.fillRect(0, 0, canvas1b.width, canvas1b.height);
var canvas1f = document.getElementById("cvs1front");
canvas1f.height = 400;
canvas1f.width = 400;
var ctx1f = canvas1f.getContext("2d");
ctx1f.beginPath();
ctx1f.moveTo(0, 0);
ctx1f.lineTo(0, 0);
ctx1f.stroke();
var canvas2b = document.getElementById("cvs2back");
canvas2b.height = 400;
canvas2b.width = 400;
var ctx2b = canvas2b.getContext("2d");
ctx2b.fillStyle = "blue";
ctx2b.fillRect(0, 0, canvas2b.width, canvas2b.height);
var canvas2f = document.getElementById("cvs2front");
canvas2f.height = 400;
canvas2f.width = 400;
var ctx2f = canvas2f.getContext("2d");
.row {
display: flex;
}
.column {
flex: 1;
}
.canvasarea {
position: relative;
flex: 1;
}
.canvasarea canvas {
position: absolute;
max-width: 100%;
}
span {
background: yellow;
}
<div class="row">
<span>Some text</span>
</div>
<div class="row">
<div class="column">
<div class="canvasarea">
<canvas id="cvs1back"></canvas>
<canvas id="cvs1front"></canvas>
</div>
</div>
<div class="column">
<div class="row">
<span>Some other text</span>
</div>
<div class="row">
<div class="canvasarea">
<canvas id="cvs2back"></canvas>
<canvas id="cvs2front"></canvas>
</div>
</div>
</div>
</div>