马黑黑 发表于 2022-11-6 08:21

夜 萤火虫和你

<style>
#papa { margin-left: calc(50% - 681px); margin-top:100px; width: 1200px; height: 675px; display: grid; place-items: center; background: #000 url('https://638183.freep.cn/638183/t22/webp/yhi.webp') no-repeat center/cover; box-shadow: 3px 3px 20px #000; perspective: 1000px; overflow: hidden; user-select: none; position: relative; z-index: 1; }
#papa::after { position: absolute; content: '萤火虫'; top: 10px; font: bold 2.6em sans-serif; color: gold; text-shadow: 2px 2px 3px #000; opacity: .6; }
#mplayer { position: absolute; bottom: 10px; z-index: 1000; }
#btnwrap { display: block; fill: hsla(30, 20%, 80%, .9); cursor: pointer; }
#btnwrap:hover { fill: hsla(90, 100%, 100%, .9); }
#tmsg { fill: hsla(30, 100%, 90%, .9); stroke: none; font: bold 1em sans-serif; }
.firefly { position: absolute; width: 2px; height: 2px; border-radius: 50%; background: silver; transform-style: preserve-3d; }
@keyframes move { to { transform: rotate(0) translate3d(0, 0, 0); } }
@keyframes flash { from {opacity: .95; } to { opacity: 1; } }
</style>

<div id="papa">
        <svg id="mplayer" width="120" height="120">
                <g id="mama" transform="rotate(-90, 60, 60)" style="cursor: pointer;">
                        <circle id="track" cx="60" cy="60" r="50" fill="none" stroke-width="10" stroke="hsla(60, 20%, 80%, .77)" />
                        <circle id="prog" cx="60" cy="60" r="50" fill="none" stroke-width="10" stroke="hsla(60,100%,40%,.65)" />
                </g>
                <g id="btnwrap">
                        <path id="btnplay" d="M 50 50,50 70,70, 60 z"></path>
                        <path id="btnpause" d="M 52 50,52 70,57 70,57 50,52 50 z M 60 50,60 70,65 70,65 50,60 50 z" style="display: none;"></path>
                        <path d="M 57 50,60 50,60 70,57 70 z" fill="transparent" />
                </g>
                <path id="curPath" d="M 20 70 Q 60 0 100 70" fill="none" stroke="none"/>
                <path id="durPath" d="M 20 55 Q 60 110 100 55" fill="none" stroke="none"/>
                <g id="tmsg">
                        <text x="34" y="0"><textPath id="curMsg" xlink:href="#curPath" dominant-baseline="text-after-edge">00:00</textPath></text>
                        <text x="29" y="0"><textPath id="durMsg" xlink:href="#durPath" dominant-baseline="text-before-edge">00:00</textPath></text>
                </g>
        </svg>
</div>
<audio id="aud" src="https://music.163.com/song/media/outer/url?id=509720124.mp3" loop autoplay></audio>

<script>
(function() {
        (function() {for(j=0; j<500; j++) {let ele = document.createElement('span');ele.className = 'firefly';ele.style.cssText += `left: ${Math.random() * 1100}px;top: ${Math.random() * 600}px;background: hsl(${Math.random() * 10 + 50}, ${Math.random() * 50 + 40}%,${Math.random() * 40 + 20}%);box-shadow: 0 0 5px hsla(0,10%,100%,.25);transform: rotate(${Math.random() * 360 + 360}deg) translate3d(${Math.random() * 400 + 100}px,${Math.random() * 300}px,${Math.random() * 1000}px);animation: move 60s infinite ${-10 - Math.random() * 20}s linear;`;papa.appendChild(ele);}})();
        let cc = { x: 1*track.getAttribute('cx'), y: 1*track.getAttribute('cy'), len: track.getTotalLength(),};
        prog.style.strokeDasharray = prog.style.strokeDashoffset =cc.len;
        btnwrap.onclick = () => aud.paused ? aud.play() : aud.pause();
        mama.onclick = (e) => {let deg = Math.atan2(e.offsetY - cc.y, e.offsetX - cc.x) * 180 / Math.PI;deg += (e.offsetX < cc.x && e.offsetY < cc.y) ? 450 : 90;aud.currentTime = aud.duration * deg / 360;};
        aud.addEventListener('pause', () => mState());
        aud.addEventListener('play', () => mState());
        aud.addEventListener('seeked', () => aud.play());
        aud.addEventListener('timeupdate', () => {prog.style.strokeDashoffset = cc.len - cc.len * aud.currentTime / aud.duration;curMsg.textContent = toMin(aud.currentTime);durMsg.textContent = toMin(aud.duration);});
        let mState = () => aud.paused ? (btnplay.style.display = 'block', btnpause.style.display = 'none') : (btnplay.style.display = 'none', btnpause.style.display = 'block');
        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;};
        document.title = '萤火虫';
})();

