Angular 6 - Not able to add text on canvas dynamically

后端 未结 2 2010
谎友^
谎友^ 2021-01-25 13:30

I have created canvas element. Once user add some text with the help of keyboard, on click of done button I want to add text on canvas. I did below changes

1. im

相关标签:
2条回答
  • 2021-01-25 14:11

    In a pure JS implementation your code works fine:

    const canvas = document.getElementById('test');
    canvas.width = canvas.height = 100;
    ctx = canvas.getContext('2d');
    ctx.font = 'italic 18px Arial';
    ctx.textAlign = 'center';
    ctx. textBaseline = 'middle';
    ctx.fillStyle = 'red';  
    ctx.fillText('Hello!', 50, 50); 
      
    <canvas id="test" style="border: 1px solid blue;" ></canvas>

    The only issue that I could see is the:
    this.canvasElement = this.canvas.nativeElement;
    That must not be getting the correct canvas

    0 讨论(0)
  • 2021-01-25 14:32

    For TypeScript/Angular:

    const canvas = <HTMLCanvasElement> document.getElementById('test');
    ctx = canvas.getContext('2d');
    ctx.font = 'italic 18px Arial';
    ctx.textAlign = 'center';
    ctx. textBaseline = 'middle';
    ctx.fillStyle = 'red';  
    ctx.fillText('Hello!', 150, 50); 
    
    0 讨论(0)
提交回复
热议问题