Yogesh Chauhan's Blog

How to Draw a Text Image using JavaScript?

in JavaScript on September 18, 2020

We can create a text image using HTML canvas.

Step 1

Build a canvas to hold the image pixel info.

var canvas = document.createElement('canvas');
canvas.width = 500;
canvas.height = 250;

Step 2

Then we need to select a context for the canvas. We can add 2d for two dimensional.

var ctx = canvas.getContext('2d');

Step 3

Finally, we can add text properties to draw the final text image.

ctx.font = '10px Cursive';
ctx.fillText("HTML Canvas!", 0, 30);

Step 4

Now, all we need to do is to add the newly created image to the body.


This is how it will look like:

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
WordPress: How to display fields from ACF Flexible Contents?WordPressHow to replace HTML lists using CSS Counters?CSSHow to add Local State to a Class in React?ReactHow to use HTML picture Tag?HTMLA Quick Guide to Object-Oriented Programming in PHPPHPHow to change HTML content on click using JavaScript?JavaScriptWhat are Controlled Components in React?ReactWhat is the correct way to enqueue multiple CSS files in WordPress?WordPressHow to change the style for checked radio button or checkboxes using CSS?CSSHow to create ‘share on LinkedIn’ link using just HTML?HTMLThe difference between Class declarations and Class expressions in JavaScriptJavaScriptHow to use Context in React?React