
Security News
Package Maintainers Call for Improvements to GitHub’s New npm Security Plan
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
node-canvas-text
Advanced tools
Using a provided Opentype.js font object, draws a string, sized to fit inside a given rectangle on an HTML5 canvas
Draws your string on a canvas, fit inside of a rectangle. I had to make this, because measureText() from node-canvas is unpredictable, and ignores font selectors besides font-size and font-family.
npm install node-canvas-text canvas opentype.js --save
This module exports a single function with signature:
{ x, y, width, height }
{ minSize, maxSize, granularity, hAlign, vAlign, fitMethod, drawRect }
float
float
float
'left' | 'center' | 'right'
'top' | 'center' | 'bottom'
'baseline' | 'box'
'true' | 'false'
string
string
'true' | 'false'
float
float
{
minSize: 10,
maxSize: 200,
granularity: 1,
hAlign: 'left',
vAlign: 'bottom',
fitMethod: 'box',
textFillStyle: '#000',
rectFillStyle: '#fff',
rectFillOnlyText: false,
textPadding: 0,
fillPadding: 0,
drawRect: false
}
import drawText from 'node-canvas-text'
import opentype from 'opentype.js'
import Canvas from 'canvas'
let canvas = new Canvas(imgWidth, imgHeight);
let ctx = canvas.getContext('2d');
// Load OpenType fonts from files
let titleFont = opentype.loadSync(__dirname + '/fonts/PTN57F.ttf');
let priceFont = opentype.loadSync(__dirname + '/fonts/PTC75F.ttf');
let barcodeFont = opentype.loadSync(__dirname + '/fonts/code128.ttf');
// Strings to draw
let titleString = "A string, but not too long",
priceString = "200",
barcodeString = "54490000052117";
// Calculate bounding rectangles
let headerRect = {
x: 0,
y: 0,
width: canvas.width,
height: canvas.height / 3.5 };
let priceRect = {
x: canvas.width / 2,
y: headerRect.height,
width: canvas.width / 2,
height: canvas.height - headerRect.height };
let barcodeRect = {
x: 0,
y: headerRect.height + priceRect.height / 2,
width: canvas.width - priceRect.width,
height: priceRect.height / 2
};
// Draw
let drawRect = true;
drawText(ctx, titleString, titleFont, headerRect,
{
minSize: 5,
maxSize: 100,
vAlign: 'bottom',
hAlign: 'left',
fitMethod: 'box',
drawRect: drawRect} );
drawText(ctx, priceString, priceFont, priceRect,
{
minSize: 5,
maxSize: 200,
hAlign: 'right',
vAlign: 'bottom',
fitMethod: 'box',
drawRect: drawRect } );
drawText(ctx, barcodeString, barcodeFont, barcodeRect,
{
minSize: 5,
maxSize: 200,
hAlign: 'center',
vAlign: 'center',
fitMethod: 'box',
drawRect: drawRect });
FAQs
Using a provided Opentype.js font object, draws a string, sized to fit inside a given rectangle on an HTML5 canvas
We found that node-canvas-text 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
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.