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.


document.body.appendChild(canvas);

This is how it will look like:


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Solution to “TypeError: ‘x’ is not iterable” in Angular 9AngularHow to progressively load images and add a blurry placeholder?CSSHow to create a simple tab interaction using CSS?CSSWordPress: How to access first and random row values from a repeater field in ACF?WordPressA Step by Step Guide to Make RSS in XML For Any Website or Blog For FreeMiscellaneousUseful (and probably ignored) HTML tags: Part 1HTMLThe 7 Security Objectives of Any Organization for IT and Network SecurityMiscellaneousHow to add a Bar Chart in Angular App?AngularUnderstand Inheritance in PHP in this Basic Example (For Beginners)PHPJavaScript Data Types and Data Structures Things to RememberJavaScriptWindow setInterval() Method in JavaScriptJavaScriptCustom Post Type Template Files in WordPressWordPress