Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ritz078/text-dimensions
Performant Calculation of the width and height of a text string using canvas.
https://github.com/ritz078/text-dimensions
Last synced: 16 days ago
JSON representation
Performant Calculation of the width and height of a text string using canvas.
- Host: GitHub
- URL: https://github.com/ritz078/text-dimensions
- Owner: ritz078
- License: mit
- Created: 2020-03-01T16:55:50.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-07-18T21:07:21.000Z (over 1 year ago)
- Last Synced: 2024-10-12T23:18:55.963Z (about 1 month ago)
- Language: TypeScript
- Size: 409 KB
- Stars: 23
- Watchers: 3
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# text-dimensions
Performant Calculation of the width and height of a text string using canvas (`OffscreenCanvas` if it is supported).
## Installation
```shell script
npm install text-dimensions
```
or```shell script
yarn add text-dimensions
```## Usage
```typescript
import TextDimensions from 'text-dimensions';const dimensions = new TextDimensions('bold 12pt arial');
const { height, width } = dimensions.measureText('text-dimensions');
console.log('Width of text', width);
console.log('height of text', height);// cleanup
dimensions.clean();
```## Methods
- `.measureText(text: string, font?: string)`: return an object having the height and width of the text string.
- `.clean()`: removes the canvas from the DOM if it was added.## License
MIT