• Stars
    star
    502
  • Rank 87,851 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Multiline text on HTML5 Canvas
canvas-txt multiline text on html canvas

Canvas Txt 📐

Transforming Your Canvas with Multiline Magic

NPM NPM NPM

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

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

More Repositories

1

i-hate-regex

The code for iHateregex.io 😈 - The Regex Cheat Sheet
Vue
4,492
star
2

Git-User-Switch

Switch git user and email with ease
JavaScript
635
star
3

regex-colorizer

Highlighter for JavaScript regex syntax
JavaScript
45
star
4

Nest-Remixed

Single server NestJS starter with Remix frontend.
TypeScript
12
star
5

QuicMath-Math-Quiz

Never ending math quiz made with Vue
Vue
5
star
6

Chat-Application-Node-Socket-io

A chat application using express and sockets.io
HTML
5
star
7

GST-HSN-Codes-Fetch

Scrape latest GST HSN codes from official website
JavaScript
5
star
8

Pomodoro-timer-react-js

Pomodoro timer in React js
CSS
5
star
9

Flappy-Bird-Phaser3

Flappy Bird clone using Phaser3
JavaScript
4
star
10

Kopp-app-Group-Marathon

Kopp.app helps people progress togather
Vue
4
star
11

Rock-Paper-Scissors-Js

Rock Paper Scissors in js
JavaScript
4
star
12

Warehouse-Inventory-Management

E-commerce platform + Inventory Management in Node JS and MySql
JavaScript
4
star
13

Pure-Bliss-Distraction-Free-Notepad

Pure Bliss! A distraction free notepad to help your creative juice flowing.
JavaScript
4
star
14

Life-in-Weeks

Life in weeks visualization inspired by waitbutwhy
JavaScript
3
star
15

Facebook-Auto-Poster

Autoposter for facebook groups
JavaScript
3
star
16

Final-Project-Server

Flask server that runs the model on the cloud for cnn image models
Python
3
star
17

ML-Lens-character-extracter

Vue
2
star
18

regexper-styled

clone of https://regexper.com
JavaScript
2
star
19

Facebook-Instant-Game-Bot

Messenger bot for instant game
JavaScript
2
star
20

Conway-s-Game-of-Life-with-p5.js

My attempt to make a Conway's game of life using p5.js
JavaScript
2
star
21

Yet-another-Face-extracter

extracts faces (disclaimer: might summon demons)
Python
2
star
22

Canvas-txt-demo-site

Demo Site for Canvas Txt
Vue
2
star
23

react-use-print

Detect and conditionally render for Print in react
TypeScript
2
star
24

Dodge-The-Creeps-Game-GoDot

Dodge The Creeps Game created with godot engine
GDScript
1
star
25

Hyperion-for-Thea

Hyperion, Brother app of Theia
Vue
1
star
26

Bible-Scan

HTML
1
star
27

vue-quotes-app

A quotes application with Vue JS
Vue
1
star
28

platformer-game-godot

godot platformer
GDScript
1
star
29

KSEB-Rate-Calculator-Vue

A simple rate Calculator application for KSEB
Vue
1
star
30

Theia-Scan-Extracter

Extract characters from scanned images
Python
1
star
31

rebuild-earth-nuxt

rebuild earth client in nuxt
Vue
1
star
32

Minimal-HN-React

Minimal Hacker News Reader React
CSS
1
star
33

Youtube-Bulk-Video-downloader

Download multiple Youtube videos using node js
JavaScript
1
star