Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
feat: parallel animation and custom animation of json
Browse files Browse the repository at this point in the history
  • Loading branch information
sheepbox8646 committed Aug 14, 2024
1 parent f056329 commit 9ddb164
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
6 changes: 5 additions & 1 deletion examples/scene1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ export default importScene(
}
},
{
"type": "destroy",
"custom": "change-property",
"target": "radius",
"parameters": {
"from": 1,
"to": 100,
"duration": 2
}
}
Expand All @@ -40,6 +43,7 @@ export default importScene(
`,
nc as any,
nc as any,
nc as any,
)
//
// export default createScene(
Expand Down
1 change: 1 addition & 0 deletions packages/json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"canvaskit-wasm": ">=0.39.1"
},
"dependencies": {
"@newcar/basic": "workspace:*",
"@newcar/core": "workspace:*",
"@newcar/utils": "workspace:*",
"mitt": "^3.0.1"
Expand Down
2 changes: 2 additions & 0 deletions packages/json/src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface SceneFormat {
export interface AnimFormat {
type: string
parameters: Record<string, unknown>
custom?: 'change-property'
target?: string
}

export interface ActionFormat {
Expand Down
45 changes: 35 additions & 10 deletions packages/json/src/import-widget.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Widget, WidgetOptions } from '@newcar/core'
import { parallel } from '@newcar/core'
import { changeProperty, parallel } from '@newcar/core'
import { Color, Shader, isString } from '@newcar/utils'
import type { AnimFormat, WidgetFormat } from './format'
import { linear } from '@newcar/basic'
import type { WidgetFormat } from './format'
import { processAction } from './process-action'
import { processResource } from './process-resource'

Expand Down Expand Up @@ -91,17 +92,41 @@ export function importWidget<T extends typeof Widget>(
if (Array.isArray(animation)) {
widget.animate(
parallel(...animation.map((ani) => {
return anims[ani.type]().withAttr({
...processOptions(ani.parameters),
by: easingFunctions[ani.parameters.by as string],
})
return ani.custom
? (ani.custom === 'change-property'
? changeProperty(() => (widget as Record<string, any>)[ani.target])
.withAttr({
...processOptions(ani.parameters),
by: easingFunctions[ani.parameters.by as string] ?? linear,
})
: console.warn(`[Newcar Warn] Json import error: there is no custom type named '${ani.custom}'`))
: anims[ani.type]().withAttr({
...processOptions(ani.parameters),
by: easingFunctions[ani.parameters.by as string] ?? linear,
})
})),
)
}
widget.animate(anims[(animation as AnimFormat).type]().withAttr({
...processOptions((animation as AnimFormat).parameters),
by: easingFunctions[(animation as AnimFormat).parameters.by as string],
}))
else {
if (animation.custom) {
if (animation.custom === 'change-property') {
widget.animate(changeProperty(() => (widget as Record<string, any>)[animation.target])
.withAttr({
...processOptions(animation.parameters),
by: easingFunctions[animation.parameters.by as string] ?? easingFunctions.linear,
}) as any)
}
else {
console.warn(`[Newcar Warn] Json import error: there is no custom type named '${animation.custom}'`)
}
}
else {
widget.animate(anims[animation.type]().withAttr({
...processOptions(animation.parameters),
by: easingFunctions[animation.parameters.by as string] ?? easingFunctions.linear,
}))
}
}
})
}
if (widgetData.actions) {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9ddb164

Please sign in to comment.