4.1.1 • Published 8 months ago

canvas-txt v4.1.1

Weekly downloads
746
License
MIT
Repository
github
Last release
8 months ago

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

Features

  • Multiline text
  • Auto line breaks
  • Horizontal Align
  • Vertical Align
  • Justify Align
  • Easy Debugging
  • 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

npm.io

Properties

PropertiesDefaultDescription
widthRequiredWidth of the text box
heightRequiredHeight of the text box
xRequiredX position of the text box
yRequiredY position of the text box
debugfalseShows the border and align gravity for debugging purposes
aligncenterText align. Other possible values: left, right
vAlignmiddleText vertical align. Other possible values: top, bottom
fontArialFont family of the text
fontSize14Font 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
lineHeightnullLine height of the text, if set to null it tries to auto-detect the value
justifyfalseJustify text if true, it will insert spaces between words when necessary.

Methods

import { drawText, splitText, getTextHeight } from 'canvas-txt'
MethodDescription
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
4.0.5

9 months ago

4.0.4

10 months ago

4.0.7

8 months ago

4.0.6

8 months ago

4.1.0

8 months ago

4.0.1

10 months ago

4.0.0

10 months ago

4.0.3

10 months ago

4.1.1

8 months ago

4.0.2

10 months ago

3.0.0

4 years ago

3.0.0-beta.3

4 years ago

3.0.0-beta.1

4 years ago

2.0.10

4 years ago

2.0.9

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago