test: testing apng

This commit is contained in:
gofnnp 2023-09-19 22:33:35 +04:00
parent 0616191f78
commit b6340b360a
4 changed files with 50 additions and 12 deletions

11
package-lock.json generated
View File

@ -10,6 +10,7 @@
"dependencies": { "dependencies": {
"@chargebee/chargebee-js-react-wrapper": "^0.6.3", "@chargebee/chargebee-js-react-wrapper": "^0.6.3",
"@reduxjs/toolkit": "^1.9.5", "@reduxjs/toolkit": "^1.9.5",
"apng-js": "^1.1.1",
"html-react-parser": "^3.0.16", "html-react-parser": "^3.0.16",
"i18next": "^22.5.0", "i18next": "^22.5.0",
"i18next-react-postprocessor": "^3.1.0", "i18next-react-postprocessor": "^3.1.0",
@ -1351,6 +1352,11 @@
"node": ">=4" "node": ">=4"
} }
}, },
"node_modules/apng-js": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/apng-js/-/apng-js-1.1.1.tgz",
"integrity": "sha512-UWaloDssWCE8Bj0wipyNxEXPnMadYS0VAjghCLas5nKGqfiBMNdQJhg8Fawq2+jZ50IOM1feKwjiqPAC/bvKgg=="
},
"node_modules/argparse": { "node_modules/argparse": {
"version": "2.0.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
@ -4245,6 +4251,11 @@
"color-convert": "^1.9.0" "color-convert": "^1.9.0"
} }
}, },
"apng-js": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/apng-js/-/apng-js-1.1.1.tgz",
"integrity": "sha512-UWaloDssWCE8Bj0wipyNxEXPnMadYS0VAjghCLas5nKGqfiBMNdQJhg8Fawq2+jZ50IOM1feKwjiqPAC/bvKgg=="
},
"argparse": { "argparse": {
"version": "2.0.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",

View File

@ -12,6 +12,7 @@
"dependencies": { "dependencies": {
"@chargebee/chargebee-js-react-wrapper": "^0.6.3", "@chargebee/chargebee-js-react-wrapper": "^0.6.3",
"@reduxjs/toolkit": "^1.9.5", "@reduxjs/toolkit": "^1.9.5",
"apng-js": "^1.1.1",
"html-react-parser": "^3.0.16", "html-react-parser": "^3.0.16",
"i18next": "^22.5.0", "i18next": "^22.5.0",
"i18next-react-postprocessor": "^3.1.0", "i18next-react-postprocessor": "^3.1.0",

View File

@ -1,5 +1,5 @@
import styles from "./styles.module.css"; import styles from "./styles.module.css";
import { useCallback, useEffect, useState } from "react"; import { useCallback, useEffect, useRef, useState } from "react";
import { UserCallbacks, useApi, useApiCall } from "@/api"; import { UserCallbacks, useApi, useApiCall } from "@/api";
import { Asset } from "@/api/resources/Assets"; import { Asset } from "@/api/resources/Assets";
import { useDispatch } from "react-redux"; import { useDispatch } from "react-redux";
@ -11,16 +11,24 @@ import StartBreathModalChild from "../StartBreathModalChild";
import Title from "../Title"; import Title from "../Title";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import routes from "@/routes"; import routes from "@/routes";
import parseAPNG from "apng-js";
function BreathPage(): JSX.Element { function BreathPage(): JSX.Element {
const { t } = useTranslation(); const { t } = useTranslation();
const [asset, setAsset] = useState<Asset>(); const [asset, setAsset] = useState<Asset>();
const [isOpenModal, setIsOpenModal] = useState<boolean>(true); const [isOpenModal, setIsOpenModal] = useState<boolean>(true);
const [isShowPreview, setIsShowPreview] = useState<boolean>(true); const [isShowPreview, setIsShowPreview] = useState<boolean>(true);
const leoCanvasRef = useRef<HTMLCanvasElement>(null);
const api = useApi(); const api = useApi();
const dispatch = useDispatch(); const dispatch = useDispatch();
const navigate = useNavigate(); const navigate = useNavigate();
const fetchAPNG = async () => {
const response = await fetch(asset?.url || "");
const arrayBuffer = await response.arrayBuffer();
return parseAPNG(arrayBuffer);
};
const assetsData = useCallback(async () => { const assetsData = useCallback(async () => {
const { assets } = await api.getAssets({ const { assets } = await api.getAssets({
category: String("au"), category: String("au"),
@ -36,26 +44,39 @@ function BreathPage(): JSX.Element {
setIsShowPreview(false); setIsShowPreview(false);
}, 10_000); }, 10_000);
const navigateTimeOut = setTimeout(() => { // const navigateTimeOut = setTimeout(() => {
navigate(routes.client.breathResult()); // navigate(routes.client.breathResult());
}, 60_000); // }, 60_000);
return () => { return () => {
clearTimeout(navigateTimeOut); // clearTimeout(navigateTimeOut);
clearTimeout(previewTimeOut); clearTimeout(previewTimeOut);
}; };
}, [navigate, isOpenModal]); }, [navigate, isOpenModal]);
useEffect(() => { useEffect(() => {
if (!data) return; if (!data) return;
const leoTimeOut = setTimeout(() => { const leoTimeOut = setTimeout(async () => {
setAsset(data[getRandomArbitrary(0, data?.length || 0)]); setAsset(data.find((item) => item.key === "au.apng.leo"));
const apng = await fetchAPNG();
if (apng instanceof Error) {
return;
}
// apng.createImages().then(() => {
// })
const context = leoCanvasRef.current?.getContext("2d");
console.log('context: ', context);
if (context) {
(await apng.getPlayer(context)).pause();
}
}, 9_000); }, 9_000);
return () => { return () => {
clearTimeout(leoTimeOut); clearTimeout(leoTimeOut);
}; };
}, [data, isOpenModal]); }, [data, isOpenModal, fetchAPNG]);
const beginBreath = () => { const beginBreath = () => {
setIsOpenModal(false); setIsOpenModal(false);
@ -107,13 +128,17 @@ function BreathPage(): JSX.Element {
// }} // }}
> >
{!isOpenModal && asset?.url && ( {!isOpenModal && asset?.url && (
<div className={styles["leo-apng-container"]}> <canvas
<img // style={{ backgroundImage: `url(${asset?.url})` }}
className={styles["leo-apng-container"]}
ref={leoCanvasRef}
>
{/* <img
className={styles["leo-apng"]} className={styles["leo-apng"]}
src={asset.url} src={asset.url}
alt="leo apng" alt="leo apng"
/> /> */}
</div> </canvas>
)} )}
{!isOpenModal && isShowPreview && ( {!isOpenModal && isShowPreview && (
<div className={styles.preview}> <div className={styles.preview}>

View File

@ -18,6 +18,7 @@
} }
.leo-apng { .leo-apng {
width: 100%;
animation-name: leo-apng; animation-name: leo-apng;
animation-duration: 1.5s; animation-duration: 1.5s;
animation-iteration-count: 1; animation-iteration-count: 1;