koishi-plugin-canvas
Advanced tools
Comparing version 0.1.3 to 0.2.0
{ | ||
"name": "koishi-plugin-canvas", | ||
"version": "0.1.3", | ||
"description": "Provide `ctx.canvas` using skia-canvas for Koishi.js", | ||
"author": "i'DLisT <me@idl.ist> (https://idl.ist)", | ||
"description": "canvas service for Koishi", | ||
"version": "0.2.0", | ||
"license": "MIT", | ||
"keywords": [ | ||
"koishi", | ||
"koishi.js" | ||
"main": "lib/index.js", | ||
"author": "Anillc <void@anil.lc>", | ||
"files": [ | ||
"lib" | ||
], | ||
"main": "dist/index.js", | ||
"homepage": "https://github.com/idlist/koishi-plugin-canvas#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/idlist/koishi-plugin-canvas.git" | ||
"koishi": { | ||
"service": { | ||
"optional": [ | ||
"nix" | ||
], | ||
"implements": [ | ||
"canvas" | ||
] | ||
} | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/idlist/koishi-plugin-canvas/issues" | ||
"peerDependencies": { | ||
"koishi": "^4.15.0", | ||
"koishi-plugin-downloads": "^0.1.1", | ||
"koishi-plugin-nix": "^0.0.3" | ||
}, | ||
"files": [ | ||
"dist", | ||
"LICENSE" | ||
], | ||
"scripts": { | ||
"build": "tsc && node build.js" | ||
}, | ||
"dependencies": { | ||
"skia-canvas": "^0.9.0" | ||
"@koishijs/canvas": "^0.2.0", | ||
"detect-libc": "^2.0.2", | ||
"get-registry": "^1.1.0", | ||
"glob": "^8.0.3", | ||
"simple-get": "^4.0.1", | ||
"string-split-by": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^16.0.0", | ||
"@typescript-eslint/eslint-plugin": "^5.0.0", | ||
"@typescript-eslint/parser": "^5.0.0", | ||
"esbuild": "^0.14.0", | ||
"eslint": "^8.0.0", | ||
"koishi": "^4.0.0", | ||
"typescript": "^4.0.0" | ||
"@types/node": "^20.8.2", | ||
"typescript": "^5.2.2" | ||
} | ||
} |
123
README.md
# koishi-plugin-canvas | ||
使用 [skia-canvas](https://github.com/samizdatco/skia-canvas) 提供的简陋的 `ctx.canvas` 接口,主要是我自己用着方便(虽然其实并没有多方便)。 | ||
## build and publish | ||
## 为什么? | ||
Enter development environment | ||
这个服务主要是为了解决 **字体注册** 的问题。为了使用本地字体,需要使用 `registerFont` (`node-canvas`) 或者 `Fontlibrary.use` (`skia-canvas`) 注册字体,但是这么做有个问题:同一个字体在不同的插件中会需要以不同的名字重复注册,因为不知道其他插件是否注册过这个字体。同名重复注册会导致报错,而且就算 `try catch` 了报错,也不知道其他插件用这个名字注册的字体是不是就是想用的字体。 | ||
将 `canvas` 独立成服务能一定程度上解决这个问题,因为可以在 `canvas` 服务的配置项中统一注册字体,然后在其他需要用到字体的插件中将字体暴露为配置项,进行统一配置。 | ||
## 安装方法 | ||
```shell | ||
npm i koishi-plugin-canvas | ||
```bash | ||
nix develop | ||
``` | ||
然后在配置文件或入口文件中将插件添加至你的机器人中。 | ||
### nereid | ||
## 插件配置项 | ||
这个插件提供了以下配置项: | ||
| 配置项 | 默认值 | 说明 | | ||
| - | - | - | | ||
| `fonts` | `[]` **\*1** | 注册字体 | | ||
**\*1** 这个列表的每个元素的类型为 | ||
```ts | ||
interface { | ||
path: string | ||
family: string | ||
weight?: string | ||
style?: string | ||
} | ||
```bash | ||
$ build-nereid | ||
$ pub-nereid | ||
``` | ||
- `path`: 字体对于工作路径的相对路径 | ||
### plugin | ||
- `family`: 字体注册成的名字 | ||
- `weight`(可选): 字体注册成的字重,如 700 或者 bold | ||
- `style`(可选):字体注册成的样式,如斜体、花体等 | ||
**注:** 实际上,`skia-canvas` 注册字体的时候是 **无法设置** `weight` 和 `style` 的,所以不填也没关系。我也不知道为什么要留这两个选项在这里。 | ||
成功注册字体时,控制台会提示字体注册成功,以及字体的名字、字重和样式。 | ||
## 服务拓展方法 | ||
以下方法通过 `ctx.canvas` 暴露给插件: | ||
#### `canvas.createCanvas(width?: number, height?: number)` | ||
- `width`(可选): 画布的宽度 | ||
- `height`(可选): 画布的高度 | ||
- 返回值: `Canvas` | ||
新建一个 `Canvas` 对象并返回它。下面是一个一般的范例,因为 `canvas` 的 `ctx` 和 `koishi` 的 `ctx` 重名,你需要在代码中给其中一个赋予不同的名字。 | ||
```js | ||
const canvas = ctx.canvas.createCanvas // 这里的 ctx 是 koishi 的 ctx | ||
const canvasCtx = canvas.getContext('2d') | ||
```bash | ||
$ copy | ||
$ yarn tsc | ||
$ yarn publish | ||
``` | ||
`Canvas` 的用法请参照 MDN。 | ||
#### `canvas.registerFont(path: string, options: FontOptions)` | ||
- `path`: 字体对于工作路径的相对路径 | ||
- `options`: 字体详情,各属性的意义和 **插件配置项** 中的意义一致。 | ||
```ts | ||
interface FontOptions { | ||
family: string | ||
weight?: string | ||
style?: string | ||
} | ||
``` | ||
注册本地字体。配置项中的 `fonts` 也是通过这个方法进行注册。 | ||
#### `canvas.loadImage(url: string)` | ||
加载图片,详情见 [这里](https://github.com/samizdatco/skia-canvas#loadimage) 。可以快速加载网络图片或者本地图片为 `Canvas` 可用的对象。 | ||
## `Canvas` 拓展方法 | ||
以下方法扩展由 `ctx.createCanvas` 所创建的 `Canvas` 对象,并非标准方法,基本是我自己用起来方便所以整出来的。 | ||
#### `Canvas.renderResize(factor: number)` | ||
- `factor`: 倍率 | ||
- 返回值:`Canvas` | ||
将当前 `Canvas` 按一定倍率(`factor`)缩放渲染,然后返回选然后的新的 `Canvas`。这个方法的意义在于通过在创建的时候按 N 倍于设计大小作图,然后输出时缩放回设计大小,达成不同平台之间渲染结果在一定程度上的一致。 | ||
#### `Canvas.toBase64()` | ||
- 返回值:`string` 被 Base64 编码的图片 | ||
将图片转化为 Base64 编码以供发送。 | ||
## 最终手段 | ||
这个插件也重新导出了 `skia-canvas` 本身。在有需要的情况下,可以直接从插件中获取原始方法。反正一时半会这玩意成不了规范,先随便写了。 | ||
```js | ||
const { FontLibrary } = require('koishi-plugin-canvas') | ||
``` | ||
## Q&A | ||
- 这玩意怎么实现得这么粗糙? | ||
因为就是这么粗糙。 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
105696
2
16
2481
9
1
2
25
1
3
4
+ Added@koishijs/canvas@^0.2.0
+ Addeddetect-libc@^2.0.2
+ Addedget-registry@^1.1.0
+ Addedglob@^8.0.3
+ Addedsimple-get@^4.0.1
+ Addedstring-split-by@^1.0.0
+ Added@cordisjs/core@3.18.1(transitive)
+ Added@cordisjs/loader@0.13.1(transitive)
+ Added@cordisjs/logger@0.3.3(transitive)
+ Added@cordisjs/plugin-http@0.6.3(transitive)
+ Added@cordisjs/plugin-proxy-agent@0.3.3(transitive)
+ Added@cordisjs/plugin-server@0.2.4(transitive)
+ Added@cordisjs/schema@0.1.1(transitive)
+ Added@cordisjs/timer@0.3.2(transitive)
+ Added@hapi/bourne@3.0.0(transitive)
+ Added@koa/router@10.1.1(transitive)
+ Added@koishijs/canvas@0.2.0(transitive)
+ Added@koishijs/core@4.18.2(transitive)
+ Added@koishijs/i18n-utils@1.0.1(transitive)
+ Added@koishijs/loader@4.6.2(transitive)
+ Added@koishijs/plugin-http@0.6.3(transitive)
+ Added@koishijs/plugin-proxy-agent@0.3.3(transitive)
+ Added@koishijs/plugin-server@3.2.4(transitive)
+ Added@koishijs/utils@7.2.1(transitive)
+ Added@satorijs/core@4.2.12(transitive)
+ Added@satorijs/element@3.1.7(transitive)
+ Added@satorijs/protocol@1.4.2(transitive)
+ Added@tokenizer/token@0.3.0(transitive)
+ Added@types/accepts@1.3.7(transitive)
+ Added@types/body-parser@1.19.5(transitive)
+ Added@types/co-body@6.1.3(transitive)
+ Added@types/connect@3.4.38(transitive)
+ Added@types/content-disposition@0.5.8(transitive)
+ Added@types/cookies@0.9.0(transitive)
+ Added@types/express@5.0.0(transitive)
+ Added@types/express-serve-static-core@5.0.2(transitive)
+ Added@types/formidable@2.0.6(transitive)
+ Added@types/http-assert@1.5.6(transitive)
+ Added@types/http-errors@2.0.4(transitive)
+ Added@types/keygrip@1.0.6(transitive)
+ Added@types/koa@2.15.0(transitive)
+ Added@types/koa-compose@3.2.8(transitive)
+ Added@types/koa__router@12.0.4(transitive)
+ Added@types/mime@1.3.5(transitive)
+ Added@types/node@22.10.1(transitive)
+ Added@types/qs@6.9.17(transitive)
+ Added@types/range-parser@1.2.7(transitive)
+ Added@types/send@0.17.4(transitive)
+ Added@types/serve-static@1.15.7(transitive)
+ Added@types/ws@8.5.13(transitive)
+ Addedaccepts@1.3.8(transitive)
+ Addedagent-base@7.1.1(transitive)
+ Addedargparse@2.0.1(transitive)
+ Addedasap@2.0.6(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaxios@1.7.8(transitive)
+ Addedb4a@1.6.7(transitive)
+ Addedbare-events@2.5.0(transitive)
+ Addedbytes@3.1.2(transitive)
+ Addedcac@6.7.14(transitive)
+ Addedcache-content-type@1.0.1(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedco@4.6.0(transitive)
+ Addedco-body@6.2.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcontent-disposition@0.5.4(transitive)
+ Addedcontent-type@1.0.5(transitive)
+ Addedcookies@0.9.1(transitive)
+ Addedcordis@3.18.1(transitive)
+ Addedcosmokit@1.6.3(transitive)
+ Addeddeep-equal@1.0.1(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-lazy-prop@2.0.0(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addeddepd@1.1.22.0.0(transitive)
+ Addeddestroy@1.2.0(transitive)
+ Addeddezalgo@1.0.4(transitive)
+ Addeddotenv@16.4.5(transitive)
+ Addedee-first@1.1.1(transitive)
+ Addedencodeurl@1.0.2(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedescape-html@1.0.3(transitive)
+ Addedfast-fifo@1.3.2(transitive)
+ Addedfastest-levenshtein@1.0.16(transitive)
+ Addedfile-type@16.5.4(transitive)
+ Addedfollow-redirects@1.15.9(transitive)
+ Addedform-data@4.0.1(transitive)
+ Addedformidable@2.1.2(transitive)
+ Addedfresh@0.5.2(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedget-registry@1.1.0(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhexoid@1.0.0(transitive)
+ Addedhttp-assert@1.5.0(transitive)
+ Addedhttp-errors@1.8.12.0.0(transitive)
+ Addedhttp-proxy-agent@7.0.2(transitive)
+ Addedhttps-proxy-agent@7.0.5(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedinaba@1.1.1(transitive)
+ Addedinflation@2.1.0(transitive)
+ Addedip-address@9.0.5(transitive)
+ Addedis-docker@2.2.1(transitive)
+ Addedis-generator-function@1.0.10(transitive)
+ Addedis-wsl@2.2.0(transitive)
+ Addedisexe@3.1.1(transitive)
+ Addedjs-yaml@4.1.0(transitive)
+ Addedjsbn@1.1.0(transitive)
+ Addedkeygrip@1.1.0(transitive)
+ Addedkleur@4.1.5(transitive)
+ Addedkoa@2.15.3(transitive)
+ Addedkoa-body@6.0.1(transitive)
+ Addedkoa-compose@4.2.0(transitive)
+ Addedkoa-convert@2.0.0(transitive)
+ Addedkoishi@4.18.2(transitive)
+ Addedkoishi-plugin-downloads@0.1.3(transitive)
+ Addedkoishi-plugin-nix@0.0.3(transitive)
+ Addedmedia-typer@0.3.0(transitive)
+ Addedmethods@1.1.2(transitive)
+ Addedmime-db@1.52.01.53.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedminato@3.6.1(transitive)
+ Addednegotiator@0.6.3(transitive)
+ Addednereid@0.0.5(transitive)
+ Addedns-require@1.1.4(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedon-finished@2.4.1(transitive)
+ Addedonly@0.0.2(transitive)
+ Addedopen@8.4.2(transitive)
+ Addedparseurl@1.3.3(transitive)
+ Addedpath-to-regexp@6.3.0(transitive)
+ Addedpeek-readable@4.1.0(transitive)
+ Addedproxy-from-env@1.1.0(transitive)
+ Addedqs@6.13.1(transitive)
+ Addedqueue-tick@1.0.1(transitive)
+ Addedraw-body@2.5.2(transitive)
+ Addedreadable-web-to-node-stream@3.0.2(transitive)
+ Addedreggol@1.7.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedschemastery@3.14.7(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedside-channel@1.0.6(transitive)
+ Addedsmart-buffer@4.2.0(transitive)
+ Addedsocks@2.8.3(transitive)
+ Addedsocks-proxy-agent@8.0.4(transitive)
+ Addedsprintf-js@1.1.3(transitive)
+ Addedstatuses@1.5.02.0.1(transitive)
+ Addedstreamx@2.20.2(transitive)
+ Addedstrtok3@6.3.0(transitive)
+ Addedsupports-color@8.1.1(transitive)
+ Addedtar-stream@3.1.7(transitive)
+ Addedtext-decoder@1.2.1(transitive)
+ Addedthrottle-debounce@5.0.2(transitive)
+ Addedtoidentifier@1.0.1(transitive)
+ Addedtoken-types@4.2.1(transitive)
+ Addedtsscmp@1.0.6(transitive)
+ Addedtype-is@1.6.18(transitive)
+ Addedundici@6.21.0(transitive)
+ Addedundici-types@6.20.0(transitive)
+ Addedunpipe@1.0.0(transitive)
+ Addedvary@1.1.2(transitive)
+ Addedwhich@4.0.0(transitive)
+ Addedwhich-pm-runs@1.1.0(transitive)
+ Addedws@8.18.0(transitive)
+ Addedylru@1.4.0(transitive)
+ Addedzod@3.23.8(transitive)
- Removedskia-canvas@^0.9.0
- Removed@mapbox/node-pre-gyp@1.0.11(transitive)
- Removedabbrev@1.1.1(transitive)
- Removedagent-base@6.0.2(transitive)
- Removedansi-regex@5.0.1(transitive)
- Removedaproba@2.0.0(transitive)
- Removedare-we-there-yet@2.0.0(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedcargo-cp-artifact@0.1.9(transitive)
- Removedchownr@2.0.0(transitive)
- Removedcolor-support@1.1.3(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedconsole-control-strings@1.1.0(transitive)
- Removedemoji-regex@8.0.0(transitive)
- Removedfs-minipass@2.1.0(transitive)
- Removedgauge@3.0.2(transitive)
- Removedglob@7.2.3(transitive)
- Removedhas-unicode@2.0.1(transitive)
- Removedhttps-proxy-agent@5.0.1(transitive)
- Removedis-fullwidth-code-point@3.0.0(transitive)
- Removedmake-dir@3.1.0(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedminipass@3.3.65.0.0(transitive)
- Removedminizlib@2.1.2(transitive)
- Removedmkdirp@1.0.4(transitive)
- Removednode-fetch@2.7.0(transitive)
- Removednopt@5.0.0(transitive)
- Removednpmlog@5.0.1(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedpath-browserify@1.0.1(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedrimraf@3.0.2(transitive)
- Removedsemver@6.3.17.6.3(transitive)
- Removedset-blocking@2.0.0(transitive)
- Removedsignal-exit@3.0.7(transitive)
- Removedskia-canvas@0.9.30(transitive)
- Removedstring-width@4.2.3(transitive)
- Removedstrip-ansi@6.0.1(transitive)
- Removedtar@6.2.1(transitive)
- Removedtr46@0.0.3(transitive)
- Removedwebidl-conversions@3.0.1(transitive)
- Removedwhatwg-url@5.0.0(transitive)
- Removedwide-align@1.1.5(transitive)
- Removedyallist@4.0.0(transitive)