亚伦影音工作室 发表于 2023-3-30 17:11

HTML5音画还能这样做

本帖最后由 亚伦影音工作室 于 2023-3-30 17:48 编辑 <br /><br /><style>
#papa {
        margin: 10px -300px;
        width: 1164px;
        height: 620px;
        background: linear-gradient(132deg, rgba(13, 34, 237, 0.41) 0%,#1a2a6c 33%,#b21f1f 82%,#F20411 100%);
        box-shadow: 3px 3px 2px #000;
        position: relative;
        z-index: 1;
}
#papa:hover #mplayer { transition: .7s; opacity: .9; }
#tp {position: absolute;box-shadow: 0px 0px 0px 1px #000000, 0px 0px 10px 13px #cccccc;
        left: 520px;border-radius: 1%;z-index: 2;
        top: 20%;background: url('https://img-baofun.zhhainiao.com/pcwallpaper_ugc/static/29c50e716edbe7f5bd4004c1fae3a5cc.jpg') center/cover no-repeat;
        width: 570px;transform:perspective(600px) rotateY(-10deg)scale(1);
        height: 350px;opacity: 0.7;}


#vid { display: none; }
#canv {position: absolute;
        display: block;background: url('https://pic.imgdb.cn/item/64255765a682492fcc2ccc83.jpg')0 0/100% 100%;
        left: 0%; mix-blend-mode: soft-light;
        top: 0%;
        border-radius: 0%;
        opacity: 1;
        animation: opa 0s infinite alternate ;
}
#mplayer {
        position: absolute;
        left:5%;
        bottom: 200px;
        width: 40px;
        height: 30px;
        border: 2px solid #bbbbbb;
        border-radius: 10%;
        opacity: 0.2;transform:perspective(80px) rotateY(15deg)rotate(-5deg)scale(1);
        transition: .7s;
        display: grid;
        place-items: center;
        --disp1: 0; --disp2: 1;
}
#mplayer::before, #mplayer::after {
        position: absolute;
        content: '';
        border-style: solid;
        border-color: #bbbbbb;
        cursor: pointer;
        transition: .4s;
}
#mplayer::before {
        width: 0;
        height: 0;
        left:40%;
        border-width: 10px 12px;
        border-color: transparent transparent transparent #bbbbbb;
        opacity: var(--disp1);
}
#mplayer::after {
        width: 2px;
        height: 20px;left:40%;
        border-width: 0 4px 0 4px;
        opacity: var(--disp2);
}
@keyframes opa { to {opacity: 0;} }
</style>

<div id="papa">
<div id="tp"></div>
        <canvas id="canv"></canvas>
        <div id="mplayer"></div>
<div data-lrc="亚伦影音工作室" id="lrc">亚伦影音工作室</div>
</div>
<audio id="aud" src="https://www.qqmc.com/mp3/music257220424.mp3" loop autoplay></audio>
<video id="vid" src="https://www.kumeiwp.com/sub/filestores/2023/03/29/77eb63d2a8e740063d49b905d2c9fdb1.mp4" imgurl="https://pic.imgdb.cn/item/64255765a682492fcc2ccc83.jpg"
poster="https://pic.imgdb.cn/item/64255765a682492fcc2ccc83.jpg" loop autoplay muted></video>

