|
|

楼主 |
发表于 2022-12-21 08:34
|
显示全部楼层
三十三、模拟动态示波进度条播放器
(播放控制器基于canvas画布,示波随机动态抖动。插件支持歌词同步)
(一)插件代码- (function(mkPlayer) {let defaults = {lrcAr: [[2.6,"花潮LRC在线",3]],lrc_css: 'top: 15px; left: 50%; transform: translate(-50%)',player_css: 'bottom: 10px; left: 50%; transform: translate(-50%)',playerCode: `<style>#mplayer { --ww: 300px; --color1: hsla(0,80%,50%,.95); --color2: hsla(0,0%,95%,.45); position: absolute; z-index: 901; }#lrc { --motion: cover2; --tt: 1s; --state: running; --bg: linear-gradient(180deg,hsla(100,10%,50%,.75),hsla(100,100%,20%,.65)); position: absolute; font: bold 2.4em sans-serif; color: hsl(100, 100%, 90%); white-space: pre; -webkit-background-clip: text; filter: drop-shadow(1px 1px 2px hsla(0, 100%, 0%, .85)); z-index: 900; }#lrc::before { position: absolute; content: attr(data-lrc); width: 20%; height: 100%; color: transparent; overflow: hidden; white-space: pre; background: var(--bg); filter: inherit; -webkit-background-clip: text; animation: var(--motion) var(--tt) linear forwards; animation-play-state: var(--state); }@keyframes cover1 { from { width: 0; } to { width: 100%; } }@keyframes cover2 { from { width: 0; } to { width: 100%; } }</style><canvas id="mplayer" width="300" height="75"></canvas><div id="lrc" data-lrc="HCPlayer">HCPlayer</div>`,};let playCode = (user_config) => {let data = Object.assign({}, defaults, user_config);papa.innerHTML += data.playerCode;mplayer.style.cssText += data.player_css;lrc.style.cssText += data.lrc_css;let mKey = 0, mFlag = true, btfFlag = false;let getCssVal = (e,v) => getComputedStyle(e).getPropertyValue(v);let ctx = mplayer.getContext('2d');let w = mplayer.width = getCssVal(mplayer,'--ww').replace(/[^0-9]/ig,''), h = mplayer.height = 75;let btnFlag = false;let player = {prog: 0,track: w,color: [getCssVal(mplayer,'--color1'),getCssVal(mplayer,'--color2')],cur: '00:00',dur: '00:00'};let drawCircle = (x,y,r,color) => {ctx.beginPath();ctx.strokeStyle = color;ctx.lineWidth = 2;ctx.arc(x,y,r,0,2*Math.PI);ctx.stroke();};let drawTriangle = (x,y,len,color) => {ctx.beginPath();ctx.fillStyle = color;ctx.moveTo(x,y);ctx.lineTo(x, y+len);ctx.lineTo(x+len, y + len/2);ctx.lineTo(x,y);ctx.fill();};let drawRect = (x,y,ww,hh,color) => {ctx.beginPath();ctx.fillStyle = color;ctx.fillRect(x,y,ww,hh);};let drawOsc = (w1,w2) => {ctx.beginPath();ctx.lineWidth = 1;ctx.strokeStyle = player.color[0];let slice = 1, tt1 = Math.round(w1/slice), tt2 = Math.round(w2/slice);for(j=0; j<=tt2; j++) {ctx.lineTo(j*slice, Math.random() *30);}ctx.stroke();ctx.beginPath();ctx.strokeStyle = player.color[1];for(j=tt2; j<tt1; j++) {ctx.lineTo(j*slice, Math.random() *30);}ctx.stroke();};let drawTxt = (text,x,y,align,color) => {ctx.beginPath();ctx.font = '16px sans-serif';ctx.textAlign = align;ctx.textBaseline = 'middle';ctx.fillStyle = color;ctx.fillText(text,x,y);};let btnRender = () => {let btnColor = btnFlag ? player.color[0] : player.color[1];ctx.clearRect(w/2-18, h-38, 36, 36);drawCircle(w/2,h-20,16,btnColor);aud.paused ? drawTriangle(w/2-6,h-28,16,btnColor) : (drawRect(w/2-6, h-28,4,16,btnColor), drawRect(w/2+2, h-28,4,16,btnColor));};let drawAll = () => {ctx.clearRect(0,0,w,h);drawOsc(w,player.prog);drawTxt(player.cur,w/2-26,h-20,'right',player.color[1]);drawTxt(player.dur,w/2 + 26,h-20,'left',player.color[1]);btnRender();};let overBtn = (e) => Math.sqrt((e.offsetX-w/2) ** 2 + (e.offsetY-(h-20)) ** 2) < 16;let overProg = (e) => e.offsetY > 0 && e.offsetY < 30;mplayer.onmousemove = (e) => {mplayer.style.cursor = overBtn(e) || overProg(e) ? 'pointer' : 'default';mplayer.title = overProg(e) ? toMin(e.offsetX*aud.duration/w) : '';overBtn(e) ? (btnFlag = true,btnRender()) : (btnFlag = false,btnRender());};mplayer.onclick = (e) => {if(overBtn(e)) { aud.paused ? aud.play() : aud.pause(); drawAll(); }if(overProg(e)) aud.currentTime = aud.duration * e.offsetX / w;};mplayer.onmouseout = () => { btnFlag = false; btnRender(); };aud.addEventListener('timeupdate', () => {player.prog = aud.currentTime * w /aud.duration;player.cur = toMin(aud.currentTime);player.dur = toMin(aud.duration);drawAll();for (j = 0; j < data.lrcAr.length; j++) {if (aud.currentTime >= data.lrcAr[j][0]) {if (mKey === j) showLrc(data.lrcAr[j][2]);else continue;}}});aud.addEventListener('pause', () => mState());aud.addEventListener('play', () => mState());aud.addEventListener('seeked', () => calcKey());let mState = () => aud.paused ? (lrc.style.setProperty('--state', 'paused')) : (lrc.style.setProperty('--state', 'running'));let showLrc = (time) => {let name = mFlag ? 'cover1' : 'cover2';lrc.innerHTML = data.lrcAr[mKey][1];lrc.dataset.lrc = data.lrcAr[mKey][1].replace(/<br>/, '\n');lrc.style.setProperty('--motion', name);lrc.style.setProperty('--tt', time + 's');lrc.style.setProperty('--state', 'running');mKey += 1;mFlag = !mFlag;};let calcKey = () => {for (j = 0; j < data.lrcAr.length; j++) {if (aud.currentTime <= data.lrcAr[j][0]) {mKey = j - 1;break;}}if (mKey < 0) mKey = 0;if (mKey > data.lrcAr.length - 1) mKey = data.lrcAr.length - 1;let time = data.lrcAr[mKey][2] - (aud.currentTime - data.lrcAr[mKey][0]);showLrc(time);};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;};drawAll();};mkPlayer.HCPlayer = playCode;})(this);
复制代码 (二)参数即配置
1、lrcAr : 接收帖子歌词同步数组,例如:let arr = [ [2,"帖子标题",3] ]; 配置举例如下:
<div id="papa"></div>
<audio id="aud" src="MP3地址" autoplay loop></audio>
<script>
//这里是插件代码
let arr = [ [2,"歌词一",3], [6,"歌词二",2.7], /* ... */ ];
HCPlayer({
lrcAr: arr,
});
2、lrc_css : 歌词显示样式设定,① 接收一个CSS变量 --bg ,支持颜色和渐变颜色,例如:--bg: red; 或 --bg: linear(180deg, red, pink); ② 接收CSS属性 color 设置歌词底色,仅支持颜色表达法,例如: color: #ccc; ③ 定位使用CSS属性 left/right 和 top/bottom 配对进行。 配置举例:
<div id="papa"></div>
<audio id="aud" src="MP3地址" autoplay loop></audio>
<script>
//这里是插件代码
let arr = [ [2,"歌词一",3], [6,"歌词二",2.7], /* ... */ ];
HCPlayer({
lrcAr: arr,
lrc_css: '--bg: linear-gradient(180deg,hsla(0,100%,50%,.35),hsla(200,100%,50%,.65)); color: lightblue; top: 10px;',
});
3、player_css : 播放控制器设定。① --ww 变量设置播放器长度;② --color1 设置示波表示播放进度部分的颜色,--color2 设置示波底轨颜色;③ ③ 定位使用CSS属性 left/right 和 top/bottom 配对进行。配置举例:
<div id="papa"></div>
<audio id="aud" src="MP3地址" autoplay loop></audio>
<script>
//这里是插件代码
let arr = [ [2,"歌词一",3], [6,"歌词二",2.7], /* ... */ ];
HCPlayer({
lrcAr: arr,
lrc_css: '--bg: linear-gradient(180deg,hsla(0,100%,50%,.35),hsla(200,100%,50%,.65)); color: lightblue; top: 10px;',
player_css: '--color1: red; --color2: lightblue; --ww: 400px; bottom: 5px;',
});
【注意】每一个参数配置结束,均以小角逗号 , 收尾!
(三)应用实例:少女净妖师 - 欢乐水吧 - 花潮论坛 - Powered by Discuz! (huachaowang.com)
|
|