Search K
Appearance
👍欢迎大家积极投稿交流👍
如果文档内容陈旧或者链接失效,请发现后及时同步,我将尽快修改
👇微信👇

Appearance
LeaferJS 是一款好用的 Canvas 引擎,革新的开发体验,可用于高效绘图 、UI 交互(小游戏、互动应用、组态)、图形编辑。
提供了丰富的 UI 绘图元素,和开箱即用的功能,如自动布局、图形编辑、SVG 导出等,方便与 PS、 Figma、Sketch 等产品进行对接。并为跨平台开发提供了统一的交互事件,如拖拽、旋转、缩放手势等。

LeaferJS 是一个好用的 Canvas 引擎,专注于提供高性能、易用的 2D 图形绘制能力。它基于 Canvas API 构建,为开发者提供了更简洁、更强大的图形绘制接口,适用于创建各种交互式图形应用、游戏、数据可视化等场景。
# 安装核心包
npm install leafer-ui
# 或者使用 yarn
yarn add leafer-ui<!-- 引入 LeaferJS -->
<script src="https://unpkg.com/leafer-ui@latest/dist/leafer.min.js"></script>import { Leafer, Rect } from 'leafer-ui'
// 创建一个自适应窗口的交互应用
const leafer = new Leafer({ view: window })
// 创建一个可以被拖拽的矩形
const rect = new Rect({
x: 100,
y: 100,
width: 200,
height: 200,
fill: '#32cd79',
draggable: true
})
leafer.add(rect)创建 Leafer 实例时,可以配置以下选项:
| 选项 | 类型 | 描述 | 默认值 |
|---|---|---|---|
| view | HTMLElement Window | 渲染容器 | 无(必填) |
| width | number | 画布宽度 | 容器宽度 |
| height | number | 画布高度 | 容器高度 |
| fill | string | 背景颜色 | 透明 |
| interactive | boolean | 是否开启交互 | true |
| resize | boolean | 是否自动调整大小 | true |
LeaferJS 提供了多种内置图形元素:
所有图形元素都支持以下基本属性:
LeaferJS 提供了丰富的事件系统,支持以下事件:
// 示例:添加点击事件
rect.on('click', function(e) {
console.log('矩形被点击了', e)
// 改变矩形颜色
this.fill = '#ff6b6b'
})LeaferJS 支持图层管理,可以创建和组织多个图层:
import { Leafer, Layer, Rect, Circle } from 'leafer-ui'
const leafer = new Leafer({ view: window })
// 创建图层
const layer1 = new Layer()
const layer2 = new Layer()
// 添加图形到不同图层
const rect = new Rect({ x: 100, y: 100, width: 100, height: 100, fill: '#32cd79' })
const circle = new Circle({ x: 150, y: 150, radius: 50, fill: '#ff6b6b' })
layer1.add(rect)
layer2.add(circle)
// 将图层添加到 Leafer 实例
leafer.add(layer1)
leafer.add(layer2)LeaferJS 内置了动画系统,支持属性动画:
import { Leafer, Rect, Animate } from 'leafer-ui'
const leafer = new Leafer({ view: window })
const rect = new Rect({
x: 100,
y: 100,
width: 100,
height: 100,
fill: '#32cd79'
})
leafer.add(rect)
// 创建动画
Animate.to(rect, {
duration: 1000, // 动画持续时间(毫秒)
x: 300, // 目标位置 x
y: 200, // 目标位置 y
rotate: 360, // 目标旋转角度
scale: 1.5, // 目标缩放比例
ease: 'easeOut' // 缓动函数
})Leafer 是 LeaferJS 的核心类,用于创建和管理画布:
const leafer = new Leafer(options)
// 方法
leafer.add(element) // 添加元素
leafer.remove(element) // 移除元素
leafer.clear() // 清空所有元素
leafer.destroy() // 销毁实例
// 属性
leafer.width // 画布宽度
leafer.height // 画布高度
leafer.interactive // 是否开启交互const rect = new Rect({
x: 0, // 位置 x
y: 0, // 位置 y
width: 100, // 宽度
height: 100, // 高度
fill: '#000', // 填充色
stroke: '#fff', // 描边色
strokeWidth: 2, // 描边宽度
draggable: true // 是否可拖拽
})const circle = new Circle({
x: 100, // 圆心位置 x
y: 100, // 圆心位置 y
radius: 50, // 半径
fill: '#000', // 填充色
stroke: '#fff', // 描边色
strokeWidth: 2, // 描边宽度
draggable: true // 是否可拖拽
})const text = new Text({
x: 100, // 位置 x
y: 100, // 位置 y
text: 'Hello', // 文本内容
fontSize: 20, // 字体大小
fontWeight: 'bold', // 字体粗细
fill: '#000', // 文本颜色
draggable: true // 是否可拖拽
})// 添加事件监听器
element.on('click', handler)
// 移除事件监听器
element.off('click', handler)
// 触发事件
element.emit('customEvent', data)import { Leafer, Rect, Circle, Text } from 'leafer-ui'
const leafer = new Leafer({ view: window, fill: '#f5f5f5' })
// 创建矩形
const rect = new Rect({
x: 100,
y: 100,
width: 150,
height: 150,
fill: '#32cd79',
draggable: true
})
// 创建圆形
const circle = new Circle({
x: 350,
y: 175,
radius: 75,
fill: '#ff6b6b',
draggable: true
})
// 创建文本
const text = new Text({
x: 150,
y: 270,
text: 'LeaferJS 示例',
fontSize: 24,
fontWeight: 'bold',
fill: '#333'
})
// 添加点击事件
rect.on('click', function() {
this.fill = '#4ecdc4'
})
circle.on('click', function() {
this.fill = '#45b7d1'
})
// 添加到画布
leafer.add(rect)
leafer.add(circle)
leafer.add(text)import { Leafer, Rect, Animate } from 'leafer-ui'
const leafer = new Leafer({ view: window, fill: '#f5f5f5' })
// 创建多个矩形
const colors = ['#32cd79', '#ff6b6b', '#45b7d1', '#f9ca24', '#6c5ce7']
const rects = []
for (let i = 0; i < 5; i++) {
const rect = new Rect({
x: 50 + i * 120,
y: 200,
width: 100,
height: 100,
fill: colors[i]
})
rects.push(rect)
leafer.add(rect)
}
// 为每个矩形添加动画
rects.forEach((rect, index) => {
Animate.to(rect, {
duration: 1000,
delay: index * 200,
y: 100,
scale: 1.2,
repeat: -1, // 无限重复
yoyo: true, // 往返动画
ease: 'easeInOut'
})
})import { Leafer, Rect } from 'leafer-ui'
// 创建响应式画布
const leafer = new Leafer({ view: window, fill: '#f5f5f5' })
// 创建一个始终居中的矩形
const rect = new Rect({
width: 200,
height: 200,
fill: '#32cd79',
draggable: true
})
leafer.add(rect)
// 更新矩形位置以保持居中
function updateCenter() {
rect.x = (leafer.width - rect.width) / 2
rect.y = (leafer.height - rect.height) / 2
}
// 初始居中
updateCenter()
// 监听窗口大小变化
window.addEventListener('resize', updateCenter)解决方案:
解决方案:
解决方案:
解决方案:
解决方案:
通过本文档的学习,你应该能够快速上手 LeaferJS 并创建各种交互式图形应用。如果在使用过程中遇到问题,建议参考官方文档或 GitHub 仓库寻求帮助。