<script>
(function(){
let ctx = canv.getContext('2d');

let ww = canv.width =1164, hh = canv.height =620;

let loop = () => {ctx.drawImage(vid, 0, 0, ww, hh);if(!vid.paused) {requestAnimationFrame(loop);return;}}
let mState = () => aud.paused ? (mplayer.style.setProperty('--disp1','1'), mplayer.style.setProperty('--disp2','0'), vid.pause()) : (mplayer.style.setProperty('--disp1','0'), mplayer.style.setProperty('--disp2','1'), vid.play());
vid.addEventListener('play', loop, false);
aud.addEventListener('play', () => mState());
aud.addEventListener('pause', () => mState());
mplayer.addEventListener('click', () => aud.paused ? aud.play() : aud.pause());
})();
</script>
<style type="text/css">#lrc {
        --state: paused;
        --motion: cover2;
        --tt: 2s;
        --bg: linear-gradient(180deg, #880000, #880000);
        position: absolute;z-index: 3;
        left: 60%;
        transform: translate(50%);
        top: 68%;transform:perspective(200px) rotate(3deg)rotateY(-10deg)scale(1);
        font:normal 2em 华文隶书;
        color: #000078;
        white-space: pre;
        -webkit-background-clip: text;
        filter:drop-shadow(#FFFFFF 1px 0 0)drop-shadow(#FFFFFF 0 1px 0)drop-shadow(#FFFFFF -1px 0 0) drop-shadow(#FFFFFF 0 -1px0);
}
#lrc::before {
        position: absolute;
        content: attr(data-lrc);
        width: 20%;
        height: 100%;
        color: transparent;
        overflow: hidden;
        white-space: pre;
        background: var(--bg);
       
        -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>
<script >
(function() {
/*原始lrc歌词*/
let lrcStr = `我们爱过也痛过
演唱:李英
词:徐晓岚
曲:李勇军
制作人:亚伦
OP 亚伦影音工作室
LRC歌词编辑:亚伦
爱情就像一条长河
怎么用力也趟不过
回忆让我心如刀割
想着想着我却哭了
纵然也有太多不舍
可感情不能再续了
现实的枷锁难逃脱
面对生活无可奈何
我们曾爱过也痛过
风风雨雨一起度过
曾经有过卿卿我我
只是如今没了结果
曾经彼此暖在心窝
曾经我们狂热似火
只是如今变得冷了
欢迎聆听




`;

/*变量 :mKey - 当前歌词索引;mFlag :调用关键帧动画索引;averAdd :平均值补偿*/
let mKey = 0, mFlag = true, averAdd = 0.3;

/*函数 :获取每句歌词用时,歌词用时若超过平均值则取平均值,最后一句歌词则取平均值*/
let lrcTime = (ar) => {
        let tmpAr = [];
        for(j = 0; j <ar.length - 1; j ++) {
                if(j !== ar.length - 1) tmpAr = parseFloat((ar - ar).toFixed(1));
        }
        let aver = parseInt(tmpAr.reduce((a,b) => a + b) / (tmpAr.length - 1)) + averAdd;
        tmpAr.push(aver);
        tmpAr.forEach((item,key) => {
                ar = item > aver ? aver : item;
        });
        return ar;
};

/*函数 :从原始lrc歌词获取信息并存入 n*3 数组*/
let getLrcAr = (text) => {
        let lrcAr = [];
        let calcRule = ;
        for(x of text.split('\n')) {
                let ar = [];
                let re = /\d+[\.:]\d+([\.:]\d+)?/g;
                let geci = x.replace(re,'');
                if(geci) {
                        geci = geci.replace(/[\[\]\'\"\t,]s?/g,'');
                        let time = x.match(re);
                        if(time != null) {
                                for(y of time) {
                                        let tmp = y.match(/\d+/g);
                                        let sec = 0;
                                        for(z in tmp) sec += tmp * calcRule;
                                        ar = ;
                                        lrcAr.push(ar);
                                }
                        }
                }
        }
        lrcAr.sort((a,b)=> a - b);
        return(lrcTime(lrcAr));
};

/*函数 :模拟显示同步歌词*/
let showLrc = (time) => {
        let name = mFlag ? 'cover1' : 'cover2';
        lrc.innerHTML = lrcAr;
        lrc.dataset.lrc = lrcAr;
        lrc.style.setProperty('--motion', name);
        lrc.style.setProperty('--tt', time + 's');
        lrc.style.setProperty('--state', 'running');
        mKey += 1;
        mFlag = !mFlag;
};

/*函数 :处理当前歌词索引 mKey*/
let calcKey = () => {
        for (j = 0; j < lrcAr.length; j++) {
                if (aud.currentTime <= lrcAr) {
                        mKey = j - 1;
                        break;
                }
        }
        if (mKey < 0) mKey = 0;
        if (mKey > lrcAr.length - 1) mKey = lrcAr.length - 1;
        let time = lrcAr - (aud.currentTime - lrcAr);
        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;
}

/*函数 :关键帧动画状态切换*/
let mState = () => aud.paused ? (lrc.style.setProperty('--state','paused'),mplayer.style.animationPlayState = 'paused') : (lrc.style.setProperty('--state','running'),mplayer.style.animationPlayState = 'running');

/*监听播放进度*/
aud.addEventListener('timeupdate', () => {
        for (j = 0; j < lrcAr.length; j++) {
                if (aud.currentTime >= lrcAr) {
                        cKey = j;
                        if (mKey === j) showLrc(lrcAr);
                        else continue;
                }
        }
});
aud.addEventListener('pause', () => mState());/*监听暂停事件*/
aud.addEventListener('play', () => mState());/*监听播放事件*/
aud.addEventListener('seeked', () => calcKey());/*监听查询事件*/
let lrcAr = getLrcAr(lrcStr); /*获得歌词数组*/
})();
</script>

亚伦影音工作室 发表于 2023-3-30 17:57

若手机视频背景不显示则显示静态背景,这样就不觉得背景空白了!

绿叶清舟 发表于 2023-3-30 19:28

厉害啊,象放电影了

小辣椒 发表于 2023-3-30 20:35

亚伦这个视频背景做的好,我现在电脑欣赏的

小辣椒 发表于 2023-3-30 20:36

感觉光感还可以加一点

红影 发表于 2023-3-30 20:39

这个做得漂亮。欣赏亚伦老师好帖{:4_187:}
页: [1]
查看完整版本: HTML5音画还能这样做