马黑黑 发表于 2024-4-9 20:49

檫黑板

本帖最后由 马黑黑 于 2024-4-9 20:54 编辑 <br /><br /><style>
        #papa { margin: -60px 0 0 calc(50% - 593px); width: 1024px; height: 576px; background: url('https://638183.freep.cn/638183/t24/1/girl.jpg') no-repeat center/cover; box-shadow: 3px 3px 8px #000; z-index: 1; position: relative; --dis: none; --xx: 0; --yy: 0; }
        #papa::before { position: absolute; content: ''; left: var(--xx); top: var(--yy); width: 50px; height: 50px; border: 1px solid green; pointer-events: none; display: var(--dis); }
        #canv { position: absolute; inset: 0; cursor: crosshair; }
</style>

<div id="papa">
        <audio id="aud" src="https://music.163.com/song/media/outer/url?id=1907751641" autoplay loop></audio>
        <canvas id="canv" width="1024px" height="640px"></canvas>
</div>

<script>
(function() {
let ww = canv.width = papa.offsetWidth, hh = canv.height = papa.offsetHeight;
let clearTimer;
let ctx = canv.getContext('2d');

let img = document.createElement('img');

img.onload = () => {
        if(img.complete) {
                ctx.drawImage(img, 0, 0, ww, hh);
        }
};

img.src = 'https://638183.freep.cn/638183/t24/1/u06.jpg';

canv.onmousemove = (e) => {
        if(clearTimer) clearTimeout(clearTimer);
        let x = e.offsetX, y = e.offsetY;
        papa.style.setProperty('--xx', x - 25 + 'px');
        papa.style.setProperty('--yy', y - 25 + 'px');
        papa.style.setProperty('--dis', 'block');
        ctx.clearRect(x - 25, y - 25, 50, 50);
};

canv.onmouseout = () => {
        clearTimer = setTimeout(() => {
                papa.style.setProperty('--dis', 'none');
                ctx.drawImage(img, 0, 0, ww, hh);
        }, 3000);
};

canv.onclick = () => aud.paused ? aud.play() : aud.pause();
})();
</script>

马黑黑 发表于 2024-4-9 20:49

