-1

There is no blur when I run the code in the compilers on the internet, but it seems quite blurry when I run it on my project. What could be the reason for this? Photos and codes are below.

.ts code

    public DrawTheTree() {
    var canvas: HTMLCanvasElement = document.getElementById("tree_canvas") as HTMLCanvasElement;
    var context: CanvasRenderingContext2D = canvas.getContext("2d") as CanvasRenderingContext2D;
        context.beginPath();
        context.font = "16px Comic Sans MS";
        context.fillStyle = "white";
        context.textAlign = "center";
        context.fillText("Animals", 150, 15);
        context.closePath();    
    }

.html code

<canvas id="tree_canvas" class="tree_canvas_class"></canvas>

.css code

.tree_canvas_class {
background-color: darkblue;
width: 936px;
height: 435px;
}

BlurryPhoto:https://www.dropbox.com/s/mle8vepnyesapip/Capture.JPG?dl=0 DesiredPhoto: https://www.dropbox.com/s/g0fwn46ibrgascy/ss.JPG?dl=0

osmnfrkn61
  • 173
  • 1
  • 1
  • 7

1 Answers1

2

What could be the reason for this?

That's how canvas works. It's a raster image, not a vector.

You create a text using 16px font (context.font = "16px Comic Sans MS") and then enlarge it by using CSS (width: 936px; height: 435px;).

There are a few possible solutions to make the text non-blurry.

  • Do not use canvas.
  • Do not resize canvas.
Lazar Ljubenović
  • 18,976
  • 10
  • 56
  • 91