首页 > 解决方案 > 每当我的画布加载时,它只需要全宽而不是全高

问题描述

所以我的画布只需要全宽而不是全高。

我尝试使画布全高和全宽的组件:

export class AimComponent implements OnInit {

  @ViewChild('canvas') myCanvas: ElementRef;
  public context: CanvasRenderingContext2D;

  ngOnInit() {
    this.ngViewInit();
  }
  ngViewInit(){
    this.context = 
    (<HTMLCanvasElement>this.myCanvas.nativeElement).getContext('2d');
    this.draw();
  }

  draw() {
    this.myCanvas.nativeElement.width = document.body.clientWidth;
    this.myCanvas.nativeElement.height = document.body.clientHeight;
  }
}

标签: angulartypescriptcanvas

解决方案


使用window.innerWith andwindow.innerHeight而不是document.body.clientWidth/clientHeight,我能够解决这个问题。


推荐阅读