p5.js 相关问题

p5.j​​s是一个JavaScript库,从最初的处理目标开始,为艺术家,设计师,教育工作者和初学者提供编码,并为今天的网络重新解释。

P5.js:表情符号不动

嘿嘿 我正在尝试让表情符号绕圈移动。如果我删除“background()”调用,它将围绕圆绘制它(就像一条路径)。如果我保留背景功能,它只会绘制...

回答 1 投票 0

如何反转代码中的滑块值

我想将滑块值乘以负一以使球返回而不离开画布。 (我使用的是p5js) var x = 200; var y = 200; 让速度控制x; 让速度控制; 让输出x; 让

回答 1 投票 0

如何使用 for 循环简化此代码?

如何使用 for 循环更改此代码,以便我可以创建具有相同功能的更大的矩形? 当我使用 for 循环时,当我指向较小的矩形时,较大的矩形也会

回答 1 投票 0

需要帮助在 React 中构建响应式画布

我正在使用 React 开发一个类似绘画的应用程序,并且我已经成功实现了画布大小调整。但是,我面临一个问题,画布上的绘图无法适当缩放......

回答 1 投票 0

为什么我的javascript函数输出不正确?

我认为我的代码应该更改 exampleoutput 但它显示 0 (我在 p5.js 上运行它)。我改变了outputVar=1;第 6 行 exampleoutput=1;它有效,但我希望能够改变它。 我的同事...

回答 1 投票 0

JS/博士。如果只将卡片翻到后面,Strange的传送门动画无法定位在卡片后面圆圈的外框上

包含卡牌轮换和传送门代码的部分: //音效 $(文档).ready(函数() { var FlipSound = document.getElementById("flipSound")...</desc> <question vote="-6"> <div> </div> <p><strong>包含卡牌轮换和传送门代码的部分:</strong></p> <pre><code>&lt;/script&gt; &lt;script&gt; //sound effect $(document).ready(function() { var flipSound = document.getElementById(&#34;flipSound&#34;); $(&#34;.card&#34;).click(function() { var card = $(this); // Check if the card is not already flipping to avoid multiple flips if (!card.hasClass(&#39;flipping&#39;)) { card.addClass(&#39;flipping&#39;); // Check if the front side is facing the viewer if (!card.hasClass(&#34;flipped&#34;)) { flipSound.currentTime = 0; // Reset the audio to the start flipSound.play(); startAnimation(); console.log(&#34;started&#34;) card.addClass(&#34;flipped&#34;); } else { stopAnimation(); console.log(&#34;stopped&#34;) card.removeClass(&#34;flipped&#34;); } setTimeout(function() { card.removeClass(&#39;flipping&#39;); }, 1000); // Adjust the timeout to match your animation duration } }); // Prevent sound on back side click $(&#34;.card__back&#34;).click(function(e) { e.stopPropagation(); }); }); &lt;/script&gt; &lt;script&gt; // Body hoover effect document.getElementById(&#34;gameCardLink&#34;).addEventListener(&#34;mouseover&#34;, function() { document.body.classList.add(&#34;hoverEffect&#34;); }); document.getElementById(&#34;gameCardLink&#34;).addEventListener(&#34;mouseout&#34;, function() { document.body.classList.remove(&#34;hoverEffect&#34;); }); &lt;/script&gt; &lt;script&gt; // portal effect var p5Instance; function startAnimation() { const sketch = (p) =&gt; { const createParticleSystem = (location) =&gt; { const origin = location.copy(); const particles = []; const addParticle = velocity =&gt; { const rand = p.random(0, 1); if (rand &lt;= .3) { particles.push(createSparkParticle(origin, velocity.copy())); } else { particles.push(createParticle(origin, velocity.copy())); } }; const applyForce = force =&gt; { particles.forEach(particle =&gt; { particle.applyForce(force); }); }; const run = () =&gt; { particles.forEach((particle, index) =&gt; { particle.move(); particle.draw(); if (particle.isDead()) { particles.splice(index, 1); } }); }; return { origin, addParticle, run, applyForce }; }; const createSparkParticle = (locationP, velocity) =&gt; { const particle = createParticle(locationP, velocity); let fade = 255; const draw = () =&gt; { p.colorMode(p.HSB); p.stroke(16, 62, 100, fade); const arrow = velocity.copy().normalize().mult(p.random(2, 4)); const direction = p5.Vector.add(particle.location, arrow); p.line(particle.location.x, particle.location.y, direction.x, direction.y); }; const move = () =&gt; { particle.applyForce(p.createVector(p.random(-.2, .2), p.random(-0.1, -0.4))); particle.velocity.add(particle.acc); particle.location.add(particle.velocity.copy().normalize().mult(p.random(2, 4))); particle.acc.mult(0); fade -= 5; }; return { ...particle, draw, move } } const createParticle = (locationP, velocity) =&gt; { const acc = p.createVector(0, 0); const location = locationP.copy(); let fade = 255; const fadeMinus = p.randomGaussian(15, 2); let ligther = 100; let situate = 62; const draw = () =&gt; { p.colorMode(p.HSB) p.stroke(16, p.constrain(situate, 62, 92), p.constrain(ligther, 60, 100), fade); const arrow = velocity.copy().mult(2); const direction = p5.Vector.add(location, arrow); p.line(location.x, location.y, direction.x, direction.y); }; const move = () =&gt; { velocity.add(acc); location.add(velocity.copy().div(p.map(velocity.mag(), 18, 0, 5, 1))); acc.mult(0); fade -= fadeMinus; ligther -= 8; situate += 8; }; const applyForce = force =&gt; { acc.add(force); }; const isDead = () =&gt; { if (fade &lt; 0 || location.x &lt; 0 || location.x &gt; p.width || location.y &gt; p.height) { return true; } else { return false; } }; return { draw, move, applyForce, isDead, velocity, location, acc }; }; const createMover = () =&gt; { const location = p.createVector(p.width / 2, p.height / 2); const velocity = p.createVector(0, 0); const acc = p.createVector(0, 0); const mass = 10; let angle = 0; let angleVelocity = 0; let angleAcc = 0; let len = 100; const particleSystems = [ createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location) ]; const getGotoVector = angle =&gt; { const radius = p.map(angleVelocity, 0, 0.3, 0, 55); const goToVector = p.createVector( location.x + radius * p.cos(angle), location.y + radius * p.sin(angle) ); return goToVector; }; const draw = () =&gt; { const goToVector = getGotoVector(angle); particleSystems.forEach(particleSystem =&gt; { particleSystem.run(); }); }; const renderParticleSystem = () =&gt; { particleSystems.forEach(particleSystem =&gt; { const goToVector = getGotoVector(angle - p.random(0, p.TWO_PI)); const prepencular = p.createVector( (goToVector.y - location.y)*-1, (goToVector.x - location.x) ); prepencular.normalize(); prepencular.mult(angleVelocity * 70); particleSystem.origin.set(goToVector); particleSystem.addParticle(prepencular); const gravity = p.createVector(0, 0.3); particleSystem.applyForce(gravity); }); }; const move = () =&gt; { angleAcc = 0.005; angleVelocity = p.constrain(angleVelocity + angleAcc, 0, 0.32); angle += angleVelocity; angleAcc = 0; renderParticleSystem(); }; return { draw, move }; }; let mover; p.setup = function() { p.createCanvas(240, 320); p.clear(); mover = createMover(); } p.draw = function() { p.clear(); mover.move(); mover.draw(); } }; p5Instance = new p5(sketch); } // stop animation function stopAnimation() { if (p5Instance) { p5Instance.remove(); p5Instance = null; } } &lt;/script&gt; </code></pre> <p>朋友们大家好, 我有6张可以逆转的牌。当其中一张卡片转动时,我希望 Portal 动画出现在卡片背面彩色圆圈的最外层框架中。我怎样才能在我的代码中实现这一目标?</p> <h2><em><strong>6 张卡片分别做什么</strong></em></h2> <p><a href="https://i.stack.imgur.com/o1N8P.jpg" target="_blank"><img src="https://cdn.imgupio.com/i/AWkuc3RhY2suaW1ndXIuY29tL28xTjhQLmpwZw==" alt=""/></a></p> <ul> <li><strong>主文件(含传送门动画):</strong></li> </ul> <p><a href="https://drive.google.com/file/d/1famaPF_Rz-CIjbiBCOlIFZH-LWKjlpv_/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/1famaPF_Rz-CIjbiBCOlIFZH-LWKjlpv_/view?usp=sharing</a></p> <ul> <li><strong>应用程序与门户的演示链接(无法正常工作):</strong></li> </ul> <p><a href="https://tm-game-cards-portal.netlify.app/" rel="nofollow noreferrer">https://tm-game-cards-portal.netlify.app/</a></p> <hr/> <ul> <li><strong>无门户的应用程序演示链接:</strong></li> </ul> <p><a href="https://tm-game-cards-v02.netlify.app/" rel="nofollow noreferrer">https://tm-game-cards-v02.netlify.app/</a></p> <ul> <li><strong>主文件(不包括传送门动画):</strong> <a href="https://drive.google.com/file/d/1fKABYycS1cfBZ7GWYHueSXkH_XnLZZpa/view" rel="nofollow noreferrer">https://drive.google.com/file/d/1fKABYycS1cfBZ7GWYHueSXkH_XnLZZpa/view</a></li> </ul> </question> <answer tick="false" vote="1"> <p>您的问题似乎与定位更相关,可以通过正确的 CSS 定位来简单解决。我只是用你的代码和下面的 CSS 进行实验,它会起作用</p> <pre><code>canvas { position: absolute; z-index: 100; left: 46.7%; top: 43%; } </code></pre> <p>您可以根据您的需求调整左侧和顶部的位置</p> </answer> <answer tick="false" vote="0"> <p>多么棒的概念啊! 您可以通过将画布附加为“card__back”div 的子项并为其指定 <pre><code>position: absolute</code></pre> CSS,将画布显示在卡片背面。 像这样的东西:</p> <p></p><div data-babel="false" data-lang="js" data-hide="false" data-console="true"> <div> <pre><code>var div = document.querySelector(&#39;.flipped .card__flipper.active .card__back&#39;); var canvas = document.getElementsByClassName(&#39;p5Canvas&#39;)[0]; div.appendChild(canvas); canvas.style.position = &#39;absolute&#39;;</code></pre> </div> </div> <p></p> <p>(如果您在提供的演示的控制台中运行,则附加的代码片段实际上可以工作<a href="https://tm-game-cards-portal.netlify.app/" rel="nofollow noreferrer">https://tm-game-cards-portal.netlify.app/</a>。尝试一下:))</p> </answer> <answer tick="false" vote="-1"> <p>实现此目的的一种方法是创建一个标志变量来跟踪卡片是否翻转。当卡片向后翻转时,您开始动画,当它们翻转到前面时,您停止动画。</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang=&#34;en&#34;&gt; &lt;head&gt; &lt;meta charset=&#34;UTF-8&#34;&gt; &lt;meta name=&#34;viewport&#34; content=&#34;width=device-width, initial-scale=1.0&#34;&gt; &lt;title&gt;Card Flip Animation&lt;/title&gt; &lt;!-- Include any necessary libraries here, such as jQuery and p5.js --&gt; &lt;script src=&#34;https://code.jquery.com/jquery-3.6.0.min.js&#34;&gt;&lt;/script&gt; &lt;script src=&#34;https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js&#34;&gt; &lt;/script&gt; &lt;!-- Include your CSS styles here --&gt; &lt;style&gt; /* Your CSS styles for cards and animations here */ &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;audio id=&#34;flipSound&#34; src=&#34;flip_sound.mp3&#34;&gt;&lt;/audio&gt; &lt;!-- Your HTML for the card here --&gt; &lt;div class=&#34;card&#34;&gt;Card Content&lt;/div&gt; &lt;script&gt; // Add a global variable to track the card flip state let cardFlipped = false; let p5Instance; $(document).ready(function() { var flipSound = document.getElementById(&#34;flipSound&#34;); $(&#34;.card&#34;).click(function() { var card = $(this); if (!card.hasClass(&#39;flipping&#39;)) { card.addClass(&#39;flipping&#39;); if (!card.hasClass(&#34;flipped&#34;)) { // Start animation and set the cardFlipped variable to true startAnimation(); cardFlipped = true; } else { // Stop animation and set the cardFlipped variable to false stopAnimation(); cardFlipped = false; } setTimeout(function() { card.removeClass(&#39;flipping&#39;); }, 1000); } }); // Prevent sound on back side click $(&#34;.card__back&#34;).click(function(e) { e.stopPropagation(); }); }); // portal effect function startAnimation() { const sketch = (p) =&gt; { // Your p5.js animation code here // ... }; p5Instance = new p5(sketch); } // stop animation function stopAnimation() { if (p5Instance) { p5Instance.remove(); p5Instance = null; } } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>现在,您有一个 cardFlipped 变量来跟踪卡片的翻转状态。当卡片向后翻转时(cardFlipped 为 true),启动动画,当卡片翻转到正面时(cardFlipped 为 false),停止动画。</p> </answer> <answer tick="false" vote="-1"> <pre><code> `// ... } else { stopAnimation(); console.log(&#34;durduruldu&#34;); card.removeClass(&#34;flipped&#34;); } // ... card.on(&#34;animationend&#34;, function() { stopAnimation(); console.log(&#34;durduruldu&#34;); });` </code></pre> </answer> </body></html>

