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

canvas-multiline

v1.0.0

Published

为canvas ctx扩展多行文本的绘制,支持对英文单词的宽度进行的解析

Downloads

5

Readme

说明:canvas-multiline支持绘制文本时,按指定的最大宽度自动换行,并能解析文本中的"\n"换行符。同时,canvas-multiline支持对英文单词的宽度进行解析。

使用

  • 浏览器端
<!DOCTYPE html>
<html>
<head>
    ...
</head>
<body>
    ...
    <script src="../dist/canvas-multiline.min.js"></script>
</body>
</html>
  • npm 模块开发,请先执行命令:npm install -S canvas-multiline
import 'canvas-multiline'
...

🌰例子

  • 1.普通的英文短句
let ctx = canvas.getContext("2d");
ctx.font = "18px Arial";
ctx.textBaseline = "top";
let text = `Her voice was so sweet as it broke the hypnotic trance we were all caught in, "Mom, let's run through the rain." she said.`
ctx.strokeRect(0, 0, 160, 500)
ctx.fillMultilineText(text, 0, 0, 160);
  • 2.存在"\n"换行符的英文短句
let ctx = canvas.getContext("2d");
ctx.font = "18px Arial";
ctx.textBaseline = "top";
//文本中存在"\n"换行符
let text = `Her \nvoice \nwas so sweet as it broke the hypnotic trance we were all caught in, "Mom, let's run through the rain." she said.`
ctx.strokeRect(0, 0, 160, 500)
ctx.fillMultilineText(text, 0, 0, 160);
  • 3.中文短句
let ctx = canvas.getContext("2d");
ctx.font = "18px Arial";
ctx.textBaseline = "top";
//中文
let text = `生命没有意义,我来到这个世界上,只为了再一次聆听那曾被判决的声音,也许对你们来说,没有任何的意义,但对我来说,就是生命中最大的意义。`
ctx.strokeRect(0, 0, 160, 500)
ctx.fillMultilineText(text, 0, 0, 160);

方法&参数

1. multilineText(type, text, x, y, maxWidth, verticalSpacing)

说明:此方法通过指定type填充或描边文本。

|参数|说明|默认值| |-|-|-| |type|可选值:'fill' | 'stroke',fill填充文本,stroke描边文本|-| |text|要绘制的文本|-| |x|文本区域左上角x坐标|-| |y|文本区域左上角y坐标|-| |maxWidth|文本区域的最大宽度,若文本超过最大宽度,会自动换行|-| |verticalSpacingh|文字竖直方向的间距|0|

2. fillMultilineText(text, x, y, maxWidth, verticalSpacing)

说明:此方法填充一个指定最大宽度的文本

|参数|说明|默认值| |-|-|-| |text|要绘制的文本|-| |x|文本区域左上角x坐标|-| |y|文本区域左上角y坐标|-| |maxWidth|文本区域的最大宽度,若文本超过最大宽度,会自动换行|-| |verticalSpacingh|文字竖直方向的间距|0|

3. strokeMultilineText(text, x, y, maxWidth, verticalSpacing)

说明:此方法描边一个指定最大宽度的文本

|参数|说明|默认值| |-|-|-| |text|要绘制的文本|-| |x|文本区域左上角x坐标|-| |y|文本区域左上角y坐标|-| |maxWidth|文本区域的最大宽度,若文本超过最大宽度,会自动换行|-| |verticalSpacingh|文字竖直方向的间距|0|