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

wx-ctxtext

v1.0.2

Published

小程序canvas文本绘制

Downloads

1

Readme

微信小程序canvas绘制可折行的文本

经常有这种绘制小程序海报,然后还能保存到本地分享的需求
Alt text

图片用canvas画上去,css把文字写一下就ok了~~,一通操作猛如虎,写完之后二百五(😭),好像文本没有绘制出来233。。(原来文本也得canvas绘制),文本单行还好说,多行折行的问题,总不能自己一个字一个字的试吧。


实现原理

将段落内容逐字切割,使用小程序measureText接口返回的文本宽度把每个字符宽度不断累加,计算每一段内容是否超出设置既定宽度,超出则另起一行,直到遍历完成所有文本内容。

使用方式

  • wepy,mpvue,Taro这种三方的辅助开发框架直接 npm i wx-ctxtext 引入即可

  • 原生小程序开发方式比较麻烦

    1. npm init 初始化一个npm清单
    2. npm i wx-ctxtext 安装
    3. 小程序开发工具 -> 工具 -> 构建npm
    4. 使用

    Alt text
    (因为项目用的三方辅助开发,只在本地跑了一个原生小程序测试了一下功能正常,不确定是否部署线上代码能否生效。。)

示例代码

import DrawTextWrap from 'wx-ctxtext';
const config = {
  ctx: ctx,
  startX: 30,
  startY: 30,
  maxWidth: 300,
  content: {
    fontsize: 16,
    baseline: 'top',
    align: 'left',
    lineHeight: 20,
    color: '#988',
    text: '五年多以来,在各方共同努力下,“六廊六路多国多港”的互联互通架构基本形成,一大批合作项目落地生根,首届高峰论坛的各项成果顺利落实,150多个国家和国际组织同中国签署共建“一带一路”合作协议。'
  }
}
new DrawTextWrap(config).draw()

效果如下
Alt text

配置项

所有单位默认以750px设计稿来设置的

| 配置项 | 描述 | | ---------------- | --------------------------------------------------------------------------------------------------------------------- | | ctx | 小程序获取到的canvas画布 即:wx.createCanvasContext("renderImg") | | [,screen] | 设计稿宽度 默认是2倍为750px的,非必填 | | startX | 基于canvas画布的x轴坐标开始绘制 | | startY | 基于canvas画布的y轴坐标开始绘制 | | maxWidth | 到此宽度开始折行 | | content | 文本设置样式,初始值(这些值非必填项) fontsize = 16, color = '#333', lineHeight = 22, baseline = 'top', bold = false, align = 'left' | | content.fontsize | 文字大小 | | content.baseline | 文字竖直对齐方式,参见文档 | | content.bold | 是否加粗 | | content.align | 文字对齐 | | content.maxLine | 最多显示n行字符,默认不控制 | | content.color | 文本颜色 | | content.text | 绘制的内容 |