回答 0 投票 0

JS / 奇异博士的传送门动画如果只将卡片翻到后面,则无法定位在卡片后面圆圈的外框上

包含卡牌轮换和传送门代码的部分: //音效 $(文档).ready(函数() { var FlipSound = document.getElementById("flipSound")...</desc> <question vote="-6"> <div> </div> <p><strong>包含卡牌轮换和传送门代码的部分:</strong></p> <pre><code>&lt;/script&gt; &lt;script&gt; //sound effect $(document).ready(function() { var flipSound = document.getElementById(&#34;flipSound&#34;); $(&#34;.card&#34;).click(function() { var card = $(this); // Check if the card is not already flipping to avoid multiple flips if (!card.hasClass(&#39;flipping&#39;)) { card.addClass(&#39;flipping&#39;); // Check if the front side is facing the viewer if (!card.hasClass(&#34;flipped&#34;)) { flipSound.currentTime = 0; // Reset the audio to the start flipSound.play(); startAnimation(); console.log(&#34;started&#34;) card.addClass(&#34;flipped&#34;); } else { stopAnimation(); console.log(&#34;stopped&#34;) card.removeClass(&#34;flipped&#34;); } setTimeout(function() { card.removeClass(&#39;flipping&#39;); }, 1000); // Adjust the timeout to match your animation duration } }); // Prevent sound on back side click $(&#34;.card__back&#34;).click(function(e) { e.stopPropagation(); }); }); &lt;/script&gt; &lt;script&gt; // Body hoover effect document.getElementById(&#34;gameCardLink&#34;).addEventListener(&#34;mouseover&#34;, function() { document.body.classList.add(&#34;hoverEffect&#34;); }); document.getElementById(&#34;gameCardLink&#34;).addEventListener(&#34;mouseout&#34;, function() { document.body.classList.remove(&#34;hoverEffect&#34;); }); &lt;/script&gt; &lt;script&gt; // portal effect var p5Instance; function startAnimation() { const sketch = (p) =&gt; { const createParticleSystem = (location) =&gt; { const origin = location.copy(); const particles = []; const addParticle = velocity =&gt; { const rand = p.random(0, 1); if (rand &lt;= .3) { particles.push(createSparkParticle(origin, velocity.copy())); } else { particles.push(createParticle(origin, velocity.copy())); } }; const applyForce = force =&gt; { particles.forEach(particle =&gt; { particle.applyForce(force); }); }; const run = () =&gt; { particles.forEach((particle, index) =&gt; { particle.move(); particle.draw(); if (particle.isDead()) { particles.splice(index, 1); } }); }; return { origin, addParticle, run, applyForce }; }; const createSparkParticle = (locationP, velocity) =&gt; { const particle = createParticle(locationP, velocity); let fade = 255; const draw = () =&gt; { p.colorMode(p.HSB); p.stroke(16, 62, 100, fade); const arrow = velocity.copy().normalize().mult(p.random(2, 4)); const direction = p5.Vector.add(particle.location, arrow); p.line(particle.location.x, particle.location.y, direction.x, direction.y); }; const move = () =&gt; { particle.applyForce(p.createVector(p.random(-.2, .2), p.random(-0.1, -0.4))); particle.velocity.add(particle.acc); particle.location.add(particle.velocity.copy().normalize().mult(p.random(2, 4))); particle.acc.mult(0); fade -= 5; }; return { ...particle, draw, move } } const createParticle = (locationP, velocity) =&gt; { const acc = p.createVector(0, 0); const location = locationP.copy(); let fade = 255; const fadeMinus = p.randomGaussian(15, 2); let ligther = 100; let situate = 62; const draw = () =&gt; { p.colorMode(p.HSB) p.stroke(16, p.constrain(situate, 62, 92), p.constrain(ligther, 60, 100), fade); const arrow = velocity.copy().mult(2); const direction = p5.Vector.add(location, arrow); p.line(location.x, location.y, direction.x, direction.y); }; const move = () =&gt; { velocity.add(acc); location.add(velocity.copy().div(p.map(velocity.mag(), 18, 0, 5, 1))); acc.mult(0); fade -= fadeMinus; ligther -= 8; situate += 8; }; const applyForce = force =&gt; { acc.add(force); }; const isDead = () =&gt; { if (fade &lt; 0 || location.x &lt; 0 || location.x &gt; p.width || location.y &gt; p.height) { return true; } else { return false; } }; return { draw, move, applyForce, isDead, velocity, location, acc }; }; const createMover = () =&gt; { const location = p.createVector(p.width / 2, p.height / 2); const velocity = p.createVector(0, 0); const acc = p.createVector(0, 0); const mass = 10; let angle = 0; let angleVelocity = 0; let angleAcc = 0; let len = 100; const particleSystems = [ createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location) ]; const getGotoVector = angle =&gt; { const radius = p.map(angleVelocity, 0, 0.3, 0, 55); const goToVector = p.createVector( location.x + radius * p.cos(angle), location.y + radius * p.sin(angle) ); return goToVector; }; const draw = () =&gt; { const goToVector = getGotoVector(angle); particleSystems.forEach(particleSystem =&gt; { particleSystem.run(); }); }; const renderParticleSystem = () =&gt; { particleSystems.forEach(particleSystem =&gt; { const goToVector = getGotoVector(angle - p.random(0, p.TWO_PI)); const prepencular = p.createVector( (goToVector.y - location.y)*-1, (goToVector.x - location.x) ); prepencular.normalize(); prepencular.mult(angleVelocity * 70); particleSystem.origin.set(goToVector); particleSystem.addParticle(prepencular); const gravity = p.createVector(0, 0.3); particleSystem.applyForce(gravity); }); }; const move = () =&gt; { angleAcc = 0.005; angleVelocity = p.constrain(angleVelocity + angleAcc, 0, 0.32); angle += angleVelocity; angleAcc = 0; renderParticleSystem(); }; return { draw, move }; }; let mover; p.setup = function() { p.createCanvas(240, 320); p.clear(); mover = createMover(); } p.draw = function() { p.clear(); mover.move(); mover.draw(); } }; p5Instance = new p5(sketch); } // stop animation function stopAnimation() { if (p5Instance) { p5Instance.remove(); p5Instance = null; } } &lt;/script&gt; </code></pre> <p>朋友们大家好, 我有6张可以逆转的牌。当其中一张卡片转动时,我希望 Portal 动画出现在卡片背面彩色圆圈的最外层框架中。我怎样才能在我的代码中实现这一目标?</p> <h2><em><strong>6 张卡片分别做什么</strong></em></h2> <p><a href="https://i.stack.imgur.com/o1N8P.jpg" target="_blank"><img src="https://cdn.imgupio.com/i/AWkuc3RhY2suaW1ndXIuY29tL28xTjhQLmpwZw==" alt=""/></a></p> <ul> <li><strong>主文件(含传送门动画):</strong></li> </ul> <p><a href="https://drive.google.com/file/d/1famaPF_Rz-CIjbiBCOlIFZH-LWKjlpv_/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/1famaPF_Rz-CIjbiBCOlIFZH-LWKjlpv_/view?usp=sharing</a></p> <ul> <li><strong>应用程序与门户的演示链接(无法正常工作):</strong></li> </ul> <p><a href="https://tm-game-cards-portal.netlify.app/" rel="nofollow noreferrer">https://tm-game-cards-portal.netlify.app/</a></p> <hr/> <ul> <li><strong>无门户的应用程序演示链接:</strong></li> </ul> <p><a href="https://tm-game-cards-v02.netlify.app/" rel="nofollow noreferrer">https://tm-game-cards-v02.netlify.app/</a></p> <ul> <li><strong>主文件(不包括传送门动画):</strong> <a href="https://drive.google.com/file/d/1fKABYycS1cfBZ7GWYHueSXkH_XnLZZpa/view" rel="nofollow noreferrer">https://drive.google.com/file/d/1fKABYycS1cfBZ7GWYHueSXkH_XnLZZpa/view</a></li> </ul> </question> <answer tick="false" vote="1"> <p>您的问题似乎与定位更相关,可以通过正确的 CSS 定位来简单解决。我只是用你的代码和下面的 CSS 进行实验,它会起作用</p> <pre><code>canvas { position: absolute; z-index: 100; left: 46.7%; top: 43%; } </code></pre> <p>您可以根据您的需求调整左侧和顶部的位置</p> </answer> <answer tick="false" vote="0"> <p>多么棒的概念啊! 您可以通过将画布附加为“card__back”div 的子项并为其指定 <pre><code>position: absolute</code></pre> CSS,将画布显示在卡片背面。 像这样的东西:</p> <p></p><div data-babel="false" data-lang="js" data-hide="false" data-console="true"> <div> <pre><code>var div = document.querySelector(&#39;.flipped .card__flipper.active .card__back&#39;); var canvas = document.getElementsByClassName(&#39;p5Canvas&#39;)[0]; div.appendChild(canvas); canvas.style.position = &#39;absolute&#39;;</code></pre> </div> </div> <p></p> <p>(如果您在提供的演示的控制台中运行,则附加的代码片段实际上可以工作<a href="https://tm-game-cards-portal.netlify.app/" rel="nofollow noreferrer">https://tm-game-cards-portal.netlify.app/</a>。尝试一下:))</p> </answer> <answer tick="false" vote="-1"> <p>实现此目的的一种方法是创建一个标志变量来跟踪卡片是否翻转。当卡片向后翻转时,您开始动画,当它们翻转到前面时,您停止动画。</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang=&#34;en&#34;&gt; &lt;head&gt; &lt;meta charset=&#34;UTF-8&#34;&gt; &lt;meta name=&#34;viewport&#34; content=&#34;width=device-width, initial-scale=1.0&#34;&gt; &lt;title&gt;Card Flip Animation&lt;/title&gt; &lt;!-- Include any necessary libraries here, such as jQuery and p5.js --&gt; &lt;script src=&#34;https://code.jquery.com/jquery-3.6.0.min.js&#34;&gt;&lt;/script&gt; &lt;script src=&#34;https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js&#34;&gt; &lt;/script&gt; &lt;!-- Include your CSS styles here --&gt; &lt;style&gt; /* Your CSS styles for cards and animations here */ &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;audio id=&#34;flipSound&#34; src=&#34;flip_sound.mp3&#34;&gt;&lt;/audio&gt; &lt;!-- Your HTML for the card here --&gt; &lt;div class=&#34;card&#34;&gt;Card Content&lt;/div&gt; &lt;script&gt; // Add a global variable to track the card flip state let cardFlipped = false; let p5Instance; $(document).ready(function() { var flipSound = document.getElementById(&#34;flipSound&#34;); $(&#34;.card&#34;).click(function() { var card = $(this); if (!card.hasClass(&#39;flipping&#39;)) { card.addClass(&#39;flipping&#39;); if (!card.hasClass(&#34;flipped&#34;)) { // Start animation and set the cardFlipped variable to true startAnimation(); cardFlipped = true; } else { // Stop animation and set the cardFlipped variable to false stopAnimation(); cardFlipped = false; } setTimeout(function() { card.removeClass(&#39;flipping&#39;); }, 1000); } }); // Prevent sound on back side click $(&#34;.card__back&#34;).click(function(e) { e.stopPropagation(); }); }); // portal effect function startAnimation() { const sketch = (p) =&gt; { // Your p5.js animation code here // ... }; p5Instance = new p5(sketch); } // stop animation function stopAnimation() { if (p5Instance) { p5Instance.remove(); p5Instance = null; } } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>现在,您有一个 cardFlipped 变量来跟踪卡片的翻转状态。当卡片向后翻转时(cardFlipped 为 true),启动动画,当卡片翻转到正面时(cardFlipped 为 false),停止动画。</p> </answer> <answer tick="false" vote="-1"> <pre><code> `// ... } else { stopAnimation(); console.log(&#34;durduruldu&#34;); card.removeClass(&#34;flipped&#34;); } // ... card.on(&#34;animationend&#34;, function() { stopAnimation(); console.log(&#34;durduruldu&#34;); });` </code></pre> </answer> </body></html>

