马黑黑 发表于 2024-3-22 11:53

叶水相逢

本帖最后由 马黑黑 于 2024-3-22 19:49 编辑 <br /><br /><style>
        #papa { margin: -60px 0 0 calc(50% - 593px); width: 1024px; height: 600px; box-shadow: 3px 3px 6px #333; z-index: 1; position: relative; }
        #canv { display: block; cursor: pointer; }
</style>

<div id="papa">
        <audio id="aud" src="https://music.163.com/song/media/outer/url?id=501214981" autoplay loop></audio>
        <canvas id="canv" title="播放/暂停"></canvas>
</div>

<script>
(function () {
        let ctx = canv.getContext("2d");
        let img = new Image();
        img.src = "https://638183.freep.cn/638183/t24/1/u9.jpg";

        let CanvasXSize = canv.width = papa.offsetWidth,
                CanvasYSize = canv.height = papa.offsetHeight,
                speed = 30,
                scale = 1.05,
                x = 0,
                y = -4.5,
                dx = 0.75;
        let imgW, imgH, clearX, clearY, mTimer;

        let draw = () => {
                ctx.clearRect(0, 0, clearX, clearY);
                if(imgW <= CanvasXSize) {
                        if(x > CanvasXSize) x = -imgW + x;
                        if(x > 0) ctx.drawImage(img, -imgW + x, y, imgW, imgH);
                        if(x - imgW > 0) ctx.drawImage(img, -imgW * 2 + x, y, imgW, imgH);
                }else{
                        if(x > CanvasXSize) x = CanvasXSize - imgW;
                        if(x > CanvasXSize - imgW) ctx.drawImage(img, x - imgW + 1, y, imgW, imgH);
                }
                ctx.drawImage(img, x, y, imgW, imgH);
                x += dx;
        };

        let mState = () => mTimer = aud.paused ? clearInterval(mTimer) : setInterval(draw, speed);

        img.onload = () => {
                imgW = img.width * scale;
                imgH = img.height * scale;
                if(imgW > CanvasXSize) x = CanvasXSize - imgW;
                clearX = imgW > CanvasXSize ? imgW : CanvasXSize;
                clearY = imgH > CanvasYSize ? imgH : CanvasYSize;
                draw();
        };

        aud.onpause = aud.onplaying = () => mState();
        aud.onseeking = () => mTimer = clearInterval(mTimer);
        canv.onclick = () => aud.paused ? aud.play() : aud.pause();
})();
</script>

马黑黑 发表于 2024-3-22 11:56

