
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
text-canvas
Advanced tools
With TextCanvas you can render multi-line text to a 2D canvas that will automatically resize to the text dimensions. This can be useful if you need to create a texture to use on a WebGL canvas or even if you just need text to wrap text automatically on a 2D canvas.
bower install ext-date
npm install text-canvas
TextCanvas is an UMD module. You can load it into your application either by importing the module, or loading the script in your page.
If you are importing the TextCanvas module via Webpack, Browserify or similar, make sure that the module name text-canvas is being resolved correctly to the Bower or NPM packages folder.
import TextCanvas from 'text-canvas'
var TextCanvas = require('text-canvas')
<script src="/scripts/TextCanvas.js"></script> // Change the path as necessary
Creates an instance of TextCanvas, which you will be able to modify and re-render at any point.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| text | String | false | '' | The text to be rendered on the canvas. |
| style | Object | false | {} | The style to use when rendering the text. |
| resolution | Number | false | window.devicePixelRatio | The resolution, or pixel density, of the rendered text. |
Returns
TextCanvas - The created instance.
Example
const textCanvas = new TextCanvas('Hello, World!', {fontFamily: 'Arial', wordWrap: 300}, 2)
Get or set the text that will be rendered. To force line breaks, use the new line character \n.
Returns
String - The text currently set to be rendered.
Example
const text = textCanvas.text // Outputs the text that will be rendered
textCanvas.text = 'Hello, World!' // Sets the text that will be rendered
Get or set the text style. Setting the style is an additive operation. All the previously existing styles that are not being set will be kept.
Properties
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| fontFamily | String | false | 'sans-serif' | The font family name. Unlike the CSS font-family property, fallback font families are not supported. |
| fontStyle | String | false | 'normal' | The font style. The possible values are normal, italic or oblique. |
| fontWeight | String/Number | false | 'normal' | The font weight. The possible values are normal, bold, or a multiple of 100 from 100 to 900. |
| fontVariant | String | false | 'normal' | The font variant. The possible values are normal or small-caps. |
| fontSize | Number | false | 16 | The font size in pixels. |
| lineHeight | Number | false | fontSize * 1.2 | The text line height, in pixels. |
| textAlign | String | false | 'left' | The alignment of the text. The possible values are left, center or right. |
| textBaseline | String | false | 'bottom' | The baseline of the text. The possible values are top, hanging, middle, alphabetic, ideographic or bottom. Changing this value might create some clipping. |
| textColor | String | false | 'black' | The text colour. It can be any CSS colour string supported by the browser. |
| wordWrap | Boolean/Number | false | false | Set to false to draw all the text in a single line. Setting it to a number will define the maximum line width in pixels. |
Returns
Object - The full text style object.
Example
const style = textCanvas.style // Outputs the full text style object
textCanvas.style = {fontSize: 24, fontWeight: 'bold'} // Sets the text style options
Get or set the pixel density or resolution of the canvas. This will affect the dimensions of the canvas.
Returns
Number - The pixel density or resolution at which the text will be rendered.
Example
const resolution = textCanvas.resolution // Outputs the resolution of the canvas
textCanvas.resolution = 2 // Sets the resolution of the canvas
Renders the text to the canvas with the currently set styles and resolution. TextCanvas uses always the same canvas per instance. If you are using the canvas from a previously rendered text you need to take this into consideration because the dimensions and contents of the canvas will change.
Returns
HTMLCanvasElement - The canvas element in which the text was rendered.
Example
const canvas = textCanvas.render()
document.body.appendChild(canvas)
TextCanvas, although not tested in all of them, was created using features supported by these browsers.
If you want to request new features or find any bugs, please open a ticket on the issues page and I'll review it as soon as possible.
Created by Luís Rodrigues (@MrSlide)
Released under the MIT license
Copyright (c) 2016 Luís Rodrigues
FAQs
Renders wrapped text to a 2D canvas element.
We found that text-canvas demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.