回答 0 投票 0

p5.js 中的向量数组出现问题

我正在尝试使用 p5.js 创建一个二维向量数组 我使用嵌套 For 循环在 setup() 中生成 2D 向量数组,并在同一嵌套内将向量值打印到控制台...

回答 1 投票 0

JS,奇异博士的传送门动画如果只将卡片翻到后面,是无法定位在卡片后面圆圈的外框上的

包含卡牌轮换和传送门代码的部分: //音效 $(文档).ready(函数() { var FlipSound = document.getElementById("flipSound")...</desc> <question vote="-4"> <div> </div> <p><strong>包含卡牌轮换和传送门代码的部分:</strong></p> <pre><code>&lt;/script&gt; &lt;script&gt; //sound effect $(document).ready(function() { var flipSound = document.getElementById(&#34;flipSound&#34;); $(&#34;.card&#34;).click(function() { var card = $(this); // Check if the card is not already flipping to avoid multiple flips if (!card.hasClass(&#39;flipping&#39;)) { card.addClass(&#39;flipping&#39;); // Check if the front side is facing the viewer if (!card.hasClass(&#34;flipped&#34;)) { flipSound.currentTime = 0; // Reset the audio to the start flipSound.play(); startAnimation(); console.log(&#34;started&#34;) card.addClass(&#34;flipped&#34;); } else { stopAnimation(); console.log(&#34;stopped&#34;) card.removeClass(&#34;flipped&#34;); } setTimeout(function() { card.removeClass(&#39;flipping&#39;); }, 1000); // Adjust the timeout to match your animation duration } }); // Prevent sound on back side click $(&#34;.card__back&#34;).click(function(e) { e.stopPropagation(); }); }); &lt;/script&gt; &lt;script&gt; // Body hoover effect document.getElementById(&#34;gameCardLink&#34;).addEventListener(&#34;mouseover&#34;, function() { document.body.classList.add(&#34;hoverEffect&#34;); }); document.getElementById(&#34;gameCardLink&#34;).addEventListener(&#34;mouseout&#34;, function() { document.body.classList.remove(&#34;hoverEffect&#34;); }); &lt;/script&gt; &lt;script&gt; // portal effect var p5Instance; function startAnimation() { const sketch = (p) =&gt; { const createParticleSystem = (location) =&gt; { const origin = location.copy(); const particles = []; const addParticle = velocity =&gt; { const rand = p.random(0, 1); if (rand &lt;= .3) { particles.push(createSparkParticle(origin, velocity.copy())); } else { particles.push(createParticle(origin, velocity.copy())); } }; const applyForce = force =&gt; { particles.forEach(particle =&gt; { particle.applyForce(force); }); }; const run = () =&gt; { particles.forEach((particle, index) =&gt; { particle.move(); particle.draw(); if (particle.isDead()) { particles.splice(index, 1); } }); }; return { origin, addParticle, run, applyForce }; }; const createSparkParticle = (locationP, velocity) =&gt; { const particle = createParticle(locationP, velocity); let fade = 255; const draw = () =&gt; { p.colorMode(p.HSB); p.stroke(16, 62, 100, fade); const arrow = velocity.copy().normalize().mult(p.random(2, 4)); const direction = p5.Vector.add(particle.location, arrow); p.line(particle.location.x, particle.location.y, direction.x, direction.y); }; const move = () =&gt; { particle.applyForce(p.createVector(p.random(-.2, .2), p.random(-0.1, -0.4))); particle.velocity.add(particle.acc); particle.location.add(particle.velocity.copy().normalize().mult(p.random(2, 4))); particle.acc.mult(0); fade -= 5; }; return { ...particle, draw, move } } const createParticle = (locationP, velocity) =&gt; { const acc = p.createVector(0, 0); const location = locationP.copy(); let fade = 255; const fadeMinus = p.randomGaussian(15, 2); let ligther = 100; let situate = 62; const draw = () =&gt; { p.colorMode(p.HSB) p.stroke(16, p.constrain(situate, 62, 92), p.constrain(ligther, 60, 100), fade); const arrow = velocity.copy().mult(2); const direction = p5.Vector.add(location, arrow); p.line(location.x, location.y, direction.x, direction.y); }; const move = () =&gt; { velocity.add(acc); location.add(velocity.copy().div(p.map(velocity.mag(), 18, 0, 5, 1))); acc.mult(0); fade -= fadeMinus; ligther -= 8; situate += 8; }; const applyForce = force =&gt; { acc.add(force); }; const isDead = () =&gt; { if (fade &lt; 0 || location.x &lt; 0 || location.x &gt; p.width || location.y &gt; p.height) { return true; } else { return false; } }; return { draw, move, applyForce, isDead, velocity, location, acc }; }; const createMover = () =&gt; { const location = p.createVector(p.width / 2, p.height / 2); const velocity = p.createVector(0, 0); const acc = p.createVector(0, 0); const mass = 10; let angle = 0; let angleVelocity = 0; let angleAcc = 0; let len = 100; const particleSystems = [ createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location) ]; const getGotoVector = angle =&gt; { const radius = p.map(angleVelocity, 0, 0.3, 0, 55); const goToVector = p.createVector( location.x + radius * p.cos(angle), location.y + radius * p.sin(angle) ); return goToVector; }; const draw = () =&gt; { const goToVector = getGotoVector(angle); particleSystems.forEach(particleSystem =&gt; { particleSystem.run(); }); }; const renderParticleSystem = () =&gt; { particleSystems.forEach(particleSystem =&gt; { const goToVector = getGotoVector(angle - p.random(0, p.TWO_PI)); const prepencular = p.createVector( (goToVector.y - location.y)*-1, (goToVector.x - location.x) ); prepencular.normalize(); prepencular.mult(angleVelocity * 70); particleSystem.origin.set(goToVector); particleSystem.addParticle(prepencular); const gravity = p.createVector(0, 0.3); particleSystem.applyForce(gravity); }); }; const move = () =&gt; { angleAcc = 0.005; angleVelocity = p.constrain(angleVelocity + angleAcc, 0, 0.32); angle += angleVelocity; angleAcc = 0; renderParticleSystem(); }; return { draw, move }; }; let mover; p.setup = function() { p.createCanvas(240, 320); p.clear(); mover = createMover(); } p.draw = function() { p.clear(); mover.move(); mover.draw(); } }; p5Instance = new p5(sketch); } // stop animation function stopAnimation() { if (p5Instance) { p5Instance.remove(); p5Instance = null; } } &lt;/script&gt; </code></pre> <p>朋友们大家好, 我有6张可以逆转的牌。当其中一张卡片转动时,我希望 Portal 动画出现在卡片背面彩色圆圈的最外面的框架中。我怎样才能在我的代码中实现这一目标?</p> <h2><em><strong>6 张卡片分别做什么</strong></em></h2> <p><a href="https://i.stack.imgur.com/o1N8P.jpg" target="_blank"><img src="https://cdn.imgupio.com/i/AWkuc3RhY2suaW1ndXIuY29tL28xTjhQLmpwZw==" alt=""/></a></p> <ul> <li><strong>主文件(含传送门动画):</strong></li> </ul> <p><a href="https://drive.google.com/file/d/1famaPF_Rz-CIjbiBCOlIFZH-LWKjlpv_/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/1famaPF_Rz-CIjbiBCOlIFZH-LWKjlpv_/view?usp=sharing</a></p> <ul> <li><strong>应用程序与门户的演示链接(无法正常工作):</strong></li> </ul> <p><a href="https://tm-game-cards-portal.netlify.app/" rel="nofollow noreferrer">https://tm-game-cards-portal.netlify.app/</a></p> <hr/> <ul> <li><strong>不带门户的应用程序演示链接:</strong></li> </ul> <p><a href="https://tm-game-cards-v02.netlify.app/" rel="nofollow noreferrer">https://tm-game-cards-v02.netlify.app/</a></p> <ul> <li><strong>主文件(不包括传送门动画):</strong> <a href="https://drive.google.com/file/d/1fKABYycS1cfBZ7GWYHueSXkH_XnLZZpa/view" rel="nofollow noreferrer">https://drive.google.com/file/d/1fKABYycS1cfBZ7GWYHueSXkH_XnLZZpa/view</a></li> </ul> </question> <answer tick="false" vote="1"> <p>您的问题似乎与定位更相关,可以通过正确的 CSS 定位来简单解决。我只是用你的代码和下面的 CSS 进行实验,它会起作用</p> <pre><code>canvas { position: absolute; z-index: 100; left: 46.7%; top: 43%; } </code></pre> <p>您可以根据您的需求调整左侧和顶部的位置</p> </answer> <answer tick="false" vote="-1"> <p>实现此目的的一种方法是创建一个标志变量来跟踪卡片是否翻转。当卡片向后翻转时,您开始动画,当它们翻转到前面时,您停止动画。</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang=&#34;en&#34;&gt; &lt;head&gt; &lt;meta charset=&#34;UTF-8&#34;&gt; &lt;meta name=&#34;viewport&#34; content=&#34;width=device-width, initial-scale=1.0&#34;&gt; &lt;title&gt;Card Flip Animation&lt;/title&gt; &lt;!-- Include any necessary libraries here, such as jQuery and p5.js --&gt; &lt;script src=&#34;https://code.jquery.com/jquery-3.6.0.min.js&#34;&gt;&lt;/script&gt; &lt;script src=&#34;https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js&#34;&gt; &lt;/script&gt; &lt;!-- Include your CSS styles here --&gt; &lt;style&gt; /* Your CSS styles for cards and animations here */ &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;audio id=&#34;flipSound&#34; src=&#34;flip_sound.mp3&#34;&gt;&lt;/audio&gt; &lt;!-- Your HTML for the card here --&gt; &lt;div class=&#34;card&#34;&gt;Card Content&lt;/div&gt; &lt;script&gt; // Add a global variable to track the card flip state let cardFlipped = false; let p5Instance; $(document).ready(function() { var flipSound = document.getElementById(&#34;flipSound&#34;); $(&#34;.card&#34;).click(function() { var card = $(this); if (!card.hasClass(&#39;flipping&#39;)) { card.addClass(&#39;flipping&#39;); if (!card.hasClass(&#34;flipped&#34;)) { // Start animation and set the cardFlipped variable to true startAnimation(); cardFlipped = true; } else { // Stop animation and set the cardFlipped variable to false stopAnimation(); cardFlipped = false; } setTimeout(function() { card.removeClass(&#39;flipping&#39;); }, 1000); } }); // Prevent sound on back side click $(&#34;.card__back&#34;).click(function(e) { e.stopPropagation(); }); }); // portal effect function startAnimation() { const sketch = (p) =&gt; { // Your p5.js animation code here // ... }; p5Instance = new p5(sketch); } // stop animation function stopAnimation() { if (p5Instance) { p5Instance.remove(); p5Instance = null; } } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>现在,您有一个 cardFlipped 变量来跟踪卡片的翻转状态。当卡片向后翻转时(cardFlipped为true),启动动画,当卡片翻转到正面时(cardFlipped为false),停止动画。</p> </answer> <answer tick="false" vote="-1"> <pre><code> `// ... } else { stopAnimation(); console.log(&#34;durduruldu&#34;); card.removeClass(&#34;flipped&#34;); } // ... card.on(&#34;animationend&#34;, function() { stopAnimation(); console.log(&#34;durduruldu&#34;); });` </code></pre> </answer> </body></html>