<style>
.mum { position: relative; margin: 0; padding: 10px; font: normal 16px/20px Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; color: black; background: rgba(240, 240, 240,.95); box-shadow: 2px 2px 4px gray; border: thick groove lightblue; border-radius: 6px; }
.mum ::selection { background-color: rgba(0,100,100,.35); }
.mum div { margin: 0; padding: 0; }
.mum cl-cd { display: block; position: relative; margin: 0 0 0 50px; padding: 0 0 0 10px; white-space: pre-wrap; overflow-wrap: break-word; border-left: 1px solid silver; }
.mum cl-cd::before { position: absolute; content: attr(data-idx); width: 50px; color: gray; text-align: right; transform: translate(-70px); }
.tRed { color: red; }
.tBlue { color: blue; }
.tGreen { color: green; }
.tDarkRed { color: darkred; }
.tMagenta { color: magenta; }
</style>
<h2>帖子代码</h2>
<div class='mum'>
<cl-cd data-idx="1">&lt;<span class="tDarkRed">style</span>&gt;</cl-cd>
<cl-cd data-idx="2">&nbsp; &nbsp; #papa { <span class="tBlue">margin:</span>0 0 0 calc(50% - 593px); <span class="tBlue">width:</span> 1024px; <span class="tBlue">height:</span> 600px; <span class="tBlue">box-shadow:</span> 3px 3px 6px #333; <span class="tBlue">z-index:</span> 1; }</cl-cd>
<cl-cd data-idx="3">&nbsp; &nbsp; #canv { <span class="tBlue">cursor:</span> pointer; }</cl-cd>
<cl-cd data-idx="4">&lt;<span class="tDarkRed">/style</span>&gt;</cl-cd>
<cl-cd data-idx="5">&nbsp;</cl-cd>
<cl-cd data-idx="6">&lt;<span class="tDarkRed">div</span> <span class="tRed">id</span>=<span class="tMagenta">"papa"</span>&gt;</cl-cd>
<cl-cd data-idx="7">&nbsp; &nbsp; &lt;<span class="tDarkRed">audio</span> <span class="tRed">id</span>=<span class="tMagenta">"aud"</span> src=<span class="tMagenta">"https://music.163.com/song/media/outer/url?<span class="tRed">id</span>=501214981"</span> autoplay loop&gt;&lt;<span class="tDarkRed">/audio</span>&gt;</cl-cd>
<cl-cd data-idx="8">&nbsp; &nbsp; &lt;<span class="tDarkRed">canvas</span> <span class="tRed">id</span>=<span class="tMagenta">"canv"</span> width=<span class="tMagenta">"1024"</span> height=<span class="tMagenta">"600"</span> style=<span class="tMagenta">"<span class="tBlue">border:</span> 1px solid gray"</span> title=<span class="tMagenta">"播放/暂停"</span>&gt;&lt;<span class="tDarkRed">/canvas</span>&gt;</cl-cd>
<cl-cd data-idx="9">&lt;<span class="tDarkRed">/div</span>&gt;</cl-cd>
<cl-cd data-idx="10">&nbsp;</cl-cd>
<cl-cd data-idx="11">&lt;<span class="tDarkRed">script</span>&gt;</cl-cd>
<cl-cd data-idx="12"><span class="tGreen">/* 画面移动代码改自Firefox工程师的示例 */</span></cl-cd>
<cl-cd data-idx="13">(<span class="tBlue">function</span> () {</cl-cd>
<cl-cd data-idx="14">&nbsp; &nbsp; <span class="tBlue">let</span> ctx = canv.getContext(<span class="tMagenta">"2d"</span>);</cl-cd>
<cl-cd data-idx="15">&nbsp; &nbsp; <span class="tBlue">let</span> img = <span class="tBlue">new</span> Image();</cl-cd>
<cl-cd data-idx="16">&nbsp; &nbsp; img.src = <span class="tMagenta">"https://638183.freep.cn/638183/t24/1/u9.jpg"</span>;</cl-cd>
<cl-cd data-idx="17">&nbsp;</cl-cd>
<cl-cd data-idx="18">&nbsp; &nbsp; <span class="tBlue">let</span> CanvasXSize = papa.offsetWidth,</cl-cd>
<cl-cd data-idx="19">&nbsp; &nbsp; &nbsp; &nbsp; CanvasYSize = papa.offsetHeight,</cl-cd>
<cl-cd data-idx="20">&nbsp; &nbsp; &nbsp; &nbsp; speed = 30,</cl-cd>
<cl-cd data-idx="21">&nbsp; &nbsp; &nbsp; &nbsp; scale = 1.05,</cl-cd>
<cl-cd data-idx="22">&nbsp; &nbsp; &nbsp; &nbsp; x = 0,</cl-cd>
<cl-cd data-idx="23">&nbsp; &nbsp; &nbsp; &nbsp; y = -4.5,</cl-cd>
<cl-cd data-idx="24">&nbsp; &nbsp; &nbsp; &nbsp; dx = 0.75;</cl-cd>
<cl-cd data-idx="25">&nbsp; &nbsp; <span class="tBlue">let</span> imgW, imgH, clearX, clearY, mTimer;</cl-cd>
<cl-cd data-idx="26">&nbsp;</cl-cd>
<cl-cd data-idx="27">&nbsp; &nbsp; <span class="tBlue">let</span> draw = () =&gt; {</cl-cd>
<cl-cd data-idx="28">&nbsp; &nbsp; &nbsp; &nbsp; ctx.clearRect(0, 0, clearX, clearY);</cl-cd>
<cl-cd data-idx="29">&nbsp; &nbsp; &nbsp; &nbsp; <span class="tBlue">if</span>(imgW &lt;= CanvasXSize) {</cl-cd>
<cl-cd data-idx="30">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="tBlue">if</span>(x &gt; CanvasXSize) x = -imgW + x;</cl-cd>
<cl-cd data-idx="31">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="tBlue">if</span>(x &gt; 0) ctx.drawImage(img, -imgW + x, y, imgW, imgH);</cl-cd>
<cl-cd data-idx="32">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="tBlue">if</span>(x - imgW &gt; 0) ctx.drawImage(img, -imgW * 2 + x, y, imgW, imgH);</cl-cd>
<cl-cd data-idx="33">&nbsp; &nbsp; &nbsp; &nbsp; }<span class="tBlue">else</span>{</cl-cd>
<cl-cd data-idx="34">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="tBlue">if</span>(x &gt; CanvasXSize) x = CanvasXSize - imgW;</cl-cd>
<cl-cd data-idx="35">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="tBlue">if</span>(x &gt; CanvasXSize - imgW) ctx.drawImage(img, x - imgW + 1, y, imgW, imgH);</cl-cd>
<cl-cd data-idx="36">&nbsp; &nbsp; &nbsp; &nbsp; }</cl-cd>
<cl-cd data-idx="37">&nbsp; &nbsp; &nbsp; &nbsp; ctx.drawImage(img, x, y, imgW, imgH);</cl-cd>
<cl-cd data-idx="38">&nbsp; &nbsp; &nbsp; &nbsp; x += dx;</cl-cd>
<cl-cd data-idx="39">&nbsp; &nbsp; };</cl-cd>
<cl-cd data-idx="40">&nbsp;</cl-cd>
<cl-cd data-idx="41">&nbsp; &nbsp; <span class="tBlue">let</span> mState = () =&gt; mTimer = aud.paused ? clearInterval(mTimer) : setInterval(draw, speed);</cl-cd>
<cl-cd data-idx="42">&nbsp;</cl-cd>
<cl-cd data-idx="43">&nbsp; &nbsp; img.onload = () =&gt; {</cl-cd>
<cl-cd data-idx="44">&nbsp; &nbsp; &nbsp; &nbsp; imgW = img.width * scale;</cl-cd>
<cl-cd data-idx="45">&nbsp; &nbsp; &nbsp; &nbsp; imgH = img.height * scale;</cl-cd>
<cl-cd data-idx="46">&nbsp; &nbsp; &nbsp; &nbsp; <span class="tBlue">if</span>(imgW &gt; CanvasXSize) x = CanvasXSize - imgW;</cl-cd>
<cl-cd data-idx="47">&nbsp; &nbsp; &nbsp; &nbsp; clearX = imgW &gt; CanvasXSize ? imgW&nbsp;: CanvasXSize;</cl-cd>
<cl-cd data-idx="48">&nbsp; &nbsp; &nbsp; &nbsp; clearY = imgH &gt; CanvasYSize ? imgH&nbsp;: CanvasYSize;</cl-cd>
<cl-cd data-idx="49">&nbsp; &nbsp; &nbsp; &nbsp; draw();</cl-cd>
<cl-cd data-idx="50">&nbsp; &nbsp; };</cl-cd>
<cl-cd data-idx="51">&nbsp;</cl-cd>
<cl-cd data-idx="52">&nbsp; &nbsp; aud.onpause = aud.onplaying = () =&gt; mState();</cl-cd>
<cl-cd data-idx="53">&nbsp; &nbsp; canv.onclick = () =&gt; aud.paused ? aud.play() : aud.pause();</cl-cd>
<cl-cd data-idx="54">})();</cl-cd>
<cl-cd data-idx="55">&lt;<span class="tDarkRed">/script</span>&gt;</cl-cd>
<cl-cd data-idx="56">&nbsp;</cl-cd>
</div>

