将画布调整为其父元素ion-grid

时间:2017-07-18 10:32:21

标签: javascript html5 canvas flexbox ionic3

我正在离子3中构建一个组件,它放置一个图像网格,并用画布覆盖它们。我已经使用了离子的离子网格,但是当我尝试调整画布大小时,我找不到容器的正确尺寸,因此画布容器保持其初始大小。我想让它变得动态,所以我需要画布具有与网格容器相同的大小。这是我的代码:

    <ion-content>
  <ion-card>
    <div ion-card-content>

        <div ion-grid id= "container-graph">
           <ion-row responsive-sm>
            <canvas id="connection-canvas" style="position: absolute;"></canvas>
          </ion-row>
          <ion-row responsive-sm>
            <ion-col col-6 text-center>
              Generation
              <img id="generation" src="../../assets/img/solar-panel.png">
            </ion-col>
            <ion-col col-6 text-center>
              Grid
              <img id="grid" src="../../assets/img/grid.png">
            </ion-col>
          </ion-row>
          <ion-row responsive-sm>
            <img id="inverter" src="../../assets/img/logo.png">
          </ion-row>
          <ion-row responsive-sm>
            <ion-col col-6 text-center>
              Battery
              <img id="battery" src="../../assets/img/battery.png">
            </ion-col>
            <ion-col col-6 text-center>
              Consumption
              <img id="consumption" src="../../assets/img/house.png">
            </ion-col>
          </ion-row>
        </div>
    </div>
  </ion-card>
  <ion-card>
    <ion-card-content>
      {{description}}
    </ion-card-content>
  </ion-card>
</ion-content>





public drawGraphs (center:string, a:string, b:string, c:string, d:string){
    var canvas: any = document.getElementById("connection-canvas");
    var connectionGraph: any = document.getElementById("container-graph");
    canvas.width = connectionGraph.clientWidth;
    canvas.height = connectionGraph.clientHeight;
    var elCenter = document.getElementById(center);
    var contCenter = elCenter.getBoundingClientRect();
    var elA = document.getElementById(a);
    this.drawLine(elCenter,elA);
    var elB = document.getElementById(b);
    this.drawLine(elCenter,elB);
    var elC = document.getElementById(c);
    this.drawLine(elCenter,elC);
    var elD = document.getElementById(d);
    this.drawLine(elCenter,elD);


  }

1 个答案:

答案 0 :(得分:0)

我刚刚找到了可行的解决方案。我把这个函数称为&#34; drawgraphs&#34;通过使用生命周期挂钩界面 ngAfterViewChecked ,在视图完全重新设置后 More info

ngAfterViewChecked(){
    this.drawGraphs("inverter", "generation", "grid", "battery", "consumption")}