Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox, canvas & dynamic resizing

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.

like image 402
lsagetlethias Avatar asked Aug 21 '15 16:08

lsagetlethias


People also ask

What is a flexbox used for?

Flexbox is a layout model that allows elements to align and distribute space within a container. Using flexible widths and heights, elements can be aligned to fill a space or distribute space between elements, which makes it a great tool to use for responsive design systems.

Can I use flexbox in HTML?

You can apply Flexbox styling to any HTML element, which means that you can easily restyle and reflow elements independently of each other as there is no fixed container hierarchy like there is in tables.

What are the disadvantages of using flexbox?

Disadvantages. While flexbox is supported by most major browsers, it's still newer than the traditional box model. This means older browsers don't support it as well (some not at all). There are also more inconsistencies across different browsers.

Is flexbox same as Flex?

Flexbox is a purely css layout, which means the entire layout is manage and control at css level, no need to play with the markups. Flex model is capable to adjust its items height/width to best fill in the available space of container. It can expand or shrink to avoid overflow.


1 Answers

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.org/TR/css-flexbox-1/#abspos-items

https://jsfiddle.net/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;
}
like image 92
derp Avatar answered Oct 09 '22 11:10

derp