<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> 576px; <span class="tBlue">background:</span> url(<span class="tMagenta">'https://638183.freep.cn/638183/t24/1/girl.jpg'</span>) no-repeat center/cover; <span class="tBlue">box-shadow:</span> 3px 3px 8px #000; <span class="tBlue">position:</span> relative; <span class="tBlue">--dis:</span> none; <span class="tBlue">--xx:</span> 0; <span class="tBlue">--yy:</span> 0; }</cl-cd>
<cl-cd data-idx="3">&nbsp; &nbsp; #papa::before { <span class="tBlue">position:</span> absolute; <span class="tBlue">content:</span> <span class="tMagenta">''</span>; <span class="tBlue">left:</span> <span class="tBlue">var</span>(--xx); <span class="tBlue">top:</span> <span class="tBlue">var</span>(--yy); <span class="tBlue">width:</span> 50px; <span class="tBlue">height:</span> 50px; <span class="tBlue">border:</span> 1px solid green; <span class="tBlue">pointer-events:</span> none; <span class="tBlue">display:</span> <span class="tBlue">var</span>(--dis); }</cl-cd>
<cl-cd data-idx="4">&nbsp; &nbsp; #canv { <span class="tBlue">position:</span> absolute; <span class="tBlue">inset:</span> 0; <span class="tBlue">cursor:</span> crosshair; }</cl-cd>
<cl-cd data-idx="5">&lt;<span class="tDarkRed">/style</span>&gt;</cl-cd>
<cl-cd data-idx="6">&nbsp;</cl-cd>
<cl-cd data-idx="7">&lt;<span class="tDarkRed">div</span> <span class="tRed">id</span>=<span class="tMagenta">"papa"</span>&gt;</cl-cd>
<cl-cd data-idx="8">&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>=1907751641"</span> autoplay loop&gt;&lt;<span class="tDarkRed">/audio</span>&gt;</cl-cd>
<cl-cd data-idx="9">&nbsp; &nbsp; &lt;<span class="tDarkRed">canvas</span> <span class="tRed">id</span>=<span class="tMagenta">"canv"</span> width=<span class="tMagenta">"1024px"</span> height=<span class="tMagenta">"640px"</span>&gt;&lt;<span class="tDarkRed">/canvas</span>&gt;</cl-cd>
<cl-cd data-idx="10">&lt;<span class="tDarkRed">/div</span>&gt;</cl-cd>
<cl-cd data-idx="11">&nbsp;</cl-cd>
<cl-cd data-idx="12">&lt;<span class="tDarkRed">script</span>&gt;</cl-cd>
<cl-cd data-idx="13">&nbsp; &nbsp; </cl-cd>
<cl-cd data-idx="14"><span class="tBlue">let</span> ww = canv.width = papa.offsetWidth, hh = canv.height = papa.offsetHeight;</cl-cd>
<cl-cd data-idx="15"><span class="tBlue">let</span> clearTimer;</cl-cd>
<cl-cd data-idx="16"><span class="tBlue">let</span> ctx = canv.getContext(<span class="tMagenta">'2d'</span>);</cl-cd>
<cl-cd data-idx="17">&nbsp;</cl-cd>
<cl-cd data-idx="18"><span class="tBlue">let</span> img = <span class="tRed">document</span>.createElement(<span class="tMagenta">'img'</span>);</cl-cd>
<cl-cd data-idx="19">&nbsp;</cl-cd>
<cl-cd data-idx="20">img.onload = () =&gt; {</cl-cd>
<cl-cd data-idx="21">&nbsp; &nbsp; <span class="tBlue">if</span>(img.complete) {</cl-cd>
<cl-cd data-idx="22">&nbsp; &nbsp; &nbsp; &nbsp; ctx.drawImage(img, 0, 0, ww, hh);</cl-cd>
<cl-cd data-idx="23">&nbsp; &nbsp; }</cl-cd>
<cl-cd data-idx="24">};</cl-cd>
<cl-cd data-idx="25">&nbsp;</cl-cd>
<cl-cd data-idx="26">img.src = <span class="tMagenta">'https://638183.freep.cn/638183/t24/1/u06.jpg'</span>;</cl-cd>
<cl-cd data-idx="27">&nbsp;</cl-cd>
<cl-cd data-idx="28">canv.onmousemove = (e) =&gt; {</cl-cd>
<cl-cd data-idx="29">&nbsp; &nbsp; <span class="tBlue">if</span>(clearTimer) clearTimeout(clearTimer);</cl-cd>
<cl-cd data-idx="30">&nbsp; &nbsp; <span class="tBlue">let</span> x = e.offsetX, y = e.offsetY;</cl-cd>
<cl-cd data-idx="31">&nbsp; &nbsp; papa.style.setProperty(<span class="tMagenta">'--xx'</span>, x - 25 + <span class="tMagenta">'px'</span>);</cl-cd>
<cl-cd data-idx="32">&nbsp; &nbsp; papa.style.setProperty(<span class="tMagenta">'--yy'</span>, y - 25 + <span class="tMagenta">'px'</span>);</cl-cd>
<cl-cd data-idx="33">&nbsp; &nbsp; papa.style.setProperty(<span class="tMagenta">'--dis'</span>, <span class="tMagenta">'block'</span>);</cl-cd>
<cl-cd data-idx="34">&nbsp; &nbsp; ctx.clearRect(x - 25, y - 25, 50, 50);</cl-cd>
<cl-cd data-idx="35">};</cl-cd>
<cl-cd data-idx="36">&nbsp;</cl-cd>
<cl-cd data-idx="37">canv.onmouseout = () =&gt; {</cl-cd>
<cl-cd data-idx="38">&nbsp; &nbsp; clearTimer = setTimeout(() =&gt; {</cl-cd>
<cl-cd data-idx="39">&nbsp; &nbsp; &nbsp; &nbsp; papa.style.setProperty(<span class="tMagenta">'--dis'</span>, <span class="tMagenta">'none'</span>);</cl-cd>
<cl-cd data-idx="40">&nbsp; &nbsp; &nbsp; &nbsp; ctx.drawImage(img, 0, 0, ww, hh);</cl-cd>
<cl-cd data-idx="41">&nbsp; &nbsp; }, 3000);</cl-cd>
<cl-cd data-idx="42">};</cl-cd>
<cl-cd data-idx="43">&nbsp;</cl-cd>
<cl-cd data-idx="44">canv.onclick = () =&gt; aud.paused ? aud.play() : aud.pause();</cl-cd>
<cl-cd data-idx="45">&nbsp;</cl-cd>
<cl-cd data-idx="46">&lt;<span class="tDarkRed">/script</span>&gt;</cl-cd>
</div>

