Autosizing canvas inside a polymer grid layout - dart

I have a component that's laid out using polymer-grid-layout which contains a canvas as the main part of the content. I want the canvas to auto resize based on the size that polymer-grid-layout allocates.
I can't put width:100%; height:100% on the canvas as that just stretches the canvas making it distorted and grainy. Instead I wrapped a div around the canvas and manually resize the canvas based on the div size in code.
Code looks somewhat like (note I've left out the boiler plate like etc)
<polymer-grid-layout xnodes="{{xnodes}}"
layout="[[1, 2, 3],
[4, 4, 4]]">
</polymer-grid-layout>
<gauge-slider id='front' label='Front'></gauge-slider>
<panel flex></panel>
<gauge-slider id='back' label='Back'></gauge-slider>
<canvas-view id="canvasView" canvasModel="{{canvasModel}}" flex></canvas-view>
where canvas-view contains
<panel id="canvasContainer" flex>
<canvas id="canvas"></canvas>
</panel>
In the canvas-view dart code I set the canvas width like so
void _resize() {
canvas.width = canvasContainer.clientWidth;
canvas.height = canvasContainer.clientHeight;
_redraw();
}
which I call from
#override
void ready() {
canvas = $['canvas'];
canvasContainer = $['canvasContainer'];
// doesn't seem to be a resize on anything other than window!?!
window.onResize.listen((_) => _resize());
}
and from
#published void set canvasModel(CanvasModel m) {
_canvasModel = m;
// TODO: need to build in a delay as otherwise the containing div has not been sized properly yet
new Timer(new Duration(milliseconds: 50), _resize);
}
The problem is in the above code. If I call _resize directly here then the container div doesn't yet know it's proper height. After experimenting I found that 50 millisecs allows the container to be sized correctly by the grid layout on my machine but that seems really dodgy and likely to be problematic on different devices.
How can I avoid that delay? Is there some event I can listen to that tells me when the grid layout has finished sizing components?
Also was there a simpler way to have achieved the canvas resizing in the first place?
Lastly, I had to style the canvasContainer panel to have 0 margins, border etc. Is there a way to have fetched the inner dimensions of that container?

Is there some event I can listen to that tells me when the grid layout has finished sizing components?
The grid layout sends a polymer-grid-layout event after it has finished sizing components.
Also was there a simpler way to have achieved the canvas resizing in the first place?
I don't think so, but maybe a Canvas expert out there will chime in with better advice.
Lastly, I had to style the canvasContainer panel to have 0 margins, border etc. Is there a way to have fetched the inner dimensions of that container?
clientWidth/Height ignores the margins and border, but does count padding. There are other ways of measuring boxes, but it gets complicated quickly (mostly because of x-browser concerns). This can be a difficult topic, here is a starter link: https://developer.mozilla.org/en-US/docs/Determining_the_dimensions_of_elements

Related

Vertical or horizontal rule in Vaadin Flow

