更新记录
1.0.0(2026-12-06)
- 首次发布:纯前端零原生依赖的全端 Canvas 组件。
- 图层:文字、图片、SVG、GIF、Sprite、平铺、遮罩条、二维码、圆角矩形、圆形、group、path。
- 变换:全局/图层旋转、翻转、透明度、裁剪、阴影、混合模式、渐变。
- SVG / GIF / Sprite:路径解析、首帧导出、序列帧播放控制。
- 导出:PNG / JPG、pixelRatio、maxSize、保存相册。
- 模板:poster / certificate / social-cover / quote 等一键切换。
平台兼容性
CanvasCraft 使用说明
插件 ID:
canvas-craft当前版本:1.0.0 适用框架:uni-app(Vue2 / Vue3) 支持平台:H5、微信 / 支付宝 / 抖音等小程序、App-vue 原生依赖:无
CanvasCraft 是纯前端 Canvas 组件。用声明式图层组合文字、图片、SVG、GIF、Sprite、二维码与形状,支持旋转、透明度、裁剪、渐变、动画与图片导出。
1. 安装
把 uni_modules/canvas-craft 复制到 uni-app 项目根目录。无需 npm,无 nativeplugins。
2. 快速开始
<template>
<canvas-craft
ref="cc"
:width="600"
:height="800"
:layers="layers"
background="#f4f7fb"
:preview-width="680"
@ready="onReady"
@error="onError"
/>
</template>
<script>
export default {
data() {
return {
layers: [
{ type: 'text', content: 'CanvasCraft', x: 40, y: 40, fontSize: 36, color: '#0f172a' },
{ type: 'rect', x: 40, y: 100, width: 300, height: 8, radius: 4, color: '#2563eb' }
]
}
},
methods: {
onReady(info) { console.log(info) },
onError(error) { console.error(error) }
}
}
</script>
3. 一键模板
import { listCanvasTemplates, createCanvasTemplate } from '@/uni_modules/canvas-craft'
listCanvasTemplates()
// poster / certificate / social-cover / quote / minimal / promo / knowledge / resume / sprite-showcase / layer-demo
组件内切换:
this.$refs.cc.setTemplate('poster', {
title: '品牌海报',
src: '/static/photo.png',
qr: 'https://example.com/canvas-craft'
})
this.$refs.cc.clearTemplate()
模板会自动切换画布尺寸与背景,也可以只取图层:
const tpl = createCanvasTemplate('minimal', { title: '极简画布' })
this.layers = tpl.layers
4. 图层类型
| 类型 | 能力 |
|---|---|
text |
多行、换行、截断、描边、阴影、背景条、字距、渐变、旋转 |
image |
缩放、圆角、圆形裁剪、object-fit、旋转、透明度 |
svg |
path / rect / circle / ellipse / line / polyline / polygon / g / text |
gif |
组件内原生 GIF 动画显示;导出首帧 |
sprite |
序列帧、fps、循环、播放 / 暂停 / seek |
qrcode |
真实可扫二维码 |
tile |
全屏平铺文字 |
strip |
顶部 / 底部文字遮罩条 |
rect |
圆角矩形 |
circle |
圆形 |
group |
嵌套图层分组、背景、变换 |
path |
SVG path 路径图形 |
文字
{
type: 'text',
content: '你好,CanvasCraft',
x: 40, y: 40,
fontSize: 30,
color: '#111827',
fontFamily: 'sans-serif',
stroke: '#ffffff',
strokeWidth: 2,
letterSpacing: 1,
rotate: -10
}
图片
{
type: 'image',
src: '/static/logo.png',
x: 40, y: 120,
width: 160, height: 160,
mode: 'aspectFill',
radius: 24
}
SVG
{
type: 'svg',
svg: '<svg viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" fill="#2563eb"/></svg>',
x: 240, y: 120,
width: 160, height: 160
}
GIF
{
type: 'gif',
src: '/static/sample.gif',
x: 40, y: 320,
width: 200, height: 140
}
Sprite
{
type: 'sprite',
src: '/static/sprite.png',
frameWidth: 100,
frameHeight: 100,
frameCount: 4,
fps: 8,
loop: true,
x: 40, y: 480,
width: 120, height: 120
}
group / path
{
type: 'group',
x: 40, y: 100,
width: 500, height: 200,
background: 'rgba(255,255,255,0.06)',
rotate: 3,
children: [
{ type: 'text', content: 'Group', x: 24, y: 24, fontSize: 28 },
{ type: 'rect', x: 24, y: 76, width: 200, height: 60, radius: 14, color: '#2563eb' }
]
}
{
type: 'path',
d: 'M50 4 L61 36 L95 36 L68 56 L78 90 L50 69 L22 90 L32 56 L5 36 L39 36 Z',
viewBox: '0 0 100 100',
x: 40, y: 480, width: 180, height: 180,
fill: '#f97316', stroke: '#ffffff', strokeWidth: 2
}
动画
{
type: 'text',
content: '淡入标题',
x: 40, y: 60, fontSize: 36,
animation: { property: 'opacity', from: 0, to: 1, duration: 800, loop: false, easing: 'easeOut' }
}
关键帧:
{
type: 'rect',
x: 40, y: 200, width: 120, height: 120, radius: 24, color: '#2563eb',
animation: {
duration: 1600,
loop: true,
easing: 'easeInOut',
keyframes: [
{ offset: 0, x: 40, rotate: 0, opacity: 1 },
{ offset: 0.5, x: 300, rotate: 90, opacity: 0.6 },
{ offset: 1, x: 40, rotate: 0, opacity: 1 }
]
}
}
animation 也可放在 group 上;group 子图层会继续递归执行自己的动画。
二维码 / 形状
{ type: 'qrcode', content: 'https://example.com', x: 480, y: 60, size: 100 }
{ type: 'rect', x: 40, y: 40, width: 300, height: 160, radius: 24, color: '#2563eb' }
{ type: 'circle', x: 360, y: 40, width: 100, height: 100, color: '#f97316' }
5. 组件事件
| 事件 | 参数 |
|---|---|
ready |
{ width, height, canvasWidth, canvasHeight } |
error |
Error |
frame |
[{ frame, frameCount }] |
export |
{ tempFilePath, destWidth, destHeight } |
save |
{ path } / { dataUrl } |
6. 组件方法
this.$refs.cc.play()
this.$refs.cc.pause()
this.$refs.cc.seek(2)
this.$refs.cc.exportCanvas()
this.$refs.cc.exportFrames({ duration: 3000, fps: 12 })
this.$refs.cc.toTempFilePath()
this.$refs.cc.toDataURL() // H5
this.$refs.cc.save()
this.$refs.cc.addLayer(layer)
this.$refs.cc.updateLayer(0, { rotate: 15 })
this.$refs.cc.removeLayer(0)
this.$refs.cc.redraw()
7. 直接使用 API
import { renderCanvas } from '@/uni_modules/canvas-craft'
const res = await renderCanvas({
canvasId: 'my-canvas',
component: this,
width: 600,
height: 800,
layers: [/* ... */],
fileType: 'png',
pixelRatio: 2
})
8. 平台注意事项
- 小程序使用
uni.createCanvasContext,微信建议基础库 2.11+。 - GIF 动画通过组件内
<image>原生显示;导出只包含当前 Canvas 首帧。 - 本地 SVG 文件路径不支持;请使用内联 SVG、data URI 或 HTTP 地址。
- 保存图片到相册需要用户授权。
- App-nvue 未适配,请使用 App-vue。
- 鸿蒙提供 UTS API,Vue 组件需按 uni-app x 规范适配。
- group 内嵌 GIF 仅绘制首帧,不参与组件
<image>动画叠加。
9. 常见问题
Q:SVG 显示空白?
A:检查 <svg> 是否带 viewBox;本地路径请改为内联或 data URI。
Q:GIF 不动?
A:组件用 <image> 显示 GIF;请确认平台支持 GIF,且 show=true。
Q:Sprite 只显示一帧?
A:设置 frameWidth、frameHeight、frameCount 或 columns/rows;组件会自动播放。
Q:导出图片模糊?
A:提高 pixelRatio(2–4)并设置合适的 width/height。
Q:保存失败?
A:检查相册权限;H5 使用下载链接保存。

收藏人数:
购买普通授权版(
试用
赞赏(0)
下载 0
赞赏 0
下载 12806065
赞赏 1961
赞赏
京公网安备:11010802035340号