Skip to content

Commit faccba2

Browse files
authored
Merge pull request #16 from flycran/feat/css-animation-engine
feat: CSS 动画引擎
2 parents 42a1755 + 4b58fd2 commit faccba2

15 files changed

Lines changed: 485 additions & 190 deletions

‎README.md‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Inspired by [Orta Therox](https://github.com/Orta). Thanks Orta for your outstan
5050
| `text-color` | ❌ | `black` | Color of the collision text (CSS color value) |
5151
| `scale` | ❌ | `3` | Display zoom factor: the SVG `width`/`height` are the physics dimensions × scale, while the viewBox keeps the raw physics size |
5252
| `speed` | ❌ | `3` | Playback speed multiplier: the animation duration is the simulated time ÷ speed |
53+
| `css` | ❌ | `false` | Use the CSS animation engine instead of SMIL `animateTransform`. See below |
54+
| `animation` | ❌ | `var(--n) var(--dur) linear both` | Custom CSS `animation` value for the CSS engine. ⚠️ Modify with caution — a wrong value breaks the animation. Requires `css: true` |
5355
| `width` | ❌ | `300` | Width of the physics world. ⚠️ Affects layout and SVG size |
5456
| `height` | ❌ | `150` | Height of the physics world. ⚠️ Affects layout and SVG size |
5557
| `size` | ❌ | `4` | Side length of each contribution block. ⚠️ Affects physics |
@@ -60,9 +62,8 @@ Inspired by [Orta Therox](https://github.com/Orta). Thanks Orta for your outstan
6062

6163
The trajectory is compressed by default: `compression-tolerance` (default `0.5`) merges
6264
trajectory points into keyframes so that, at every original moment, the interpolated
63-
position stays within the given distance (in SVG pixels) of the real one. Each keyframe
64-
keeps its original timing via `keyTimes`, so speed changes — bounces, landings, stops —
65-
play back at their real pace.
65+
position stays within the given distance (in SVG pixels) of the real one — speed changes
66+
such as bounces, landings and stops keep their real pace.
6667

6768
The default is invisible to the eye and already shrinks the output by orders of magnitude,
6869
so most setups can keep it. Set `0` for a lossless trajectory — only uniform straight
@@ -83,6 +84,30 @@ points each at the default tolerance `0.5`:
8384

8485
In most cases, just keep the default.
8586

87+
### Animation engine
88+
89+
Two output engines are available; both produce a single static SVG file and play the
90+
same trajectory at the same pace:
91+
92+
- **SMIL** (default) drives every block with `<animateTransform>` elements.
93+
- **CSS** (`css: true`) drops those elements and declares all animations in one
94+
self-contained `<style>` block: each block's trajectory becomes a `@keyframes` rule.
95+
96+
The rendered animation is identical. Choose CSS when the target environment handles
97+
SMIL poorly — some sanitizers strip SMIL animation elements, while CSS animation in
98+
SVG is more widely supported (GitHub READMEs load SVGs via `<img>`, where both work).
99+
The CSS output is slightly larger than SMIL.
100+
101+
With `css: true`, the `animation` input replaces the animation declaration applied to
102+
every block (default `var(--n) var(--dur) linear both`). Available variables:
103+
104+
- `--n` — each block's keyframes name, required
105+
- `--dur` — the playback duration
106+
107+
⚠️ This value is spliced into the generated style block as-is — modify with caution,
108+
a wrong value breaks the animation. Verify in the web preview before committing.
109+
Must not contain braces, semicolons or `]]>`.
110+
86111
### Advanced customization
87112

88113
`width`, `height`, `size` and `interval` change the physics world itself, not just the output

‎README_zh.md‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
| `text-color` | ❌ | `black` | 碰撞文字颜色(CSS 颜色值) |
5050
| `scale` | ❌ | `3` | 显示缩放:SVG 的 `width`/`height` 为物理尺寸 × 缩放值,viewBox 保持原始物理尺寸 |
5151
| `speed` | ❌ | `3` | 播放倍速:动画时长 = 模拟时间 ÷ 倍速 |
52+
| `css` | ❌ | `false` | 启用 CSS 动画引擎替代 SMIL `animateTransform`,见下文 |
53+
| `animation` | ❌ | `var(--n) var(--dur) linear both` | 自定义 CSS `animation` 值(仅 `css: true` 时有效)。⚠️ 谨慎修改,可能破坏动画效果 |
5254
| `width` | ❌ | `300` | 物理世界宽度。⚠️ 会影响布局与 SVG 尺寸 |
5355
| `height` | ❌ | `150` | 物理世界高度。⚠️ 会影响布局与 SVG 尺寸 |
5456
| `size` | ❌ | `4` | 贡献方块边长。⚠️ 会影响物理表现 |
@@ -58,8 +60,8 @@
5860
### 减小文件体积
5961

6062
轨迹默认开启压缩:`compression-tolerance`(默认 `0.5`)会把轨迹点合并为关键帧,保证播放时
61-
每个原始时刻的插值位置与真实位置的偏差(SVG 像素)不超过阈值,并通过 `keyTimes` 保留每个
62-
关键帧的原始时间——碰撞、落地、停顿等速度变化都会保留为关键帧,按真实节奏播放。
63+
每个原始时刻的插值位置与真实位置的偏差(SVG 像素)不超过阈值——碰撞、落地、停顿等速度
64+
变化按真实节奏保留。
6365

6466
默认值肉眼不可见,体积已经能小几个数量级,多数场景保留默认即可。设为 `0` 得到无损轨迹——
6567
只有匀速直线段和静止段会被压缩;调大(如 `1.5`)压缩更狠,但轨迹会明显走样。
@@ -77,6 +79,27 @@
7779

7880
通常保持默认值即可。
7981

82+
### 动画引擎
83+
84+
提供两种输出引擎;两者都输出单个静态 SVG 文件,播放轨迹与节奏完全一致:
85+
86+
- **SMIL**(默认):每个方块由 `<animateTransform>` 元素驱动。
87+
- **CSS**(`css: true`):不再输出 animate 元素,所有动画声明收敛进单个自包含的
88+
`<style>` 块,每个方块的轨迹生成一条 `@keyframes` 规则。
89+
90+
渲染出的动画完全一致。当目标环境对 SMIL 支持不佳时选 CSS——部分 sanitizer 会剥除
91+
SMIL 动画元素,而 SVG 内的 CSS 动画支持面更广(GitHub README 以 `<img>` 加载 SVG,
92+
两种引擎都能正常播放)。文件体积略大于 SMIL。
93+
94+
`css: true` 时,`animation` 输入可替换作用在每个方块上的 animation 声明,默认
95+
`var(--n) var(--dur) linear both`。可用变量:
96+
97+
- `--n`:方块绑定的关键帧名,必须包含
98+
- `--dur`:播放时长
99+
100+
⚠️ 该值会原样拼进生成的 style 块,谨慎修改,可能破坏动画效果,修改前先在
101+
web preview 里验证。值中不能包含花括号、分号或 `]]>`。
102+
80103
### 高级自定义
81104

82105
`width`、`height`、`size`、`interval` 改动的是物理世界本身,而不只是输出尺寸。贡献方块按

‎__tests__/integration.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,52 @@ describe('full pipeline integration', () => {
297297
).rejects.toThrow('Failed to load font "/nonexistent/font.ttf"')
298298
})
299299
})
300+
301+
describe('css animation engine', () => {
302+
beforeEach(() => {
303+
mockFetchContributions.mockImplementation(async () =>
304+
mockContributionsResponse()
305+
)
306+
})
307+
308+
afterEach(() => {
309+
mockFetchContributions.mockReset()
310+
})
311+
312+
// 只冒烟引擎切换本身;动画的具体声明失效会直接表现为不动/走样,浏览器里肉眼可见
313+
it('renders with a self-contained style block instead of animateTransform', async () => {
314+
const svg = await gensvg({
315+
username: 'testuser',
316+
text: 'Hi',
317+
fontSize: 40,
318+
css: true
319+
})
320+
321+
expect(svg).toContain('<style>')
322+
expect(svg).not.toContain('animateTransform')
323+
})
324+
325+
it('applies a custom animation value', async () => {
326+
const svg = await gensvg({
327+
username: 'testuser',
328+
text: 'Hi',
329+
fontSize: 40,
330+
css: true,
331+
animation: 'none'
332+
})
333+
334+
expect(svg).toContain('animation:none')
335+
})
336+
337+
it('rejects animation values that would break the style block', async () => {
338+
await expect(
339+
gensvg({
340+
username: 'testuser',
341+
text: 'Hi',
342+
fontSize: 40,
343+
css: true,
344+
animation: '2s;color:red'
345+
})
346+
).rejects.toThrow('Invalid animation')
347+
})
348+
})

