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

text-canvas

v0.1.3-alpha

Published

Renders wrapped text to a 2D canvas element.

Downloads

960

Readme

TextCanvas

js-standard-style latest-release GitHub issues license

With TextCanvas you can render multi-line text to a 2D canvas that will automatically resize to the text dimensions. This can be useful if you need to create a texture to use on a WebGL canvas or even if you just need text to wrap text automatically on a 2D canvas.

Installation

Via Bower

bower install ext-date

Via NPM

npm install text-canvas

Usage

TextCanvas is an UMD module. You can load it into your application either by importing the module, or loading the script in your page.

If you are importing the TextCanvas module via Webpack, Browserify or similar, make sure that the module name text-canvas is being resolved correctly to the Bower or NPM packages folder.

Via ES6 syntax

import TextCanvas from 'text-canvas'

Via CommonJS syntax

var TextCanvas = require('text-canvas')

Via the script tag

<script src="/scripts/TextCanvas.js"></script> // Change the path as necessary

API

TextCanvas.prototype.constructor([String text[, Object style[, Number resolution]]])

Creates an instance of TextCanvas, which you will be able to modify and re-render at any point.

Parameters

| Name | Type | Required | Default | Description | |------------|----------|----------|-------------------------|---------------------------------------------------------| | text | String | false | '' | The text to be rendered on the canvas. | | style | Object | false | {} | The style to use when rendering the text. | | resolution | Number | false | window.devicePixelRatio | The resolution, or pixel density, of the rendered text. |

Returns

TextCanvas - The created instance.

Example

const textCanvas = new TextCanvas('Hello, World!', {fontFamily: 'Arial', wordWrap: 300}, 2)

Properties

TextCanvas.prototype.text = String

Get or set the text that will be rendered. To force line breaks, use the new line character \n.

Returns

String - The text currently set to be rendered.

Example

const text = textCanvas.text // Outputs the text that will be rendered

textCanvas.text = 'Hello, World!' // Sets the text that will be rendered

TextCanvas.prototype.style = Object

Get or set the text style. Setting the style is an additive operation. All the previously existing styles that are not being set will be kept.

Properties

| Name | Type | Required | Default | Description | |--------------|--------------------|----------|----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | fontFamily | String | false | 'sans-serif' | The font family name. Unlike the CSS font-family property, fallback font families are not supported. | | fontStyle | String | false | 'normal' | The font style. The possible values are normal, italic or oblique. | | fontWeight | String/Number | false | 'normal' | The font weight. The possible values are normal, bold, or a multiple of 100 from 100 to 900. | | fontVariant | String | false | 'normal' | The font variant. The possible values are normal or small-caps. | | fontSize | Number | false | 16 | The font size in pixels. | | lineHeight | Number | false | fontSize * 1.2 | The text line height, in pixels. | | textAlign | String | false | 'left' | The alignment of the text. The possible values are left, center or right. | | textBaseline | String | false | 'bottom' | The baseline of the text. The possible values are top, hanging, middle, alphabetic, ideographic or bottom. Changing this value might create some clipping. | | textColor | String | false | 'black' | The text colour. It can be any CSS colour string supported by the browser. | | wordWrap | Boolean/Number | false | false | Set to false to draw all the text in a single line. Setting it to a number will define the maximum line width in pixels. |

Returns

Object - The full text style object.

Example

const style = textCanvas.style // Outputs the full text style object

textCanvas.style = {fontSize: 24, fontWeight: 'bold'} // Sets the text style options

TextCanvas.prototype.resolution = Number

Get or set the pixel density or resolution of the canvas. This will affect the dimensions of the canvas.

Returns

Number - The pixel density or resolution at which the text will be rendered.

Example

const resolution = textCanvas.resolution // Outputs the resolution of the canvas

textCanvas.resolution = 2 // Sets the resolution of the canvas

Methods

TextCanvas.prototype.render()

Renders the text to the canvas with the currently set styles and resolution. TextCanvas uses always the same canvas per instance. If you are using the canvas from a previously rendered text you need to take this into consideration because the dimensions and contents of the canvas will change.

Returns

HTMLCanvasElement - The canvas element in which the text was rendered.

Example

const canvas = textCanvas.render()
document.body.appendChild(canvas)

Brower support

TextCanvas, although not tested in all of them, was created using features supported by these browsers.

  • Android Browser 4+
  • Blackberry Browser 7+
  • Chrome 13+
  • Firefox 4+
  • Internet Explorer 9+
  • Opera 12+
  • Opera Mini 5+
  • Safari 7+

Support

If you want to request new features or find any bugs, please open a ticket on the issues page and I'll review it as soon as possible.

Authors and Contributors

Created by Luís Rodrigues (@MrSlide)

License and copyright

Released under the MIT license

Copyright (c) 2016 Luís Rodrigues