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

skia-canvas-draw-text

v1.3.1

Published

A function to make drawing text easier with skia-canvas

Downloads

17

Readme

skia-canvas-draw-text

A function to make drawing text easier with skia-canvas

await drawText("Hello there!")

Install

$ npm i skia-canvas-draw-text

Usage

import drawText from "skia-canvas-draw-text"

await drawText(textString, arguments)

Arguments

Certain arguments are not needed if no context is provided. These are marked with *

  • ctx
    • ctx: canvas.getContext("2d")
    • The context to draw to
    • If no context is provided, the function will return a canvas with the text drawn on it
  • location*
    • location: [512, 512]
    • Location to draw the text
  • colour
    • colour: "red"
    • The colour of the text
  • fontSize
    • fontSize: 50
    • The size of the text
  • fontFamily
    • fontFamily: "Arial"
    • The font family of the text
  • width
    • width: 512
    • The maximum width of the text area
    • The font size will shrink until the text fits within the width
  • height
    • height: 512
    • The maximum height of the text area
    • The font size will shrink until the text fits within height
    • Requires a width and word wrapping enabled
  • align
    • align: "left"
    • How to align the text
    • left, center, right
  • baseline*
    • baseline: "top"
    • The text baseline
    • Requires word wrapping disabled
    • top, middle, bottom
  • gravity*
    • gravity: "ne"
    • What corner/side to draw the output text image from
    • Requires word wrapping enabled
    • n, ne, e, se, s, sw, w, nw, c
  • wrap
    • wrap: true
    • Enable word wrapping
    • Requires a width
  • spacing
    • spacing: 100
    • The size of the vertical spacing between the lines
  • shadowColour
    • shadowColour: "#000"
    • The colour of the text shadow
  • shadowBlur
    • shadowBlur: 5
    • The blur amount of the text shadow
  • shadowOffset
    • shadowOffset: [10, 10]
    • The offset amount of the text shadow
  • bold
    • bold: true
    • Set the text to be bold
  • padding
    • padding: [5, 10, 15, 20]
    • The amount of padding added to each side
    • The array is in the order [top, right, bottom, left]
    • Requires no context, or a context with word wrapping enabled

Return value

When a ctx is provided, an object is returned with the follow properties:

  • object.textWidth
    • The width of the text in the output canvas
  • object.textHeight
    • The height of the text in the output canvas

When no ctx is provided, a canvas is returned with the following custom properties set:

  • canvas.textWidth
    • The width of the text in the output canvas
  • canvas.textHeight
    • The height of the text in the output canvas
  • canvas.padding
    • The size of the padding on each side of the text, in the order [top, right, bottom, left]
      • Padding comes from the initial padding argument, plus any extra padding added by text shadows

Examples

Drawing text and saving to a file

const text = await drawText("Hello there!")

text.saveAs("out.png")
const text = await drawText("This is an example", {
  fontSize: 50,
  colour: "#f0f"
})

text.saveAs("out.png")
const text = await drawText("omg flushed emoji and weary emoji are kissing\n\n", {
  colour: "red",
  fontSize: 50,
  fontFamily: "Arial",
  width: 512,
  height: 512,
  align: "center",
  wrap: true,
  spacing: 20,
  shadowColour: "#000",
  shadowBlur: 5,
  shadowOffset: [10, 10],
  bold: true
})

text.saveAs("out.png")

Drawing to an existing canvas

const canvas = new Canvas(1024, 1024)
const ctx = canvas.getContext("2d")
ctx.fillStyle = "white"
ctx.fillRect(0, 0, 1024, 1024)
ctx.fillStyle = "orange"
ctx.fillRect(500, 500, 24, 24)

const text = await drawText("This is a test\nwith multiple lines of text\nto show gravity and stuff.", {
  ctx,
  location: [512, 512],
  colour: "red",
  fontSize: 50,
  fontFamily: "Arial",
  width: 512,
  height: 512,
  align: "left",
  gravity: "e",
  wrap: true,
  shadowColour: "#000",
  shadowBlur: 5,
  shadowOffset: [20, 20],
  bold: true
})

text.saveAs("out.png")