I want to section off one area of a layout from another visually in my Vaadin Flow layout using the Java API.
I want something like the hr horizontal rule found in HTML. I would also want the equivalent, a vertical rule (which was never defined in HTML).
Is there some easy way to have a visual indicator of a thematic shift between parts of a layout?
Hr class
For an <hr> there is the Hr class.
verticalLayout.add(new Span("First"), new Hr(), new Span("Second"));
Roll-your-own
Another option is to create classes for the dividers, there are a few different ways of doing this, here's an example
public class Divider extends Span {
public Divider() {
getStyle().set("background-color", "blue");
getStyle().set("flex", "0 0 2px");
getStyle().set("align-self", "stretch");
}
}
And used as such
horizontalLayout.add(new Span("First"), new Divider(), new Span("Second"));
Using align-self and flex will only work in flex layouts, which includes HorizontalLayout and VerticalLayout. The beauty of this approach is that the same class will work in both. The flex: 0 0 2px tells it to be 2 pixels wide in the direction of the container, and not grow or shrink. The align-self: stretch will tell it to take the full size of the container in the perpendicular direction.
I write this answer as follow-up to my comment on Tazavoo's answer, which is great! I love their custom Divider class, and it has been asked whether this divider can be customized/styled further, something like it is done in this gradient borders page.
Of course this divider can be styled further! But the difference between the divider and the elements in the link is that in the link, the borders of an element is styled, while we need to style the actual element itself here.
CSS attribute in the linked page: border-image. CSS attribute for the Divider background-image.
(I am not familiar enough with CSS -webkit attrributes, so I don't know if you need more than just background-image for a good visualisation in all browsers)
The linked page makes the linear-gradient go in the direction to bottom. We could use that too, but then using the Divider horizontally would look different than using it vertically. That is why we need to set the direction to a diagonal, so both usages of the divider have a similar gradient. See proof of concept in w3schools' TryIt Editor
Here is how I set up the Divider class with a gradient:
public class Divider extends Span {
public Divider(){
getStyle().set("background-image", "linear-gradient(135deg, #777 , rgba(0, 0, 0, 0))");
getStyle().set("flex", "0 0 2px");
getStyle().set("align-self", "stretch");
}
}
To customize the linear gradient even more, please see the docs on w3schools
All the credits of the divider class go to #Tazavoo. Please go upvote their answer

reduce empty space at the top of presentation

I'd really like to be able to decrease the amount of padding at the top of all my level 2 reveal.js slides (version 2.6.2). It doesn't appear to be possible to customise this with CSS because the offset is a negative number calculated dynamically.
e.g. in http://fommil.github.io/scalax14/#/5/1 I would like to reclaim at least 200 pixels at the top. The top parameter is currently being calculated to be -350px but I'd really rather it was closer to -500px.
Is there a way to do this with config?
Reveal keeps the aspect ratio of the resolution you entered in the config.
If you want your slides to have less padding, there are multiple ways:
disable centering:
center: true, // add this in init or config
slides get positioned at the top of the page, and all the padding happens at the bottom
use a 4:3 format for your slides
width: 1024, // add this in init or config
height: 768,
this will move all padding to sides of the page (well, assuming the browser window is not resized) as long as the page ratio is wider than 4:3
create fully responsive slides:
var resizeSlide = function() {
Reveal.configure({
width: window.innerWidth,
height: window.innerHeight
});
}
setInterval(resizeSlide, 1000);
you will then need to make your slides responsive to changes in slide format, but this will make them utilize the full page for the presentation.
I'm using setInterval and not onResize because onResize just don't work on mobile and some browser/os combination (i.e. chrome on windows 8.1 when using keyboard shortcut and desktop hotspots to resize the window)

how to change jquerymobile ui-blocks height and width

am using jquerymobile for representing a two column radiobuttons in my project, the problem am facing is the contents of the block-a is lengther than the contents of the block-b so am getting a difference of div size the output is at (http://i40.tinypic.com/r0tt85.png) if u can lookat my out put the second question 6th option is small when compare to all the other ones the block size is based on the content how can i make same size for all the blocks without based on the content any help please needed
If you want all blocks to be the same height, you can set the height in CSS:
.ui-bar {
height: 100px !important;
}

jquery gradient image height

I am new to jQuery I am facing a some problem with jQuery gradient image please give me some solution for this
I have one div divided vertically in three part left, middle,right.The div has a background images (that is gradient) .and there is some data in the div. I want that the image height should be increase when the user input more data in the div
Can we increase the height of the gradient image using jQuery. Please give me some reference site or some solution
I don't know that you need JQuery to do this for you; it will be fun to do it on you own.
As you know, you can style the div such that its height will increase as you add more content to it. So your question really is how to make the gradient background appear the way you want it: you might want the gradient to grow lighter and lighter at the bottom, say, as the div increases in height. It just so happens (lucky you!) that background-image CSS is rich and capable. That's why it's fun: it actually works!
Read about CSS background-position for some ideas. You might find that the effect you want is automatic.
-- pete

Which is smaller footprint in C#

A lable sized like a rectangle with no text but has a border and is invisible (for a visual rectangle on the form around controls but not to contain the controls) or a panel?
What you want to use is a GroupBox. Not that it really matters, most likely, but a label should be cheaper than a panel.
The answer is; it doesn't matter which has the smaller footprint, and if it does you have a design problem (i.e., you have way too many controls on your form). Anyhow, you should just use the control that fits the job, in this case, a Panel or a GroupBox.
If this is really a problem, then the best way to provide a visual separation between controls is to handle each tab page's Paint event, and use e.Graphics.FillRectangle(...) to draw the separator. You would get rid of a very large number of controls that way.
If you can't do something as simple as just drawing a rectangle underneath each control on each tab page, you can write a code-generating routine that you run one time on the form, and for each tab page you generate something like this (by iterating through all the separator controls on the page):
List<Rectangle> rects = new List<Rectangle>();
rects.Add(new Rectangle(10, 40, 200, 5)); // position of first separator
rects.Add(new Rectangle(10, 80, 200, 5)); // position of second separator
// etc.
Then you copy-and-paste these generated code routines into your application, and use them for each page's Paint event, like so:
SolidBrush brush = new SolidBrush(Color.PeachPuff);
foreach (Rectangle rect in rects)
{
e.Graphics.FillRectangle(brush, rect);
}
Then you delete all the separators from your tab control. What you should end up with is an array of type List<Rectangle> (one list for each page) that you instantiate and fill in the form's Load event or its constructor (using the generated code).
I have to reiterate what Ed said, though. .Net forms can have a lot of controls on them without any real problems, so if you're running into problems stemming from having too many controls on the form, you might be better off redesigning the whole thing.

Resources