# canvas-string-ellipsis

> 通过 canvas 测量文字长度，超过长度则使用省略号

Latest version **1.0.1** (published 2020-02-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install canvas-string-ellipsis
pnpm add canvas-string-ellipsis
yarn add canvas-string-ellipsis
bun add canvas-string-ellipsis
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2020-02-20 |
| First published | 2020-02-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Maintainers | huruji |

## Links

- npm: https://www.npmjs.com/package/canvas-string-ellipsis
- Repository: https://github.com/huruji/text-ellipsis
- Homepage: https://github.com/huruji/text-ellipsis#readme
- Issues: https://github.com/huruji/text-ellipsis/issues
- npm.io page: https://npm.io/package/canvas-string-ellipsis

## Recent versions

- 1.0.1 (latest) — 2020-02-20

## README

# canvas-string-ellipsis

通过 canvas 测量文字长度，超过长度则使用省略号

> 有时候我们不得不手动根据长度测量截取字符串，那么这个时候这个就很有用

## How to use

``` bash
npm i canvas-string-ellipsis -S
```

``` js
import cse from 'canvas-string-ellipsis';
const str = 'canvas-string-ellipisis';

const [res, sliced, width] = cse(str, 100)
// res: canvas-string-ellipisis
// sliced: false
// width: 96

const [res2, sliced2, width2] = cse(str, 50)
// res: canvas-stri...
// sliced: true
// width: 50
```

## API 参数

``` typescript
function cse(str: string, maxWidth: number, font?: string): [string, boolean, number]
```

### str

原始字符串

### maxWidth

长度

### font

canvas font 设置，默认为 `14px sans-serif` 

### 返回值

返回值有三个组成一个数组，分别为截取后的字符串，是否截取，真实宽度

---
_Source: https://npm.io/package/canvas-string-ellipsis · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
