Skip to content

Commit 56306c0

Browse files
authored
feat: add VFXProps.zIndex (#80)
* feat: add VFXProps.zIndex * chore: improve doc comments
1 parent d454e68 commit 56306c0

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

packages/vfx-js/src/types.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export type VFXProps = {
4242
shader?: ShaderPreset | string;
4343

4444
/**
45-
* The release time for the element.
45+
* The release time for the element. (Default: `0`)
4646
*
4747
* Basically, VFX-JS starts rendering the element when the element entered the viewport,
4848
* and it stops rendering after it got out of the viewport by scroll etc.
@@ -86,7 +86,7 @@ export type VFXProps = {
8686
uniforms?: VFXUniforms;
8787

8888
/**
89-
* The opacity for the original HTML element.
89+
* The opacity for the original HTML element. (Default: `false`)
9090
*
9191
* By default, VFX-JS hides the original element by setting its opacity to 0.
9292
* However, in some cases you might want not to hide the original element.
@@ -100,7 +100,8 @@ export type VFXProps = {
100100
overlay?: true | number;
101101

102102
/**
103-
* Allow shader outputs to oveflow the original element area.
103+
* Allow shader outputs to oveflow the original element area. (Default: `0`)
104+
*
104105
* If true, REACT-VFX will render the shader in fullscreen.
105106
* If number is specified, REACT-VFX adds paddings with the given value.
106107
*
@@ -119,14 +120,23 @@ export type VFXProps = {
119120
| { top?: number; right?: number; bottom?: number; left?: number };
120121

121122
/**
122-
* Texture wrapping mode.
123+
* Texture wrapping mode. (Default: `"repeat"`)
123124
*
124125
* You can pass a single value to specify both horizontal and vertical wrapping at once,
125126
* or you can provide a tuple to spefify different modes for horizontal / vertical wrapping.
126127
*
127128
* If not provided, VFX-JS will use "repeat" mode for both horizontal and vertical wrapping.
128129
*/
129130
wrap?: VFXWrap | [VFXWrap, VFXWrap];
131+
132+
/**
133+
* Z-index inside WebGL world. (Default: `0`)
134+
*
135+
* VFX-JS renders elements in ascending order by `zIndex`.
136+
* For example, when we have elements with `zIndex: 1` and `zIndex: -1`, the second element is rendered first.
137+
* When elements have the same `zIndex`, they are rendered in the order they were added.
138+
*/
139+
zIndex?: number;
130140
};
131141

132142
/**
@@ -176,6 +186,7 @@ export type VFXElement = {
176186
isGif: boolean;
177187
overflow: VFXElementOverflow;
178188
originalOpacity: number;
189+
zIndex: number;
179190
};
180191

181192
/**

packages/vfx-js/src/vfx-player.ts

+5
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,14 @@ export class VFXPlayer {
284284
isGif,
285285
overflow,
286286
originalOpacity,
287+
zIndex: opts.zIndex ?? 0,
287288
};
288289

290+
// Insert element and sort elements by z-index.
291+
// Array.prototype.sort is stable sort, so the elements with same z
292+
// will be rendered by the order they are added to VFX.
289293
this.#elements.push(elem);
294+
this.#elements.sort((a, b) => a.zIndex - b.zIndex);
290295
}
291296

292297
removeElement(element: HTMLElement): void {

0 commit comments

Comments
 (0)