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

@xg4/text2image

v1.0.0

Published

convert text to image by canvas

Downloads

2,954

Readme

text2image

Build Status npm npm

convert text to image by canvas

Installation

Install with npm or Yarn

# npm
$ npm install @xg4/text2image --save
# yarn
$ yarn add @xg4/text2image

Usage

import Text2Image from '@xg4/text2image'

const ti = new Text2Image()
// or
// initialization default options
const ti = new Text2Image({
  fontSize: 13,
  color: '#000000',
  fontFamily: 'arial',
  fontWeight: 'bold',
  type: 'image/png',
  quality: 0.92,
})
// get mask image
Text2Image.createMask(imgUrl).then((image) => {
  // set background image
  ti.setMask(image)
})

// create object url
const url = ti.createURL('hello world')
// or
const url = ti.createURL({
  text: 'hello world',
  // some options
})

const img = new Image()
// img loaded, remenber to destroy object url
img.onload = function () {
  ti.destroyURL(this.src)
}
img.src = url

document.body.appendChild(img)
// get mask image
Text2Image.createMask(imgUrl).then((image) => {
  // set background image
  ti.setMask(image)
})

// create data url
const url = ti.toDataURL('hello world')
// or
const url = ti.toDataURL({
  text: 'hello world',
  // some options
})

const img = new Image()
img.src = url

document.body.appendChild(img)

Example

https://xg4.github.io/text2image

API

Constructor Options

options

| name | type | default | description | | ------------ | ---------------- | ----------- | ---------------------------- | | text | string | null | image content | | fontSize | number\|string | 30 | font size(like css) | | fontWeight | number\|string | normal | font weight(like css) | | fontFamily | string | arial | font family(like css) | | color | string | #000000 | font color(like css) | | type | string | image/png | image type | | quality | number | 0.92 | image quality | | alpha | number | 0.3 | mask alpha(水印图片的透明度) |

// use current options convert default options
ti.setDefaultOptions({
  // some options
})

// reset default options
ti.resetDefaultOptions()

Contributing

Welcome

  • Fork it

  • Submit pull request

Polyfills needed to support older browsers

HTMLCanvasElement.prototype.toBlob: see MDN for details about unsupported older browsers and a simple polyfill.

;(function () {
  if (!HTMLCanvasElement.prototype.toBlob) {
    Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {
      value: function (callback, type, quality) {
        var binStr = atob(this.toDataURL(type, quality).split(',')[1]),
          len = binStr.length,
          arr = new Uint8Array(len)

        for (var i = 0; i < len; i++) {
          arr[i] = binStr.charCodeAt(i)
        }

        callback(new Blob([arr], { type: type || 'image/png' }))
      },
    })
  }
})()

Browsers support

Modern browsers and IE10.

| IE / Edge | Firefox | Chrome | Safari | Opera | | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | IE10, Edge | last 2 versions | last 2 versions | last 2 versions | last 2 versions |

LICENSE

MIT