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.
April 2, 2026 A new downtown bar is coming to East Eighth Street — but…
April 2, 2026 This piece is sponsored by Avera Health. As oncology research and medicine…
April 2, 2026 The University of South Dakota Sanford School of Medicine will be moving…
2to3.xyz – InterNetX customer – (Germany) Teams across the blockchain ecosystem use .xyz to build…
Asked to name famous shipwrecks at a bar trivia night, a fair few participants might…
Three companies with sites in Newark will eliminate 357 jobs before summer arrives. The Worker…
This website uses cookies.