马黑黑 发表于 2024-4-9 20:50

帖子显示次序可能处理的不太好,如果只有镁铝,请刷新一下

马黑黑 发表于 2024-4-9 20:56

二楼代码,需要将JS放置在闭包里面,整体效果才能在论坛完美渲染:

(function() {
   //这里是 14~44行代码
})();

南无月 发表于 2024-4-9 21:17

马黑黑 发表于 2024-4-9 20:50
帖子显示次序可能处理的不太好,如果只有镁铝,请刷新一下

开始黑板上画的是郁金香,擦完黑板是美女{:4_170:}

南无月 发表于 2024-4-9 21:18

马黑黑 发表于 2024-4-9 20:56
二楼代码,需要将JS放置在闭包里面,整体效果才能在论坛完美渲染:

(function() {


这个闭包是论坛的特色。。必须记住。。。{:4_187:}

红影 发表于 2024-4-9 21:29

原来擦黑板就是自己用鼠标把一层图片擦掉,露出另一层图图啊。这个有趣{:4_173:}

红影 发表于 2024-4-9 21:33

擦完的黑板还能图片另存呢。试了一下,存的是擦完部分的png图图,底下的美女不见了。
这个代码倒是方便用鼠标涂抹一个想要的透明图了呢。

红影 发表于 2024-4-9 21:38

点击画面就暂停了,暂停的时候还不耽误继续擦黑板{:4_173:}

马黑黑 发表于 2024-4-9 22:37

红影 发表于 2024-4-9 21:38
点击画面就暂停了,暂停的时候还不耽误继续擦黑板

檫黑板不需要点

马黑黑 发表于 2024-4-9 22:38

红影 发表于 2024-4-9 21:29
原来擦黑板就是自己用鼠标把一层图片擦掉,露出另一层图图啊。这个有趣

目前不支持移动端浏览器。移动端没有 mousemove 事件,它需要 touchmove 事件,我还没加上

马黑黑 发表于 2024-4-9 22:39

红影 发表于 2024-4-9 21:33
擦完的黑板还能图片另存呢。试了一下,存的是擦完部分的png图图,底下的美女不见了。
这个代码倒是方便用 ...

这里面可以有许多创意的

马黑黑 发表于 2024-4-9 22:42

南无月 发表于 2024-4-9 21:18
这个闭包是论坛的特色。。必须记住。。。

{:4_190:}

马黑黑 发表于 2024-4-9 22:43

南无月 发表于 2024-4-9 21:17
开始黑板上画的是郁金香,擦完黑板是美女

偶尔会出现先镁铝出来

红影 发表于 2024-4-10 10:35

马黑黑 发表于 2024-4-9 22:37
檫黑板不需要点

嗯嗯,点击是控制暂停的,擦的时候不需要点击。

红影 发表于 2024-4-10 10:36

马黑黑 发表于 2024-4-9 22:38
目前不支持移动端浏览器。移动端没有 mousemove 事件,它需要 touchmove 事件,我还没加上

我很少手机玩,现在的就很好{:4_173:}
嗯,一个是鼠标操控,一个是触屏操作。

红影 发表于 2024-4-10 10:37

马黑黑 发表于 2024-4-9 22:39
这里面可以有许多创意的

是啊,特别好玩,发现鼠标移动得快点,还是一个个不连续的方块挖空呢{:4_173:}

老谟深虑 发表于 2024-4-10 13:01

         谢谢黑黑老师,这个很好玩,收藏学习了。

马黑黑 发表于 2024-4-10 14:14

老谟深虑 发表于 2024-4-10 13:01
谢谢黑黑老师,这个很好玩,收藏学习了。

{:4_190:}

马黑黑 发表于 2024-4-10 14:14

红影 发表于 2024-4-10 10:37
是啊,特别好玩,发现鼠标移动得快点,还是一个个不连续的方块挖空呢

要从容
页: [1] 2 3 4 5
查看完整版本: 檫黑板