mirror of
https://github.com/motajs/template.git
synced 2026-12-03 21:04:31 +08:00
docs: 完成动画文档与编写流程
This commit is contained in:
1 parent
ab09cf1990
commit
b194d58c16
76 files changed
+11718
-26
No files matched your search
@@ -0,0 +1,3 @@
|
||||
import { DelayedPlayback } from './animater-examples';
|
||||
|
||||
export default DelayedPlayback;
|
||||
@@ -0,0 +1,3 @@
|
||||
import { DelayedTimeline } from './animater-examples';
|
||||
|
||||
export default DelayedTimeline;
|
||||
@@ -0,0 +1,503 @@
|
||||
import { EndRelation, linear } from '@motajs/animate';
|
||||
import { Font } from '@motajs/render';
|
||||
import { useAnimater } from '@motajs/render-vue';
|
||||
import { defineComponent, nextTick, onMounted, ref } from 'vue';
|
||||
|
||||
const LABEL_FONT = new Font('Verdana', 12);
|
||||
|
||||
const TRACK_X = 110;
|
||||
const TRACK_WIDTH = 330;
|
||||
const TIMELINE_TOP = 112;
|
||||
const ROW_GAP = 36;
|
||||
const BUTTON_TOP = 276;
|
||||
|
||||
const BLUE = '#58c4dd';
|
||||
const ORANGE = '#f2b84b';
|
||||
|
||||
type ScenarioKind =
|
||||
| 'sequential'
|
||||
| 'parallel'
|
||||
| 'delayed'
|
||||
| 'relay'
|
||||
| 'query'
|
||||
| 'retarget';
|
||||
|
||||
type DemoView = 'timeline' | 'playback';
|
||||
|
||||
type Animater = NonNullable<ReturnType<typeof useAnimater>>;
|
||||
|
||||
interface ISegment {
|
||||
label: string;
|
||||
start: number;
|
||||
end: number;
|
||||
row: number;
|
||||
color: string;
|
||||
}
|
||||
|
||||
interface IScenario {
|
||||
title: string;
|
||||
hint: string;
|
||||
total: number;
|
||||
segments: ISegment[];
|
||||
}
|
||||
|
||||
const SCENARIOS: Record<ScenarioKind, IScenario> = {
|
||||
sequential: {
|
||||
title: 'Animater:顺序执行',
|
||||
hint: 'x 走完,y 才开始',
|
||||
total: 1150,
|
||||
segments: [
|
||||
{ label: 'x1', start: 0, end: 700, row: 0, color: BLUE },
|
||||
{ label: 'y1', start: 700, end: 1150, row: 1, color: ORANGE }
|
||||
]
|
||||
},
|
||||
parallel: {
|
||||
title: 'Animater:并行执行',
|
||||
hint: 'x 与 y 同时开始',
|
||||
total: 900,
|
||||
segments: [
|
||||
{ label: 'x1', start: 0, end: 900, row: 0, color: BLUE },
|
||||
{ label: 'y1', start: 0, end: 900, row: 1, color: ORANGE }
|
||||
]
|
||||
},
|
||||
delayed: {
|
||||
title: 'Animater:延迟与重叠',
|
||||
hint: 'y 在 x 开始 300ms 后启动',
|
||||
total: 1000,
|
||||
segments: [
|
||||
{ label: 'x1', start: 0, end: 1000, row: 0, color: BLUE },
|
||||
{ label: 'y1', start: 300, end: 900, row: 1, color: ORANGE }
|
||||
]
|
||||
},
|
||||
relay: {
|
||||
title: 'Animater:组接力',
|
||||
hint: '组 2 等组 1 全部结束才开始',
|
||||
total: 1200,
|
||||
segments: [
|
||||
{ label: 'x1', start: 0, end: 600, row: 0, color: BLUE },
|
||||
{ label: 'y1', start: 600, end: 1200, row: 1, color: ORANGE }
|
||||
]
|
||||
},
|
||||
query: {
|
||||
title: 'Animater:查询与等待',
|
||||
hint: '读出 y 的步骤,只等待这一条',
|
||||
total: 300,
|
||||
segments: [
|
||||
{ label: 'x1', start: 0, end: 300, row: 0, color: BLUE },
|
||||
{ label: 'y1', start: 100, end: 300, row: 1, color: ORANGE }
|
||||
]
|
||||
},
|
||||
retarget: {
|
||||
title: 'Animater:中途改道',
|
||||
hint: 'x 第 2 段在 700ms 接管第 1 段',
|
||||
total: 1500,
|
||||
segments: [
|
||||
{ label: 'x1', start: 0, end: 700, row: 0, color: BLUE },
|
||||
{ label: 'x2', start: 700, end: 1200, row: 0, color: BLUE },
|
||||
{ label: 'y1', start: 0, end: 1500, row: 1, color: ORANGE }
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
const VIEW_HINTS: Record<ScenarioKind, string> = {
|
||||
sequential: 'x 结束后,y 才开始。',
|
||||
parallel: 'x 与 y 在同一组里同时推进。',
|
||||
delayed: 'y 在 x 开始 300ms 后启动。',
|
||||
relay: '组 2 等组 1 全部结束才开始。',
|
||||
query: '读出 y 第 1 段,并只等待它。',
|
||||
retarget: 'x 第 2 段在 700ms 接管第 1 段。'
|
||||
};
|
||||
|
||||
async function runScenario(
|
||||
animater: Animater,
|
||||
kind: ScenarioKind,
|
||||
x: { value: number },
|
||||
y: { value: number },
|
||||
setStatus: (text: string) => void
|
||||
): Promise<void> {
|
||||
switch (kind) {
|
||||
case 'sequential': {
|
||||
const group = animater
|
||||
.animate(x)
|
||||
.curve(linear())
|
||||
.to(1, 700)
|
||||
.animate(y)
|
||||
.after()
|
||||
.to(1, 450)
|
||||
.planEnd();
|
||||
|
||||
setStatus('x 执行中');
|
||||
await animater.wait(x, 1, group);
|
||||
setStatus('x 完成,y 开始');
|
||||
await animater.wait(y, 1, group);
|
||||
setStatus('全部完成');
|
||||
break;
|
||||
}
|
||||
case 'parallel': {
|
||||
const group = animater
|
||||
.animate(x)
|
||||
.curve(linear())
|
||||
.to(1, 900)
|
||||
.animate(y)
|
||||
.curve(linear())
|
||||
.to(1, 900)
|
||||
.planEnd();
|
||||
|
||||
setStatus('x 与 y 同时执行');
|
||||
await animater.wait(y, 1, group);
|
||||
setStatus('全部完成');
|
||||
break;
|
||||
}
|
||||
case 'delayed': {
|
||||
const group = animater
|
||||
.animate(x)
|
||||
.curve(linear())
|
||||
.to(1, 1000)
|
||||
.when(300)
|
||||
.animate(y)
|
||||
.curve(linear())
|
||||
.to(1, 600)
|
||||
.planEnd();
|
||||
|
||||
setStatus('x 先走,y 延迟 300ms 启动');
|
||||
await animater.wait(y, 1, group);
|
||||
setStatus('y 完成,x 仍在走');
|
||||
await animater.wait(x, 1, group);
|
||||
setStatus('全部完成');
|
||||
break;
|
||||
}
|
||||
case 'relay': {
|
||||
const first = animater
|
||||
.animate(x)
|
||||
.curve(linear())
|
||||
.to(1, 600)
|
||||
.planEnd();
|
||||
const second = animater
|
||||
.animate(y)
|
||||
.curve(linear())
|
||||
.to(1, 600)
|
||||
.planEnd();
|
||||
|
||||
setStatus('组 1:x 执行中');
|
||||
await animater.wait(x, 1, first);
|
||||
setStatus('组 1 完成,组 2 开始');
|
||||
await animater.wait(y, 1, second);
|
||||
setStatus('全部完成');
|
||||
break;
|
||||
}
|
||||
case 'query': {
|
||||
const group = animater
|
||||
.animate(x)
|
||||
.curve(linear())
|
||||
.to(1, 300)
|
||||
.when(100)
|
||||
.animate(y)
|
||||
.curve(linear())
|
||||
.to(1, 200)
|
||||
.planEnd();
|
||||
const yStep = animater.query(y, 1, group);
|
||||
|
||||
setStatus(
|
||||
yStep
|
||||
? `y 第 1 段:目标 ${yStep.targetValue},时长 ${yStep.time}ms`
|
||||
: '未查询到 y 第 1 段'
|
||||
);
|
||||
await animater.wait(x, 1, group);
|
||||
setStatus('x 完成');
|
||||
await animater.wait(y, 1, group);
|
||||
setStatus('y 完成,只等了指定步骤');
|
||||
break;
|
||||
}
|
||||
case 'retarget': {
|
||||
const group = animater
|
||||
.animate(x)
|
||||
.curve(linear())
|
||||
.to(1, 1200, EndRelation.Self)
|
||||
.animate(y)
|
||||
.curve(linear())
|
||||
.to(1, 1500)
|
||||
.whenPlan(x, 1, 700)
|
||||
.animate(x)
|
||||
.curve(linear())
|
||||
.to(0, 500)
|
||||
.planEnd();
|
||||
const second = animater.query(x, 2, group);
|
||||
|
||||
setStatus(
|
||||
second
|
||||
? `x 第 2 段:目标 ${second.targetValue},时长 ${second.time}ms`
|
||||
: '未查询到 x 第 2 段'
|
||||
);
|
||||
await animater.wait(x, 1, group);
|
||||
setStatus('x 第 1 段在改道时结束');
|
||||
await animater.wait(x, 2, group);
|
||||
setStatus('x 第 2 段完成,y 仍在走');
|
||||
await animater.wait(y, 1, group);
|
||||
setStatus('全部完成');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function createDemo(kind: ScenarioKind, view: DemoView) {
|
||||
const scenario = SCENARIOS[kind];
|
||||
|
||||
return defineComponent(() => {
|
||||
const animater = useAnimater()!;
|
||||
const clock = useAnimater()!;
|
||||
const x = ref(0);
|
||||
const y = ref(0);
|
||||
const phase = ref(0);
|
||||
const running = ref(false);
|
||||
const status = ref('等待开始');
|
||||
|
||||
const play = async () => {
|
||||
if (running.value) return;
|
||||
running.value = true;
|
||||
x.value = 0;
|
||||
y.value = 0;
|
||||
phase.value = 0;
|
||||
status.value = '准备开始';
|
||||
|
||||
clock
|
||||
.animate(phase)
|
||||
.curve(linear())
|
||||
.to(1, scenario.total)
|
||||
.planEnd();
|
||||
|
||||
await runScenario(animater, kind, x, y, text => {
|
||||
status.value = text;
|
||||
});
|
||||
running.value = false;
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
nextTick(() => {
|
||||
void play();
|
||||
});
|
||||
});
|
||||
|
||||
const renderTimeline = () => {
|
||||
const playhead = TRACK_X + phase.value * TRACK_WIDTH;
|
||||
const elapsed = Math.round(phase.value * scenario.total);
|
||||
|
||||
return (
|
||||
<>
|
||||
<text
|
||||
loc={[24, TIMELINE_TOP - 14]}
|
||||
text="x"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[24, TIMELINE_TOP + ROW_GAP - 14]}
|
||||
text="y"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
{[0, 1].map(row => (
|
||||
<g-rect
|
||||
key={row}
|
||||
loc={[
|
||||
TRACK_X,
|
||||
TIMELINE_TOP + row * ROW_GAP,
|
||||
TRACK_WIDTH,
|
||||
16
|
||||
]}
|
||||
fillStyle="#1c2633"
|
||||
/>
|
||||
))}
|
||||
{scenario.segments.map(segment => {
|
||||
const left =
|
||||
TRACK_X +
|
||||
(segment.start / scenario.total) * TRACK_WIDTH;
|
||||
const width =
|
||||
((segment.end - segment.start) / scenario.total) *
|
||||
TRACK_WIDTH;
|
||||
const top = TIMELINE_TOP + segment.row * ROW_GAP;
|
||||
|
||||
return (
|
||||
<container
|
||||
key={segment.label}
|
||||
loc={[left, top, width, 16]}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, width, 16]}
|
||||
circle={[4]}
|
||||
fillStyle={segment.color}
|
||||
/>
|
||||
<text
|
||||
loc={[5, 2]}
|
||||
text={segment.label}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#0e141b"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
})}
|
||||
<g-rect
|
||||
loc={[playhead - 1, TIMELINE_TOP - 8, 2, ROW_GAP + 32]}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<g-circle
|
||||
circle={[playhead, TIMELINE_TOP - 8, 4]}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[456, TIMELINE_TOP - 18]}
|
||||
anc={[1, 0]}
|
||||
text={`${elapsed} ms`}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 214]}
|
||||
text={`x ${x.value.toFixed(2)} y ${y.value.toFixed(2)}`}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 236]}
|
||||
text="色块是步骤段,竖线是播放头。"
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 254]}
|
||||
text="播放头由另一个真实执行器推进。"
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
const renderPlayback = () => {
|
||||
const retargetX = TRACK_X + (700 / scenario.total) * TRACK_WIDTH;
|
||||
|
||||
return (
|
||||
<>
|
||||
<text loc={[24, 96]} text="x" fillStyle="#cfe0ff" />
|
||||
<g-rect
|
||||
loc={[TRACK_X, 104, TRACK_WIDTH, 8]}
|
||||
fillStyle="#26364d"
|
||||
/>
|
||||
<g-circle
|
||||
circle={[TRACK_X + x.value * TRACK_WIDTH, 108, 7]}
|
||||
fillStyle={BLUE}
|
||||
/>
|
||||
<text
|
||||
loc={[456, 96]}
|
||||
anc={[1, 0]}
|
||||
text={x.value.toFixed(2)}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<text loc={[24, 146]} text="y" fillStyle="#cfe0ff" />
|
||||
<g-rect
|
||||
loc={[TRACK_X, 154, TRACK_WIDTH, 8]}
|
||||
fillStyle="#26364d"
|
||||
/>
|
||||
<g-circle
|
||||
circle={[TRACK_X + y.value * TRACK_WIDTH, 158, 7]}
|
||||
fillStyle={ORANGE}
|
||||
/>
|
||||
<text
|
||||
loc={[456, 146]}
|
||||
anc={[1, 0]}
|
||||
text={y.value.toFixed(2)}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
|
||||
{kind === 'retarget' && (
|
||||
<>
|
||||
<g-rect
|
||||
loc={[retargetX - 1, 96, 2, 24]}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[retargetX - 16, 82]}
|
||||
text="改道"
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
|
||||
<text
|
||||
loc={[24, 202]}
|
||||
text={status.value}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#7ee787"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 226]}
|
||||
text={VIEW_HINTS[kind]}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text
|
||||
loc={[24, 14]}
|
||||
text={scenario.title}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text loc={[24, 36]} text={scenario.hint} fillStyle="#8b98a8" />
|
||||
<text
|
||||
loc={[24, 56]}
|
||||
text={view === 'timeline' ? '时间轴视图' : '播放视图'}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
{view === 'timeline' ? renderTimeline() : renderPlayback()}
|
||||
|
||||
<container
|
||||
loc={[24, BUTTON_TOP, 100, 30]}
|
||||
cursor="pointer"
|
||||
onClick={() => {
|
||||
void play();
|
||||
}}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, 100, 30]}
|
||||
circle={[8]}
|
||||
fillStyle={running.value ? '#33465f' : '#2f6fed'}
|
||||
/>
|
||||
<text
|
||||
loc={[50, 15]}
|
||||
anc={[0.5, 0.5]}
|
||||
text={running.value ? '播放中' : '重播'}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
</container>
|
||||
<text
|
||||
loc={[136, BUTTON_TOP + 8]}
|
||||
text={running.value ? '真实执行器正在推进' : '点击重播'}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export const SequentialTimeline = createDemo('sequential', 'timeline');
|
||||
export const SequentialPlayback = createDemo('sequential', 'playback');
|
||||
export const ParallelTimeline = createDemo('parallel', 'timeline');
|
||||
export const ParallelPlayback = createDemo('parallel', 'playback');
|
||||
export const DelayedTimeline = createDemo('delayed', 'timeline');
|
||||
export const DelayedPlayback = createDemo('delayed', 'playback');
|
||||
export const RelayTimeline = createDemo('relay', 'timeline');
|
||||
export const RelayPlayback = createDemo('relay', 'playback');
|
||||
export const QueryTimeline = createDemo('query', 'timeline');
|
||||
export const QueryPlayback = createDemo('query', 'playback');
|
||||
export const RetargetTimeline = createDemo('retarget', 'timeline');
|
||||
export const RetargetPlayback = createDemo('retarget', 'playback');
|
||||
@@ -0,0 +1,3 @@
|
||||
import { ParallelPlayback } from './animater-examples';
|
||||
|
||||
export default ParallelPlayback;
|
||||
@@ -0,0 +1,3 @@
|
||||
import { ParallelTimeline } from './animater-examples';
|
||||
|
||||
export default ParallelTimeline;
|
||||
@@ -0,0 +1,3 @@
|
||||
import { QueryPlayback } from './animater-examples';
|
||||
|
||||
export default QueryPlayback;
|
||||
@@ -0,0 +1,3 @@
|
||||
import { QueryTimeline } from './animater-examples';
|
||||
|
||||
export default QueryTimeline;
|
||||
@@ -0,0 +1,3 @@
|
||||
import { RelayPlayback } from './animater-examples';
|
||||
|
||||
export default RelayPlayback;
|
||||
@@ -0,0 +1,3 @@
|
||||
import { RelayTimeline } from './animater-examples';
|
||||
|
||||
export default RelayTimeline;
|
||||
@@ -0,0 +1,3 @@
|
||||
import { RetargetPlayback } from './animater-examples';
|
||||
|
||||
export default RetargetPlayback;
|
||||
@@ -0,0 +1,3 @@
|
||||
import { RetargetTimeline } from './animater-examples';
|
||||
|
||||
export default RetargetTimeline;
|
||||
@@ -0,0 +1,3 @@
|
||||
import { SequentialPlayback } from './animater-examples';
|
||||
|
||||
export default SequentialPlayback;
|
||||
@@ -0,0 +1,3 @@
|
||||
import { SequentialTimeline } from './animater-examples';
|
||||
|
||||
export default SequentialTimeline;
|
||||
@@ -0,0 +1,241 @@
|
||||
import { linear } from '@motajs/animate';
|
||||
import { Font } from '@motajs/render';
|
||||
import { useAnimater } from '@motajs/render-vue';
|
||||
import { defineComponent, nextTick, onMounted, ref } from 'vue';
|
||||
|
||||
const LABEL_FONT = new Font('Verdana', 12);
|
||||
|
||||
const TOTAL = 1400;
|
||||
const A1_TIME = 800;
|
||||
const A2_DELAY = 200;
|
||||
const A2_TIME = 400;
|
||||
const B1_DELAY = 200;
|
||||
const B1_TIME = 500;
|
||||
const B2_DELAY = 150;
|
||||
const B2_TIME = 300;
|
||||
|
||||
const TRACK_X = 70;
|
||||
const TRACK_WIDTH = 350;
|
||||
const TIMELINE_TOP = 164;
|
||||
|
||||
interface ITimelineSegment {
|
||||
label: string;
|
||||
start: number;
|
||||
end: number;
|
||||
row: number;
|
||||
color: string;
|
||||
}
|
||||
|
||||
const SEGMENTS: ITimelineSegment[] = [
|
||||
{ label: 'A1', start: 0, end: A1_TIME, row: 0, color: '#58c4dd' },
|
||||
{
|
||||
label: 'A2',
|
||||
start: A1_TIME + A2_DELAY,
|
||||
end: A1_TIME + A2_DELAY + A2_TIME,
|
||||
row: 0,
|
||||
color: '#58c4dd'
|
||||
},
|
||||
{
|
||||
label: 'B1',
|
||||
start: B1_DELAY,
|
||||
end: B1_DELAY + B1_TIME,
|
||||
row: 1,
|
||||
color: '#f2b84b'
|
||||
},
|
||||
{
|
||||
label: 'B2',
|
||||
start: B1_DELAY + B1_TIME + B2_DELAY,
|
||||
end: B1_DELAY + B1_TIME + B2_DELAY + B2_TIME,
|
||||
row: 1,
|
||||
color: '#f2b84b'
|
||||
}
|
||||
];
|
||||
|
||||
// 时间线演示:两个动画对象由同一个 Animater 推进,播放头本身也是一条计划
|
||||
export default defineComponent(() => {
|
||||
const animater = useAnimater()!;
|
||||
const left = ref(0);
|
||||
const right = ref(0);
|
||||
const phase = ref(0);
|
||||
const running = ref(false);
|
||||
|
||||
const play = async () => {
|
||||
if (running.value) return;
|
||||
running.value = true;
|
||||
left.value = 0;
|
||||
right.value = 0;
|
||||
phase.value = 0;
|
||||
|
||||
const group = animater
|
||||
.animate(phase)
|
||||
.curve(linear())
|
||||
.to(1, TOTAL)
|
||||
.animate(left)
|
||||
.when(0)
|
||||
.to(1, A1_TIME)
|
||||
.animate(right)
|
||||
.when(B1_DELAY)
|
||||
.to(1, B1_TIME)
|
||||
.animate(right)
|
||||
.after(B2_DELAY)
|
||||
.to(0, B2_TIME)
|
||||
.animate(left)
|
||||
.afterPlan(left, 1, A2_DELAY)
|
||||
.to(0, A2_TIME)
|
||||
.planEnd();
|
||||
|
||||
await animater.wait(phase, 1, group);
|
||||
running.value = false;
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
nextTick(() => {
|
||||
void play();
|
||||
});
|
||||
});
|
||||
|
||||
return () => {
|
||||
const playhead = TRACK_X + phase.value * TRACK_WIDTH;
|
||||
const elapsed = Math.round(phase.value * TOTAL);
|
||||
|
||||
return (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text
|
||||
loc={[24, 16]}
|
||||
text="Animater:计划组时间线"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 38]}
|
||||
text="两条轨迹由同一个执行器推进,色块是计划段"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<text loc={[24, 64]} text="对象 A" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[456, 64]}
|
||||
anc={[1, 0]}
|
||||
text={`value ${left.value.toFixed(2)}`}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
<g-rect
|
||||
loc={[TRACK_X, 81, TRACK_WIDTH, 2]}
|
||||
fillStyle="#26364d"
|
||||
/>
|
||||
<g-circle
|
||||
circle={[TRACK_X + left.value * TRACK_WIDTH, 82, 7]}
|
||||
fillStyle="#58c4dd"
|
||||
/>
|
||||
|
||||
<text loc={[24, 100]} text="对象 B" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[456, 100]}
|
||||
anc={[1, 0]}
|
||||
text={`value ${right.value.toFixed(2)}`}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
<g-rect
|
||||
loc={[TRACK_X, 117, TRACK_WIDTH, 2]}
|
||||
fillStyle="#26364d"
|
||||
/>
|
||||
<g-circle
|
||||
circle={[TRACK_X + right.value * TRACK_WIDTH, 118, 7]}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
|
||||
<text loc={[24, 142]} text="计划组 1" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[456, 142]}
|
||||
anc={[1, 0]}
|
||||
text={`${elapsed} ms`}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<g-rect
|
||||
loc={[TRACK_X, TIMELINE_TOP, TRACK_WIDTH, 16]}
|
||||
fillStyle="#1c2633"
|
||||
/>
|
||||
<g-rect
|
||||
loc={[TRACK_X, TIMELINE_TOP + 26, TRACK_WIDTH, 16]}
|
||||
fillStyle="#1c2633"
|
||||
/>
|
||||
<text loc={[24, TIMELINE_TOP]} text="A" fillStyle="#e8f0ff" />
|
||||
<text
|
||||
loc={[24, TIMELINE_TOP + 26]}
|
||||
text="B"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
|
||||
{SEGMENTS.map(segment => {
|
||||
const x = TRACK_X + (segment.start / TOTAL) * TRACK_WIDTH;
|
||||
const width =
|
||||
((segment.end - segment.start) / TOTAL) * TRACK_WIDTH;
|
||||
const y = TIMELINE_TOP + segment.row * 26;
|
||||
|
||||
return (
|
||||
<container key={segment.label} loc={[x, y, width, 16]}>
|
||||
<g-rectr
|
||||
loc={[0, 0, width, 16]}
|
||||
circle={[4]}
|
||||
fillStyle={segment.color}
|
||||
/>
|
||||
<text
|
||||
loc={[5, 2]}
|
||||
text={segment.label}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#0e141b"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
})}
|
||||
|
||||
<g-rect loc={[playhead - 1, 156, 2, 60]} fillStyle="#e8f0ff" />
|
||||
<g-circle circle={[playhead, 156, 4]} fillStyle="#e8f0ff" />
|
||||
|
||||
<text
|
||||
loc={[24, 224]}
|
||||
text="A1 0–800ms;A2 在 A1 结束后 200ms 开始"
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 242]}
|
||||
text="B1 在 A1 开始后 200ms 开始;B2 在 B1 结束后 150ms 开始"
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<container
|
||||
loc={[70, 268, 100, 30]}
|
||||
cursor="pointer"
|
||||
onClick={() => {
|
||||
void play();
|
||||
}}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, 100, 30]}
|
||||
circle={[8]}
|
||||
fillStyle={running.value ? '#33465f' : '#2f6fed'}
|
||||
/>
|
||||
<text
|
||||
loc={[50, 15]}
|
||||
anc={[0.5, 0.5]}
|
||||
text={running.value ? '播放中' : '重播'}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
</container>
|
||||
<text
|
||||
loc={[184, 276]}
|
||||
text={running.value ? '播放头正在推进' : '点击重播时间线'}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,123 @@
|
||||
import { CurveMode, linear, sin } from '@motajs/animate';
|
||||
import { transitioned } from '@motajs/render-vue';
|
||||
import { defineComponent, ref } from 'vue';
|
||||
|
||||
// 数值渐变:alpha、位置与进度条
|
||||
export default defineComponent(() => {
|
||||
const alpha = transitioned(0.25, 600, sin(CurveMode.EaseOut))!;
|
||||
const slide = transitioned(0, 600, sin(CurveMode.EaseOut))!;
|
||||
const bar = transitioned(0.3, 600, sin(CurveMode.EaseOut))!;
|
||||
const high = ref(false);
|
||||
const smooth = ref(true);
|
||||
const slow = ref(false);
|
||||
|
||||
const toggle = () => {
|
||||
high.value = !high.value;
|
||||
alpha.set(high.value ? 1 : 0.25);
|
||||
slide.set(high.value ? 1 : 0);
|
||||
bar.set(high.value ? 1 : 0.3);
|
||||
};
|
||||
|
||||
const toggleCurve = () => {
|
||||
smooth.value = !smooth.value;
|
||||
const curve = smooth.value ? sin() : linear();
|
||||
alpha.mode(curve);
|
||||
slide.mode(curve);
|
||||
bar.mode(curve);
|
||||
toggle();
|
||||
};
|
||||
|
||||
const toggleTime = () => {
|
||||
slow.value = !slow.value;
|
||||
const time = slow.value ? 1200 : 600;
|
||||
alpha.setTime(time);
|
||||
slide.setTime(time);
|
||||
bar.setTime(time);
|
||||
toggle();
|
||||
};
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text
|
||||
loc={[24, 16]}
|
||||
text="transitioned:数值渐变"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 40]}
|
||||
text="alpha 与位置都读 ref.value"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<text loc={[40, 72]} text="alpha" fillStyle="#cfe0ff" />
|
||||
<g-rect
|
||||
loc={[40, 92, 120, 88]}
|
||||
fillStyle="#2f6fed"
|
||||
alpha={alpha.ref.value}
|
||||
/>
|
||||
<text
|
||||
loc={[40, 190]}
|
||||
text={alpha.ref.value.toFixed(2)}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<text loc={[200, 72]} text="position" fillStyle="#cfe0ff" />
|
||||
<g-rect loc={[200, 136, 200, 8]} fillStyle="#26364d" />
|
||||
<g-circle
|
||||
circle={[204 + slide.ref.value * 192, 140, 12]}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
<text
|
||||
loc={[200, 190]}
|
||||
text={slide.ref.value.toFixed(2)}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<text loc={[40, 214]} text="progress" fillStyle="#cfe0ff" />
|
||||
<g-rect loc={[40, 234, 280, 12]} fillStyle="#26364d" />
|
||||
<g-rect
|
||||
loc={[40, 234, 280 * bar.ref.value, 12]}
|
||||
fillStyle="#7ee787"
|
||||
/>
|
||||
<text
|
||||
loc={[330, 230]}
|
||||
text={`${Math.round(bar.ref.value * 100)}%`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<container loc={[40, 264, 96, 32]} onClick={toggle}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 96, 32]}
|
||||
circle={[8]}
|
||||
fillStyle="#2f6fed"
|
||||
/>
|
||||
<text loc={[12, 8]} text="切换" fillStyle="#ffffff" />
|
||||
</container>
|
||||
<container loc={[144, 264, 96, 32]} onClick={toggleCurve}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 96, 32]}
|
||||
circle={[8]}
|
||||
fillStyle="#33465f"
|
||||
/>
|
||||
<text
|
||||
loc={[12, 8]}
|
||||
text={smooth.value ? '缓动' : '线性'}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
</container>
|
||||
<container loc={[248, 264, 96, 32]} onClick={toggleTime}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 96, 32]}
|
||||
circle={[8]}
|
||||
fillStyle="#33465f"
|
||||
/>
|
||||
<text
|
||||
loc={[12, 8]}
|
||||
text={slow.value ? '慢速' : '快速'}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,61 @@
|
||||
import { CurveMode, sin } from '@motajs/animate';
|
||||
import { transitionedColor } from '@motajs/render-vue';
|
||||
import { defineComponent, ref } from 'vue';
|
||||
|
||||
// 颜色渐变:填充色与描边色
|
||||
export default defineComponent(() => {
|
||||
const fill = transitionedColor('#2f6fed', 600, sin(CurveMode.EaseOut))!;
|
||||
const stroke = transitionedColor(
|
||||
'rgba(242, 184, 75, 0.9)',
|
||||
600,
|
||||
sin(CurveMode.EaseOut)
|
||||
)!;
|
||||
const warm = ref(true);
|
||||
|
||||
const toggle = () => {
|
||||
warm.value = !warm.value;
|
||||
fill.set(warm.value ? '#2f6fed' : '#f2b84b');
|
||||
stroke.set(warm.value ? 'rgba(242, 184, 75, 0.9)' : '#7ee787');
|
||||
};
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text
|
||||
loc={[24, 16]}
|
||||
text="transitionedColor:颜色渐变"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 40]}
|
||||
text="填充与描边都读 ref.value"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<text loc={[40, 64]} text="fillStyle" fillStyle="#cfe0ff" />
|
||||
<g-rectr
|
||||
loc={[40, 88, 180, 140]}
|
||||
circle={[16]}
|
||||
fillStyle={fill.ref.value}
|
||||
/>
|
||||
<text loc={[60, 144]} text="color" fillStyle="#ffffff" />
|
||||
|
||||
<text loc={[280, 64]} text="strokeStyle" fillStyle="#cfe0ff" />
|
||||
<g-circle
|
||||
circle={[320, 158, 56]}
|
||||
stroke
|
||||
strokeStyle={stroke.ref.value}
|
||||
lineWidth={10}
|
||||
/>
|
||||
|
||||
<container loc={[40, 264, 120, 32]} onClick={toggle}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 120, 32]}
|
||||
circle={[8]}
|
||||
fillStyle="#33465f"
|
||||
/>
|
||||
<text loc={[12, 8]} text="切换颜色" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import { addCurve, constant, sin } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveCalcLab from './animation-curve-calc';
|
||||
|
||||
const SOURCE_1 = sin();
|
||||
const SOURCE_2 = constant(0.2);
|
||||
const RESULT = addCurve(SOURCE_1, SOURCE_2);
|
||||
|
||||
// 相加:两条来源曲线的逐点和
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveCalcLab
|
||||
title="addCurve"
|
||||
subtitle="结果 = f(x) + g(x)"
|
||||
curves={[
|
||||
{ label: 'sin()', color: '#2f6fed', curve: SOURCE_1 },
|
||||
{ label: 'constant(0.2)', color: '#f2b84b', curve: SOURCE_2 },
|
||||
{ label: 'addCurve', color: '#7ee787', curve: RESULT }
|
||||
]}
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import { compositeCurve, pow, sin } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveCalcLab from './animation-curve-calc';
|
||||
|
||||
const OUTER = sin();
|
||||
const INNER = pow(2);
|
||||
const RESULT = compositeCurve(OUTER, INNER);
|
||||
|
||||
// 复合:外层曲线接收内层曲线的输出
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveCalcLab
|
||||
title="compositeCurve"
|
||||
subtitle="结果 = 外层(内层(p))"
|
||||
curves={[
|
||||
{ label: 'sin()', color: '#2f6fed', curve: OUTER },
|
||||
{ label: 'pow(2)', color: '#f2b84b', curve: INNER },
|
||||
{ label: 'compositeCurve', color: '#7ee787', curve: RESULT }
|
||||
]}
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import { constant, divCurve, linear } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveCalcLab from './animation-curve-calc';
|
||||
|
||||
const SOURCE_1 = linear();
|
||||
const SOURCE_2 = constant(0.5);
|
||||
const RESULT = divCurve(SOURCE_1, SOURCE_2);
|
||||
|
||||
// 相除:两条来源曲线的逐点商,分母取常数避免过零
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveCalcLab
|
||||
title="divCurve"
|
||||
subtitle="结果 = f(x) / g(x)"
|
||||
curves={[
|
||||
{ label: 'linear()', color: '#2f6fed', curve: SOURCE_1 },
|
||||
{ label: 'constant(0.5)', color: '#f2b84b', curve: SOURCE_2 },
|
||||
{ label: 'divCurve', color: '#7ee787', curve: RESULT }
|
||||
]}
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
import { moveCurve, sin } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveCalcLab from './animation-curve-calc';
|
||||
|
||||
const SOURCE = sin();
|
||||
const RESULT = moveCurve(SOURCE, 0.2);
|
||||
|
||||
// 平移:在来源曲线上加一个纵向偏移
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveCalcLab
|
||||
title="moveCurve"
|
||||
subtitle="结果 = f(x) + move"
|
||||
curves={[
|
||||
{ label: 'sin()', color: '#2f6fed', curve: SOURCE },
|
||||
{ label: 'moveCurve', color: '#7ee787', curve: RESULT }
|
||||
]}
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import { linear, mulCurve, sin } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveCalcLab from './animation-curve-calc';
|
||||
|
||||
const SOURCE_1 = sin();
|
||||
const SOURCE_2 = linear();
|
||||
const RESULT = mulCurve(SOURCE_1, SOURCE_2);
|
||||
|
||||
// 相乘:两条来源曲线的逐点积
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveCalcLab
|
||||
title="mulCurve"
|
||||
subtitle="结果 = f(x) · g(x)"
|
||||
curves={[
|
||||
{ label: 'sin()', color: '#2f6fed', curve: SOURCE_1 },
|
||||
{ label: 'linear()', color: '#f2b84b', curve: SOURCE_2 },
|
||||
{ label: 'mulCurve', color: '#7ee787', curve: RESULT }
|
||||
]}
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
import { moveCurve, normalize, scaleCurve, sin } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveCalcLab from './animation-curve-calc';
|
||||
|
||||
const SOURCE = moveCurve(scaleCurve(sin(), 0.6), 0.2);
|
||||
const RESULT = normalize(SOURCE);
|
||||
|
||||
// 归一化:把来源曲线的两端对齐到 0 与 1
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveCalcLab
|
||||
title="normalize"
|
||||
subtitle="结果按 curve(0) 与 curve(1) 归一化"
|
||||
curves={[
|
||||
{ label: '0.2 + 0.6·sin()', color: '#2f6fed', curve: SOURCE },
|
||||
{ label: 'normalize', color: '#7ee787', curve: RESULT }
|
||||
]}
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
import { oppositeCurve, sin } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveCalcLab from './animation-curve-calc';
|
||||
|
||||
const SOURCE = sin();
|
||||
const RESULT = oppositeCurve(SOURCE, 1);
|
||||
|
||||
// 取反:以 move 为基准翻转来源曲线
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveCalcLab
|
||||
title="oppositeCurve"
|
||||
subtitle="结果 = move - f(x)"
|
||||
curves={[
|
||||
{ label: 'sin()', color: '#2f6fed', curve: SOURCE },
|
||||
{ label: 'oppositeCurve', color: '#7ee787', curve: RESULT }
|
||||
]}
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import { constant, linear, powCurve } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveCalcLab from './animation-curve-calc';
|
||||
|
||||
const SOURCE_1 = linear();
|
||||
const SOURCE_2 = constant(2);
|
||||
const RESULT = powCurve(SOURCE_1, SOURCE_2);
|
||||
|
||||
// 取幂:第一条曲线为底,第二条曲线为指数
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveCalcLab
|
||||
title="powCurve"
|
||||
subtitle="结果 = f(x) ^ g(x)"
|
||||
curves={[
|
||||
{ label: 'linear()', color: '#2f6fed', curve: SOURCE_1 },
|
||||
{ label: 'constant(2)', color: '#f2b84b', curve: SOURCE_2 },
|
||||
{ label: 'powCurve', color: '#7ee787', curve: RESULT }
|
||||
]}
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import {
|
||||
linear,
|
||||
moveCurve,
|
||||
reciprocalCurve,
|
||||
scaleCurve
|
||||
} from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveCalcLab from './animation-curve-calc';
|
||||
|
||||
const SOURCE = moveCurve(scaleCurve(linear(), 0.5), 0.5);
|
||||
const RESULT = reciprocalCurve(SOURCE, 0.5);
|
||||
|
||||
// 倒数:来源曲线取值在 0.5 到 1,避免分母过零
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveCalcLab
|
||||
title="reciprocalCurve"
|
||||
subtitle="结果 = scale / f(x)"
|
||||
curves={[
|
||||
{ label: '0.5 + 0.5p', color: '#2f6fed', curve: SOURCE },
|
||||
{ label: 'reciprocalCurve', color: '#7ee787', curve: RESULT }
|
||||
]}
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
import { scaleCurve, sin } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveCalcLab from './animation-curve-calc';
|
||||
|
||||
const SOURCE = sin();
|
||||
const RESULT = scaleCurve(SOURCE, 0.5);
|
||||
|
||||
// 缩放:纵向缩小来源曲线
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveCalcLab
|
||||
title="scaleCurve"
|
||||
subtitle="结果 = f(x) · scale"
|
||||
curves={[
|
||||
{ label: 'sin()', color: '#2f6fed', curve: SOURCE },
|
||||
{ label: 'scaleCurve', color: '#7ee787', curve: RESULT }
|
||||
]}
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
import { linear, sequenceCurve, sin } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveCalcLab from './animation-curve-calc';
|
||||
|
||||
const SEGMENT_1 = linear();
|
||||
const SEGMENT_2 = sin();
|
||||
const RESULT = sequenceCurve(
|
||||
[SEGMENT_1, SEGMENT_2],
|
||||
[0.5, 0.5],
|
||||
[1, 1],
|
||||
[0, 0]
|
||||
);
|
||||
|
||||
// 拼接:两条来源段按各占一半时长合成一条曲线
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveCalcLab
|
||||
title="sequenceCurve"
|
||||
subtitle="来源段按 0.5 / 0.5 的时长拼接"
|
||||
curves={[
|
||||
{ label: 'linear()', color: '#2f6fed', curve: SEGMENT_1 },
|
||||
{ label: 'sin()', color: '#f2b84b', curve: SEGMENT_2 },
|
||||
{ label: 'sequenceCurve', color: '#7ee787', curve: RESULT }
|
||||
]}
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import { constant, linear, subCurve } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveCalcLab from './animation-curve-calc';
|
||||
|
||||
const SOURCE_1 = constant(0.8);
|
||||
const SOURCE_2 = linear();
|
||||
const RESULT = subCurve(SOURCE_1, SOURCE_2);
|
||||
|
||||
// 相减:两条来源曲线的逐点差
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveCalcLab
|
||||
title="subCurve"
|
||||
subtitle="结果 = f(x) - g(x)"
|
||||
curves={[
|
||||
{ label: 'constant(0.8)', color: '#2f6fed', curve: SOURCE_1 },
|
||||
{ label: 'linear()', color: '#f2b84b', curve: SOURCE_2 },
|
||||
{ label: 'subCurve', color: '#7ee787', curve: RESULT }
|
||||
]}
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,99 @@
|
||||
import { Font } from '@motajs/render';
|
||||
import { SetupComponentOptions } from '@motajs/system';
|
||||
import { defineComponent } from 'vue';
|
||||
import { createCurveRender, type CurveEntry } from './animation-curve-plot';
|
||||
|
||||
const LABEL_FONT = new Font('Verdana', 12);
|
||||
|
||||
export interface ICurveCalcLabProps {
|
||||
/** 计算函数名称,显示在标题位置 */
|
||||
title: string;
|
||||
/** 来源与结果的简短说明 */
|
||||
subtitle: string;
|
||||
/** 来源曲线与结果曲线;通常前两项是来源,最后一项是结果 */
|
||||
curves: CurveEntry[];
|
||||
}
|
||||
|
||||
const curveCalcLabProps = {
|
||||
props: ['title', 'subtitle', 'curves']
|
||||
} satisfies SetupComponentOptions<ICurveCalcLabProps>;
|
||||
|
||||
const RANGE_SAMPLES = 120;
|
||||
|
||||
// 按来源与结果曲线的实际采样计算显示范围,避免把越界输出截断
|
||||
function getDisplayRange(curves: CurveEntry[]): {
|
||||
min: number;
|
||||
max: number;
|
||||
} {
|
||||
let min = Infinity;
|
||||
let max = -Infinity;
|
||||
|
||||
for (const entry of curves) {
|
||||
for (let i = 0; i <= RANGE_SAMPLES; i++) {
|
||||
const value = entry.curve(i / RANGE_SAMPLES);
|
||||
if (!Number.isFinite(value)) continue;
|
||||
min = Math.min(min, value);
|
||||
max = Math.max(max, value);
|
||||
}
|
||||
}
|
||||
|
||||
if (!Number.isFinite(min) || !Number.isFinite(max)) {
|
||||
return { min: 0, max: 1 };
|
||||
}
|
||||
|
||||
// 0 是进度映射的基准,显示范围需要包含它
|
||||
min = Math.min(min, 0);
|
||||
max = Math.max(max, 0);
|
||||
|
||||
const span = max - min;
|
||||
const padding = span > 0 ? span * 0.1 : 0.1;
|
||||
|
||||
return { min: min - padding, max: max + padding };
|
||||
}
|
||||
|
||||
// 曲线计算实验台:标题、说明、来源与结果曲线图
|
||||
const CurveCalcLab = defineComponent<ICurveCalcLabProps>((props, { slots }) => {
|
||||
return () => {
|
||||
const render = createCurveRender(
|
||||
props.curves,
|
||||
getDisplayRange(props.curves)
|
||||
);
|
||||
|
||||
return (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text loc={[24, 16]} text={props.title} fillStyle="#e8f0ff" />
|
||||
<text
|
||||
loc={[24, 38]}
|
||||
text={props.subtitle}
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<custom loc={[36, 64, 408, 190]} render={render} />
|
||||
|
||||
{props.curves.map((entry, index) => (
|
||||
<container
|
||||
key={entry.label}
|
||||
loc={[40 + index * 140, 266, 132, 20]}
|
||||
>
|
||||
<g-line
|
||||
line={[0, 10, 18, 10]}
|
||||
strokeStyle={entry.color}
|
||||
lineWidth={4}
|
||||
/>
|
||||
<text
|
||||
loc={[24, 4]}
|
||||
text={entry.label}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
</container>
|
||||
))}
|
||||
|
||||
{slots.default?.()}
|
||||
</container>
|
||||
);
|
||||
};
|
||||
}, curveCalcLabProps);
|
||||
|
||||
export default CurveCalcLab;
|
||||
@@ -0,0 +1,30 @@
|
||||
import { constant } from '@motajs/animate';
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import { Slider } from '../../../../../packages-user/client-base/src/components/slider';
|
||||
import CurveLab from './animation-curve-lab';
|
||||
|
||||
const VALUE_MIN = 0;
|
||||
const VALUE_MAX = 1;
|
||||
|
||||
// 常数曲线:输出始终等于 k
|
||||
export default defineComponent(() => {
|
||||
const k = ref(0.5);
|
||||
|
||||
return () => (
|
||||
<CurveLab
|
||||
title="constant(k)"
|
||||
subtitle="常数曲线:输出始终等于 k(示例 k 取 0 到 1)"
|
||||
curve={() => constant(k.value)}
|
||||
bindings={[k.value]}
|
||||
>
|
||||
<text loc={[24, 4]} text="常数值 k" fillStyle="#cfe0ff" />
|
||||
<Slider
|
||||
loc={[112, 0, 336, 24]}
|
||||
v-model:value={k.value}
|
||||
min={VALUE_MIN}
|
||||
max={VALUE_MAX}
|
||||
step={0.05}
|
||||
/>
|
||||
</CurveLab>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { cosh } from '@motajs/animate';
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import { Slider } from '../../../../../packages-user/client-base/src/components/slider';
|
||||
import CurveLab from './animation-curve-lab';
|
||||
|
||||
const K_MIN = 0.5;
|
||||
const K_MAX = 4;
|
||||
|
||||
// 双曲余弦曲线:调整比例参数 k,切换四种 CurveMode
|
||||
export default defineComponent(() => {
|
||||
const k = ref(2);
|
||||
|
||||
return () => (
|
||||
<CurveLab
|
||||
title="cosh(k = 2, mode?)"
|
||||
subtitle="双曲余弦曲线:调整 k 与模式"
|
||||
curve={mode => cosh(k.value, mode)}
|
||||
bindings={[k.value]}
|
||||
supportsMode
|
||||
>
|
||||
<text loc={[24, 4]} text="比例参数 k" fillStyle="#cfe0ff" />
|
||||
<Slider
|
||||
loc={[112, 0, 336, 24]}
|
||||
v-model:value={k.value}
|
||||
min={K_MIN}
|
||||
max={K_MAX}
|
||||
step={0.1}
|
||||
/>
|
||||
</CurveLab>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,131 @@
|
||||
import { CurveMode, type ExcitationCurve } from '@motajs/animate';
|
||||
import { Font } from '@motajs/render';
|
||||
import { SetupComponentOptions } from '@motajs/system';
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import { createCurveRender } from './animation-curve-plot';
|
||||
|
||||
const LABEL_FONT = new Font('Verdana', 12);
|
||||
|
||||
const MODE_ITEMS: { mode: CurveMode; label: string; hint: string }[] = [
|
||||
{ mode: CurveMode.EaseIn, label: 'EaseIn', hint: '缓进快出' },
|
||||
{ mode: CurveMode.EaseOut, label: 'EaseOut', hint: '快进缓出' },
|
||||
{ mode: CurveMode.EaseInOut, label: 'EaseInOut', hint: '缓进缓出,中间快' },
|
||||
{
|
||||
mode: CurveMode.EaseCenter,
|
||||
label: 'EaseCenter',
|
||||
hint: '快进快出,中间缓'
|
||||
}
|
||||
];
|
||||
|
||||
export interface ICurveLabProps {
|
||||
/** 曲线名称,显示在标题位置 */
|
||||
title: string;
|
||||
/** 曲线说明,显示在标题下方 */
|
||||
subtitle: string;
|
||||
/** 按当前模式生成要绘制的曲线 */
|
||||
curve: (mode: CurveMode) => ExcitationCurve;
|
||||
/** 曲线依赖的响应式值;变化时重绘 */
|
||||
bindings: unknown[];
|
||||
/** 是否显示四种曲线模式的按钮 */
|
||||
supportsMode?: boolean;
|
||||
/** 没有控件时显示的说明 */
|
||||
note?: string;
|
||||
}
|
||||
|
||||
const curveLabProps = {
|
||||
props: ['title', 'subtitle', 'curve', 'bindings', 'supportsMode', 'note']
|
||||
} satisfies SetupComponentOptions<ICurveLabProps>;
|
||||
|
||||
// 曲线实验台:标题、说明、曲线图,以及可选的模式按钮与参数控件
|
||||
const CurveLab = defineComponent<ICurveLabProps>((props, { slots }) => {
|
||||
const mode = ref<CurveMode>(CurveMode.EaseIn);
|
||||
|
||||
return () => {
|
||||
const supportsMode = props.supportsMode ?? false;
|
||||
const currentMode =
|
||||
MODE_ITEMS.find(item => item.mode === mode.value) ?? MODE_ITEMS[0];
|
||||
const hasBody = Boolean(slots.default) || Boolean(props.note);
|
||||
const modeTop = hasBody ? 258 : 272;
|
||||
const bodyTop = supportsMode ? 292 : 268;
|
||||
const render = createCurveRender([
|
||||
{
|
||||
label: props.title,
|
||||
color: '#7ee787',
|
||||
curve: props.curve(mode.value)
|
||||
}
|
||||
]);
|
||||
|
||||
return (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text loc={[24, 16]} text={props.title} fillStyle="#e8f0ff" />
|
||||
<text
|
||||
loc={[24, 38]}
|
||||
text={props.subtitle}
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
{supportsMode && (
|
||||
<text
|
||||
loc={[456, 16]}
|
||||
anc={[1, 0]}
|
||||
text={currentMode.hint}
|
||||
font={LABEL_FONT}
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
)}
|
||||
|
||||
<custom
|
||||
loc={[36, 64, 408, 190]}
|
||||
bindings={[...props.bindings, mode.value]}
|
||||
render={render}
|
||||
/>
|
||||
|
||||
{supportsMode &&
|
||||
MODE_ITEMS.map((item, index) => {
|
||||
const active = item.mode === mode.value;
|
||||
|
||||
return (
|
||||
<container
|
||||
key={item.label}
|
||||
loc={[28 + index * 108, modeTop, 100, 26]}
|
||||
cursor="pointer"
|
||||
onClick={() => {
|
||||
mode.value = item.mode;
|
||||
}}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, 100, 26]}
|
||||
circle={[8]}
|
||||
fillStyle={active ? '#2f6fed' : '#26364d'}
|
||||
/>
|
||||
<text
|
||||
loc={[50, 13]}
|
||||
anc={[0.5, 0.5]}
|
||||
text={item.label}
|
||||
font={LABEL_FONT}
|
||||
fillStyle={active ? '#ffffff' : '#cfe0ff'}
|
||||
noevent
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
})}
|
||||
|
||||
{hasBody && (
|
||||
<container loc={[0, bodyTop, 480, 26]}>
|
||||
{slots.default?.()}
|
||||
{!slots.default && props.note && (
|
||||
<text
|
||||
loc={[24, 4]}
|
||||
text={props.note}
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
)}
|
||||
</container>
|
||||
)}
|
||||
</container>
|
||||
);
|
||||
};
|
||||
}, curveLabProps);
|
||||
|
||||
export default CurveLab;
|
||||
@@ -0,0 +1,16 @@
|
||||
import { linear } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveLab from './animation-curve-lab';
|
||||
|
||||
// 线性曲线:没有参数,输出始终等于 x
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveLab
|
||||
title="linear()"
|
||||
subtitle="线性曲线:输出等于 x"
|
||||
curve={() => linear()}
|
||||
bindings={[]}
|
||||
note="无参数"
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { sin } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveLab from './animation-curve-lab';
|
||||
|
||||
// 曲线模式:同一条正弦曲线在四种 CurveMode 之间切换
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveLab
|
||||
title="sin()"
|
||||
subtitle="同一条正弦曲线在四种模式之间切换"
|
||||
curve={mode => sin(mode)}
|
||||
bindings={[]}
|
||||
supportsMode
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,113 @@
|
||||
import type { ExcitationCurve } from '@motajs/animate';
|
||||
import type { CustomRenderFunction } from '@motajs/render';
|
||||
|
||||
// 一条曲线的名称、颜色与函数
|
||||
export interface CurveEntry {
|
||||
label: string;
|
||||
color: string;
|
||||
curve: ExcitationCurve;
|
||||
}
|
||||
|
||||
const WIDTH = 408;
|
||||
const HEIGHT = 190;
|
||||
const LEFT = 46;
|
||||
const TOP = 12;
|
||||
const RIGHT = 12;
|
||||
const BOTTOM = 36;
|
||||
|
||||
// 刻度文字:整数不带小数点,其余最多保留两位
|
||||
function formatTick(value: number): string {
|
||||
if (Number.isInteger(value)) return String(value);
|
||||
return value.toFixed(2).replace(/0+$/, '').replace(/\.$/, '');
|
||||
}
|
||||
|
||||
// 生成绘制坐标轴与多条曲线的 render 函数;纵轴默认 0 到 1,可指定显示范围
|
||||
export function createCurveRender(
|
||||
curves: CurveEntry[],
|
||||
options: { min?: number; max?: number } = {}
|
||||
): CustomRenderFunction {
|
||||
const min = options.min ?? 0;
|
||||
const max = options.max ?? 1;
|
||||
|
||||
return canvas => {
|
||||
const ctx = canvas.ctx;
|
||||
const plotWidth = WIDTH - LEFT - RIGHT;
|
||||
const plotHeight = HEIGHT - TOP - BOTTOM;
|
||||
const bottom = TOP + plotHeight;
|
||||
const toY = (value: number) =>
|
||||
TOP + ((max - value) / (max - min)) * plotHeight;
|
||||
|
||||
// 背景
|
||||
ctx.fillStyle = '#101820';
|
||||
ctx.fillRect(0, 0, WIDTH, HEIGHT);
|
||||
|
||||
// 网格
|
||||
ctx.strokeStyle = '#22304a';
|
||||
ctx.lineWidth = 1;
|
||||
for (let i = 0; i <= 4; i++) {
|
||||
const y = TOP + (plotHeight * i) / 4;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(LEFT, y);
|
||||
ctx.lineTo(LEFT + plotWidth, y);
|
||||
ctx.stroke();
|
||||
|
||||
const x = LEFT + (plotWidth * i) / 4;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x, TOP);
|
||||
ctx.lineTo(x, bottom);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
// 坐标轴
|
||||
ctx.strokeStyle = '#7d8ea6';
|
||||
ctx.lineWidth = 1.5;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(LEFT, TOP);
|
||||
ctx.lineTo(LEFT, bottom);
|
||||
ctx.lineTo(LEFT + plotWidth, bottom);
|
||||
ctx.stroke();
|
||||
|
||||
// 刻度
|
||||
ctx.fillStyle = '#8b98a8';
|
||||
ctx.font = '11px Verdana';
|
||||
ctx.textAlign = 'right';
|
||||
ctx.textBaseline = 'middle';
|
||||
for (let i = 0; i <= 4; i++) {
|
||||
const value = max - ((max - min) * i) / 4;
|
||||
const y = TOP + (plotHeight * i) / 4;
|
||||
ctx.fillText(formatTick(value), LEFT - 8, y);
|
||||
}
|
||||
|
||||
ctx.textAlign = 'center';
|
||||
ctx.textBaseline = 'top';
|
||||
ctx.fillText('0', LEFT, bottom + 6);
|
||||
ctx.fillText('0.5', LEFT + plotWidth / 2, bottom + 6);
|
||||
ctx.fillText('1', LEFT + plotWidth, bottom + 6);
|
||||
|
||||
// 轴名
|
||||
ctx.fillText('x', LEFT + plotWidth / 2, bottom + 22);
|
||||
|
||||
ctx.save();
|
||||
ctx.translate(12, TOP + plotHeight / 2);
|
||||
ctx.rotate(-Math.PI / 2);
|
||||
ctx.textBaseline = 'middle';
|
||||
ctx.fillText('输出', 0, 0);
|
||||
ctx.restore();
|
||||
|
||||
// 曲线
|
||||
ctx.lineWidth = 2.5;
|
||||
for (const entry of curves) {
|
||||
ctx.beginPath();
|
||||
ctx.strokeStyle = entry.color;
|
||||
for (let i = 0; i <= 120; i++) {
|
||||
const p = i / 120;
|
||||
const value = entry.curve(p);
|
||||
const x = LEFT + p * plotWidth;
|
||||
const y = toY(value);
|
||||
if (i === 0) ctx.moveTo(x, y);
|
||||
else ctx.lineTo(x, y);
|
||||
}
|
||||
ctx.stroke();
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { pow } from '@motajs/animate';
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import { Slider } from '../../../../../packages-user/client-base/src/components/slider';
|
||||
import CurveLab from './animation-curve-lab';
|
||||
|
||||
const EXP_MIN = 0.5;
|
||||
const EXP_MAX = 4;
|
||||
|
||||
// 幂曲线:调整指数,切换四种 CurveMode
|
||||
export default defineComponent(() => {
|
||||
const exp = ref(2);
|
||||
|
||||
return () => (
|
||||
<CurveLab
|
||||
title="pow(exp, mode?)"
|
||||
subtitle="幂曲线:调整指数与模式"
|
||||
curve={mode => pow(exp.value, mode)}
|
||||
bindings={[exp.value]}
|
||||
supportsMode
|
||||
>
|
||||
<text loc={[24, 4]} text="指数 exp" fillStyle="#cfe0ff" />
|
||||
<Slider
|
||||
loc={[112, 0, 336, 24]}
|
||||
v-model:value={exp.value}
|
||||
min={EXP_MIN}
|
||||
max={EXP_MAX}
|
||||
step={0.1}
|
||||
/>
|
||||
</CurveLab>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { sec } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveLab from './animation-curve-lab';
|
||||
|
||||
// 正割曲线:切换四种 CurveMode,观察快慢分布
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveLab
|
||||
title="sec(mode?)"
|
||||
subtitle="正割曲线:切换模式观察快慢分布"
|
||||
curve={mode => sec(mode)}
|
||||
bindings={[]}
|
||||
supportsMode
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { sech } from '@motajs/animate';
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import { Slider } from '../../../../../packages-user/client-base/src/components/slider';
|
||||
import CurveLab from './animation-curve-lab';
|
||||
|
||||
const K_MIN = 0.5;
|
||||
const K_MAX = 4;
|
||||
|
||||
// 双曲正割曲线:调整比例参数 k,切换四种 CurveMode
|
||||
export default defineComponent(() => {
|
||||
const k = ref(1);
|
||||
|
||||
return () => (
|
||||
<CurveLab
|
||||
title="sech(k = 1, mode?)"
|
||||
subtitle="双曲正割曲线:调整 k 与模式"
|
||||
curve={mode => sech(k.value, mode)}
|
||||
bindings={[k.value]}
|
||||
supportsMode
|
||||
>
|
||||
<text loc={[24, 4]} text="比例参数 k" fillStyle="#cfe0ff" />
|
||||
<Slider
|
||||
loc={[112, 0, 336, 24]}
|
||||
v-model:value={k.value}
|
||||
min={K_MIN}
|
||||
max={K_MAX}
|
||||
step={0.1}
|
||||
/>
|
||||
</CurveLab>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { sin } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveLab from './animation-curve-lab';
|
||||
|
||||
// 正弦曲线:切换四种 CurveMode,观察快慢分布
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveLab
|
||||
title="sin(mode?)"
|
||||
subtitle="正弦曲线:切换模式观察快慢分布"
|
||||
curve={mode => sin(mode)}
|
||||
bindings={[]}
|
||||
supportsMode
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
import { step } from '@motajs/animate';
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import { Slider } from '../../../../../packages-user/client-base/src/components/slider';
|
||||
import CurveLab from './animation-curve-lab';
|
||||
|
||||
const STEP_MIN = 2;
|
||||
const STEP_MAX = 12;
|
||||
|
||||
// 阶梯曲线:进度每走 1/k,输出跳变一次
|
||||
export default defineComponent(() => {
|
||||
const k = ref(5);
|
||||
|
||||
return () => (
|
||||
<CurveLab
|
||||
title="step(k)"
|
||||
subtitle="阶梯曲线:每 1/k 段跳变一次"
|
||||
curve={() => step(k.value)}
|
||||
bindings={[k.value]}
|
||||
>
|
||||
<text loc={[24, 4]} text="阶梯数 k" fillStyle="#cfe0ff" />
|
||||
<Slider
|
||||
loc={[112, 0, 336, 24]}
|
||||
v-model:value={k.value}
|
||||
min={STEP_MIN}
|
||||
max={STEP_MAX}
|
||||
step={1}
|
||||
/>
|
||||
</CurveLab>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { tan } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import CurveLab from './animation-curve-lab';
|
||||
|
||||
// 正切曲线:切换四种 CurveMode,观察快慢分布
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<CurveLab
|
||||
title="tan(mode?)"
|
||||
subtitle="正切曲线:切换模式观察快慢分布"
|
||||
curve={mode => tan(mode)}
|
||||
bindings={[]}
|
||||
supportsMode
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { tanh } from '@motajs/animate';
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import { Slider } from '../../../../../packages-user/client-base/src/components/slider';
|
||||
import CurveLab from './animation-curve-lab';
|
||||
|
||||
const K_MIN = 0.5;
|
||||
const K_MAX = 4;
|
||||
|
||||
// 双曲正切曲线:调整比例参数 k,切换四种 CurveMode
|
||||
export default defineComponent(() => {
|
||||
const k = ref(2);
|
||||
|
||||
return () => (
|
||||
<CurveLab
|
||||
title="tanh(k = 2, mode?)"
|
||||
subtitle="双曲正切曲线:调整 k 与模式"
|
||||
curve={mode => tanh(k.value, mode)}
|
||||
bindings={[k.value]}
|
||||
supportsMode
|
||||
>
|
||||
<text loc={[24, 4]} text="比例参数 k" fillStyle="#cfe0ff" />
|
||||
<Slider
|
||||
loc={[112, 0, 336, 24]}
|
||||
v-model:value={k.value}
|
||||
min={K_MIN}
|
||||
max={K_MAX}
|
||||
step={0.1}
|
||||
/>
|
||||
</CurveLab>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
import { constant, linear, step } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import { createCurveRender, type CurveEntry } from './animation-curve-plot';
|
||||
|
||||
const CURVES: CurveEntry[] = [
|
||||
{ label: 'linear()', color: '#7ee787', curve: linear() },
|
||||
{ label: 'step(5)', color: '#f2b84b', curve: step(5) },
|
||||
{ label: 'constant(0.5)', color: '#c58af9', curve: constant(0.5) }
|
||||
];
|
||||
|
||||
// 基础曲线:线性、阶梯与常数
|
||||
export default defineComponent(() => {
|
||||
const render = createCurveRender(CURVES);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text loc={[24, 16]} text="基础曲线" fillStyle="#e8f0ff" />
|
||||
<text
|
||||
loc={[24, 38]}
|
||||
text="横轴 progress 0→1;纵轴输出"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<custom loc={[36, 64, 408, 190]} render={render} />
|
||||
|
||||
{CURVES.map((entry, index) => (
|
||||
<container
|
||||
key={entry.label}
|
||||
loc={[
|
||||
40 + (index % 2) * 210,
|
||||
266 + Math.floor(index / 2) * 26,
|
||||
200,
|
||||
20
|
||||
]}
|
||||
>
|
||||
<g-line
|
||||
line={[0, 10, 20, 10]}
|
||||
strokeStyle={entry.color}
|
||||
lineWidth={4}
|
||||
/>
|
||||
<text
|
||||
loc={[28, 0]}
|
||||
text={entry.label}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
</container>
|
||||
))}
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
import { CurveMode, pow, sec, sin, tan } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import { createCurveRender, type CurveEntry } from './animation-curve-plot';
|
||||
|
||||
const CURVES: CurveEntry[] = [
|
||||
{ label: 'sin()', color: '#2f6fed', curve: sin(CurveMode.EaseOut) },
|
||||
{
|
||||
label: 'pow(2)',
|
||||
color: '#7ee787',
|
||||
curve: pow(2, CurveMode.EaseOut)
|
||||
},
|
||||
{ label: 'tan()', color: '#f2b84b', curve: tan(CurveMode.EaseOut) },
|
||||
{ label: 'sec()', color: '#c58af9', curve: sec(CurveMode.EaseOut) }
|
||||
];
|
||||
|
||||
// 缓动曲线:正弦、幂、正切与正割
|
||||
export default defineComponent(() => {
|
||||
const render = createCurveRender(CURVES);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text loc={[24, 16]} text="缓动曲线" fillStyle="#e8f0ff" />
|
||||
<text
|
||||
loc={[24, 38]}
|
||||
text="横轴 progress 0→1;纵轴输出"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<custom loc={[36, 64, 408, 190]} render={render} />
|
||||
|
||||
{CURVES.map((entry, index) => (
|
||||
<container
|
||||
key={entry.label}
|
||||
loc={[
|
||||
40 + (index % 2) * 210,
|
||||
266 + Math.floor(index / 2) * 26,
|
||||
200,
|
||||
20
|
||||
]}
|
||||
>
|
||||
<g-line
|
||||
line={[0, 10, 20, 10]}
|
||||
strokeStyle={entry.color}
|
||||
lineWidth={4}
|
||||
/>
|
||||
<text
|
||||
loc={[28, 0]}
|
||||
text={entry.label}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
</container>
|
||||
))}
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,64 @@
|
||||
import { CurveMode, cosh, sech, tanh } from '@motajs/animate';
|
||||
import { defineComponent } from 'vue';
|
||||
import { createCurveRender, type CurveEntry } from './animation-curve-plot';
|
||||
|
||||
const CURVES: CurveEntry[] = [
|
||||
{
|
||||
label: 'cosh()',
|
||||
color: '#2f6fed',
|
||||
curve: cosh(2, CurveMode.EaseOut)
|
||||
},
|
||||
{
|
||||
label: 'tanh()',
|
||||
color: '#7ee787',
|
||||
curve: tanh(2, CurveMode.EaseOut)
|
||||
},
|
||||
{
|
||||
label: 'sech()',
|
||||
color: '#f2b84b',
|
||||
curve: sech(1, CurveMode.EaseOut)
|
||||
}
|
||||
];
|
||||
|
||||
// 双曲曲线:双曲余弦、双曲正切与双曲正割
|
||||
export default defineComponent(() => {
|
||||
// 三条曲线都在 0 到 1 之间,纵轴固定为 0 到 1
|
||||
const render = createCurveRender(CURVES);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text loc={[24, 16]} text="双曲曲线" fillStyle="#e8f0ff" />
|
||||
<text
|
||||
loc={[24, 38]}
|
||||
text="横轴 progress 0→1;纵轴输出"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<custom loc={[36, 64, 408, 190]} render={render} />
|
||||
|
||||
{CURVES.map((entry, index) => (
|
||||
<container
|
||||
key={entry.label}
|
||||
loc={[
|
||||
40 + (index % 2) * 210,
|
||||
266 + Math.floor(index / 2) * 26,
|
||||
200,
|
||||
20
|
||||
]}
|
||||
>
|
||||
<g-line
|
||||
line={[0, 10, 20, 10]}
|
||||
strokeStyle={entry.color}
|
||||
lineWidth={4}
|
||||
/>
|
||||
<text
|
||||
loc={[28, 0]}
|
||||
text={entry.label}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
</container>
|
||||
))}
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,123 @@
|
||||
import { CurveMode, sin } from '@motajs/animate';
|
||||
import { cycledTransition } from '@motajs/render-vue';
|
||||
import { defineComponent, ref, watch } from 'vue';
|
||||
import { Slider } from '../../../../../packages-user/client-base/src/components/slider';
|
||||
|
||||
// 循环渐变:两个滑动条调整循环的起点与终点
|
||||
export default defineComponent(() => {
|
||||
const minValue = ref(0.25);
|
||||
const maxValue = ref(0.9);
|
||||
const glow = cycledTransition(
|
||||
minValue.value,
|
||||
maxValue.value,
|
||||
900,
|
||||
sin(CurveMode.EaseOut)
|
||||
)!;
|
||||
|
||||
const applyRange = () => {
|
||||
// 两个端点保持一前一后,避免区间塌成一个点
|
||||
if (maxValue.value - minValue.value < 0.05) {
|
||||
if (minValue.value <= 0.95) {
|
||||
maxValue.value = minValue.value + 0.05;
|
||||
} else {
|
||||
minValue.value = maxValue.value - 0.05;
|
||||
}
|
||||
}
|
||||
glow.setRange(minValue.value, maxValue.value);
|
||||
};
|
||||
|
||||
watch([minValue, maxValue], applyRange);
|
||||
|
||||
return () => {
|
||||
const trackLeft = 64;
|
||||
const trackWidth = 352;
|
||||
const trackY = 130;
|
||||
const minX = trackLeft + minValue.value * trackWidth;
|
||||
const maxX = trackLeft + maxValue.value * trackWidth;
|
||||
const currentX = trackLeft + glow.ref.value * trackWidth;
|
||||
|
||||
return (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text
|
||||
loc={[24, 16]}
|
||||
text="cycledTransition:循环渐变"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 40]}
|
||||
text="拖动两个滑动条调整循环的起点与终点"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<text loc={[24, 76]} text="当前值" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[24, 98]}
|
||||
text={glow.ref.value.toFixed(2)}
|
||||
fillStyle="#7ee787"
|
||||
/>
|
||||
|
||||
<g-line
|
||||
line={[trackLeft, trackY, trackLeft + trackWidth, trackY]}
|
||||
strokeStyle="#26364d"
|
||||
lineWidth={8}
|
||||
lineCap="round"
|
||||
/>
|
||||
<g-line
|
||||
line={[minX, trackY, maxX, trackY]}
|
||||
strokeStyle="#2f6fed"
|
||||
lineWidth={8}
|
||||
lineCap="round"
|
||||
/>
|
||||
<g-line
|
||||
line={[minX, trackY - 12, minX, trackY + 12]}
|
||||
strokeStyle="#8fb6ff"
|
||||
lineWidth={2}
|
||||
/>
|
||||
<g-line
|
||||
line={[maxX, trackY - 12, maxX, trackY + 12]}
|
||||
strokeStyle="#8fb6ff"
|
||||
lineWidth={2}
|
||||
/>
|
||||
<g-circle circle={[currentX, trackY, 9]} fillStyle="#f2b84b" />
|
||||
|
||||
<text
|
||||
loc={[minX, trackY + 22]}
|
||||
anc={[0.5, 0]}
|
||||
text={`min ${minValue.value.toFixed(2)}`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
<text
|
||||
loc={[maxX, trackY + 22]}
|
||||
anc={[0.5, 0]}
|
||||
text={`max ${maxValue.value.toFixed(2)}`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<text loc={[64, 196]} text="起点" fillStyle="#cfe0ff" />
|
||||
<Slider
|
||||
loc={[108, 192, 300, 24]}
|
||||
v-model:value={minValue.value}
|
||||
min={0}
|
||||
max={1}
|
||||
step={0.05}
|
||||
valueWidth={44}
|
||||
filledStyle="#2f6fed"
|
||||
emptyStyle="#26364d"
|
||||
/>
|
||||
|
||||
<text loc={[64, 244]} text="终点" fillStyle="#cfe0ff" />
|
||||
<Slider
|
||||
loc={[108, 240, 300, 24]}
|
||||
v-model:value={maxValue.value}
|
||||
min={0}
|
||||
max={1}
|
||||
step={0.05}
|
||||
valueWidth={44}
|
||||
filledStyle="#2f6fed"
|
||||
emptyStyle="#26364d"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,137 @@
|
||||
import { EndRelation } from '@motajs/animate';
|
||||
import { useTransition } from '@motajs/render-vue';
|
||||
import { defineComponent, onMounted, ref } from 'vue';
|
||||
|
||||
// 半程曲线在 1 处输出 0.5,回程曲线在 1 处回到 0
|
||||
const HALF = (p: number) => p * 0.5;
|
||||
const RETURN = (p: number) => Math.sin(p * Math.PI) * 0.5;
|
||||
|
||||
const curves = [
|
||||
{ label: '半程曲线', curve: HALF, time: 600 },
|
||||
{ label: '回程曲线', curve: RETURN, time: 900 }
|
||||
];
|
||||
|
||||
// 同一目标配同一条曲线,比较 Target 与 Curve 两种结尾参考
|
||||
export default defineComponent(() => {
|
||||
const targetValue = ref(0);
|
||||
const curveValue = ref(0);
|
||||
const index = ref(0);
|
||||
const transition = useTransition()!;
|
||||
|
||||
const play = () => {
|
||||
const item = curves[index.value];
|
||||
targetValue.value = 0;
|
||||
curveValue.value = 0;
|
||||
transition
|
||||
.transition(targetValue)
|
||||
.curve(item.curve)
|
||||
.to(1, item.time, EndRelation.Target);
|
||||
transition
|
||||
.transition(curveValue)
|
||||
.curve(item.curve)
|
||||
.to(1, item.time, EndRelation.Curve);
|
||||
};
|
||||
|
||||
const next = () => {
|
||||
index.value = (index.value + 1) % curves.length;
|
||||
play();
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
requestAnimationFrame(play);
|
||||
});
|
||||
|
||||
return () => {
|
||||
const item = curves[index.value];
|
||||
const endpoint = item.curve(1);
|
||||
const targetX = 24 + targetValue.value * 400;
|
||||
const curveX = 24 + curveValue.value * 400;
|
||||
const endX = 24 + endpoint * 400;
|
||||
|
||||
return (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text
|
||||
loc={[24, 16]}
|
||||
text="Transition:EndRelation"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 40]}
|
||||
text="同一条曲线:Target 结束于目标值,Curve 停在曲线终点"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<text
|
||||
loc={[24, 72]}
|
||||
text="Target:结束写目标值"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<g-rect loc={[24, 96, 400, 14]} fillStyle="#26364d" />
|
||||
<g-rect
|
||||
loc={[24, 96, 400 * targetValue.value, 14]}
|
||||
fillStyle="#2f6fed"
|
||||
/>
|
||||
<g-rect loc={[targetX - 1, 86, 2, 34]} fillStyle="#f2b84b" />
|
||||
<text
|
||||
loc={[24, 120]}
|
||||
text={`当前 ${targetValue.value.toFixed(2)} · 终点 1.00`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<text
|
||||
loc={[24, 150]}
|
||||
text="Curve:结束写曲线终点"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<g-rect loc={[24, 174, 400, 14]} fillStyle="#26364d" />
|
||||
<g-rect
|
||||
loc={[24, 174, 400 * curveValue.value, 14]}
|
||||
fillStyle="#7ee787"
|
||||
/>
|
||||
<g-rect loc={[curveX - 1, 164, 2, 34]} fillStyle="#58c4dd" />
|
||||
<g-rect loc={[endX - 1, 164, 2, 34]} fillStyle="#f2b84b" />
|
||||
<text
|
||||
loc={[24, 198]}
|
||||
text={`当前 ${curveValue.value.toFixed(2)} · 终点 ${endpoint.toFixed(2)}`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<text
|
||||
loc={[24, 226]}
|
||||
text={`曲线:${item.label} · ${item.time}ms · 终点 ${endpoint.toFixed(2)}`}
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<container
|
||||
loc={[24, 254, 100, 30]}
|
||||
cursor="pointer"
|
||||
onClick={play}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, 100, 30]}
|
||||
circle={[8]}
|
||||
fillStyle="#2f6fed"
|
||||
/>
|
||||
<text loc={[14, 7]} text="重播" fillStyle="#ffffff" />
|
||||
</container>
|
||||
<container
|
||||
loc={[140, 254, 160, 30]}
|
||||
cursor="pointer"
|
||||
onClick={next}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, 160, 30]}
|
||||
circle={[8]}
|
||||
fillStyle="#33465f"
|
||||
/>
|
||||
<text
|
||||
loc={[14, 7]}
|
||||
text="换曲线与时长"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,93 @@
|
||||
import { linear, sin } from '@motajs/animate';
|
||||
import { useTransition } from '@motajs/render-vue';
|
||||
import { defineComponent, onMounted, ref } from 'vue';
|
||||
|
||||
// 单值渐变:Transition 让当前值走向目标值
|
||||
export default defineComponent(() => {
|
||||
const value = ref(0.18);
|
||||
const target = ref(0.82);
|
||||
const transition = useTransition()!;
|
||||
|
||||
// 设定新的目标值,执行器从当前值继续渐变
|
||||
const moveTo = (next: number, curve = sin()) => {
|
||||
target.value = next;
|
||||
transition.transition(value).curve(curve).to(next, 900);
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
// 等执行器绑定到根激励源后再启动首段渐变
|
||||
requestAnimationFrame(() => moveTo(0.82));
|
||||
});
|
||||
|
||||
return () => {
|
||||
const current = value.value;
|
||||
const goal = target.value;
|
||||
const targetX = 40 + goal * 400;
|
||||
|
||||
return (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text
|
||||
loc={[24, 16]}
|
||||
text="Transition:单值渐变"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 40]}
|
||||
text="当前值由执行器逐帧写入,目标值由 to 设定"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<text loc={[40, 80]} text="当前值" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[40, 100]}
|
||||
text={current.toFixed(2)}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
<text loc={[160, 80]} text="目标值" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[160, 100]}
|
||||
text={goal.toFixed(2)}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
|
||||
<g-rect loc={[40, 140, 400, 16]} fillStyle="#26364d" />
|
||||
<g-rect
|
||||
loc={[40, 140, 400 * current, 16]}
|
||||
fillStyle="#2f6fed"
|
||||
/>
|
||||
<g-rect loc={[targetX - 1, 128, 2, 40]} fillStyle="#f2b84b" />
|
||||
<g-circle circle={[targetX, 148, 6]} fillStyle="#f2b84b" />
|
||||
|
||||
<container
|
||||
loc={[40, 208, 120, 32]}
|
||||
onClick={() => moveTo(0.82)}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, 120, 32]}
|
||||
circle={[8]}
|
||||
fillStyle="#2f6fed"
|
||||
/>
|
||||
<text loc={[12, 8]} text="去 0.82" fillStyle="#ffffff" />
|
||||
</container>
|
||||
<container
|
||||
loc={[176, 208, 120, 32]}
|
||||
onClick={() => moveTo(0.18, linear())}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, 120, 32]}
|
||||
circle={[8]}
|
||||
fillStyle="#33465f"
|
||||
/>
|
||||
<text loc={[12, 8]} text="回 0.18" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
|
||||
<text
|
||||
loc={[40, 264]}
|
||||
text={`当前值 ${current.toFixed(2)} → 目标值 ${goal.toFixed(2)}`}
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,155 @@
|
||||
import { ExcitationVariator, RafExcitation } from '@motajs/animate';
|
||||
import { defineComponent, onUnmounted, ref } from 'vue';
|
||||
|
||||
// 变速曲线:最后两次激励时间戳与时间差
|
||||
export default defineComponent(() => {
|
||||
const speed = ref(1);
|
||||
const progress = ref(0);
|
||||
const prevTs = ref(0);
|
||||
const lastTs = ref(0);
|
||||
const gap = ref(0);
|
||||
const running = ref(false);
|
||||
|
||||
let base = 0;
|
||||
let hasBase = false;
|
||||
let hasPrev = false;
|
||||
let curveStart = 0;
|
||||
const curveTime = 3000;
|
||||
|
||||
const source = new RafExcitation();
|
||||
const variator = new ExcitationVariator();
|
||||
variator.bindExcitation(source);
|
||||
|
||||
variator.add({
|
||||
excited(payload: number) {
|
||||
if (!hasBase) {
|
||||
base = payload;
|
||||
hasBase = true;
|
||||
}
|
||||
if (hasPrev) prevTs.value = lastTs.value;
|
||||
lastTs.value = payload - base;
|
||||
if (hasPrev) gap.value = lastTs.value - prevTs.value;
|
||||
hasPrev = true;
|
||||
speed.value = variator.speed;
|
||||
|
||||
if (running.value) {
|
||||
const elapsed = source.payload() - curveStart;
|
||||
progress.value = Math.min(elapsed / curveTime, 1);
|
||||
if (progress.value >= 1) running.value = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const runCurve = () => {
|
||||
if (running.value) return;
|
||||
curveStart = source.payload();
|
||||
running.value = true;
|
||||
progress.value = 0;
|
||||
void variator.curveSpeed(p => 1.5 - 1.25 * p, curveTime);
|
||||
};
|
||||
|
||||
const resetSpeed = () => {
|
||||
variator.endAllCurves();
|
||||
variator.setSpeed(1);
|
||||
speed.value = 1;
|
||||
running.value = false;
|
||||
progress.value = 0;
|
||||
};
|
||||
|
||||
onUnmounted(() => {
|
||||
variator.destroy();
|
||||
source.destroy();
|
||||
});
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text
|
||||
loc={[24, 16]}
|
||||
text="curveSpeed:时间流速随时间变化"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 40]}
|
||||
text="曲线速度从 1.5× 降到 0.25×,持续 3 秒"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<text
|
||||
loc={[40, 72]}
|
||||
text="输出端时间(相对开始)"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[40, 98]}
|
||||
text={`${lastTs.value.toFixed(1)} ms`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<text loc={[260, 72]} text="当前速度" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[260, 98]}
|
||||
text={`${speed.value.toFixed(2)}×`}
|
||||
fillStyle="#7ee787"
|
||||
/>
|
||||
|
||||
<g-rect loc={[40, 122, 400, 92]} fillStyle="#1b2433" />
|
||||
<text loc={[52, 134]} text="最后两次激励" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[52, 158]}
|
||||
text={`上次 = ${prevTs.value.toFixed(1)} ms`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
<text
|
||||
loc={[52, 180]}
|
||||
text={`本次 = ${lastTs.value.toFixed(1)} ms`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
<text loc={[252, 158]} text="时间差" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[252, 180]}
|
||||
text={`${gap.value.toFixed(2)} ms`}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
|
||||
<text
|
||||
loc={[40, 220]}
|
||||
text="速度越高,输出端时间推进越快,时间差越大"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<text loc={[40, 248]} text="曲线进度" fillStyle="#cfe0ff" />
|
||||
<g-rect loc={[40, 266, 300, 10]} fillStyle="#26364d" />
|
||||
<g-rect
|
||||
loc={[40, 266, 300 * progress.value, 10]}
|
||||
fillStyle="#7ee787"
|
||||
/>
|
||||
<text
|
||||
loc={[352, 262]}
|
||||
text={`${Math.round(progress.value * 100)}%`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<container loc={[40, 286, 140, 32]} onClick={runCurve}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 140, 32]}
|
||||
circle={[8]}
|
||||
fillStyle={running.value ? '#26364d' : '#2f6fed'}
|
||||
/>
|
||||
<text
|
||||
loc={[26, 8]}
|
||||
text={running.value ? '曲线运行中' : '运行曲线'}
|
||||
fillStyle="#ffffff"
|
||||
/>
|
||||
</container>
|
||||
<container loc={[196, 286, 140, 32]} onClick={resetSpeed}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 140, 32]}
|
||||
circle={[8]}
|
||||
fillStyle="#33465f"
|
||||
/>
|
||||
<text loc={[26, 8]} text="重置为 1×" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,114 @@
|
||||
import { ExcitationDivider, IntervalExcitation } from '@motajs/animate';
|
||||
import { defineComponent, onUnmounted, ref } from 'vue';
|
||||
|
||||
// 分频器:统计输入源激励次数与输出端转发次数
|
||||
export default defineComponent(() => {
|
||||
const divider = ref(1);
|
||||
const upCount = ref(0);
|
||||
const downCount = ref(0);
|
||||
const upPayload = ref(0);
|
||||
const downPayload = ref(0);
|
||||
|
||||
const source = new IntervalExcitation(200);
|
||||
const splitter = new ExcitationDivider<number>();
|
||||
splitter.bindExcitation(source);
|
||||
|
||||
source.add({
|
||||
excited(payload: number) {
|
||||
upCount.value += 1;
|
||||
upPayload.value = payload;
|
||||
}
|
||||
});
|
||||
|
||||
splitter.add({
|
||||
excited(payload: number) {
|
||||
downCount.value += 1;
|
||||
downPayload.value = payload;
|
||||
}
|
||||
});
|
||||
|
||||
const changeDivider = (value: number) => {
|
||||
splitter.setDivider(value);
|
||||
divider.value = value;
|
||||
};
|
||||
|
||||
onUnmounted(() => {
|
||||
splitter.unbindExcitation();
|
||||
splitter.destroy();
|
||||
source.destroy();
|
||||
});
|
||||
|
||||
const options = [1, 2, 3, 5];
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text
|
||||
loc={[24, 16]}
|
||||
text="ExcitationDivider:输入与转发次数"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 40]}
|
||||
text="分频器只转发部分激励,负载本身不变"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<text loc={[40, 76]} text="输入源原始载荷" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[40, 102]}
|
||||
text={`${Math.round(upPayload.value)} ms`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
<text loc={[40, 146]} text="输入源次数" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[40, 172]}
|
||||
text={String(upCount.value)}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<text loc={[260, 76]} text="输出端收到的载荷" fillStyle="#f2b84b" />
|
||||
<text
|
||||
loc={[260, 102]}
|
||||
text={`${Math.round(downPayload.value)} ms`}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
<text loc={[260, 146]} text="输出端次数" fillStyle="#f2b84b" />
|
||||
<text
|
||||
loc={[260, 172]}
|
||||
text={String(downCount.value)}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
|
||||
<text loc={[40, 210]} text="当前分频比" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[40, 236]}
|
||||
text={`每 ${divider.value} 次输入 → 1 次输出`}
|
||||
fillStyle="#7ee787"
|
||||
/>
|
||||
|
||||
{options.map((value, index) => {
|
||||
const active = divider.value === value;
|
||||
|
||||
return (
|
||||
<container
|
||||
key={value}
|
||||
loc={[40 + index * 72, 270, 64, 32]}
|
||||
onClick={() => changeDivider(value)}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, 64, 32]}
|
||||
circle={[8]}
|
||||
fillStyle={active ? '#2f6fed' : '#33465f'}
|
||||
/>
|
||||
<text
|
||||
loc={[20, 8]}
|
||||
text={`÷${value}`}
|
||||
fillStyle="#ffffff"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
})}
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,84 @@
|
||||
import { RafExcitation } from '@motajs/animate';
|
||||
import { defineComponent, onUnmounted, ref } from 'vue';
|
||||
|
||||
// 载荷演示:RafExcitation 每帧广播当前时间戳
|
||||
export default defineComponent(() => {
|
||||
const now = ref(0);
|
||||
const start = ref(0);
|
||||
const frames = ref(0);
|
||||
const frameGap = ref(0);
|
||||
|
||||
let last = 0;
|
||||
let hasLast = false;
|
||||
|
||||
const source = new RafExcitation();
|
||||
source.add({
|
||||
excited(payload: number) {
|
||||
if (frames.value === 0) start.value = payload;
|
||||
if (hasLast) frameGap.value = payload - last;
|
||||
last = payload;
|
||||
hasLast = true;
|
||||
now.value = payload;
|
||||
frames.value += 1;
|
||||
}
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
source.destroy();
|
||||
});
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text
|
||||
loc={[24, 16]}
|
||||
text="RafExcitation:当前载荷"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 40]}
|
||||
text="每帧广播 requestAnimationFrame 的时间戳"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<text loc={[40, 84]} text="当前载荷" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[40, 112]}
|
||||
text={`${Math.round(now.value)} ms`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<text loc={[260, 84]} text="帧间隔" fillStyle="#f2b84b" />
|
||||
<text
|
||||
loc={[260, 112]}
|
||||
text={`${frameGap.value.toFixed(1)} ms`}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
|
||||
<text loc={[40, 164]} text="距开始" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[40, 192]}
|
||||
text={`${Math.round(now.value - start.value)} ms`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<text loc={[260, 164]} text="帧数" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[260, 192]}
|
||||
text={String(frames.value)}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<text
|
||||
loc={[40, 248]}
|
||||
text="帧间隔 = 本次时间戳 − 上一次时间戳"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
<text
|
||||
loc={[40, 278]}
|
||||
text="它反映相邻两帧之间过了多久"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,131 @@
|
||||
import { ExcitationVariator, RafExcitation } from '@motajs/animate';
|
||||
import { defineComponent, onUnmounted, ref } from 'vue';
|
||||
|
||||
// 变速器:输出端连续两次时间的差值
|
||||
export default defineComponent(() => {
|
||||
const speed = ref(1);
|
||||
const inputNow = ref(0);
|
||||
const prevOut = ref(0);
|
||||
const currOut = ref(0);
|
||||
const gap = ref(0);
|
||||
const hasPrev = ref(false);
|
||||
|
||||
const source = new RafExcitation();
|
||||
const variator = new ExcitationVariator();
|
||||
variator.bindExcitation(source);
|
||||
|
||||
let lastOut = 0;
|
||||
|
||||
source.add({
|
||||
excited(payload: number) {
|
||||
inputNow.value = payload;
|
||||
}
|
||||
});
|
||||
|
||||
variator.add({
|
||||
excited(payload: number) {
|
||||
if (hasPrev.value) {
|
||||
prevOut.value = lastOut;
|
||||
gap.value = payload - lastOut;
|
||||
}
|
||||
currOut.value = payload;
|
||||
lastOut = payload;
|
||||
hasPrev.value = true;
|
||||
speed.value = variator.speed;
|
||||
}
|
||||
});
|
||||
|
||||
const changeSpeed = (value: number) => {
|
||||
variator.setSpeed(value);
|
||||
speed.value = value;
|
||||
};
|
||||
|
||||
onUnmounted(() => {
|
||||
variator.destroy();
|
||||
source.destroy();
|
||||
});
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text
|
||||
loc={[24, 16]}
|
||||
text="ExcitationVariator:输出端连续两次时间"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 40]}
|
||||
text="变速器按速度换算输入源时间,再推进接收方"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<text loc={[40, 78]} text="输入源当前时间" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[40, 102]}
|
||||
text={`${Math.round(inputNow.value)} ms`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<g-rect loc={[40, 130, 400, 104]} fillStyle="#1b2433" />
|
||||
|
||||
<text loc={[52, 140]} text="上次时间" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[52, 164]}
|
||||
text={hasPrev.value ? `${prevOut.value.toFixed(1)} ms` : '—'}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<text loc={[180, 140]} text="本次时间" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[180, 164]}
|
||||
text={`${currOut.value.toFixed(1)} ms`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<text loc={[300, 140]} text="时间差" fillStyle="#f2b84b" />
|
||||
<text
|
||||
loc={[300, 164]}
|
||||
text={hasPrev.value ? `${gap.value.toFixed(2)} ms` : '—'}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
|
||||
<text
|
||||
loc={[52, 198]}
|
||||
text="速度越高,输出端相邻两次的时间差越大"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<text loc={[40, 240]} text="当前速度" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[128, 240]}
|
||||
text={`${speed.value.toFixed(2)}×`}
|
||||
fillStyle="#7ee787"
|
||||
/>
|
||||
|
||||
<container loc={[40, 274, 88, 32]} onClick={() => changeSpeed(0.5)}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 88, 32]}
|
||||
circle={[8]}
|
||||
fillStyle={speed.value === 0.5 ? '#2f6fed' : '#33465f'}
|
||||
/>
|
||||
<text loc={[18, 8]} text="0.5×" fillStyle="#ffffff" />
|
||||
</container>
|
||||
<container loc={[144, 274, 88, 32]} onClick={() => changeSpeed(1)}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 88, 32]}
|
||||
circle={[8]}
|
||||
fillStyle={speed.value === 1 ? '#2f6fed' : '#33465f'}
|
||||
/>
|
||||
<text loc={[24, 8]} text="1×" fillStyle="#ffffff" />
|
||||
</container>
|
||||
<container loc={[248, 274, 88, 32]} onClick={() => changeSpeed(2)}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 88, 32]}
|
||||
circle={[8]}
|
||||
fillStyle={speed.value === 2 ? '#2f6fed' : '#33465f'}
|
||||
/>
|
||||
<text loc={[18, 8]} text="2×" fillStyle="#ffffff" />
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user