‎__tests__/main.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ const INPUT_NAMES = [
3333
'TEXT-TOP',
3434
'BACKGROUND-COLOR',
3535
'TEXT-COLOR',
36+
'CSS',
37+
'ANIMATION',
3638
'WIDTH',
3739
'HEIGHT',
3840
'SIZE',
@@ -98,6 +100,20 @@ describe('action input plumbing', () => {
98100
expect(captured?.size).toBeUndefined()
99101
expect(captured?.interval).toBeUndefined()
100102
expect(captured?.font).toBeUndefined()
103+
// 开关型输入未填写时默认关闭
104+
expect(captured?.css).toBe(false)
105+
expect(captured?.animation).toBeUndefined()
106+
})
107+
108+
it('forwards the css engine switch and custom animation', async () => {
109+
setInputs({ CSS: 'true', ANIMATION: 'var(--n) 2s linear' })
110+
111+
await run()
112+
113+
expect(captured).toMatchObject({
114+
css: true,
115+
animation: 'var(--n) 2s linear'
116+
})
101117
})
102118

103119
it('forwards compression options and world geometry as numbers', async () => {

‎action.yml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ inputs:
7373
divided by it.
7474
required: false
7575
default: '3'
76+
css:
77+
description: >-
78+
Use the CSS animation engine (a self-contained style block with @keyframes,
79+
animations bound via data-date selectors) instead of SMIL animateTransform.
80+
Both stay a single static SVG file; CSS plays more reliably in environments
81+
with poor SMIL support, and stays visually identical. Default: false.
82+
required: false
83+
default: 'false'
84+
animation:
85+
description: >-
86+
Custom CSS animation value applied to every block. Only effective with css: true.
87+
Default: var(--n) var(--dur) linear both. Available variables: --n is the
88+
block's keyframes name, --dur is the playback duration. Modify with caution:
89+
a wrong value breaks the animation effect. Must not contain braces,
90+
semicolons or "]]>".
91+
required: false
7692
width:
7793
description: >-
7894
Width of the physics world. Change with caution: the horizontal block layout, the text

0 commit comments

Comments
 (0)