-
Notifications
You must be signed in to change notification settings - Fork 122
feat(web-platform): Implement __ElementAnimate PAPI #2329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
581bc4c
172bcc2
99ee694
e7a709a
c11128f
0bf1eaa
12264f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| // Demonstrates: __ElementAnimate — Web Animations API bridge | ||
| // | ||
| // __ElementAnimate(element, [operation, name, keyframes?, options?]) | ||
| // operation: 0=START, 1=PLAY, 2=PAUSE, 3=CANCEL, 4=FINISH | ||
| // | ||
| // Tap the buttons to control the animation. | ||
|
|
||
| globalThis.renderPage = function renderPage() { | ||
| const page = __CreatePage('page', 0); | ||
| const container = __CreateView(0); | ||
| __AppendElement(page, container); | ||
| __SetInlineStyles( | ||
| container, | ||
| 'padding:40px; align-items:center; gap:20px;', | ||
| ); | ||
|
|
||
| const title = __CreateText(0); | ||
| __AppendElement(container, title); | ||
| __AppendElement(title, __CreateRawText('__ElementAnimate')); | ||
| __SetInlineStyles( | ||
| title, | ||
| 'font-size:18px; font-weight:700; margin-bottom:4px;', | ||
| ); | ||
|
|
||
| const subtitle = __CreateText(0); | ||
| __AppendElement(container, subtitle); | ||
| __AppendElement( | ||
| subtitle, | ||
| __CreateRawText('Controls the Web Animations API from the main thread'), | ||
| ); | ||
| __SetInlineStyles( | ||
| subtitle, | ||
| 'font-size:13px; color:#888; margin-bottom:16px;', | ||
| ); | ||
|
|
||
| // Animated box | ||
| const box = __CreateView(0); | ||
| __AppendElement(container, box); | ||
| __SetInlineStyles( | ||
| box, | ||
| 'width:120px; height:120px; background-color:#3b82f6; border-radius:16px; margin-bottom:16px;', | ||
| ); | ||
|
|
||
| // Start a looping pulse animation | ||
| const animName = 'demo-pulse'; | ||
| __ElementAnimate(box, [ | ||
| 0, // START | ||
| animName, | ||
| [ | ||
| { opacity: 1, transform: 'scale(1)' }, | ||
| { opacity: 0.5, transform: 'scale(0.8)' }, | ||
| { opacity: 1, transform: 'scale(1)' }, | ||
| ], | ||
| { | ||
| duration: 1500, | ||
| iterationCount: 'infinite', | ||
| timingFunction: 'ease-in-out', | ||
| }, | ||
| ]); | ||
|
|
||
| // Status text | ||
| const statusRaw = __CreateRawText('Playing'); | ||
| const statusLabel = __CreateText(0); | ||
| __AppendElement(container, statusLabel); | ||
| __AppendElement(statusLabel, statusRaw); | ||
| __SetInlineStyles( | ||
| statusLabel, | ||
| 'font-size:14px; color:#666; margin-bottom:16px;', | ||
| ); | ||
|
|
||
| // Worklet handler router | ||
| const handlers = {}; | ||
| globalThis.runWorklet = function(handlerId, args) { | ||
| if (handlers[handlerId]) handlers[handlerId](...args); | ||
| }; | ||
|
|
||
| // Button row | ||
| const btnRow = __CreateView(0); | ||
| __AppendElement(container, btnRow); | ||
| __SetInlineStyles(btnRow, 'flex-direction:row; gap:10px;'); | ||
|
|
||
| function makeButton(label, handlerId) { | ||
| const btn = __CreateView(0); | ||
| __AppendElement(btnRow, btn); | ||
| __SetInlineStyles( | ||
| btn, | ||
| 'padding:8px 18px; background-color:#1e293b; border-radius:6px; align-items:center; justify-content:center;', | ||
| ); | ||
| const txt = __CreateText(0); | ||
| __AppendElement(btn, txt); | ||
| __AppendElement(txt, __CreateRawText(label)); | ||
| __SetInlineStyles(txt, 'color:#fff; font-size:13px;'); | ||
| __AddEvent(btn, 'bindEvent', 'tap', { | ||
| type: 'worklet', | ||
| value: handlerId, | ||
| }); | ||
| } | ||
|
|
||
| handlers['onPause'] = function() { | ||
| __ElementAnimate(box, [2, /* PAUSE */ animName]); | ||
| __SetAttribute(statusRaw, 'text', 'Paused'); | ||
| __FlushElementTree(); | ||
| }; | ||
|
|
||
| handlers['onPlay'] = function() { | ||
| __ElementAnimate(box, [1, /* PLAY */ animName]); | ||
| __SetAttribute(statusRaw, 'text', 'Playing'); | ||
| __FlushElementTree(); | ||
| }; | ||
|
|
||
| handlers['onCancel'] = function() { | ||
| __ElementAnimate(box, [3, /* CANCEL */ animName]); | ||
| __SetAttribute(statusRaw, 'text', 'Cancelled'); | ||
| __FlushElementTree(); | ||
| }; | ||
|
|
||
| handlers['onRestart'] = function() { | ||
| __ElementAnimate(box, [ | ||
| 0, /* START */ | ||
| animName, | ||
| [ | ||
| { opacity: 1, transform: 'scale(1)' }, | ||
| { opacity: 0.5, transform: 'scale(0.8)' }, | ||
| { opacity: 1, transform: 'scale(1)' }, | ||
| ], | ||
| { | ||
| duration: 1500, | ||
| iterationCount: 'infinite', | ||
| timingFunction: 'ease-in-out', | ||
| }, | ||
| ]); | ||
| __SetAttribute(statusRaw, 'text', 'Playing'); | ||
| __FlushElementTree(); | ||
| }; | ||
|
|
||
| makeButton('Pause', 'onPause'); | ||
| makeButton('Play', 'onPlay'); | ||
| makeButton('Cancel', 'onCancel'); | ||
| makeButton('Restart', 'onRestart'); | ||
|
|
||
| __FlushElementTree(); | ||
| }; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -328,6 +328,18 @@ export type QuerySelectorPAPI = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| selector: string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) => unknown; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| export type ElementAnimatePAPI = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| element: HTMLElement, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| operation: number, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| keyframes: Record<string, string | number>[], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| options?: Record<string, string | number>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | [operation: number, name: string], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| export type ElementAnimatePAPI = ( | |
| element: HTMLElement, | |
| args: | |
| | [ | |
| operation: number, | |
| name: string, | |
| keyframes: Record<string, string | number>[], | |
| options?: Record<string, string | number>, | |
| ] | |
| | [operation: number, name: string], | |
| export enum AnimationOperation { | |
| Start = 0, | |
| Cancel = 1, | |
| } | |
| export type ElementAnimatePAPI = ( | |
| element: HTMLElement, | |
| args: | |
| | [ | |
| operation: AnimationOperation.Start, | |
| name: string, | |
| keyframes: Record<string, string | number>[], | |
| options?: Record<string, string | number>, | |
| ] | |
| | [operation: AnimationOperation.Cancel, name: string], |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -64,6 +64,8 @@ import { | |||||||||||||||||||||||||||||||||
| ErrorCode, | ||||||||||||||||||||||||||||||||||
| type QuerySelectorPAPI, | ||||||||||||||||||||||||||||||||||
| type InvokeUIMethodPAPI, | ||||||||||||||||||||||||||||||||||
| type ElementAnimatePAPI, | ||||||||||||||||||||||||||||||||||
| AnimationOperation, | ||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||
| } from '@lynx-js/web-constants'; | ||||||||||||||||||||||||||||||||||
| import { createMainThreadLynx } from './createMainThreadLynx.js'; | ||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||
|
|
@@ -781,6 +783,60 @@ export function createMainThreadGlobalThis( | |||||||||||||||||||||||||||||||||
| return el; | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const animationMap = new Map<string, globalThis.Animation | undefined>(); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| const animationMap = new Map<string, globalThis.Animation | undefined>(); | |
| const animationMap = new Map<string, Animation | undefined>(); |
Copilot
AI
Mar 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On START, an existing animation with the same name is overwritten in animationMap without being cancelled first. This means repeated START calls (e.g. the REPL sample's Restart button) will leave the previous animation running and create multiple concurrent animations for the same logical id. Cancel any existing animation for name before starting the new one (and consider replacing the entry only after that).
| animationMap.set( | |
| name, | |
| element.animate( | |
| keyframes as Keyframe[], | |
| mapTimingOptions(options), | |
| ), | |
| ); | |
| const existingAnimation = animationMap.get(name); | |
| if (existingAnimation) { | |
| existingAnimation.cancel(); | |
| } | |
| const newAnimation = element.animate( | |
| keyframes as Keyframe[], | |
| mapTimingOptions(options), | |
| ); | |
| animationMap.set(name, newAnimation); |
Copilot
AI
Mar 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CANCEL calls animation.cancel() but does not remove the entry from animationMap, leaving a stale reference and causing the map to grow unbounded over time. Delete the map entry after cancelling (and consider also removing entries on finish/onfinish to avoid leaks when animations end naturally).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The testing environment hard-codes operation numbers (0..4) inside
__ElementAnimate. Since the web platform uses theAnimationOperationenum, importing and using the same enum here will prevent drift when operations are added/renumbered and make the tests more self-documenting.