Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is best practice for handling resize events in a polymer element?
    primarykey
    data
    text
    <p>I'm trying to create a "Viewport" type element extended from a Canvas element. I intended for it to fill the parent container, but that seems not very trivial. </p> <p>The canvas element does not work with style="width:100%; height:100%" (it will revert to a default value of 300x150 pixels if the special Canvas width and the height attributes are not set, or it will go to 100x100 pixels if I try to set those attributes to "100%". This leaves me with the task of manually setting the width of the canvas element according to the size of the parent element. However I am at a loss when trying to figure out a way to access a resize event I could add a handler to from the perspective of the custom element. I don't seem to get access to window.on.resize from anywhere within the custom element. Do I have to do this from the outside?</p> <p>Here is an example polymer element definition:</p> <pre class="lang-html prettyprint-override"><code>&lt;polymer-element name="canvas-viewport"&gt; &lt;template&gt; &lt;style&gt; @host{ :scope { margin:0px; padding:0px; display:block; position:relative; width:100%; height:100%; background:limegreen; overflow:visible; border:0; } } div { border:0; margin:0px; padding:0px; width:100%; height:100%; } &lt;/style&gt; &lt;div id="container"&gt; &lt;canvas width="{{wpWidth}}" height="{{wpHeight}}" id="theCanvas"&gt;&lt;/canvas&gt; &lt;/div&gt; &lt;/template&gt; &lt;script type="application/dart" src="canvas_viewport.dart"&gt;&lt;/script&gt; &lt;/polymer-element&gt; </code></pre> <p>Here is some dart code in the accompanying polymer class definition for trying to handle the resize that I made based on a suggested solution to this question.</p> <pre class="lang-dart prettyprint-override"><code>@override void attached() { super.attached(); viewPortContainer = shadowRoot.querySelector("#container"); window.onResize.listen(resizecontainer); } void resizecontainer(Event e){ wpWidth = viewPortContainer.clientWidth; wpHeight = viewPortContainer.clientHeight; print("resizing Width:$wpWidth , Height:$wpHeight "); } </code></pre> <p>However this results in a bug where the height grows by three pixels on each resize event, even though margins and paddings are set to zero.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload