简体   繁体   English

JS画布,如何使图像慢慢消失?

[英]JS Canvas, How to make an image slowly dissapear?

Here is the code I have for now: 这是我现在拥有的代码:

 <!DOCTYPE html> <html> <head> <style> canvas{border:#666 2px solid; } </style> </head> <body> <canvas id="canvas1" width="1650" height="825" style="width: 650px; height: 330px;"></canvas> <script> var canvas,ctx,x,y,w,e; var slikeURL = [ "https://www.a1smallbusinessmarketing.com/images/prosbo_hires.jpg", "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f9/Wiktionary_small.svg/350px-Wiktionary_small.svg.png", "http://www.jasonjsmith.com/img/small-business-seo.jpg", "https://g.twimg.com/business/page/image/11TwitterForSmallBusiness-300_1.png" ] var slike = slikeURL.map(url=>{var img = new Image(); img.src = url; return img}); function start_canvas() { var brojac = 0; function draw() { ctx.drawImage(slike[brojac], x, y); }; function draw1(w, e) { ctx.drawImage(slike[brojac], w, e); }; function update(time) { ctx.clearRect(0,0,canvas.width,canvas.height); if(w >= x) { e += 8; y += 8; } else { x -= 4; w += 4; }; if(slike){ ctx.drawImage(slike[brojac], x, y); ctx.drawImage(slike[brojac], w, e); } if (e > canvas.height) { brojac = (brojac + 1) % slike.length; x = canvas.width-190; y = 15; w = 1; e = 15; } requestAnimationFrame(update) }; canvas = document.getElementById('canvas1'); x = canvas.width-190; y = 15; w = 1; e = 15; ctx = canvas.getContext('2d'); requestAnimationFrame(update) } window.addEventListener("load",start_canvas); document.write(brojac); </script> </body> </html> 

What I want to happen is that the last two images in the cycle start to slowly dissapear( NOT fade out). 我想发生的是,循环中的最后两个图像开始逐渐消失( 淡出)。 Here are a few simple images of what I had in mind. 这是我所想到的一些简单图像

I want the two images to move towards eachother like they normally move in the code, but the moment a part of an image crosses the invisible line, that part gets deleted until the whole image dissapears, and then the cycle starts over. 我希望两个图像像通常在代码中一样朝彼此移动,但是当图像的一部分越过不可见线时,该部分将被删除,直到整个图像消失,然后循环开始。

I did try to crop the image while it is moving, but I wasn't able to make that work. 我确实尝试在移动图像时对其进行裁切,但无法完成该工作。 Any ideas how I can make this? 有什么想法可以做到吗? Thank you in advance! 先感谢您!

Simply add an extra clearRect() for the region you want to hide when the criteria (image index) is fulfilled. 满足条件(图像索引)时,只需为要隐藏的区域添加一个额外的clearRect()即可。

 var canvas,ctx,x,y,w,e; var slikeURL = [ "https://www.a1smallbusinessmarketing.com/images/prosbo_hires.jpg", "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f9/Wiktionary_small.svg/350px-Wiktionary_small.svg.png", "http://www.jasonjsmith.com/img/small-business-seo.jpg", "https://g.twimg.com/business/page/image/11TwitterForSmallBusiness-300_1.png" ] var slike = slikeURL.map(url=>{var img = new Image(); img.src = url; return img}); function start_canvas() { var brojac = 0; function draw() { ctx.drawImage(slike[brojac], x, y); }; function draw1(w, e) { ctx.drawImage(slike[brojac], w, e); }; function update(time) { ctx.clearRect(0,0,canvas.width,canvas.height); if(w >= x) { e += 8; y += 8; } else { x -= 4; w += 4; }; if(slike){ ctx.drawImage(slike[brojac], x, y); ctx.drawImage(slike[brojac], w, e); if (brojac > 0) ctx.clearRect(412,0,824,ctx.canvas.height); } if (e > canvas.height) { brojac = (brojac + 1) % slike.length; x = canvas.width-190; y = 15; w = 1; e = 15; } requestAnimationFrame(update) }; canvas = document.getElementById('canvas1'); x = canvas.width-190; y = 15; w = 1; e = 15; ctx = canvas.getContext('2d'); requestAnimationFrame(update) } window.addEventListener("load",start_canvas); document.write(brojac); 
 canvas{border:#666 2px solid; } 
 <canvas id="canvas1" width="1650" height="825" style="width: 650px; height: 330px;"></canvas> 

(adjust region as needed). (根据需要调整区域)。

我怎么做<div>标签在 JS 20 秒后消失?</div><div id="text_translate"><p> 我正在为我的公司构建一个项目,我想知道<em>如何在 20 秒后使 div 消失?</em> 因为我正在构建一个带有position: -webkit-sticky; 和</p><pre>&lt;style&gt;.sticky { position: -webkit-sticky; position: sticky; }.areaWarning { width: 100%; height: 102px; background: lightgreen; display: inline-block; } &lt;/style&gt; &lt;div class="sticky"&gt; &lt;div class="areaWarning"&gt; &lt;h1&gt; This site is currently still being built &lt;/h1&gt; &lt;p&gt; We are still working on this site, sorry&lt;/p&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> 我只想知道如何使用 JavaScript 为该警告设置超时?</p><p> 谢谢,环形游戏</p></div> - How do i make a <div> tag dissapear after 20 seconds with JS?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何悬停div并使其背景图像慢慢消失而又不影响其内部内容? - How to hover a div and make its background-image slowly dissapear without affecting the content that's inside it? 如何在html js中制作可滚动的画布图像? - How to make scrollable canvas image in html js ? 有谁知道如何使图像出现然后消失? - Does anyone know how to make an image appear and then dissapear? 我在 canvas 中的图像在页面启动后消失 - My image in the canvas dissapear after startin the page 慢慢淡出画布上的图像 - Slowly fade away image on canvas 如何使事情在负载下消失 - How to make things dissapear on load 悬停时显示图像,不显示时消失 - Make image appear when hovering and dissapear when not 如何使用可以在 p5js 中消失和重新出现的点制作 2D 图案? - How can I make a 2D pattern with dots that can dissapear and reappear in p5js? 在画布上制作一个球慢慢向鼠标移动 - Make a ball on a canvas slowly move towards the mouse 我怎么做<div>标签在 JS 20 秒后消失?</div><div id="text_translate"><p> 我正在为我的公司构建一个项目,我想知道<em>如何在 20 秒后使 div 消失?</em> 因为我正在构建一个带有position: -webkit-sticky; 和</p><pre>&lt;style&gt;.sticky { position: -webkit-sticky; position: sticky; }.areaWarning { width: 100%; height: 102px; background: lightgreen; display: inline-block; } &lt;/style&gt; &lt;div class="sticky"&gt; &lt;div class="areaWarning"&gt; &lt;h1&gt; This site is currently still being built &lt;/h1&gt; &lt;p&gt; We are still working on this site, sorry&lt;/p&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> 我只想知道如何使用 JavaScript 为该警告设置超时?</p><p> 谢谢,环形游戏</p></div> - How do i make a <div> tag dissapear after 20 seconds with JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM