Skip to content

Commit

Permalink
fix: fixed major performance issue caused by react's strict-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Space Corp Engineering authored and gitaarwerk committed Sep 24, 2023
1 parent a7cff69 commit c1faa05
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 37 deletions.
2 changes: 1 addition & 1 deletion dev-environment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@types/react-dom": "18.2.7",
"eslint": "8.49.0",
"eslint-config-next": "13.4.19",
"gleamy": "1.1.4",
"gleamy": "1.1.5",
"next": "13.4.19",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/gleamy/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gleamy",
"description": "Create a reactive shiny element in different materials",
"version": "1.1.4",
"version": "1.1.5",
"license": "MIT",
"private": false,
"sideEffects": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { type FC, useEffect, useCallback, useRef, useContext } from 'react';
import { setDPI } from '../../utils/setDPI';
import { type TStaticGradient, type TDynamicGradient } from '../../../types';
import { GleamyContext } from '../../provider';
import { drawMaterial } from '../../utils/drawMaterial';

export const GradientMaterial: FC<TStaticGradient | TDynamicGradient> = ({
animator,
Expand Down Expand Up @@ -237,11 +236,31 @@ export const GradientMaterial: FC<TStaticGradient | TDynamicGradient> = ({
]);

useEffect(() => {
if (rendering) {
const fps = gleamyProvider?.fps || 60;
drawMaterial({ drawFunction: render, fps });
const fps = gleamyProvider?.fps || 60;

let animationFrameId = 0;
const interval = 1000 / fps;
let now;
let then = Date.now();
let delta;

function draw() {
now = Date.now();
delta = now - then;

if (delta > interval) {
then = now - (delta % interval);
rendering && render();
}
animationFrameId = window.requestAnimationFrame(draw);
}
}, [gleamyProvider, render, rendering]);

draw();

return () => {
window.cancelAnimationFrame(animationFrameId);
};
}, [props]);

return (
<canvas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { type FC, useEffect, useCallback, useContext, useRef } from 'react';
import { setDPI } from '../../utils/setDPI';
import { type TParticle } from '../../../types';
import { GleamyContext } from '../../provider';
import { drawMaterial } from '../../utils/drawMaterial';

interface CreateParticles {
depth: number;
Expand Down Expand Up @@ -251,11 +250,31 @@ export const ParticleMaterial: FC<TParticle> = ({
]);

useEffect(() => {
if (rendering) {
const fps = gleamyProvider?.fps || 60;
drawMaterial({ drawFunction: render, fps });
const fps = gleamyProvider?.fps || 60;

let animationFrameId = 0;
const interval = 1000 / fps;
let now;
let then = Date.now();
let delta;

function draw() {
now = Date.now();
delta = now - then;

if (delta > interval) {
then = now - (delta % interval);
rendering && render();
}
animationFrameId = window.requestAnimationFrame(draw);
}
}, [gleamyProvider, render, rendering]);

draw();

return () => {
window.cancelAnimationFrame(animationFrameId);
};
}, [props]);

return (
<canvas
Expand Down
25 changes: 0 additions & 25 deletions packages/gleamy/src/lib/utils/drawMaterial.ts

This file was deleted.

0 comments on commit c1faa05

Please sign in to comment.