|
|

楼主 |
发表于 2024-5-27 12:06
|
显示全部楼层
代码:
- <canvas id="audCanv" width="400" height="240" style="margin: 20px auto; display: block; background: #333;"></canvas>
- <audio id="aud" src="https://music.163.com/song/media/outer/url?id=29999537" autoplay loop></audio>
- <script>
- const ww = audCanv.width, hh = audCanv.height;
- const ctx = audCanv.getContext('2d');
- var _play = 0, _change = 0;
- class Audio {
- constructor(ctx,config) {
- this.ctx = ctx;
- this.cf = config;
- this.cx = config.w / 2;
- this.cy = config.h - config.size / 2 - 20;
- this.r = config.size / 2;
- };
- drawUi(text,value,flag) {
- this.ctx.clearRect(0, 0, this.cf.w, this.cf.h);
- this.ctx.lineCap = 'round';
- this.ctx.lineWidth = 10;
- this.ctx.strokeStyle = 'rgba(192,192,192,.95)';
- ctx.beginPath();
- this.ctx.arc(this.cx, this.cy, this.r, 0, Math.PI * 2);
- this.ctx.stroke();
- this.ctx.strokeStyle = 'rgba(255,250,250,.85)';
- ctx.beginPath();
- this.ctx.arc(this.cx, this.cy, this.r, 1.5 * Math.PI, Math.PI * (2 * value + 1.5));
- this.ctx.stroke();
- this.ctx.fillStyle = 'rgba(255,250,250,.85)';
- if(flag) {
- this.ctx.beginPath();
- this.ctx.moveTo(this.cx - 10, this.cy - 10);
- this.ctx.lineTo(this.cx - 10, this.cy + 10);
- this.ctx.lineTo(this.cx + 10, this.cy);
- this.ctx.closePath();
- this.ctx.fill();
- }else{
- this.ctx.fillStyle = 'white';
- this.ctx.beginPath();
- this.ctx.fillRect(this.cx - 10, this.cy - 10, 4, 20);
- this.ctx.fillRect(this.cx + 4, this.cy - 10, 4, 20);
- }
- this.ctx.font = 'normal 13px sans-serif';
- this.ctx.textAlign = 'center';
- this.ctx.textBaseline = 'middle';
- this.ctx.beginPath();
- this.ctx.fillText(text, this.cx, this.cy + 35);
- this.ctx.fill();
- };
- };
- var option = {w: ww, h: hh, size: 140};
- var _aud = new Audio(ctx,option);
- _aud.drawUi('00:00/00:00',0,0);
- aud.ontimeupdate = () => {
- var val = aud.currentTime / aud.duration;
- var txt = s2m(aud.currentTime) + '/' + s2m(aud.duration);
- _aud.drawUi(txt, val, aud.paused);
- };
- audCanv.onclick = (e) => {
- if(_play) aud.paused ? aud.play() : aud.pause();
- if(_change) {
- var deg = Math.atan2(e.offsetY - _aud.cy, e.offsetX - _aud.cx) * 180 / Math.PI;
- deg += (e.offsetX < _aud.cx && e.offsetY < _aud.cy) ? 450 : 90;
- aud.currentTime = aud.duration * deg / 360;
- };
- };
- audCanv.onmousemove = (e) => {
- var _ex = e.offsetX,
- _ey = e.offsetY,
- _cx = _aud.cx,
- _cy = _aud.cy,
- r1 = 10,
- r2 = _aud.r + 10; //+10: lineWidth
- if(innerCircle(_ex, _ey, _cx, _cy, r2) && innerCircle(_ex, _ey, _cx, _cy, r2 - 20) === false) {
- _change = 1;
- _play = 0;
- } else {
- _change = 0;
- _play = 0;
- }
- if(innerCircle(_ex, _ey, _cx, _cy, r1)) {
- _change = 0;
- _play = 1;
- }
- audCanv.style.cursor = ['default', 'pointer'][_change + _play];
- audCanv.title = !_change && !_play ? '' : _change ? '调节进度' : aud.paused ? '播放' : '暂停';
- };
- var innerCircle = (ex,ey,cx,cy,r) => Math.sqrt((ex - cx) ** 2 + (ey - cy) ** 2) <= r;
- var s2m = (s) => (Math.floor(s / 60)).toString().padStart(2, '0') + ':' + (Math.floor(s % 60)).toString().padStart(2, '0');
- </script>
复制代码
|
评分
-
| 参与人数 2 | 威望 +80 |
金钱 +160 |
经验 +80 |
收起
理由
|
红影
| + 50 |
+ 100 |
+ 50 |
赞一个! |
南无月
| + 30 |
+ 60 |
+ 30 |
很给力! |
查看全部评分
|