{"id":13672889,"url":"https://github.com/huruji/canvas-string-ellipsis","last_synced_at":"2026-03-15T20:13:01.599Z","repository":{"id":40289462,"uuid":"241555563","full_name":"huruji/canvas-string-ellipsis","owner":"huruji","description":"通过 canvas 测量文字长度，超过长度则使用省略号","archived":false,"fork":false,"pushed_at":"2023-03-02T17:49:01.000Z","size":1790,"stargazers_count":6,"open_issues_count":5,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T14:01:34.383Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/huruji.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-02-19T07:06:12.000Z","updated_at":"2023-02-10T20:03:14.000Z","dependencies_parsed_at":"2023-08-25T00:17:00.612Z","dependency_job_id":null,"html_url":"https://github.com/huruji/canvas-string-ellipsis","commit_stats":null,"previous_names":["huruji/text-ellipsis"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huruji%2Fcanvas-string-ellipsis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huruji%2Fcanvas-string-ellipsis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huruji%2Fcanvas-string-ellipsis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huruji%2Fcanvas-string-ellipsis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/huruji","download_url":"https://codeload.github.com/huruji/canvas-string-ellipsis/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248251058,"owners_count":21072685,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T09:01:55.086Z","updated_at":"2026-03-15T20:12:56.565Z","avatar_url":"https://github.com/huruji.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# canvas-string-ellipsis\n\n通过 canvas 测量文字长度，超过长度则使用省略号\n\n\u003e 有时候我们不得不手动根据长度测量截取字符串，那么这个时候这个就很有用\n\n## How to use\n\n``` bash\nnpm i canvas-string-ellipsis -S\n```\n\n``` js\nimport cse from 'canvas-string-ellipsis';\nconst str = 'canvas-string-ellipisis';\n\nconst [res, sliced, width] = cse(str, 100)\n// res: canvas-string-ellipisis\n// sliced: false\n// width: 96\n\nconst [res2, sliced2, width2] = cse(str, 50)\n// res: canvas-stri...\n// sliced: true\n// width: 50\n```\n\n## API 参数\n\n``` typescript\nfunction cse(str: string, maxWidth: number, font?: string): [string, boolean, number]\n```\n\n### str\n\n原始字符串\n\n### maxWidth\n\n长度\n\n### font\n\ncanvas font 设置，默认为 `14px sans-serif` \n\n### 返回值\n\n返回值有三个组成一个数组，分别为截取后的字符串，是否截取，真实宽度\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhuruji%2Fcanvas-string-ellipsis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhuruji%2Fcanvas-string-ellipsis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhuruji%2Fcanvas-string-ellipsis/lists"}