马黑黑 发表于 2024-3-22 12:07

对图片的要求:

① 最理想的图片尺寸是,高度和 #papa 一样,宽度大于 #papa 的宽度;

② 图片最好是全景2d图片,纯正的2d全景图可以确保头尾衔接无缝。

樵歌 发表于 2024-3-22 12:50

流动的山水与空灵的琴声,让人放松。

南无月 发表于 2024-3-22 13:04

在画布上的移动的画。。。
无缝衔接,无限循环。。。
好看。。

马黑黑 发表于 2024-3-22 13:21

南无月 发表于 2024-3-22 13:04
在画布上的移动的画。。。
无缝衔接,无限循环。。。
好看。。

要的是这个效果

马黑黑 发表于 2024-3-22 13:22

樵歌 发表于 2024-3-22 12:50
流动的山水与空灵的琴声,让人放松。

忘记吃瓜皮了都

起个网名好难 发表于 2024-3-22 16:49

为了用canvas ?

小文 发表于 2024-3-22 16:52

美不胜收的好贴,极喜欢!{:4_191:}问好并赞许先生!

南无月 发表于 2024-3-22 17:44

马黑黑 发表于 2024-3-22 13:21
要的是这个效果

效果极佳 ,音乐极美 。。一流好贴。{:4_170:}

马黑黑 发表于 2024-3-22 18:57

南无月 发表于 2024-3-22 17:44
效果极佳 ,音乐极美 。。一流好贴。

不是三流了么

马黑黑 发表于 2024-3-22 19:00

小文 发表于 2024-3-22 16:52
美不胜收的好贴,极喜欢!问好并赞许先生!

晚上好{:4_191:}

小文 发表于 2024-3-22 19:02

马黑黑 发表于 2024-3-22 19:00
晚上好

晚安!

大猫咪 发表于 2024-3-22 19:24

人在画中游   美不胜收!   这地方好熟悉阿黑黑{:5_117:}   赞!

{:4_204:}{:4_178:}{:4_199:}

马黑黑 发表于 2024-3-22 19:49

大猫咪 发表于 2024-3-22 19:24
人在画中游   美不胜收!   这地方好熟悉阿黑黑   赞!

香格里拉往南那么一点点,俺以前经常在那里下河捞虾捉五色鱼儿

马黑黑 发表于 2024-3-22 19:50

小文 发表于 2024-3-22 19:02
晚安!

这么早休息?

大猫咪 发表于 2024-3-22 19:54

马黑黑 发表于 2024-3-22 19:49
香格里拉往南那么一点点,俺以前经常在那里下河捞虾捉五色鱼儿

普达措 – 你翩翩而来,我翘首以待{:5_117:}

马黑黑 发表于 2024-3-22 20:22

大猫咪 发表于 2024-3-22 19:54
普达措 – 你翩翩而来,我翘首以待

欧?那你得有极大的耐心,俺这人都不太留恋曾经呆过的地方

大猫咪 发表于 2024-3-22 20:25

马黑黑 发表于 2024-3-22 20:22
欧?那你得有极大的耐心,俺这人都不太留恋曾经呆过的地方

真黑{:4_397:}{:5_117:}

小文 发表于 2024-3-22 20:35

马黑黑 发表于 2024-3-22 19:50
这么早休息?

回先生,没有。是想跟先生道个晚安,别太累了!保重身体!{:4_191:}
页: [1] 2 3 4
查看完整版本: 叶水相逢