|
|

楼主 |
发表于 2022-8-26 07:09
|
显示全部楼层
本帖最后由 马黑黑 于 2022-8-26 07:54 编辑
本帖测试昨晚写的纯canvas画布播放器,代码尚未优化。完整源码分享如下:- <style>
- #papa { left: -214px; width: 1024px; height: 640px; box-shadow: 3px 3px 20px #000; background: gray url('https://638183.freep.cn/638183/t22/hl/tong.webp') no-repeat center/cover; position: relative; z-index: 1; }
- #mypic { position: absolute; top: 125px; right: 100px; width: 260px; transform: rotate(30deg); cursor: pointer; transition: all 2s; }
- #mypic:hover { box-shadow: -10px -10px 40px 10px rgba(255,255,255,.45); transform: scale(1.5); }
- #player { margin: auto; display: block; position: absolute; }
- </style>
- <div id="papa">
- <canvas id="player"></canvas>
- <img id="mypic" src="https://638183.freep.cn/638183/t22/hl/yly.webp" alt="" />
- </div>
- <script>
- let ctx = player.getContext('2d'),
- w = player.width = 350, h = player.height = 100,
- startX = 30, startY = 70, radius = 16,
- flag = false,
- color = { lrc: 'lightblue', time: 'lightblue', track: '#ccc', prg: 'red', circle: 'lightblue', btn: 'snow', btnhover: 'red' };
- aud = new Audio();
- let lrcAr = [
- ['0.01','童安格 耶利亚女郎'],
- ['34.10','很远的地方有个女郎 名字叫做耶利亚'],
- ['42.33','有人在传说她的眼睛 看了使人更年轻'],
- ['50.62','如果你得到她的拥抱 你就永远不会老'],
- ['59.15','为了这个神奇的传说 我要努力去寻找'],
- ['66.42','耶利亚神秘耶利亚 耶利耶利亚'],
- ['74.77','耶利亚神秘耶利亚 我一定要找到她'],
- ['101.12','很远的地方有个女郎 名字叫做耶利亚'],
- ['109.32','有人在传说她的眼睛 看了使人更年轻'],
- ['117.90','如果你得到她的拥抱 你就永远不会老'],
- ['126.22','为了这个神奇的传说 我要努力去寻找'],
- ['133.57','耶利亚神秘耶利亚 耶利耶利亚'],
- ['141.90','耶利亚神秘耶利亚 我一定要找到她'],
- ['150.29','耶利亚神秘耶利亚 耶利耶利亚'],
- ['158.76','耶利亚神秘耶利亚 我一定要找到她'],
- ['198.70','耶利亚神秘耶利亚 耶利耶利亚'],
- ['207.11','耶利亚神秘耶利亚 我一定要找到她'],
- ['215.28','耶利亚神秘耶利亚 耶利耶利亚'],
- ['223.72','耶利亚神秘耶利亚 我一定要找到她']
- ];
- aud.src = 'https://music.163.com/song/media/outer/url?id=150852.mp3';
- aud.autoplay = true;
- aud.loop = true;
- drawBtn(aud.paused);
- drawProgress(2, '00:00');
- drawLrc('等待播放 ...');
- let isHover = (x, y, cx, cy) => Math.pow(x - cx, 2) + Math.pow(y - cy, 2) <= Math.pow(radius, 2); //鼠标经过检测
- player.addEventListener('mousemove', (e) => { //监听鼠标经过
- flag = isHover(e.offsetX, e.offsetY, startX, startY);
- if(flag) {
- player.style.cursor = 'pointer';
- drawBtn(aud.paused);
- } else {
- player.style.cursor = 'default';
- drawBtn(aud.paused);
- }
- });
- player.addEventListener('click', (e) => { if(flag) aud.paused ? aud.play() : aud.pause(); });
- aud.addEventListener('playing',() => drawBtn(false));
- aud.addEventListener('pause',() => drawBtn(true));
- aud.addEventListener('timeupdate', () => { //进度监听
- let prg = 100 * aud.currentTime / aud.duration;
- drawProgress(2*prg, toMin(aud.duration) + ' | ' + toMin(aud.currentTime));
- for(j = 0; j < lrcAr.length; j ++) {
- if(aud.currentTime >= lrcAr[j][0]) drawLrc(lrcAr[j][1]);
- }
- });
- function drawLrc(text) { //lrc同步
- ctx.clearRect(0, 0, w, 50);
- ctx.save();
- ctx.fillStyle = color.lrc;
- ctx.textAlign = 'center';
- ctx.beginPath();
- ctx.font = '1.2em sans-serif';
- ctx.fillText(text, w/2, 30);
- ctx.fill();
- ctx.restore();
- }
- function drawProgress(prog, text) { //进度
- ctx.clearRect(startX + radius + 202, startY - 10, startX + radius + 206 + ctx.measureText(text).width, 40);
- ctx.beginPath();
- ctx.font = '14px sans-serif';
- ctx.textBaseline = 'middle';
- ctx.strokeStyle = color.track;
- ctx.moveTo(startX + radius + 4, startY);
- ctx.lineTo(startX + radius + 204, startY); //底线
- ctx.stroke();
- ctx.beginPath();
- ctx.strokeStyle = color.prg;
- ctx.moveTo(startX + radius + 4, startY);
- ctx.lineTo(startX + radius + 4 + prog, startY); //进度线
- ctx.stroke();
- ctx.fillStyle = color.time;
- ctx.fillText(text, startX + radius + 208, startY); //数字进度
- ctx.fill();
- }
- function drawBtn(id) { //绘制播放+暂停按钮
- ctx.clearRect(startX - radius, startY - radius, radius *2, radius*2);
- ctx.fillStyle = color.circle;
- ctx.beginPath();
- ctx.arc(startX, startY, radius, 0, 2*Math.PI);
- ctx.fill();
- ctx.fillStyle = flag ? color.btnhover : color.btn;
- ctx.beginPath();
- if (id) { //播放图标
- ctx.lineWidth = 1;
- ctx.moveTo(startX-radius / 2 + 1, startY - radius / 2);
- ctx.lineTo(startX - radius / 2 + 1, startY + radius / 2);
- ctx.lineTo(startX + radius / 2 + 1, startY);
- ctx.fill();
- } else { //暂停图标
- ctx.fillRect(startX - radius / 2 + 5, startY - radius / 2, 2, radius);
- ctx.fillRect(startX - radius / 2 + 10, startY - radius / 2, 2, radius);
- }
- }
- 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>
复制代码
|
评分
-
| 参与人数 1 | 威望 +50 |
金钱 +100 |
经验 +50 |
收起
理由
|
红影
| + 50 |
+ 100 |
+ 50 |
赞一个! |
查看全部评分
|