Edit Canvas by Angular Renderer2 not working

374 Views Asked by At

When reading documentation of Angular I found an information stating that DOM Elements should be altered by the Renderer2 class. I tried to update my project where I used ViewChild directive, but this way is not working with HTML Canvas Elements, normal HTML elements works like a charm. Is HTML Canvas Element modification not supported by Renderer2 ?

    @ViewChild('canvas_foreground', {static: true}) canvasForeground: ElementRef<HTMLCanvasElement>;
    @ViewChild('canvas_background', {static: true}) canvasBackground: ElementRef<HTMLCanvasElement>;
    wrapper: ElementRef<HTMLElement>;

    ngOnInit(): void {
        this.wrapper = this.el.nativeElement.querySelector('.canvases-wrapper');
        this.setWidthAndHeight();
        ...
    }

    setWidthAndHeight(): void {
        ...
        // working method
        this.renderer.setStyle(this.wrapper, 'width', this.screenWidth);
        this.renderer.setStyle(this.wrapper, 'height', this.screenHeight);

        // working method
        this.canvasBackground.nativeElement.width = this.screenWidth;
        this.canvasBackground.nativeElement.height = this.screenHeight;
        this.canvasForeground.nativeElement.width = this.screenWidth;
        this.canvasForeground.nativeElement.height = this.screenHeight;

        //not working method
        this.renderer.setStyle(this.canvasBackground.nativeElement, 'width', this.screenWidth);
        this.renderer.setStyle(this.canvasBackground.nativeElement, 'height', this.screenHeight);
        this.renderer.setStyle(this.canvasForeground.nativeElement , 'width', this.screenWidth);
        this.renderer.setStyle(this.canvasForeground.nativeElement, 'height', this.screenHeight);
     }
0

There are 0 best solutions below