fix: breath leo apng

This commit is contained in:
gofnnp 2023-09-20 01:21:00 +04:00
parent a7a95d7b31
commit 7fec68157f
4 changed files with 60 additions and 57 deletions

View File

@ -5,30 +5,25 @@ import { Asset } from "@/api/resources/Assets";
import { useDispatch } from "react-redux"; import { useDispatch } from "react-redux";
import { actions } from "@/store"; import { actions } from "@/store";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
// import { getRandomArbitrary } from "@/services/random-value";
import FullScreenModal from "../FullScreenModal"; import FullScreenModal from "../FullScreenModal";
import StartBreathModalChild from "../StartBreathModalChild"; 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"; import parseAPNG from "apng-js";
import Player from "apng-js/types/library/player";
function BreathPage(): JSX.Element { function BreathPage(): JSX.Element {
const { t } = useTranslation(); const { t } = useTranslation();
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 [apngPlayer, setApngPlayer] = useState<Player>();
const leoCanvasRef = useRef<HTMLCanvasElement>(null); const leoCanvasRef = useRef<HTMLCanvasElement>(null);
const pageRef = useRef<HTMLElement>(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"),
@ -42,41 +37,51 @@ function BreathPage(): JSX.Element {
if (isOpenModal) return; if (isOpenModal) return;
const previewTimeOut = setTimeout(() => { const previewTimeOut = setTimeout(() => {
setIsShowPreview(false); setIsShowPreview(false);
console.log(apngPlayer);
apngPlayer?.play()
}, 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; async function test() {
const leoTimeOut = setTimeout(async () => { console.log(data);
setAsset(data.find((item) => item.key === "au.apng.leo"));
const apng = await fetchAPNG();
if (apng instanceof Error) {
return;
}
// apng.createImages().then(() => {
// }) 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"); const context = leoCanvasRef.current?.getContext("2d");
console.log('context: ', context); console.log("context: ", context);
if (context) { if (context && !(apng instanceof Error)) {
context.canvas.height = apng.height;
(await apng.getPlayer(context)).pause(); context.canvas.width = apng.width;
const _apngPlayer = await apng.getPlayer(context);
console.log(5);
setApngPlayer(_apngPlayer);
if (apngPlayer) {
apngPlayer.stop();
}
} }
}, 9_000); }
test();
return () => { }, [data]);
clearTimeout(leoTimeOut);
};
}, [data, isOpenModal, fetchAPNG]);
const beginBreath = () => { const beginBreath = () => {
setIsOpenModal(false); setIsOpenModal(false);
@ -123,26 +128,16 @@ function BreathPage(): JSX.Element {
<> <>
<section <section
className={`${styles.page} page`} className={`${styles.page} page`}
// style={{ ref={pageRef}
// backgroundImage: `url(${!isOpenModal ? asset?.url : "none"})`,
// }}
> >
{!isOpenModal && asset?.url && ( <canvas
<canvas style={{ display: isOpenModal ? "hidden" : "block" }}
// style={{ backgroundImage: `url(${asset?.url})` }} className={`${styles["leo-apng"]} ${!isOpenModal && isShowPreview ? styles.leo : ''}`}
className={styles["leo-apng-container"]} ref={leoCanvasRef}
ref={leoCanvasRef} >
> </canvas>
{/* <img
className={styles["leo-apng"]}
src={asset.url}
alt="leo apng"
/> */}
</canvas>
)}
{!isOpenModal && isShowPreview && ( {!isOpenModal && isShowPreview && (
<div className={styles.preview}> <div className={styles.preview}>
<img className={styles.leo} src="/leo.png" alt="leo" />
<Title <Title
variant="h2" variant="h2"
className={`${styles.text} ${styles["breath-relax"]}`} className={`${styles.text} ${styles["breath-relax"]}`}

View File

@ -8,21 +8,27 @@
flex: auto; flex: auto;
} }
.leo-apng-container { /* .leo-apng-container {
position: absolute; position: absolute;
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
width: 100%; width: 100%;
height: 100%; height: 100%;
} } */
.leo-apng { .leo-apng {
position: absolute;
top: 0;
left: 0;
width: 100%; width: 100%;
height: 100%;
object-fit: cover;
/* 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;
animation-timing-function: linear; animation-timing-function: linear; */
} }
.text-container { .text-container {
@ -55,12 +61,13 @@
.preview { .preview {
position: relative; position: relative;
width: 100%; width: 100%;
height: 200px;
display: flex; display: flex;
justify-content: center; justify-content: center;
} }
.leo { .leo {
width: 90%; /* width: 90%; */
animation-name: leo; animation-name: leo;
animation-duration: 10s; animation-duration: 10s;
animation-iteration-count: 1; animation-iteration-count: 1;
@ -112,12 +119,12 @@
85% { 85% {
scale: 1.1; scale: 1.1;
} }
95% { /* 95% {
opacity: 1; opacity: 1;
} } */
100% { 100% {
scale: 1; scale: 1;
opacity: 0; opacity: 1;
} }
} }

View File

@ -136,7 +136,7 @@ function HomePage(): JSX.Element {
const downloadImg = () => { const downloadImg = () => {
if (!asset) return; if (!asset) return;
download(asset.url, "image.png"); download(asset.url.replace("http://", "https://"), "image.png");
}; };
return ( return (

View File

@ -26,6 +26,7 @@
.symbol { .symbol {
opacity: 0; opacity: 0;
will-change: opacity;
animation-name: appearance; animation-name: appearance;
animation-timing-function: ease; animation-timing-function: ease;
animation-duration: .3s; animation-duration: .3s;