Skip to content

Commit 960213a

Browse files
committed
Performance: disable sparkles on emoji status
1 parent f5bedb9 commit 960213a

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/components/sparkles.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ export interface Sparkle {
1010
delay: number;
1111
}
1212

13+
interface ContainerSize {
14+
width: number;
15+
height: number;
16+
}
17+
1318
const BUTTON_SPARKLES: Sparkle[] = [
1419
{x: 20, y: 0, scale: 1, delay: 500},
1520
{x: 15, y: 15, scale: 0.75, delay: 3500},
@@ -41,7 +46,7 @@ export function generateProgressSparkle(): Sparkle {
4146

4247

4348
const sparkleTemplate = template(`<svg viewBox="0 0 7 7" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em"><use href="#star-sparkle"></use></svg>`);
44-
function renderSparkle(sparkle: Sparkle, isDiv?: boolean) {
49+
function renderSparkle(sparkle: Sparkle, isDiv?: boolean, containerSize?: ContainerSize) {
4550
let element: HTMLElement;
4651
if(isDiv) {
4752
element = document.createElement('div');
@@ -51,31 +56,34 @@ function renderSparkle(sparkle: Sparkle, isDiv?: boolean) {
5156
}
5257

5358
element.classList.add('sparkles-sparkle');
54-
element.style.setProperty('--sparkle-tx', sparkle.translateX + '%');
55-
element.style.setProperty('--sparkle-ty', sparkle.translateY + '%');
59+
element.style.setProperty('--sparkle-tx', containerSize ? sparkle.translateX / 100 * containerSize.width + 'px' : sparkle.translateX + '%');
60+
element.style.setProperty('--sparkle-ty', containerSize ? sparkle.translateY / 100 * containerSize.height + 'px' : sparkle.translateY + '%');
5661
element.style.setProperty('--sparkle-scale', sparkle.scale * (Math.random() * 0.5 + 0.25) + '');
5762
element.style.setProperty('--sparkle-rotate', (Math.random() * 90 - 45) * 4 + 'deg');
5863
element.style.transform = `scale(${sparkle.scale})`;
59-
element.style.top = sparkle.y + '%';
60-
element.style.left = sparkle.x + '%';
64+
element.style.top = containerSize ? sparkle.y / 100 * containerSize.height + 'px' : sparkle.y + '%';
65+
element.style.left = containerSize ? sparkle.x / 100 * containerSize.width + 'px' : sparkle.x + '%';
6166
element.style.animationDelay = '-' + sparkle.delay + 'ms';
6267
return element;
6368
}
6469

65-
export type SparklesProps = {isDiv?: boolean} & ({mode: 'button'} | {mode: 'progress', count: number});
70+
export type SparklesProps = {
71+
isDiv?: boolean,
72+
containerSize?: ContainerSize
73+
} & ({mode: 'button'} | {mode: 'progress', count: number});
6674

6775
export function Sparkles(props: SparklesProps): HTMLDivElement {
6876
const container = document.createElement('div');
6977
container.classList.add('sparkles-container');
7078

7179
if(props.mode === 'button') {
72-
container.append(...BUTTON_SPARKLES.map((sparkle) => renderSparkle(sparkle, props.isDiv)));
80+
container.append(...BUTTON_SPARKLES.map((sparkle) => renderSparkle(sparkle, props.isDiv, props.containerSize)));
7381
} else {
7482
createEffect(on(() => props.count, (next, prev = 0) => {
7583
const diff = next - prev;
7684
if(diff > 0) {
7785
for(let i = 0; i < diff; i++) {
78-
container.appendChild(renderSparkle(generateProgressSparkle(), props.isDiv));
86+
container.appendChild(renderSparkle(generateProgressSparkle(), props.isDiv, props.containerSize));
7987
}
8088
} else if(diff < 0) {
8189
for(let i = 0; i < -diff; i++) {

src/components/wrappers/emojiStatus.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import rootScope from '../../lib/rootScope';
1313
import {Sparkles} from '../sparkles';
1414
import wrapSticker from './sticker';
1515

16+
const RENDER_SPARKLES = false; // performance issues
17+
1618
export default async function wrapEmojiStatus({
1719
wrapOptions,
1820
emojiStatus,
@@ -54,8 +56,12 @@ export default async function wrapEmojiStatus({
5456
await Promise.all(loadPromises);
5557
};
5658

57-
if(emojiStatus._ === 'emojiStatusCollectible') {
58-
container.appendChild(Sparkles({mode: 'button', isDiv: true}));
59+
if(emojiStatus._ === 'emojiStatusCollectible' && RENDER_SPARKLES) {
60+
container.appendChild(Sparkles({
61+
mode: 'button',
62+
isDiv: true,
63+
containerSize: size
64+
}));
5965
container.style.setProperty('--sparkles-color', rgbIntToHex(emojiStatus.center_color));
6066
}
6167

0 commit comments

Comments
 (0)