最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Flexbox, canvas & dynamic resizing - Stack Overflow

matteradmin4PV0评论

I have trouble with my canvas-based application.

I have 4 (or more) canvas, each one wrapped into div with aside content. Those wrapper are themselves wrapped into "hbox". The goal is to make a dynamic grid of canvasses using flexbox.

<div id="primaryScene" class="scene">
    <div class="hbox" id="hbox0" style="flex-grow: 1.2;">
        <div class="viewWrapper" id="view0" style="flex-grow: 0.4;">
            <canvas class="canvasView"></canvas>
            <aside class="sideContent"></aside>
        </div>
        <div class="viewWrapper" id="view1" style="flex-grow: 1.6;">
            <canvas class="canvasView"></canvas>
            <aside class="sideContent"></aside>
        </div>
    </div>
    <div class="hbox" id="hbox1" style="flex-grow: 0.8;">
        <div class="viewWrapper" id="view2">
            <canvas class="canvasView"></canvas>
            <aside class="sideContent"></aside>
        </div>
        <div class="viewWrapper" id="view3">
            <canvas class="canvasView"></canvas>
            <aside class="sideContent"></aside>
        </div>
    </div>
</div>

With css:

.scene {
    display: -webkit-flex;
    display: flex;
    -webkit-flex-flow: column;
    flex-flow: column;
    overflow: hidden;
}

.hbox {
    min-height: 0;
    -webkit-flex: 1 1 0%;
    flex: 1 1 0%;
    display: -webkit-flex;
    display: flex;
    position: relative;
}
.viewWrapper {
    min-height: 0;
    -webkit-flex: 1 1 0%;
    flex: 1 1 0%;
    position: relative;
    margin: 0 2px 2px 0;
}

.canvasView {
    display: block;
}

.canvasView {
    position: absolute;
}

.sideContent{
    max-width: 120px;
    overflow-x: hidden;
    overflow-y: auto;
    margin-right: 6px;
}

Because I need some resize events, I don't use the CSS property resize: both; Troubles append when I try to add .sideContent because I want them at the right of each canvas.

Before that, with the canvas in absolute, I just needed to dynamically update width and height with {.viewWrapper}.getBoundingClientRect().width|height (beside the fact that they are flex:1, viewWrapper and hbox are manually resized with flex-grow like showed in the code above). Now, when I switch the canvas to flex:1; and remove the absolute property, they do not shrink anymore. I also get different values from {oneCanvas}.getBoundingClientRect() between Chrome and Firefox (didn't test on IE) so I can't use this either.

What can I do? Tell me if you need more information.

I have trouble with my canvas-based application.

I have 4 (or more) canvas, each one wrapped into div with aside content. Those wrapper are themselves wrapped into "hbox". The goal is to make a dynamic grid of canvasses using flexbox.

<div id="primaryScene" class="scene">
    <div class="hbox" id="hbox0" style="flex-grow: 1.2;">
        <div class="viewWrapper" id="view0" style="flex-grow: 0.4;">
            <canvas class="canvasView"></canvas>
            <aside class="sideContent"></aside>
        </div>
        <div class="viewWrapper" id="view1" style="flex-grow: 1.6;">
            <canvas class="canvasView"></canvas>
            <aside class="sideContent"></aside>
        </div>
    </div>
    <div class="hbox" id="hbox1" style="flex-grow: 0.8;">
        <div class="viewWrapper" id="view2">
            <canvas class="canvasView"></canvas>
            <aside class="sideContent"></aside>
        </div>
        <div class="viewWrapper" id="view3">
            <canvas class="canvasView"></canvas>
            <aside class="sideContent"></aside>
        </div>
    </div>
</div>

With css:

.scene {
    display: -webkit-flex;
    display: flex;
    -webkit-flex-flow: column;
    flex-flow: column;
    overflow: hidden;
}

.hbox {
    min-height: 0;
    -webkit-flex: 1 1 0%;
    flex: 1 1 0%;
    display: -webkit-flex;
    display: flex;
    position: relative;
}
.viewWrapper {
    min-height: 0;
    -webkit-flex: 1 1 0%;
    flex: 1 1 0%;
    position: relative;
    margin: 0 2px 2px 0;
}

.canvasView {
    display: block;
}

.canvasView {
    position: absolute;
}

.sideContent{
    max-width: 120px;
    overflow-x: hidden;
    overflow-y: auto;
    margin-right: 6px;
}

Because I need some resize events, I don't use the CSS property resize: both; Troubles append when I try to add .sideContent because I want them at the right of each canvas.

Before that, with the canvas in absolute, I just needed to dynamically update width and height with {.viewWrapper}.getBoundingClientRect().width|height (beside the fact that they are flex:1, viewWrapper and hbox are manually resized with flex-grow like showed in the code above). Now, when I switch the canvas to flex:1; and remove the absolute property, they do not shrink anymore. I also get different values from {oneCanvas}.getBoundingClientRect() between Chrome and Firefox (didn't test on IE) so I can't use this either.

What can I do? Tell me if you need more information.

Share Improve this question edited May 10, 2016 at 13:26 ManoDestra 6,5036 gold badges29 silver badges50 bronze badges asked Aug 21, 2015 at 16:32 lsagetlethiaslsagetlethias 3382 silver badges14 bronze badges 6
  • Up. Really need some help here o/ – lsagetlethias Commented Aug 31, 2015 at 14:51
  • 2 Can you make a Fiddle? – user1156544 Commented Apr 15, 2016 at 15:36
  • 2 It's possible make a jsfiddle? – Roger Russel Commented May 24, 2016 at 17:18
  • Have you considered re-using flexbox within each .viewWrapper div? – Darvanen Commented Jun 9, 2016 at 6:08
  • @L.S. I tried making a jsfiddle with the HTML/CSS you provided, but I don't see anything in the result window. Could you please create a jsfiddle, or post enough information to create a working jsfiddle? – Austin Pocus Commented Jun 15, 2016 at 23:18
 |  Show 1 more ment

1 Answer 1

Reset to default 2

I am not sure if this is what you wanted, however here is a sample fiddle below.

Scene is a flex container so that you can manipulate the different hboxes. viewwrapper is also a flex container so that you can adjust how you want your canvas and aside to flex.

The reason why adding position:absolute would affect layout is because absolutely positioned elements do not participate in the flex layout.

https://www.w3/TR/css-flexbox-1/#abspos-items

https://jsfiddle/mzrrsunn/1/

.scene {
    display: flex;
}

.hbox {
    flex: 1;
}
.viewWrapper {
    display: flex;
    margin: 0 2px 2px 0;
}

.canvasView {
    flex: 1 1 60%;
}

.sideContent{
    flex: 0 1 40%;
}

canvas{
  border:1px solid black;
}

aside {
  border: 1px solid blue;
}
Post a comment

comment list (0)

  1. No comments so far