gitmyhub

Painter

JavaScript ★ 4.5k updated 2y ago

小程序生成图片库,轻松通过 json 方式绘制一张可以发到朋友圈的图片

Painter is a JavaScript library for WeChat Mini Programs that generates images from a JSON description, letting you compose text, images, QR codes, and shapes with CSS-like styling without touching the raw Canvas API.

JavaScriptCanvasWeChat Mini ProgrammpvueTarosetup: moderatecomplexity 3/5

Painter is a JavaScript library for WeChat Mini Programs that lets developers generate images by writing a JSON description of what the image should look like. Instead of calling the low-level Canvas drawing API directly, which the README describes as difficult and full of quirks specific to the Mini Program environment, you describe your image as a structured data object and pass it to Painter, which handles the actual drawing.

The idea is that you build a "palette" in JSON: you set a background color or image, a width and height, and then list the elements you want drawn inside it. Those elements can be text, images, rectangles, or QR codes. Each element gets CSS-like properties for things like positioning, alignment, rotation, rounded corners, borders, gradients, shadows, and custom fonts. Painter reads that palette and produces a finished image file.

There is also a dynamic editing mode where users of your Mini Program can drag and resize elements on screen before the final image is saved. The static mode generates images silently without any visible editing interface, which is useful for programmatic image creation in the background.

On the performance side, the library caches remote images using an LRU mechanism so the same image asset does not get downloaded more than once. It also includes a check after drawing completes to detect corrupted or incomplete output, and will redraw automatically if something went wrong.

The core drawing code lives in a separate repository that is pulled in as a Git submodule. The README, written in Chinese, covers the component's full attribute list, the palette and view JSON schema in detail, and integration instructions for the mpvue and Taro frameworks. The library supports the newer canvas2d API as an opt-in, and base64 image input is listed as available in testing. A few features, including a node.js server-side version and triangle shape support, are noted as not yet complete.

Where it fits