Skip to content

Commit

Permalink
feat: add hooks useAudioImg, update random music api
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-kfd committed Feb 10, 2023
1 parent a7fca24 commit a31dbd7
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 126 deletions.
29 changes: 16 additions & 13 deletions src/components/g-audio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import SPaticle from "./s-particle";
import { apiURL } from '@/global'

export const MusicVisualizerCtx = new MusicVisualizer()

export default function GAudio() {
const audio = useRef<HTMLAudioElement>(null)
useEffect(() => {
Expand All @@ -21,6 +22,7 @@ export default function GAudio() {
const [musicName, setMusicName] = useState('Please load a music...')
const [audioURL, setAudioURL] = useState<string>()
const [audioData, setAudioData] = useState<number[]>([])
const [audioImg, setAudioImg] = useState<string>()
const [isPlaying, setIsPlaying] = useState(false)

const hiddenFileInput = useRef<HTMLInputElement>(null)
Expand All @@ -47,13 +49,14 @@ export default function GAudio() {

async function handleLoadRandomMusic() {
try {
const res1 = await fetch('https://api.uomg.com/api/rand.music?sort=%E7%83%AD%E6%AD%8C%E6%A6%9C&format=json')
const { data } = await res1.json()
const transferTarget = encodeURIComponent(`https://api.wqwlkj.cn/wqwlapi/wyy_random.php?type=json`)
const res = await fetch(`${apiURL}/api/transfer?target=${transferTarget}`, { headers: { 'content-type': 'application/json; charset=utf-8' } })
const { data } = await res.json()
console.log('data', data)
const { name, url, artistsname, picurl } = data
const res2 = await fetch(`${apiURL}/api/neteaseMusic?target=${url}`)
const { url: redirect } = await res2.json()
setMusicName(`${name} - ${artistsname}`)
setAudioURL(redirect.replace('http:', ''))
setAudioURL(url)
setAudioImg(picurl)
pause()
} catch(e) {
console.error(e)
Expand Down Expand Up @@ -87,14 +90,14 @@ export default function GAudio() {
<audio controls onPlay={play} onPause={pause} ref={audio} src={audioURL} crossOrigin="anonymous"></audio>
</div>
<div className={style.exampleWrapper}>
<SLine isPlaying={isPlaying} data={audioData} />
<SPathDouble isPlaying={isPlaying} data={audioData} />
<SPath isPlaying={isPlaying} data={audioData} />
<SPathFill isPlaying={isPlaying} data={audioData} />
<SCircle isPlaying={isPlaying} data={audioData} />
<SPaticle isPlaying={isPlaying} data={audioData} />
<SDot isPlaying={isPlaying} data={audioData} />
<SPathDot isPlaying={isPlaying} data={audioData} />
<SLine isPlaying={isPlaying} data={audioData} audioImg={audioImg}/>
<SPathDouble isPlaying={isPlaying} data={audioData} audioImg={audioImg}/>
<SPath isPlaying={isPlaying} data={audioData} audioImg={audioImg}/>
<SPathFill isPlaying={isPlaying} data={audioData} audioImg={audioImg}/>
<SCircle isPlaying={isPlaying} data={audioData} audioImg={audioImg}/>
<SPaticle isPlaying={isPlaying} data={audioData} audioImg={audioImg}/>
<SDot isPlaying={isPlaying} data={audioData} audioImg={audioImg}/>
<SPathDot isPlaying={isPlaying} data={audioData} audioImg={audioImg}/>
{
Array.from({length: 5}).map((item,index) => {
return <div key={index} className="s-module-fake"></div>
Expand Down
5 changes: 5 additions & 0 deletions src/components/props.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface SComponentProps {
isPlaying: boolean;
data: number[];
audioImg?: string;
}
17 changes: 4 additions & 13 deletions src/components/s-circle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ import { Canvas, IShape } from '@antv/g-canvas';
import { getCirclePath } from '../utils'
import { getImageCircle } from '../utils/base';
import { X, Y, R } from '../utils/constanst'
import useAudioImg from "@/hooks/useAudioImg";

interface SCircleProps {
isPlaying: boolean;
data: number[];
}

export default function SCircle(props: SCircleProps) {
export default function SCircle(props: SComponentProps) {
const LINE_COLOR = '#fff'
const DOT_COLOR = '#e9dcf7'
const DOT_R = 5
Expand Down Expand Up @@ -154,15 +150,10 @@ export default function SCircle(props: SCircleProps) {

useEffect(() => {
isPlaying.current = props.isPlaying
setTimeout(() => {
if (props.isPlaying) {
circle.current?.resumeAnimate()
} else {
circle.current?.pauseAnimate()
}
})
}, [props.isPlaying])

useAudioImg(canvas, circle, props.isPlaying, props.audioImg)

return (
<div className="s-model">
<div id="SCircle" className="s-canvas-wrapper"></div>
Expand Down
19 changes: 4 additions & 15 deletions src/components/s-dot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ import { Canvas } from '@antv/g-canvas';
// import { formatToTransit } from '../utils'
import { IElement, IShape } from "@antv/g-canvas/lib/types";
import { getImageCircle } from '../utils/base';
import { X, Y, R } from '../utils/constanst'
interface SDotProps {
isPlaying: boolean;
data: number[];
}
import { X, Y, R } from '../utils/constanst';
import useAudioImg from "@/hooks/useAudioImg";

export default function SDot(props: SDotProps) {
export default function SDot(props: SComponentProps) {
const POINT_NUM = 64
const OFFSET = 10
const DOT_R = 2
Expand Down Expand Up @@ -113,15 +110,7 @@ export default function SDot(props: SDotProps) {
})
}, [])

useEffect(() => {
setTimeout(() => {
if (props.isPlaying) {
circle.current?.resumeAnimate()
} else {
circle.current?.pauseAnimate()
}
})
}, [props.isPlaying])
useAudioImg(canvas, circle, props.isPlaying, props.audioImg)

return (
<div className="s-model">
Expand Down
17 changes: 3 additions & 14 deletions src/components/s-line.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import { IElement, IShape } from "@antv/g-canvas/lib/types";
// import { formatToTransit } from '../utils'
import { getImageCircle } from '../utils/base'
import { X, Y, R } from '../utils/constanst'
interface SLineProps {
isPlaying: boolean
data: number[];
}
import useAudioImg from "@/hooks/useAudioImg";

export default function SLine(props: SLineProps) {
export default function SLine(props: SComponentProps) {
const POINT_NUM = 64
const OFFSET = 10
const RECT_WIDTH = 4
Expand Down Expand Up @@ -73,15 +70,7 @@ export default function SLine(props: SLineProps) {
})
}, [])

useEffect(() => {
setTimeout(() => {
if (props.isPlaying) {
circle.current?.resumeAnimate()
} else {
circle.current?.pauseAnimate()
}
})
}, [props.isPlaying])
useAudioImg(canvas, circle, props.isPlaying, props.audioImg)

return (
<div className="s-model">
Expand Down
17 changes: 4 additions & 13 deletions src/components/s-particle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import React, { useEffect, useRef } from 'react';
import { Canvas, IShape } from '@antv/g-canvas';
import { getImageCircle } from '../utils/base'
import { X, Y, R } from '../utils/constanst'
import useAudioImg from "@/hooks/useAudioImg";

interface SPaticle {
isPlaying: boolean;
data: number[];
}

export default function SPaticle(props: SPaticle) {
export default function SPaticle(props: SComponentProps) {
const POINT_NUM = 64
const PARTICLE_NUM = 12
const OFFSET = 0
Expand Down Expand Up @@ -147,15 +143,10 @@ export default function SPaticle(props: SPaticle) {

useEffect(() => {
isPlaying.current = props.isPlaying
setTimeout(() => {
if (props.isPlaying) {
circle.current?.resumeAnimate()
} else {
circle.current?.pauseAnimate()
}
})
}, [props.isPlaying])

useAudioImg(canvas, circle, props.isPlaying, props.audioImg)

return (
<div className="s-model">
<div id="SParticle" className="s-canvas-wrapper"></div>
Expand Down
17 changes: 3 additions & 14 deletions src/components/s-path-dot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import { IElement } from "@antv/g-canvas/lib/types";
import { line, curveCardinalClosed } from 'd3';
import { getImageCircle } from '../utils/base';
import { X, Y, R } from '../utils/constanst'
interface SPathDotProps {
isPlaying: boolean;
data: number[];
}
import useAudioImg from "@/hooks/useAudioImg";

export default function SPathDot(props: SPathDotProps) {
export default function SPathDot(props: SComponentProps) {
const POINT_NUM = 128
const PACE_NUM = 8 // 曲率优化跳点数, 2 ** n
const JUME_OFFSET = 36 // 波动幅度
Expand Down Expand Up @@ -104,15 +101,7 @@ export default function SPathDot(props: SPathDotProps) {
})
}, [])

useEffect(() => {
setTimeout(() => {
if (props.isPlaying) {
circle.current?.resumeAnimate()
} else {
circle.current?.pauseAnimate()
}
})
}, [props.isPlaying])
useAudioImg(canvas, circle, props.isPlaying, props.audioImg)

return (
<div className="s-model">
Expand Down
17 changes: 3 additions & 14 deletions src/components/s-path-double.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import { formatToTransit, getCirclePath } from '../utils'
import { line, curveCardinalClosed } from 'd3'
import { getImageCircle } from '../utils/base';
import { X, Y, R } from '../utils/constanst'
interface SPathDouble {
isPlaying: boolean;
data: number[];
}
import useAudioImg from "@/hooks/useAudioImg";

export default function SPath(props: SPathDouble) {
export default function SPath(props: SComponentProps) {
const POINT_NUM = 64
const OFFSET = 10
const COLOR = '#e9dcf7'
Expand Down Expand Up @@ -97,15 +94,7 @@ export default function SPath(props: SPathDouble) {

}, [])

useEffect(() => {
setTimeout(() => {
if (props.isPlaying) {
circle.current?.resumeAnimate()
} else {
circle.current?.pauseAnimate()
}
})
}, [props.isPlaying])
useAudioImg(canvas, circle, props.isPlaying, props.audioImg)

return (
<div className="s-model">
Expand Down
17 changes: 3 additions & 14 deletions src/components/s-path-fill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import { formatToTransit, addColorOpacity } from '../utils'
import { line, curveCardinalClosed } from 'd3'
import { getImageCircle } from '../utils/base';
import { X, Y, R } from '../utils/constanst'
interface SPathFillProps {
isPlaying: boolean;
data: number[];
}
import useAudioImg from "@/hooks/useAudioImg";

export default function SLine(props: SPathFillProps) {
export default function SLine(props: SComponentProps) {
const POINT_NUM = 64
const POINT_OFFSET = 60
// const COLORS = ['#cdf5dd', '#e8fdc8', '#dafcf0', '#f3f8c9']
Expand Down Expand Up @@ -83,15 +80,7 @@ export default function SLine(props: SPathFillProps) {
})
}, [])

useEffect(() => {
setTimeout(() => {
if (props.isPlaying) {
circle.current?.resumeAnimate()
} else {
circle.current?.pauseAnimate()
}
})
}, [props.isPlaying])
useAudioImg(canvas, circle, props.isPlaying, props.audioImg)

return (
<div className="s-model">
Expand Down
17 changes: 3 additions & 14 deletions src/components/s-path.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import { formatToTransit } from '../utils'
import { line, curveCardinalClosed } from 'd3'
import { getImageCircle } from '../utils/base';
import { X, Y, R } from '../utils/constanst'
interface SPathProps {
isPlaying: boolean;
data: number[];
}
import useAudioImg from "@/hooks/useAudioImg";

export default function SPath(props: SPathProps) {
export default function SPath(props: SComponentProps) {
const POINT_NUM = 64
const OFFSET = 4
const POINT_OFFSET = 30
Expand Down Expand Up @@ -82,15 +79,7 @@ export default function SPath(props: SPathProps) {
})
}, [])

useEffect(() => {
setTimeout(() => {
if (props.isPlaying) {
circle.current?.resumeAnimate()
} else {
circle.current?.pauseAnimate()
}
})
}, [props.isPlaying])
useAudioImg(canvas, circle, props.isPlaying, props.audioImg)

return (
<div className="s-model">
Expand Down
21 changes: 21 additions & 0 deletions src/hooks/useAudioImg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ICanvas, IShape } from "@antv/g-canvas";
import { useEffect } from "react";

export default function(canvas: React.MutableRefObject<ICanvas | undefined>, circle: React.MutableRefObject<IShape | undefined>, isPlaying: boolean, audioImg: string | undefined) {
useEffect(() => {
setTimeout(() => {
if (isPlaying) {
circle.current?.resumeAnimate()
} else {
circle.current?.pauseAnimate()
}
})
}, [isPlaying])

useEffect(() => {
const imgEl = canvas.current?.findById('audioImg')
if (imgEl) {
imgEl.attr('img', audioImg)
}
}, [audioImg])
}
4 changes: 3 additions & 1 deletion src/utils/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ export function getImageCircle(canvas: Canvas, { x, y, r, shadowColor }: ImageCi
}
})
const shape = canvas.addShape('image', {
id: 'audioImg',
attrs: {
x: x - r,
y: y - r,
width: 2 * r,
height: 2 * r,
img: `https://source.unsplash.com/random/${2 * r}x${2 * r}?Nature`
// img: `https://source.unsplash.com/random/${2 * r}x${2 * r}?Nature`
img: `https://howdz.deno.dev/unsplash/random?keyword=Nature&${2 * r}x${2 * r}`
}
})
shape.setClip({
Expand Down
3 changes: 2 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default defineConfig({
changeOrigin: true,
rewrite: (path: string) => path.replace(/^\/api/, '')
}
}
},
hmr: false
}
})

0 comments on commit a31dbd7

Please sign in to comment.