|
|

楼主 |
发表于 2022-8-21 10:45
|
显示全部楼层
代码分享(全)
- <style>
- #papa { left: -214px; width: 1024px; height: 640px; display: grid; place-items: center; background: darkgray url('https://638183.freep.cn/638183/t22/51/line.jpg') no-repeat center/cover; box-shadow: 3px 3px 20px #000; position: relative; z-index: 5; }
- #canv { position: relative; }
- #disc { position: absolute; width: 40px; height: 40px; left: 10px; top: 10px; background: conic-gradient(red,orange,yellow,green,teal,blue,purple); mask: radial-gradient(transparent 4px,red 0); -webkit-mask: radial-gradient(transparent 4px,red 0); border-radius: 50%; cursor: pointer; z-index: 10; animation: rot 2s linear infinite; }
- #lrcbox { position: absolute; left: 60px; top: 10px; font: bold 22px / 40px sans-serif; color: lightblue; text-shadow: 2px 2px 4px #222; }
- @keyframes rot { to { transform: rotate(360deg); } }
- </style>
- <div id="papa">
- <span id="disc"></span>
- <span id="lrcbox">线条之美</span>
- <canvas id="canv" width="500" height="500"></canvas>
- </div>
- <script>
- let ctx = canv.getContext('2d');
- let lines = []; //线条数组
- let total = 200, lineLen = canv.width / 2, idx = 0; //线条总数、线长、当前线条索引
- let aud = new Audio();
- aud.src = 'https://music.163.com/song/media/outer/url?id=1931775815.mp3';
- aud.loop = true;
- aud.autoplay = true;
- disc.style.animationPlayState = aud.paused ? 'paused' : 'running';
- disc.onclick = () => aud.paused ? aud.play() : aud.pause();
- aud.addEventListener('playing',() => disc.style.animationPlayState = 'running');
- aud.addEventListener('pause',() => disc.style.animationPlayState = 'paused');
- function Line(x1,y1,x2,y2) {
- this.x1 = x1;
- this.y1 = y1;
- this.x2 = x2;
- this.y2 = y2;
- lines.push(this);
- }
- Line.prototype.draw = function() {
- ctx.beginPath();
- ctx.strokeStyle = this.color;
- ctx.setLineDash([1,1,1]);
- ctx.moveTo(this.x1, this.y1);
- ctx.lineTo(this.x2,this.y2);
- ctx.stroke();
- }
- for(let j = 0; j < total; j ++) {
- let line = new Line();
- line.x1 = lineLen;
- line.y1 = lineLen;
- let angle = j* (360 / total);
- line.x2 = rotPos(line.x1,line.y1, lineLen, angle).xx;
- line.y2 = rotPos(line.x1,line.y1, lineLen, angle).yy;
- }
- (function render() {
- let id = idx;
- if(id <= lines.length - 1) {
- lines[id].color = '#' + Math.random().toString(16).substr(-6);
- lines[id].draw();
- }
- idx ++;
- if(idx > lines.length) {
- setTimeout(function() {
- ctx.clearRect(0,0,canv.width, canv.height);
- idx = 0;
- render();
- }, 4000);
- } else {
- setTimeout(render,50);
- }
- })();
- function rotPos(x,y,r,angle) {
- let xx = x + r * Math.sin(angle * Math.PI / 180);
- let yy = y + r * Math.cos(angle * Math.PI / 180);
- return ({xx, yy});
- }
- </script>
复制代码
|
|