|
|

楼主 |
发表于 2022-8-26 20:26
|
显示全部楼层
代码(不需要播放器边框时可去掉CSS里 #player 的 border 属性):- <style>
- #papa { left: -214px; width: 1024px; height: 640px; background: #ccc; box-shadow: 3px 3px 20px #000; position: relative; z-index: 1; }
- #player { position: absolute; left: 10px; bottom: 10px; border: 1px solid; }
- </style>
- <div id="papa">
- <canvas id="player"></canvas>
- </div>
- <script>
- let ctx = player.getContext('2d'), w = player.width = 350, h = player.height = 100, btnFlag, lineFlag, aud = new Audio();
- let mplayer = {
- startX: 30, startY: 70, radius: 16, lineLen: 200, c_lrc: 'teal', c_time: 'teal', c_track: 'lightblue', c_prg: 'red', c_circle: 'lightblue', c_btn: 'snow', c_btnhover: 'pink',
- drawLrc: function(text) {
- ctx.clearRect(0, 0, w, 50);
- ctx.fillStyle = this.c_lrc;
- ctx.textAlign = 'center';
- ctx.beginPath();
- ctx.font = '1.2em sans-serif';
- ctx.fillText(text, w/2, 30);
- ctx.fill();
- },
- drawProgress: function(prog, text) {
- ctx.clearRect(this.startX + this.radius, this.startY - 10, w - this.startX + this.radius, 40);
- ctx.beginPath();
- ctx.font = '14px sans-serif';
- ctx.textBaseline = 'middle';
- ctx.strokeStyle = this.c_track;
- ctx.moveTo(this.startX + this.radius + 4, this.startY);
- ctx.lineTo(this.startX + this.radius + 4 + this.lineLen, this.startY);
- ctx.stroke();
- ctx.beginPath();
- ctx.strokeStyle = this.c_prg;
- ctx.moveTo(this.startX + this.radius + 4, this.startY);
- ctx.lineTo(this.startX + this.radius + 4 + prog, this.startY);
- ctx.stroke();
- ctx.fillStyle = this.c_time;
- ctx.textAlign = 'left';
- ctx.fillText(text, this.startX + this.radius + 8 + this.lineLen, this.startY);
- ctx.fill();
- },
- drawBtn: function(id) {
- ctx.clearRect(this.startX - this.radius, this.startY - this.radius, this.radius * 2, this.radius * 2);
- ctx.fillStyle = this.c_circle;
- ctx.beginPath();
- ctx.arc(this.startX, this.startY, this.radius, 0, 2 * Math.PI);
- ctx.fill();
- ctx.fillStyle = btnFlag ? this.c_btnhover : this.c_btn;
- ctx.beginPath();
- if (id) {
- ctx.moveTo(this.startX - this.radius / 2 + 1, this.startY - this.radius / 2);
- ctx.lineTo(this.startX - this.radius / 2 + 1, this.startY + this.radius / 2);
- ctx.lineTo(this.startX + this.radius / 2 + 1, this.startY);
- ctx.fill();
- } else {
- ctx.fillRect(this.startX - this.radius / 2 + 5, this.startY - this.radius / 2, 2, this.radius);
- ctx.fillRect(this.startX - this.radius / 2 + 10, this.startY - this.radius / 2, 2, this.radius);
- }
- },
- setProgress: function() {
-
- },
- isBtnHover: function (x, y) {
- return Math.pow(x - this.startX, 2) + Math.pow(y - this.startY, 2) <= Math.pow(this.radius, 2);
- },
- isLineHover: function(x, y) {
- let xx = this.startX + this.radius + 4, yy = this.startY - 5;
- return x >= xx && x <= xx + this.lineLen && y >= yy && y <= yy + 10;
- },
- };
- 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;
- mplayer.drawBtn(aud.paused);
- mplayer.drawProgress(1, '00:00 | 00:00');
- mplayer.drawLrc('等待播放 ...');
-
- player.addEventListener('mousemove', (e) => {
- btnFlag = mplayer.isBtnHover(e.offsetX, e.offsetY), lineFlag = mplayer.isLineHover(e.offsetX, e.offsetY);
- player.style.cursor = btnFlag || lineFlag ? 'pointer' : 'default';
- mplayer.drawBtn(aud.paused);
- });
- player.addEventListener('click', (e) => {
- if(btnFlag) aud.paused ? aud.play() : aud.pause();
- if(lineFlag) aud.currentTime = aud.duration * (e.offsetX - mplayer.startX - mplayer.radius - 4) / mplayer.lineLen;
- });
- aud.addEventListener('playing', () => mplayer.drawBtn(false));
- aud.addEventListener('pause', () => mplayer.drawBtn(true));
- aud.addEventListener('timeupdate', () => {
- let prg = mplayer.lineLen * aud.currentTime / aud.duration, tMsg = toMin(aud.duration) + ' | ' + toMin(aud.currentTime);
- mplayer.drawProgress(prg, tMsg);
- for(j = 0; j < lrcAr.length; j ++) {
- if(aud.currentTime >= lrcAr[j][0]) mplayer.drawLrc(lrcAr[j][1]);
- }
- });
- let toMin = (val) => {
- if (!val) return '00:00';
- val = Math.floor(val);
- let min = parseInt(val / 60), sec = parseFloat(val % 60);
- if(min < 10) min = '0' + min;
- if(sec < 10) sec = '0' + sec;
- return min + ':' + sec;
- }
- </script>
复制代码
|
评分
-
| 参与人数 2 | 威望 +100 |
金钱 +200 |
经验 +100 |
收起
理由
|
红影
| + 50 |
+ 100 |
+ 50 |
赞一个! |
小辣椒
| + 50 |
+ 100 |
+ 50 |
赞一个! |
查看全部评分
|