/** * @description rice-paper component setup * @author 阿怪 * @date 2024/2/6 02:21 * @version v1.0.0 * * 江湖的业务千篇一律,复杂的代码好几百行。 */ import { onBeforeUnmount, onMounted, watch } from 'vue'; import { WCSetup } from '../../types/template'; import { RicePaperProps } from './index'; import useImgMove from './compositions/useImgMove.ts'; export const MRicePaperSetup: WCSetup = slot => { return (props, { slots }) => { const { baseRef: mLBaseRef, midRef: mLMidRef, frontRef: mLFrontRef, front2Ref: mLFront2Ref, onMove: leftOnMove, } = useImgMove('left'); const { baseRef: mRBaseRef, midRef: mRMidRef, frontRef: mRFrontRef, front2Ref: mRFront2Ref, onMove: rightOnMove, } = useImgMove('right'); const moveMountain = (e: MouseEvent) => { const x = e.clientX; const y = e.clientY; const w = window.innerWidth; const h = window.innerHeight; const xPercent = x / w; const yPercent = y / h; const xMove = xPercent * 10 - 5; const yMove = (yPercent * 10 - 5) * 0.5; // UI said y-axis must move very slow. leftOnMove(xMove, yMove); rightOnMove(xMove, yMove); }; // todo use a better way to handle this. onMounted(() => { if (props.mountain) { window.addEventListener('mousemove', moveMountain); } }); onBeforeUnmount(() => { if (props.mountain) { window.removeEventListener('mousemove', moveMountain); } }); watch(() => props.mountain, val => { if (val) { window.addEventListener('mousemove', moveMountain); } else { window.removeEventListener('mousemove', moveMountain); } }); return () => { const renderSlot = slot ?? slots.default?.(); const mountain = props.mountain ?
: null; return (
{mountain}
{renderSlot}
); }; }; };