Render Plain/Rich Text On HTML Canvas – text-to-canvas
This library goes beyond basic text rendering. It lets you control things like alignment, justification, and even measure text height for precise layout.
It can be useful for creating custom graphics, interactive reports, dynamic presentations, or personalized advertisements directly within the browser, without the need for external image editing software.
1. Install the text-to-canvas package.
# Yarn $ yarn add text-to-canvas # NPM $ npm install text-to-canvas
2. Import the following modules as per your needs:
import {
drawText,
specToJson,
splitText,
splitWords,
textToWords,
wordsToJson,
getTextHeight,
getWordHeight,
getTextStyle,
getTextFormat
} from 'text-to-canvas'; 3. Create an HTML canvas element on the page.
<canvas id="example" width="600" height="400"></canvas>
4. Define the text to be rendered
// plain text
const text = 'CSSScript.Com';
// With formatting options
const text = [
{ text: 'CSS', format: { fontWeight: 'bold'} },
{ text: 'Script', format: { fontStyle: 'italic' } },
{ text: '.Com' },
]; 5. Render the text on the canvas.
const canvas = document.getElementById('example');
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, 600, 400);
// Render
drawText(ctx, text, {
// Required!
// width/height of the textbox
width: 300,
height: 300,
// ... more options here
}); 6. More options to customize the rendered text.
drawText(ctx, text, {
// x/y position of the text
x: 0 ,
y: 0,
// 'left', 'right', 'center'
align: 'center',
// vertical align:
// 'top', 'bottom', 'middle'
vAlign: 'middle',
// font family
font: 'Arial',
// font size
fontSize: 14,
// font syle
fontStyle: '',
// font variant
fontVariant: '',
// font weight
fontWeight: '400',
// font color
fontColor: 'black',
// stroke color
strokeColor: 'black',
// stroke width
strokeWidth: 0,
// justify text or not
justify: false,
// If the text or word should be underlined.
// Can also be an object with customization options like color, thickness, and offset.
underline: false,
// If the text or word should have a strikethrough.
// Can also be an object with customization options like color, thickness, and offset.
strikethrough: false,
// infer whitespace in the text
inferWhitespace: true,
// allows overflow
overflow: true,
// debug mode
debug: false,
}); v3.0.1 (12/09/2025)
v3.0.0 (12/07/2025)
v2.0.0 (03/18/2025)
v1.2.1 (03/18/2025)
v1.2.0 (01/16/2025)
v1.1.2 (05/20/2024)
The post Render Plain/Rich Text On HTML Canvas – text-to-canvas appeared first on CSS Script.
Looking for a dual-purpose power bank that can charge both your iPhone and your Apple…
The Punisher: One Last Kill has been the subject of online mockery following a wonky-looking…
Warhammer 40,000: Mechanicus II launches on May 21 — alongside the Warhammer Skulls video game…
Former Nintendo staff have branded the recent leak of upcoming Switch 2 game details as…
Want a few new video games? Well, there are some incredible deals on Switch and…
A newly disclosed security vulnerability in Microsoft Teams could allow attackers to spoof local devices,…
This website uses cookies.