</script>

冬天的雨 发表于 2022-11-6 08:25

沙发吗

马黑黑 发表于 2022-11-6 08:28

本帖最后由 马黑黑 于 2022-11-6 08:38 编辑

本帖与昨日的 人类遗迹 的主要区别在于:

① 运动对象去中心化;
② 运动对象收窄颜色范围。

一切在生成运动对象的自执行匿名函数中完成——

        (function() {
                for(j=0; j<500; j++) {
                        let ele = document.createElement('span');
                        ele.className = 'firefly';
                        ele.style.cssText += `
                                left: ${Math.random() * 800}px;
                                top: ${Math.random() * 600}px;
                                background: hsl(${Math.random() * 10 + 50}, ${Math.random() * 50 + 40}%,${Math.random() * 40 + 20}%);
                                box-shadow: 0 0 5px hsla(0,10%,100%,.25);
                                transform: rotate(${Math.random() * 360 + 360}deg) translate3d(${Math.random() * 400 + 100}px,${Math.random() * 300}px,${Math.random() * 1000}px);
                                animation: move 60s infinite ${-10 - Math.random() * 20}s linear;
                        `;
                        papa.appendChild(ele);
                }
        })();

去中心化的原理是设置运动对象的 left 和 top 值。
收窄颜色范围通过设置 background 背景色实现,将 hls 颜色设置的色相 h 变为 ${Math.random() * 10 + 50},这将将能随机生成 50 - 60 个角度的色相,配合原图圆形萤火虫的色系。

冬天的雨 发表于 2022-11-6 08:28

马老师辛苦,一早就有新作品出炉{:4_199:}

马黑黑 发表于 2022-11-6 08:29

帖子全码
<style>
#papa { margin-left: calc(50% - 681px); margin-top:100px; width: 1200px; height: 675px; display: grid; place-items: center; background: #000 url('https://638183.freep.cn/638183/t22/webp/yhi.webp') no-repeat center/cover; box-shadow: 3px 3px 20px #000; perspective: 1000px; overflow: hidden; user-select: none; position: relative; z-index: 1; }
#papa::after { position: absolute; content: '萤火虫'; top: 10px; font: bold 2.6em sans-serif; color: gold; text-shadow: 2px 2px 3px #000; opacity: .6; }
#mplayer { position: absolute; bottom: 10px; z-index: 1000; }
#btnwrap { display: block; fill: hsla(30, 20%, 80%, .9); cursor: pointer; }
#btnwrap:hover { fill: hsla(90, 100%, 100%, .9); }
#tmsg { fill: hsla(30, 100%, 90%, .9); stroke: none; font: bold 1em sans-serif; }
.firefly { position: absolute; width: 2px; height: 2px; border-radius: 50%; background: silver; transform-style: preserve-3d; }
@keyframes move { to { transform: rotate(0) translate3d(0, 0, 0); } }
@keyframes flash { from {opacity: .95; } to { opacity: 1; } }
</style>

<div id="papa">
        <svg id="mplayer" width="120" height="120">
                <g id="mama" transform="rotate(-90, 60, 60)" style="cursor: pointer;">
                        <circle id="track" cx="60" cy="60" r="50" fill="none" stroke-width="10" stroke="hsla(60, 20%, 80%, .77)" />
                        <circle id="prog" cx="60" cy="60" r="50" fill="none" stroke-width="10" stroke="hsla(60,100%,40%,.65)" />
                </g>
                <g id="btnwrap">
                        <path id="btnplay" d="M 50 50,50 70,70, 60 z"></path>
                        <path id="btnpause" d="M 52 50,52 70,57 70,57 50,52 50 z M 60 50,60 70,65 70,65 50,60 50 z" style="display: none;"></path>
                        <path d="M 57 50,60 50,60 70,57 70 z" fill="transparent" />
                </g>
                <path id="curPath" d="M 20 70 Q 60 0 100 70" fill="none" stroke="none"/>
                <path id="durPath" d="M 20 55 Q 60 110 100 55" fill="none" stroke="none"/>
                <g id="tmsg">
                        <text x="34" y="0"><textPath id="curMsg" xlink:href="#curPath" dominant-baseline="text-after-edge">00:00</textPath></text>
                        <text x="29" y="0"><textPath id="durMsg" xlink:href="#durPath" dominant-baseline="text-before-edge">00:00</textPath></text>
                </g>
        </svg>
</div>
<audio id="aud" src="https://music.163.com/song/media/outer/url?id=509720124.mp3" loop autoplay></audio>

