Loading...
Loading...
Compare original and translation side by side
| 概念 | 说明 | 关键 API |
|---|---|---|
| worklet 函数 | 可运行在 JS 或 UI 线程的函数,顶部声明 | |
| 共享变量 | 跨线程同步的变量,通过 | |
| 动画驱动 | 将 SharedValue 绑定到节点样式 | |
| Concept | Description | Key API |
|---|---|---|
| worklet function | A function that can run on JS or UI thread, declared with the | |
| SharedValue | A variable synchronized across threads, read and write via | |
| Animation Driver | Bind SharedValue to node styles | |
const { shared, timing } = wx.worklet
// 1. 创建共享变量
const offset = shared(0)
// 2. 绑定到节点样式(updater 为 worklet 函数)
this.applyAnimatedStyle('#box', () => {
'worklet'
return { transform: `translateX(${offset.value}px)` }
})
// 3. 修改值驱动动画
offset.value = timing(300, { duration: 200 })const { shared, timing } = wx.worklet
// 1. Create SharedValue
const offset = shared(0)
// 2. Bind to node style (updater is a worklet function)
this.applyAnimatedStyle('#box', () => {
'worklet'
return { transform: `translateX(${offset.value}px)` }
})
// 3. Modify value to drive animation
offset.value = timing(300, { duration: 200 })references/| 我想要... | 查阅文档 |
|---|---|
| 了解 worklet 架构和完整概念 | |
| 使用 SharedValue 和 DerivedValue | |
| 在 worklet 中操作 scroll-view | |
| 使用 timing/spring/decay 动画 | |
| 查看 Easing 缓动函数 | |
| 使用序列/重复/延迟组合动画 | |
| 了解 runOnUI/runOnJS 线程通信 | |
references/| What I want to... | Refer to Document |
|---|---|
| Understand worklet architecture and complete concepts | |
| Use SharedValue and DerivedValue | |
| Operate scroll-view in worklet | |
| Use timing/spring/decay animations | |
| View Easing functions | |
| Use sequence/repeat/delay combined animations | |
| Understand runOnUI/runOnJS thread communication | |
'worklet''worklet'// ✅ Correct
function handleGesture(evt) {
'worklet'
offset.value += evt.deltaX
}
// ❌ Incorrect - 缺少 'worklet' 指令,无法在 UI 线程执行
function handleGesture(evt) {
offset.value += evt.deltaX
}// ✅ Correct
function handleGesture(evt) {
'worklet'
offset.value += evt.deltaX
}
// ❌ Incorrect - Missing 'worklet' directive, cannot execute on UI thread
function handleGesture(evt) {
offset.value += evt.deltaX
}.value.value// ✅ Correct
const offset = shared(0)
offset.value = 100
// ❌ Incorrect - 直接赋值会替换整个 SharedValue 对象
const offset = shared(0)
offset = 100// ✅ Correct
const offset = shared(0)
offset.value = 100
// ❌ Incorrect - Direct assignment replaces the entire SharedValue object
const offset = shared(0)
offset = 100runOnJSrunOnJS// ✅ Correct
function showModal(msg) {
wx.showModal({ title: msg })
}
function handleTap() {
'worklet'
const fn = this.showModal.bind(this)
runOnJS(fn)('hello')
}
// ❌ Incorrect - worklet 中直接调用普通函数
function handleTap() {
'worklet'
this.showModal('hello')
}// ✅ Correct
function showModal(msg) {
wx.showModal({ title: msg })
}
function handleTap() {
'worklet'
const fn = this.showModal.bind(this)
runOnJS(fn)('hello')
}
// ❌ Incorrect - Directly calling ordinary functions in worklet
function handleTap() {
'worklet'
this.showModal('hello')
}this.methodName.bind(this)this.methodName.bind(this)// ✅ Correct
handleTap() {
'worklet'
const showModal = this.showModal.bind(this)
runOnJS(showModal)(msg)
}
// ❌ Incorrect - 未 bind(this),this 指向丢失
handleTap() {
'worklet'
runOnJS(this.showModal)(msg)
}// ✅ Correct
handleTap() {
'worklet'
const showModal = this.showModal.bind(this)
runOnJS(showModal)(msg)
}
// ❌ Incorrect - Missing bind(this), this pointer is lost
handleTap() {
'worklet'
runOnJS(this.showModal)(msg)
}"renderer": "skyline""renderer": "skyline"wxwxrunOnJSrunOnJSthis.datathis.dataObject.freezethis.datasetData// ✅ Correct
handleTap() {
'worklet'
const msg = this.data.msg
}
// ❌ Incorrect - 解构会冻结整个 this.data
handleTap() {
'worklet'
const { msg } = this.data
}Object.freezethis.datasetData// ✅ Correct
handleTap() {
'worklet'
const msg = this.data.msg
}
// ❌ Incorrect - Destructuring will freeze the entire this.data
handleTap() {
'worklet'
const { msg } = this.data
}| 分类 | API | 说明 |
|---|---|---|
| 基础 | | 创建 SharedValue |
| 基础 | | 创建衍生值(类比 computed) |
| 基础 | | 取消动画 |
| 动画 | | 时间曲线动画(默认 300ms) |
| 动画 | | 弹簧物理动画 |
| 动画 | | 滚动衰减动画 |
| 组合 | | 依次执行 |
| 组合 | | 重复(负值=无限) |
| 组合 | | 延迟执行 |
| 工具 | | 在 UI 线程执行 |
| 工具 | | 回调 JS 线程 |
| Category | API | Description |
|---|---|---|
| Basic | | Create SharedValue |
| Basic | | Create derived value (analogous to computed) |
| Basic | | Cancel animation |
| Animation | | Time curve animation (300ms by default) |
| Animation | | Spring physics animation |
| Animation | | Scroll decay animation |
| Combine | | Execute sequentially |
| Combine | | Repeat (negative value = infinite) |
| Combine | | Execute with delay |
| Tool | | Execute on UI thread |
| Tool | | Callback to JS thread |
| 场景 | 推荐方案 |
|---|---|
| 点击后平滑移动 | |
| 手势松开回弹 | |
| 手势松开惯性滑动 | |
| 先移动再弹回 | |
| 循环脉动效果 | |
| 延迟后开始动画 | |
| Scenario | Recommended Solution |
|---|---|
| Smooth movement after click | |
| Rebound after gesture release | |
| Inertial sliding after gesture release | |
| Move first then bounce back | |
| Cyclic pulsation effect | |
| Start animation after delay | |
| 场景 | 推荐技能 | 说明 |
|---|---|---|
| 手势组件 | | pan/tap/long-press 手势处理 |
| 渲染引擎概览 | | Skyline 配置和迁移 |
| 样式开发 | | WXSS 支持与差异 |
| 路由转场 | | 自定义路由动画 |
| Scenario | Recommended Skill | Description |
|---|---|---|
| Gesture components | | pan/tap/long-press gesture handling |
| Rendering engine overview | | Skyline configuration and migration |
| Style development | | WXSS support and differences |
| Route transition | | Custom route animations |
references/
├── animation/
│ ├── combine-animation.md
│ ├── easing.md
│ └── timing-spring-decay.md
├── base/
│ ├── scroll-view-context.md
│ └── shared-derived.md
├── core/
│ └── worklet-overview.md
└── tool/
└── thread-communication.mdreferences/
├── animation/
│ ├── combine-animation.md
│ ├── easing.md
│ └── timing-spring-decay.md
├── base/
│ ├── scroll-view-context.md
│ └── shared-derived.md
├── core/
│ └── worklet-overview.md
└── tool/
└── thread-communication.md