From 7fec68157f006faca1d600ecbe7a075006c22efc Mon Sep 17 00:00:00 2001 From: gofnnp Date: Wed, 20 Sep 2023 01:21:00 +0400 Subject: [PATCH] fix: breath leo apng --- src/components/BreathPage/index.tsx | 93 +++++++++---------- src/components/BreathPage/styles.module.css | 21 +++-- src/components/HomePage/index.tsx | 2 +- .../StartBreathModalChild/styles.module.css | 1 + 4 files changed, 60 insertions(+), 57 deletions(-) diff --git a/src/components/BreathPage/index.tsx b/src/components/BreathPage/index.tsx index 7d65499..4c6d985 100644 --- a/src/components/BreathPage/index.tsx +++ b/src/components/BreathPage/index.tsx @@ -5,30 +5,25 @@ import { Asset } from "@/api/resources/Assets"; import { useDispatch } from "react-redux"; import { actions } from "@/store"; import { useTranslation } from "react-i18next"; -// import { getRandomArbitrary } from "@/services/random-value"; import FullScreenModal from "../FullScreenModal"; import StartBreathModalChild from "../StartBreathModalChild"; import Title from "../Title"; import { useNavigate } from "react-router-dom"; -// import routes from "@/routes"; +import routes from "@/routes"; import parseAPNG from "apng-js"; +import Player from "apng-js/types/library/player"; function BreathPage(): JSX.Element { const { t } = useTranslation(); - const [asset, setAsset] = useState(); const [isOpenModal, setIsOpenModal] = useState(true); const [isShowPreview, setIsShowPreview] = useState(true); + const [apngPlayer, setApngPlayer] = useState(); const leoCanvasRef = useRef(null); + const pageRef = useRef(null); const api = useApi(); const dispatch = useDispatch(); 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 { assets } = await api.getAssets({ category: String("au"), @@ -42,41 +37,51 @@ function BreathPage(): JSX.Element { if (isOpenModal) return; const previewTimeOut = setTimeout(() => { setIsShowPreview(false); + console.log(apngPlayer); + + apngPlayer?.play() }, 10_000); - // const navigateTimeOut = setTimeout(() => { - // navigate(routes.client.breathResult()); - // }, 60_000); + const navigateTimeOut = setTimeout(() => { + navigate(routes.client.breathResult()); + }, 60_000); return () => { - // clearTimeout(navigateTimeOut); + clearTimeout(navigateTimeOut); clearTimeout(previewTimeOut); }; }, [navigate, isOpenModal]); useEffect(() => { - if (!data) return; - const leoTimeOut = setTimeout(async () => { - setAsset(data.find((item) => item.key === "au.apng.leo")); - const apng = await fetchAPNG(); - if (apng instanceof Error) { - return; - } - // apng.createImages().then(() => { + async function test() { + console.log(data); - // }) + if (!data) return; + console.log(1); + + const response = await fetch( + data.find((item) => item.key === "au.apng.leo")?.url || "" + ); + console.log(2); + const arrayBuffer = await response.arrayBuffer(); + console.log(3); + const apng = parseAPNG(arrayBuffer); + console.log(4); const context = leoCanvasRef.current?.getContext("2d"); - console.log('context: ', context); - if (context) { - - (await apng.getPlayer(context)).pause(); + console.log("context: ", context); + if (context && !(apng instanceof Error)) { + context.canvas.height = apng.height; + context.canvas.width = apng.width; + const _apngPlayer = await apng.getPlayer(context); + console.log(5); + setApngPlayer(_apngPlayer); + if (apngPlayer) { + apngPlayer.stop(); + } } - }, 9_000); - - return () => { - clearTimeout(leoTimeOut); - }; - }, [data, isOpenModal, fetchAPNG]); + } + test(); + }, [data]); const beginBreath = () => { setIsOpenModal(false); @@ -123,26 +128,16 @@ function BreathPage(): JSX.Element { <>
- {!isOpenModal && asset?.url && ( - - {/* leo apng */} - - )} + + {!isOpenModal && isShowPreview && (
- leo { if (!asset) return; - download(asset.url, "image.png"); + download(asset.url.replace("http://", "https://"), "image.png"); }; return ( diff --git a/src/components/StartBreathModalChild/styles.module.css b/src/components/StartBreathModalChild/styles.module.css index 8063a55..23053d8 100644 --- a/src/components/StartBreathModalChild/styles.module.css +++ b/src/components/StartBreathModalChild/styles.module.css @@ -26,6 +26,7 @@ .symbol { opacity: 0; + will-change: opacity; animation-name: appearance; animation-timing-function: ease; animation-duration: .3s;