回答 0 投票 0

为什么 p5.js bezierPoint() 函数在给定线性三次贝塞尔曲线时返回非线性值?

好吧,我一直在创建一个 UI 框架并向其中添加动画。我想让用户使用cubicBeziers控制动画的计时功能,所以我决定使用p5.js'

回答 2 投票 0

如何在p5.js中获取换行文本的边界形状

我正在创建一个 p5 项目,在其中从字符串创建文本,使用“WORD”选项将其包装并将其添加到画布中。举个例子: 让字体; 函数预加载(){ 字体 = loadFont('SomeFont....

回答 1 投票 0

在 p5.js 中实现 Tic Tac Toe 的 Minimax 算法时遇到问题 - 需要帮助调试 AI 逻辑

问题: 我目前正在使用 p5.js 中的极小极大算法开发 Tic Tac Toe 游戏,并且遇到了 AI 行为的问题。尽管我很努力,但我似乎无法让人工智能...

回答 1 投票 0

博士。当卡片翻转时,Strange的传送门动画不能出现在卡片后面的圆圈之外,JavaScript

包含卡牌轮换和传送门代码的部分: //音效 $(文档).ready(函数() { var FlipSound = document.getElementById("flipSound")...</desc> <question vote="-5"> <div> </div> <p><strong>包含卡牌轮换和传送门代码的部分:</strong></p> <pre><code>&lt;/script&gt; &lt;script&gt; //sound effect $(document).ready(function() { var flipSound = document.getElementById(&#34;flipSound&#34;); $(&#34;.card&#34;).click(function() { var card = $(this); // Check if the card is not already flipping to avoid multiple flips if (!card.hasClass(&#39;flipping&#39;)) { card.addClass(&#39;flipping&#39;); // Check if the front side is facing the viewer if (!card.hasClass(&#34;flipped&#34;)) { flipSound.currentTime = 0; // Reset the audio to the start flipSound.play(); startAnimation(); console.log(&#34;started&#34;) card.addClass(&#34;flipped&#34;); } else { stopAnimation(); console.log(&#34;stopped&#34;) card.removeClass(&#34;flipped&#34;); } setTimeout(function() { card.removeClass(&#39;flipping&#39;); }, 1000); // Adjust the timeout to match your animation duration } }); // Prevent sound on back side click $(&#34;.card__back&#34;).click(function(e) { e.stopPropagation(); }); }); &lt;/script&gt; &lt;script&gt; // Body hoover effect document.getElementById(&#34;gameCardLink&#34;).addEventListener(&#34;mouseover&#34;, function() { document.body.classList.add(&#34;hoverEffect&#34;); }); document.getElementById(&#34;gameCardLink&#34;).addEventListener(&#34;mouseout&#34;, function() { document.body.classList.remove(&#34;hoverEffect&#34;); }); &lt;/script&gt; &lt;script&gt; // portal effect var p5Instance; function startAnimation() { const sketch = (p) =&gt; { const createParticleSystem = (location) =&gt; { const origin = location.copy(); const particles = []; const addParticle = velocity =&gt; { const rand = p.random(0, 1); if (rand &lt;= .3) { particles.push(createSparkParticle(origin, velocity.copy())); } else { particles.push(createParticle(origin, velocity.copy())); } }; const applyForce = force =&gt; { particles.forEach(particle =&gt; { particle.applyForce(force); }); }; const run = () =&gt; { particles.forEach((particle, index) =&gt; { particle.move(); particle.draw(); if (particle.isDead()) { particles.splice(index, 1); } }); }; return { origin, addParticle, run, applyForce }; }; const createSparkParticle = (locationP, velocity) =&gt; { const particle = createParticle(locationP, velocity); let fade = 255; const draw = () =&gt; { p.colorMode(p.HSB); p.stroke(16, 62, 100, fade); const arrow = velocity.copy().normalize().mult(p.random(2, 4)); const direction = p5.Vector.add(particle.location, arrow); p.line(particle.location.x, particle.location.y, direction.x, direction.y); }; const move = () =&gt; { particle.applyForce(p.createVector(p.random(-.2, .2), p.random(-0.1, -0.4))); particle.velocity.add(particle.acc); particle.location.add(particle.velocity.copy().normalize().mult(p.random(2, 4))); particle.acc.mult(0); fade -= 5; }; return { ...particle, draw, move } } const createParticle = (locationP, velocity) =&gt; { const acc = p.createVector(0, 0); const location = locationP.copy(); let fade = 255; const fadeMinus = p.randomGaussian(15, 2); let ligther = 100; let situate = 62; const draw = () =&gt; { p.colorMode(p.HSB) p.stroke(16, p.constrain(situate, 62, 92), p.constrain(ligther, 60, 100), fade); const arrow = velocity.copy().mult(2); const direction = p5.Vector.add(location, arrow); p.line(location.x, location.y, direction.x, direction.y); }; const move = () =&gt; { velocity.add(acc); location.add(velocity.copy().div(p.map(velocity.mag(), 18, 0, 5, 1))); acc.mult(0); fade -= fadeMinus; ligther -= 8; situate += 8; }; const applyForce = force =&gt; { acc.add(force); }; const isDead = () =&gt; { if (fade &lt; 0 || location.x &lt; 0 || location.x &gt; p.width || location.y &gt; p.height) { return true; } else { return false; } }; return { draw, move, applyForce, isDead, velocity, location, acc }; }; const createMover = () =&gt; { const location = p.createVector(p.width / 2, p.height / 2); const velocity = p.createVector(0, 0); const acc = p.createVector(0, 0); const mass = 10; let angle = 0; let angleVelocity = 0; let angleAcc = 0; let len = 100; const particleSystems = [ createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location) ]; const getGotoVector = angle =&gt; { const radius = p.map(angleVelocity, 0, 0.3, 0, 55); const goToVector = p.createVector( location.x + radius * p.cos(angle), location.y + radius * p.sin(angle) ); return goToVector; }; const draw = () =&gt; { const goToVector = getGotoVector(angle); particleSystems.forEach(particleSystem =&gt; { particleSystem.run(); }); }; const renderParticleSystem = () =&gt; { particleSystems.forEach(particleSystem =&gt; { const goToVector = getGotoVector(angle - p.random(0, p.TWO_PI)); const prepencular = p.createVector( (goToVector.y - location.y)*-1, (goToVector.x - location.x) ); prepencular.normalize(); prepencular.mult(angleVelocity * 70); particleSystem.origin.set(goToVector); particleSystem.addParticle(prepencular); const gravity = p.createVector(0, 0.3); particleSystem.applyForce(gravity); }); }; const move = () =&gt; { angleAcc = 0.005; angleVelocity = p.constrain(angleVelocity + angleAcc, 0, 0.32); angle += angleVelocity; angleAcc = 0; renderParticleSystem(); }; return { draw, move }; }; let mover; p.setup = function() { p.createCanvas(240, 320); p.clear(); mover = createMover(); } p.draw = function() { p.clear(); mover.move(); mover.draw(); } }; p5Instance = new p5(sketch); } // stop animation function stopAnimation() { if (p5Instance) { p5Instance.remove(); p5Instance = null; } } &lt;/script&gt; </code></pre> <p>朋友们大家好, 我有6张可以逆转的牌。当其中一张卡片转动时,我希望 Portal 动画出现在卡片背面彩色圆圈的最外层框架中。我怎样才能在我的代码中实现这一目标?</p> <h2><em><strong>6 张卡片分别做什么</strong></em></h2> <p><a href="https://i.stack.imgur.com/o1N8P.jpg" target="_blank"><img src="https://cdn.imgupio.com/i/AWkuc3RhY2suaW1ndXIuY29tL28xTjhQLmpwZw==" alt=""/></a></p> <ul> <li><strong>主文件(含传送门动画):</strong></li> </ul> <p><a href="https://drive.google.com/file/d/1famaPF_Rz-CIjbiBCOlIFZH-LWKjlpv_/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/1famaPF_Rz-CIjbiBCOlIFZH-LWKjlpv_/view?usp=sharing</a></p> <ul> <li><strong>应用程序与门户的演示链接(无法正常工作):</strong></li> </ul> <p><a href="https://tm-game-cards-portal.netlify.app/" rel="nofollow noreferrer">https://tm-game-cards-portal.netlify.app/</a></p> <hr/> <ul> <li><strong>无门户的应用程序演示链接:</strong></li> </ul> <p><a href="https://tm-game-cards-v02.netlify.app/" rel="nofollow noreferrer">https://tm-game-cards-v02.netlify.app/</a></p> <ul> <li><strong>主文件(不包括传送门动画):</strong> <a href="https://drive.google.com/file/d/1fKABYycS1cfBZ7GWYHueSXkH_XnLZZpa/view" rel="nofollow noreferrer">https://drive.google.com/file/d/1fKABYycS1cfBZ7GWYHueSXkH_XnLZZpa/view</a></li> </ul> </question> <answer tick="false" vote="0"> <p>实现此目的的一种方法是创建一个标志变量来跟踪卡片是否翻转。当卡片向后翻转时,您开始动画,当它们翻转到前面时,您停止动画。</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang=&#34;en&#34;&gt; &lt;head&gt; &lt;meta charset=&#34;UTF-8&#34;&gt; &lt;meta name=&#34;viewport&#34; content=&#34;width=device-width, initial-scale=1.0&#34;&gt; &lt;title&gt;Card Flip Animation&lt;/title&gt; &lt;!-- Include any necessary libraries here, such as jQuery and p5.js --&gt; &lt;script src=&#34;https://code.jquery.com/jquery-3.6.0.min.js&#34;&gt;&lt;/script&gt; &lt;script src=&#34;https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js&#34;&gt; &lt;/script&gt; &lt;!-- Include your CSS styles here --&gt; &lt;style&gt; /* Your CSS styles for cards and animations here */ &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;audio id=&#34;flipSound&#34; src=&#34;flip_sound.mp3&#34;&gt;&lt;/audio&gt; &lt;!-- Your HTML for the card here --&gt; &lt;div class=&#34;card&#34;&gt;Card Content&lt;/div&gt; &lt;script&gt; // Add a global variable to track the card flip state let cardFlipped = false; let p5Instance; $(document).ready(function() { var flipSound = document.getElementById(&#34;flipSound&#34;); $(&#34;.card&#34;).click(function() { var card = $(this); if (!card.hasClass(&#39;flipping&#39;)) { card.addClass(&#39;flipping&#39;); if (!card.hasClass(&#34;flipped&#34;)) { // Start animation and set the cardFlipped variable to true startAnimation(); cardFlipped = true; } else { // Stop animation and set the cardFlipped variable to false stopAnimation(); cardFlipped = false; } setTimeout(function() { card.removeClass(&#39;flipping&#39;); }, 1000); } }); // Prevent sound on back side click $(&#34;.card__back&#34;).click(function(e) { e.stopPropagation(); }); }); // portal effect function startAnimation() { const sketch = (p) =&gt; { // Your p5.js animation code here // ... }; p5Instance = new p5(sketch); } // stop animation function stopAnimation() { if (p5Instance) { p5Instance.remove(); p5Instance = null; } } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>现在,您有一个 cardFlipped 变量来跟踪卡片的翻转状态。当卡片向后翻转时(cardFlipped 为 true),启动动画,当卡片翻转到正面时(cardFlipped 为 false),停止动画。</p> </answer> <answer tick="false" vote="0"> <pre><code> `// ... } else { stopAnimation(); console.log(&#34;durduruldu&#34;); card.removeClass(&#34;flipped&#34;); } // ... card.on(&#34;animationend&#34;, function() { stopAnimation(); console.log(&#34;durduruldu&#34;); });` </code></pre> </answer> </body></html>

回答 0 投票 0

Javascript,当卡片翻转时,传送门动画不能出现在卡片后面的圆圈之外

//音效 $(文档).ready(函数() { var FlipSound = document.getElementById("flipSound"); $(".card").click(function() { var ca...</desc> <question vote="-5"> <div> </div> <pre><code>&lt;/script&gt; &lt;script&gt; //sound effect $(document).ready(function() { var flipSound = document.getElementById(&#34;flipSound&#34;); $(&#34;.card&#34;).click(function() { var card = $(this); // Check if the card is not already flipping to avoid multiple flips if (!card.hasClass(&#39;flipping&#39;)) { card.addClass(&#39;flipping&#39;); // Check if the front side is facing the viewer if (!card.hasClass(&#34;flipped&#34;)) { flipSound.currentTime = 0; // Reset the audio to the start flipSound.play(); startAnimation(); console.log(&#34;started&#34;) card.addClass(&#34;flipped&#34;); } else { stopAnimation(); console.log(&#34;stopped&#34;) card.removeClass(&#34;flipped&#34;); } setTimeout(function() { card.removeClass(&#39;flipping&#39;); }, 1000); // Adjust the timeout to match your animation duration } }); // Prevent sound on back side click $(&#34;.card__back&#34;).click(function(e) { e.stopPropagation(); }); }); &lt;/script&gt; &lt;script&gt; // Body hoover effect document.getElementById(&#34;gameCardLink&#34;).addEventListener(&#34;mouseover&#34;, function() { document.body.classList.add(&#34;hoverEffect&#34;); }); document.getElementById(&#34;gameCardLink&#34;).addEventListener(&#34;mouseout&#34;, function() { document.body.classList.remove(&#34;hoverEffect&#34;); }); &lt;/script&gt; &lt;script&gt; // portal effect var p5Instance; function startAnimation() { const sketch = (p) =&gt; { const createParticleSystem = (location) =&gt; { const origin = location.copy(); const particles = []; const addParticle = velocity =&gt; { const rand = p.random(0, 1); if (rand &lt;= .3) { particles.push(createSparkParticle(origin, velocity.copy())); } else { particles.push(createParticle(origin, velocity.copy())); } }; const applyForce = force =&gt; { particles.forEach(particle =&gt; { particle.applyForce(force); }); }; const run = () =&gt; { particles.forEach((particle, index) =&gt; { particle.move(); particle.draw(); if (particle.isDead()) { particles.splice(index, 1); } }); }; return { origin, addParticle, run, applyForce }; }; const createSparkParticle = (locationP, velocity) =&gt; { const particle = createParticle(locationP, velocity); let fade = 255; const draw = () =&gt; { p.colorMode(p.HSB); p.stroke(16, 62, 100, fade); const arrow = velocity.copy().normalize().mult(p.random(2, 4)); const direction = p5.Vector.add(particle.location, arrow); p.line(particle.location.x, particle.location.y, direction.x, direction.y); }; const move = () =&gt; { particle.applyForce(p.createVector(p.random(-.2, .2), p.random(-0.1, -0.4))); particle.velocity.add(particle.acc); particle.location.add(particle.velocity.copy().normalize().mult(p.random(2, 4))); particle.acc.mult(0); fade -= 5; }; return { ...particle, draw, move } } const createParticle = (locationP, velocity) =&gt; { const acc = p.createVector(0, 0); const location = locationP.copy(); let fade = 255; const fadeMinus = p.randomGaussian(15, 2); let ligther = 100; let situate = 62; const draw = () =&gt; { p.colorMode(p.HSB) p.stroke(16, p.constrain(situate, 62, 92), p.constrain(ligther, 60, 100), fade); const arrow = velocity.copy().mult(2); const direction = p5.Vector.add(location, arrow); p.line(location.x, location.y, direction.x, direction.y); }; const move = () =&gt; { velocity.add(acc); location.add(velocity.copy().div(p.map(velocity.mag(), 18, 0, 5, 1))); acc.mult(0); fade -= fadeMinus; ligther -= 8; situate += 8; }; const applyForce = force =&gt; { acc.add(force); }; const isDead = () =&gt; { if (fade &lt; 0 || location.x &lt; 0 || location.x &gt; p.width || location.y &gt; p.height) { return true; } else { return false; } }; return { draw, move, applyForce, isDead, velocity, location, acc }; }; const createMover = () =&gt; { const location = p.createVector(p.width / 2, p.height / 2); const velocity = p.createVector(0, 0); const acc = p.createVector(0, 0); const mass = 10; let angle = 0; let angleVelocity = 0; let angleAcc = 0; let len = 100; const particleSystems = [ createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location) ]; const getGotoVector = angle =&gt; { const radius = p.map(angleVelocity, 0, 0.3, 0, 55); const goToVector = p.createVector( location.x + radius * p.cos(angle), location.y + radius * p.sin(angle) ); return goToVector; }; const draw = () =&gt; { const goToVector = getGotoVector(angle); particleSystems.forEach(particleSystem =&gt; { particleSystem.run(); }); }; const renderParticleSystem = () =&gt; { particleSystems.forEach(particleSystem =&gt; { const goToVector = getGotoVector(angle - p.random(0, p.TWO_PI)); const prepencular = p.createVector( (goToVector.y - location.y)*-1, (goToVector.x - location.x) ); prepencular.normalize(); prepencular.mult(angleVelocity * 70); particleSystem.origin.set(goToVector); particleSystem.addParticle(prepencular); const gravity = p.createVector(0, 0.3); particleSystem.applyForce(gravity); }); }; const move = () =&gt; { angleAcc = 0.005; angleVelocity = p.constrain(angleVelocity + angleAcc, 0, 0.32); angle += angleVelocity; angleAcc = 0; renderParticleSystem(); }; return { draw, move }; }; let mover; p.setup = function() { p.createCanvas(240, 320); p.clear(); mover = createMover(); } p.draw = function() { p.clear(); mover.move(); mover.draw(); } }; p5Instance = new p5(sketch); } // stop animation function stopAnimation() { if (p5Instance) { p5Instance.remove(); p5Instance = null; } } &lt;/script&gt; </code></pre> <p>朋友们您好,我希望当卡片转回时传送门动画开始(确实如此)我希望当卡片转到前面时传送门动画消失,但控制台上没有出现停止消息,动画也没有停止。我怎么解决这个问题?我正在等待您的帮助...</p> <p><strong>主文件(包括传送门动画):</strong> <a href="https://drive.google.com/file/d/1famaPF_Rz-CIjbiBCOlIFZH-LWKjlpv_/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/1famaPF_Rz-CIjbiBColIFZH-LWKjlpv_/view?usp=sharing</a></p> <p><strong>不带门户的应用程序演示链接:</strong> <a href="https://tm-game-cards-v02.netlify.app/" rel="nofollow noreferrer">https://tm-game-cards-v02.netlify.app/</a></p> <p><strong>主文件(不包括传送门动画):</strong> <a href="https://drive.google.com/file/d/1fKABYycS1cfBZ7GWYHueSXkH_XnLZZpa/view" rel="nofollow noreferrer">https://drive.google.com/file/d/1fKABYycS1cfBZ7GWYHueSXkH_XnLZZpa/view</a></p> </question> <answer tick="false" vote="0"> <p>实现此目的的一种方法是创建一个标志变量来跟踪卡片是否翻转。当卡片向后翻转时,您开始动画,当它们翻转到前面时,您停止动画。</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang=&#34;en&#34;&gt; &lt;head&gt; &lt;meta charset=&#34;UTF-8&#34;&gt; &lt;meta name=&#34;viewport&#34; content=&#34;width=device-width, initial-scale=1.0&#34;&gt; &lt;title&gt;Card Flip Animation&lt;/title&gt; &lt;!-- Include any necessary libraries here, such as jQuery and p5.js --&gt; &lt;script src=&#34;https://code.jquery.com/jquery-3.6.0.min.js&#34;&gt;&lt;/script&gt; &lt;script src=&#34;https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js&#34;&gt; &lt;/script&gt; &lt;!-- Include your CSS styles here --&gt; &lt;style&gt; /* Your CSS styles for cards and animations here */ &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;audio id=&#34;flipSound&#34; src=&#34;flip_sound.mp3&#34;&gt;&lt;/audio&gt; &lt;!-- Your HTML for the card here --&gt; &lt;div class=&#34;card&#34;&gt;Card Content&lt;/div&gt; &lt;script&gt; // Add a global variable to track the card flip state let cardFlipped = false; let p5Instance; $(document).ready(function() { var flipSound = document.getElementById(&#34;flipSound&#34;); $(&#34;.card&#34;).click(function() { var card = $(this); if (!card.hasClass(&#39;flipping&#39;)) { card.addClass(&#39;flipping&#39;); if (!card.hasClass(&#34;flipped&#34;)) { // Start animation and set the cardFlipped variable to true startAnimation(); cardFlipped = true; } else { // Stop animation and set the cardFlipped variable to false stopAnimation(); cardFlipped = false; } setTimeout(function() { card.removeClass(&#39;flipping&#39;); }, 1000); } }); // Prevent sound on back side click $(&#34;.card__back&#34;).click(function(e) { e.stopPropagation(); }); }); // portal effect function startAnimation() { const sketch = (p) =&gt; { // Your p5.js animation code here // ... }; p5Instance = new p5(sketch); } // stop animation function stopAnimation() { if (p5Instance) { p5Instance.remove(); p5Instance = null; } } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>现在,您有一个 cardFlipped 变量来跟踪卡片的翻转状态。当卡片向后翻转时(cardFlipped 为 true),启动动画,当卡片翻转到正面时(cardFlipped 为 false),停止动画。</p> </answer> <answer tick="false" vote="0"> <pre><code> `// ... } else { stopAnimation(); console.log(&#34;durduruldu&#34;); card.removeClass(&#34;flipped&#34;); } // ... card.on(&#34;animationend&#34;, function() { stopAnimation(); console.log(&#34;durduruldu&#34;); });` </code></pre> </answer> </body></html>

回答 0 投票 0

洪水填充算法 - 仅在尝试填充的区域之外着色

我正在关注有关实现洪水填充算法的教程(https://www.youtube.com/watch?v=a7qCVxq-dWE),我认为我的代码看起来与我关注的人完全相同,但我...

回答 1 投票 0

如何使画布完全适合p5中的窗口尺寸

我想让 p5 画布完全适合窗口大小,但是每当我使用 windowWidth 和 windowHeight 时,画布似乎更大: 有什么办法可以解决这个问题吗?

回答 2 投票 0

javascript,卡牌翻转时传送门动画无法消失

//音效 $(文档).ready(函数() { var FlipSound = document.getElementById("flipSound"); $(".card").click(function() { var ca...</desc> <question vote="-5"> <div> </div> <pre><code>&lt;/script&gt; &lt;script&gt; //sound effect $(document).ready(function() { var flipSound = document.getElementById(&#34;flipSound&#34;); $(&#34;.card&#34;).click(function() { var card = $(this); // Check if the card is not already flipping to avoid multiple flips if (!card.hasClass(&#39;flipping&#39;)) { card.addClass(&#39;flipping&#39;); // Check if the front side is facing the viewer if (!card.hasClass(&#34;flipped&#34;)) { flipSound.currentTime = 0; // Reset the audio to the start flipSound.play(); startAnimation(); console.log(&#34;started&#34;) card.addClass(&#34;flipped&#34;); } else { stopAnimation(); console.log(&#34;stopped&#34;) card.removeClass(&#34;flipped&#34;); } setTimeout(function() { card.removeClass(&#39;flipping&#39;); }, 1000); // Adjust the timeout to match your animation duration } }); // Prevent sound on back side click $(&#34;.card__back&#34;).click(function(e) { e.stopPropagation(); }); }); &lt;/script&gt; &lt;script&gt; // Body hoover effect document.getElementById(&#34;gameCardLink&#34;).addEventListener(&#34;mouseover&#34;, function() { document.body.classList.add(&#34;hoverEffect&#34;); }); document.getElementById(&#34;gameCardLink&#34;).addEventListener(&#34;mouseout&#34;, function() { document.body.classList.remove(&#34;hoverEffect&#34;); }); &lt;/script&gt; &lt;script&gt; // portal effect var p5Instance; function startAnimation() { const sketch = (p) =&gt; { const createParticleSystem = (location) =&gt; { const origin = location.copy(); const particles = []; const addParticle = velocity =&gt; { const rand = p.random(0, 1); if (rand &lt;= .3) { particles.push(createSparkParticle(origin, velocity.copy())); } else { particles.push(createParticle(origin, velocity.copy())); } }; const applyForce = force =&gt; { particles.forEach(particle =&gt; { particle.applyForce(force); }); }; const run = () =&gt; { particles.forEach((particle, index) =&gt; { particle.move(); particle.draw(); if (particle.isDead()) { particles.splice(index, 1); } }); }; return { origin, addParticle, run, applyForce }; }; const createSparkParticle = (locationP, velocity) =&gt; { const particle = createParticle(locationP, velocity); let fade = 255; const draw = () =&gt; { p.colorMode(p.HSB); p.stroke(16, 62, 100, fade); const arrow = velocity.copy().normalize().mult(p.random(2, 4)); const direction = p5.Vector.add(particle.location, arrow); p.line(particle.location.x, particle.location.y, direction.x, direction.y); }; const move = () =&gt; { particle.applyForce(p.createVector(p.random(-.2, .2), p.random(-0.1, -0.4))); particle.velocity.add(particle.acc); particle.location.add(particle.velocity.copy().normalize().mult(p.random(2, 4))); particle.acc.mult(0); fade -= 5; }; return { ...particle, draw, move } } const createParticle = (locationP, velocity) =&gt; { const acc = p.createVector(0, 0); const location = locationP.copy(); let fade = 255; const fadeMinus = p.randomGaussian(15, 2); let ligther = 100; let situate = 62; const draw = () =&gt; { p.colorMode(p.HSB) p.stroke(16, p.constrain(situate, 62, 92), p.constrain(ligther, 60, 100), fade); const arrow = velocity.copy().mult(2); const direction = p5.Vector.add(location, arrow); p.line(location.x, location.y, direction.x, direction.y); }; const move = () =&gt; { velocity.add(acc); location.add(velocity.copy().div(p.map(velocity.mag(), 18, 0, 5, 1))); acc.mult(0); fade -= fadeMinus; ligther -= 8; situate += 8; }; const applyForce = force =&gt; { acc.add(force); }; const isDead = () =&gt; { if (fade &lt; 0 || location.x &lt; 0 || location.x &gt; p.width || location.y &gt; p.height) { return true; } else { return false; } }; return { draw, move, applyForce, isDead, velocity, location, acc }; }; const createMover = () =&gt; { const location = p.createVector(p.width / 2, p.height / 2); const velocity = p.createVector(0, 0); const acc = p.createVector(0, 0); const mass = 10; let angle = 0; let angleVelocity = 0; let angleAcc = 0; let len = 100; const particleSystems = [ createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location), createParticleSystem(location) ]; const getGotoVector = angle =&gt; { const radius = p.map(angleVelocity, 0, 0.3, 0, 55); const goToVector = p.createVector( location.x + radius * p.cos(angle), location.y + radius * p.sin(angle) ); return goToVector; }; const draw = () =&gt; { const goToVector = getGotoVector(angle); particleSystems.forEach(particleSystem =&gt; { particleSystem.run(); }); }; const renderParticleSystem = () =&gt; { particleSystems.forEach(particleSystem =&gt; { const goToVector = getGotoVector(angle - p.random(0, p.TWO_PI)); const prepencular = p.createVector( (goToVector.y - location.y)*-1, (goToVector.x - location.x) ); prepencular.normalize(); prepencular.mult(angleVelocity * 70); particleSystem.origin.set(goToVector); particleSystem.addParticle(prepencular); const gravity = p.createVector(0, 0.3); particleSystem.applyForce(gravity); }); }; const move = () =&gt; { angleAcc = 0.005; angleVelocity = p.constrain(angleVelocity + angleAcc, 0, 0.32); angle += angleVelocity; angleAcc = 0; renderParticleSystem(); }; return { draw, move }; }; let mover; p.setup = function() { p.createCanvas(240, 320); p.clear(); mover = createMover(); } p.draw = function() { p.clear(); mover.move(); mover.draw(); } }; p5Instance = new p5(sketch); } // stop animation function stopAnimation() { if (p5Instance) { p5Instance.remove(); p5Instance = null; } } &lt;/script&gt; </code></pre> <p>朋友们您好,我希望当卡片转回时传送门动画开始(确实如此)我希望当卡片转到前面时传送门动画消失,但控制台上没有出现停止消息,动画也没有停止。我怎么解决这个问题?我正在等待您的帮助...</p> <p><strong>应用程序的演示链接:</strong> <a href="https://tm-game-cards-v02.netlify.app/" rel="nofollow noreferrer">https://tm-game-cards-v02.netlify.app/</a></p> <p><strong>主文件:</strong> <a href="https://drive.google.com/file/d/1fKABYycS1cfBZ7GWYHueSXkH_XnLZZpa/view" rel="nofollow noreferrer">https://drive.google.com/file/d/1fKABYycS1cfBZ7GWYHueSXkH_XnLZZpa/view</a></p> </question> <answer tick="false" vote="0"> <p>实现此目的的一种方法是创建一个标志变量来跟踪卡片是否翻转。当卡片向后翻转时,您开始动画,当它们翻转到前面时,您停止动画。</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang=&#34;en&#34;&gt; &lt;head&gt; &lt;meta charset=&#34;UTF-8&#34;&gt; &lt;meta name=&#34;viewport&#34; content=&#34;width=device-width, initial-scale=1.0&#34;&gt; &lt;title&gt;Card Flip Animation&lt;/title&gt; &lt;!-- Include any necessary libraries here, such as jQuery and p5.js --&gt; &lt;script src=&#34;https://code.jquery.com/jquery-3.6.0.min.js&#34;&gt;&lt;/script&gt; &lt;script src=&#34;https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js&#34;&gt; &lt;/script&gt; &lt;!-- Include your CSS styles here --&gt; &lt;style&gt; /* Your CSS styles for cards and animations here */ &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;audio id=&#34;flipSound&#34; src=&#34;flip_sound.mp3&#34;&gt;&lt;/audio&gt; &lt;!-- Your HTML for the card here --&gt; &lt;div class=&#34;card&#34;&gt;Card Content&lt;/div&gt; &lt;script&gt; // Add a global variable to track the card flip state let cardFlipped = false; let p5Instance; $(document).ready(function() { var flipSound = document.getElementById(&#34;flipSound&#34;); $(&#34;.card&#34;).click(function() { var card = $(this); if (!card.hasClass(&#39;flipping&#39;)) { card.addClass(&#39;flipping&#39;); if (!card.hasClass(&#34;flipped&#34;)) { // Start animation and set the cardFlipped variable to true startAnimation(); cardFlipped = true; } else { // Stop animation and set the cardFlipped variable to false stopAnimation(); cardFlipped = false; } setTimeout(function() { card.removeClass(&#39;flipping&#39;); }, 1000); } }); // Prevent sound on back side click $(&#34;.card__back&#34;).click(function(e) { e.stopPropagation(); }); }); // portal effect function startAnimation() { const sketch = (p) =&gt; { // Your p5.js animation code here // ... }; p5Instance = new p5(sketch); } // stop animation function stopAnimation() { if (p5Instance) { p5Instance.remove(); p5Instance = null; } } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>现在,您有一个 cardFlipped 变量来跟踪卡片的翻转状态。当卡片向后翻转时(cardFlipped 为 true),启动动画,当卡片翻转到正面时(cardFlipped 为 false),停止动画。</p> </answer> </body></html>

回答 0 投票 0

用于产品草图绘制的 JS 库

我需要帮助找到一个好的 JS 库,可以创建如下产品草图: 分区草图。我正在使用 Next.js,并且大多数库都遇到麻烦,因为 Next.js 使用 SSR 和大多数

回答 1 投票 0

如何从菜单画面切换到实际游戏?

所以我尝试在菜单屏幕(目前只是带有一行文本的空白画布)和 p5.js 中的实际游戏屏幕之间切换。我目前尝试的是使用一个名为 's...

回答 1 投票 0

如何从菜单画面切换到实际游戏? p5.js

所以我尝试在菜单屏幕(目前只是带有一行文本的空白画布)和 p5.js 中的实际游戏屏幕之间切换。我目前尝试的是使用一个名为 's...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.