Get the App
SLTechnology News&Howtos  ›  Development  › 

How html draws text on canvas

Shulou Source: shulou.com Published: 2022-06-01 07:26:09 09月25日 Update

This article mainly shows you "html how to draw text on the canvas", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "html how to draw text on the canvas" this article.

You can also draw text on the canvas. The text can contain any Unicode character. The following example draws a simple greeting message "Hello World!" . On the canvas.

Try this code for example:

_ window.onload = function () {var canvas = document.getElementById ("myCanvas"); var context = canvas.getContext ("2d"); context.font = "bold 32px Arial"; context.fillText ("Hello World!", 50,100);}

You can also set the color and alignment of the text on the canvas, as follows:

Try this code for example:

_ window.onload = function () {var canvas = document.getElementById ("myCanvas"); var context = canvas.getContext ("2d"); context.font = "bold 32px Arial"; context.textAlign = "center"; context.textBaseline = "middle"; context.fillStyle = "orange"; context.fillText ("Hello World!", 150,100);}

You can also apply strokes to text using the strokeText () method. This method will color around the text instead of filling it. However, if you want to set both fills and strokes on the canvas, you can use both the fillText () and strokeText () methods.

Try this code for example:

_ window.onload = function () {var canvas = document.getElementById ("myCanvas"); var context = canvas.getContext ("2d"); context.font = "bold 32px Arial"; context.textAlign = "center"; context.textBaseline = "middle"; context.strokeStyle = "orange"; context.strokeText ("Hello World!", 150,100);} The above is all the content of the article "how to draw text on canvas by html". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Text canvas code content articles at the same time methods strokes learning help characters ways easy to understand more organized this method messages knowledge examples editing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Linux MySQL Xiaomi Apple