<script>
(function() {
        (function() {for(j=0; j<500; j++) {let ele = document.createElement('span');ele.className = 'firefly';ele.style.cssText += `left: ${Math.random() * 1100}px;top: ${Math.random() * 600}px;background: hsl(${Math.random() * 10 + 50}, ${Math.random() * 50 + 40}%,${Math.random() * 40 + 20}%);box-shadow: 0 0 5px hsla(0,10%,100%,.25);transform: rotate(${Math.random() * 360 + 360}deg) translate3d(${Math.random() * 400 + 100}px,${Math.random() * 300}px,${Math.random() * 1000}px);animation: move 60s infinite ${-10 - Math.random() * 20}s linear;`;papa.appendChild(ele);}})();
        let cc = { x: 1*track.getAttribute('cx'), y: 1*track.getAttribute('cy'), len: track.getTotalLength(),};
        prog.style.strokeDasharray = prog.style.strokeDashoffset =cc.len;
        btnwrap.onclick = () => aud.paused ? aud.play() : aud.pause();
        mama.onclick = (e) => {let deg = Math.atan2(e.offsetY - cc.y, e.offsetX - cc.x) * 180 / Math.PI;deg += (e.offsetX < cc.x && e.offsetY < cc.y) ? 450 : 90;aud.currentTime = aud.duration * deg / 360;};
        aud.addEventListener('pause', () => mState());
        aud.addEventListener('play', () => mState());
        aud.addEventListener('seeked', () => aud.play());
        aud.addEventListener('timeupdate', () => {prog.style.strokeDashoffset = cc.len - cc.len * aud.currentTime / aud.duration;curMsg.textContent = toMin(aud.currentTime);durMsg.textContent = toMin(aud.duration);});
        let mState = () => aud.paused ? (btnplay.style.display = 'block', btnpause.style.display = 'none') : (btnplay.style.display = 'none', btnpause.style.display = 'block');
        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;};
        document.title = '萤火虫';
})();

</script>


马黑黑 发表于 2022-11-6 08:30

冬天的雨 发表于 2022-11-6 08:28
马老师辛苦,一早就有新作品出炉

这个不是从头制作,在人类遗迹的基础上改动,主要想测试下运动物体的去中心化即色系的制定

马黑黑 发表于 2022-11-6 08:31

冬天的雨 发表于 2022-11-6 08:25
沙发吗

此刻 @小辣椒 的沙发随便抢{:4_170:}

冬天的雨 发表于 2022-11-6 08:34

马黑黑 发表于 2022-11-6 08:31
此刻 @小辣椒 的沙发随便抢

小辣椒肯定还在睡懒觉的{:4_170:}

起个网名好难 发表于 2022-11-6 08:36

本帖最后由 起个网名好难 于 2022-11-6 08:37 编辑

https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_match%2F0%2F13500260565%2F0&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1670286915&t=e3bea9ee3f81bbc5b248754619afbd36

马黑黑 发表于 2022-11-6 08:38

冬天的雨 发表于 2022-11-6 08:34
小辣椒肯定还在睡懒觉的

我拿个放大镜看看{:4_173:}

梦油 发表于 2022-11-6 09:34

现在,萤火虫可少见了,恐怕一些年轻的朋友都没见过。

马黑黑 发表于 2022-11-6 09:43

梦油 发表于 2022-11-6 09:34
现在,萤火虫可少见了,恐怕一些年轻的朋友都没见过。

对,城市生态如此

醉美水芙蓉 发表于 2022-11-6 09:49

相约爱晚亭 发表于 2022-11-6 10:46

收藏备用 ,黑黑老师辛苦了!

马黑黑 发表于 2022-11-6 11:15

相约爱晚亭 发表于 2022-11-6 10:46
收藏备用 ,黑黑老师辛苦了!

{:5_108:}

马黑黑 发表于 2022-11-6 11:16

醉美水芙蓉 发表于 2022-11-6 09:49
小辣椒快来坐沙发!@小辣椒 黑黑老师又有新作品了!

这个是改造一下昨天的,不算新吧

小辣椒 发表于 2022-11-6 11:43

冬天的雨 发表于 2022-11-6 08:25
沙发吗

冬雨今天早的,打游戏吧{:4_203:}

小辣椒 发表于 2022-11-6 11:45

马黑黑 发表于 2022-11-6 08:28
本帖与昨日的 人类遗迹 的主要区别在于:

① 运动对象去中心化;


是的,昨天我用 left 和 top没有效果出来

小辣椒 发表于 2022-11-6 11:46

马黑黑 发表于 2022-11-6 08:30
这个不是从头制作,在人类遗迹的基础上改动,主要想测试下运动物体的去中心化即色系的制定

谢谢黑黑的修改版出来{:4_199:}

马黑黑 发表于 2022-11-6 11:46

小辣椒 发表于 2022-11-6 11:45
是的,昨天我用 left 和 top没有效果出来

对于运动对象,就是那些多多的span,要用物理定位。它们的关键帧动作以它们的元素位置为参照
页: [1] 2 3 4 5
查看完整版本: 夜 萤火虫和你