npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

canvas-txt

v4.1.1

Published

Render multiline textboxes in HTML5 canvas with auto line breaks and better alignment system

Downloads

17,610

Readme

A Miniscule library to render text on HTML5 Canvas with ZERO dependencies

Features

  • [x] Multiline text
  • [x] Auto line breaks
  • [x] Horizontal Align
  • [x] Vertical Align
  • [x] Justify Align
  • [x] Easy Debugging
  • [x] Improved Performance

Demo

See Demo: Here

Install

yarn add canvas-txt

or

npm i canvas-txt

Usage

<canvas id="myCanvas" width="500" height="500"></canvas>

Bundler

import { drawText } from 'canvas-txt'

const c = document.getElementById('myCanvas')
const ctx = c.getContext('2d')

ctx.clearRect(0, 0, 500, 500)

const txt = 'Lorem ipsum dolor sit amet'

const { height } = drawText(ctx, txt, {
  x: 100,
  y: 200,
  width: 200,
  height: 200,
  fontSize: 24,
})

console.log(`Total height = ${height}`)

Node canvas

See Node js demo in ./src/node-test.ts

const { createCanvas } = require('canvas')
const { drawText } = require('canvas-txt')
const fs = require('fs')

// Or
// import { createCanvas } from 'canvas'
// import { drawText } from 'canvas-txt'
// import * as fs from 'fs'

function main() {
  const canvas = createCanvas(400, 400)
  const ctx = canvas.getContext('2d')
  const txt = 'Hello World!'

  const { height } = drawText(ctx, txt, {
    x: 100,
    y: 200,
    width: 200,
    height: 200,
    fontSize: 24,
  })

  // Convert the canvas to a buffer in PNG format
  const buffer = canvas.toBuffer('image/png')
  fs.writeFileSync('output.png', buffer)
  console.log(`Total height = ${height}`)
}

main()

CDN

See fiddle : https://jsfiddle.net/geongeorgek/n5xw3ufj/2/

<script src="//unpkg.com/canvas-txt"></script>
const { drawText, getTextHeight, splitText } = window.canvasTxt
/// ...remaining same

Properties

| Properties | Default | Description | | :-----------: | :----------: | :----------------------------------------------------------------------------- | | width | Required | Width of the text box | | height | Required | Height of the text box | | x | Required | X position of the text box | | y | Required | Y position of the text box | | debug | false | Shows the border and align gravity for debugging purposes | | align | center | Text align. Other possible values: left, right | | vAlign | middle | Text vertical align. Other possible values: top, bottom | | font | Arial | Font family of the text | | fontSize | 14 | Font size of the text in px | | fontStyle | '' | Font style, same as css font-style. Examples: italic, oblique 40deg | | fontVariant | '' | Font variant, same as css font-variant. Examples: small-caps, slashed-zero | | fontWeight | '' | Font weight, same as css font-weight. Examples: bold, 100 | | lineHeight | null | Line height of the text, if set to null it tries to auto-detect the value | | justify | false | Justify text if true, it will insert spaces between words when necessary. |

Methods

import { drawText, splitText, getTextHeight } from 'canvas-txt'

| Method | Description | | :---------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | drawText(ctx,text, config) | To draw the text to the canvas | | splitText({ ctx, text, justify, width } | To split the text { ctx: CanvasRenderingContext2D, text: string, justify: boolean, width: number } | | getTextHeight({ ctx, text, style }) | To get the height of the text { ctx: CanvasRenderingContext2D, text: string, style: string (font style we pass to ctx.font) } ctx.font docs |