|
|

楼主 |
发表于 2022-8-23 07:22
|
显示全部楼层
本帖最后由 马黑黑 于 2022-8-23 08:20 编辑
代码分享(全)- <style>
- #papa { margin: auto; width: 1024px; height: 640px; background: #ccc url('https://638183.freep.cn/638183/t22/51/hx.webp') no-repeat center/cover; box-shadow: 3px 3px 20px #000; position: relative; }
- #player { position: absolute; left: 458px; top: 78px; cursor: pointer; }
- #hx { position: absolute;left : calc(50% - 180px); top: 10px; }
- </style>
- <div id="papa">
- <img id="hx" src="https://638183.freep.cn/638183/t22/51/hx.png" alt="" />
- <canvas id="player"></canvas>
- </div>
- <script>
- let ctx = player.getContext('2d'),
- w = h = player.width = player.height = 120,
- prog = '点这播放',
- angle = 0,
- aud = new Audio();
- ctx.arc(50,50,40,0, 2*Math.PI);
- ctx.stroke();
- aud.src = 'https://music.163.com/song/media/outer/url?id=5035947.mp3';
- aud.loop = true;
- aud.autoplay = true;
- drawCircle();
- player.onclick = () => aud.paused ? aud.play() : aud.pause();
- aud.addEventListener('timeupdate', () => {
- angle = 360 * aud.currentTime / aud.duration;
- prog = toMin(aud.currentTime) + ' | ' + toMin(aud.duration);
- drawCircle();
- });
- function drawCircle() {
- ctx.clearRect(0, 0, w, h);
- ctx.beginPath();
- ctx.strokeStyle = '#eee';
- ctx.lineWidth = 8;
- ctx.arc(w/2, h/2, w/2 - 4, 0, 2 * Math.PI);
- ctx.stroke();
- ctx.beginPath();
- ctx.strokeStyle = '#EA0001';
- ctx.arc(w/2, h/2, w/2 - 4, -90 * Math.PI / 180, (angle - 90) * Math.PI / 180);
- ctx.stroke();
- ctx.fillStyle = '#eee';
- ctx.font = '14px sans-serif';
- ctx.textAlign = 'center';
- ctx.textBaseline = 'middle';
- ctx.fillText(prog, w/2, h/2);
- }
- function toMin(val) {
- if(!val) return '00:00';
- val = Math.floor(val);
- let min = parseInt(val / 60);
- let sec = parseFloat(val % 60);
- if(min < 10) min = '0' + min;
- if(sec < 10) sec = '0' + sec;
- return min + ':' + sec;
- }
- </script>
复制代码
|
|