gpt4 book ai didi

javascript - 如何将统一渐变应用于 (particule.js) Canvas ?

转载 作者:太空宇宙 更新时间:2023-11-04 05:41:05 25 4
gpt4 key购买 nike

我一直在寻找一种简单有效的方法来将单个梯度应用于来自 particles.js 的移动粒子图书馆。理想情况下,我只想要使用 CSS 的东西,但我现在很确定,即使我已经阅读了有关 clip-path 的有趣内容,这也是不可能的 explanations on clipping and masking using CSS .

我的目标是做这样的动态(除了Asesprite我没有真正的版本软件,请不要怪我,如果你知道一个好的linux矢量工具,请告诉我):enter image description here

要查看库的实现,请查看 demo on CodePen .

我不使用带有 particles.js 的服务器(也可用 there (GitHub),所以我想要一个可以在链接到我的 .html 文件的 app.js 文件上实现的解决方案:

/* ---- particles.js config ---- */
// default shape: circles

particlesJS("particles-js", {
"particles": {
"number": {
"value": 80,
"density": {
"enable": true,
"value_area": 800
}
},
"color": {
"value": "#ffffff"
},
"shape": {
"type": "stroke",
"stroke": {
"width": 0,
"color": "#000000"
},
"polygon": {
"nb_sides": 5
},
"image": {
"src": "../img/particle.png",
"width": 100,
"height": 100
}
},
"opacity": {
"value": 0.5,
"random": true,
"anim": {
"enable": true,
"speed": 1,
"opacity_min": 0.1,
"sync": false
}
},
"size": {
"value": 3,
"random": true,
"anim": {
"enable": false,
"speed": 40,
"size_min": 0.1,
"sync": false
}
},
"line_linked": {
"enable": true,
"distance": 180,
"color": "#ffffff",
"opacity": 0.8,
"width": 2
},
"move": {
"enable": true,
"speed": 6,
"direction": "none",
"random": false,
"straight": false,
"out_mode": "out",
"bounce": false,
"attract": {
"enable": false,
"rotateX": 600,
"rotateY": 1200
}
}
},
"interactivity": {
"detect_on": "canvas",
"events": {
"onhover": {
"enable": true,
"mode": "repulse"
},
"onclick": {
"enable": false,
"mode": "bubble"
},
"resize": true
},
"modes": {
"grab": {
"distance": 140,
"line_linked": {
"opacity": 1
}
},
"bubble": {
"distance": 400,
"size": 40,
"duration": 2,
"opacity": 8,
"speed": 3
},
"repulse": {
"distance": 90,
"duration": 0.4
},
"push": {
"particles_nb": 4
},
"remove": {
"particles_nb": 2
}
}
},
"retina_detect": true
});

我知道有类似的问题,但我不明白我如何设法使粒子充当后固定梯度的过滤器: a similar case , another one .

最佳答案

也许可以通过 CSS 和 mix-blend-mode 来做到这一点,但这也会影响底层元素(这里是红色背景),所以你也必须使用过滤器,最后你会得到您会发现自己在初始 Canvas 上进行的如此复杂的图像处理可能更容易和更快(就性能而言),使用简单的 composite operation :

particlesJS("particles-js", {
"particles": {
"number": {
"value": 380,
"density": {
"enable": true,
"value_area": 800
}
},
"color": {
"value": "#ffffff"
},
"shape": {
"type": "circle",
"stroke": {
"width": 0,
"color": "#000000"
},
"polygon": {
"nb_sides": 5
},
"image": {
"src": "img/github.svg",
"width": 100,
"height": 100
}
},
"opacity": {
"value": 0.5,
"random": false,
"anim": {
"enable": false,
"speed": 1,
"opacity_min": 0.1,
"sync": false
}
},
"size": {
"value": 3,
"random": true,
"anim": {
"enable": false,
"speed": 40,
"size_min": 0.1,
"sync": false
}
},
"line_linked": {
"enable": true,
"distance": 150,
"color": "#ffffff",
"opacity": 0.4,
"width": 1
},
"move": {
"enable": true,
"speed": 6,
"direction": "none",
"random": false,
"straight": false,
"out_mode": "out",
"bounce": false,
"attract": {
"enable": false,
"rotateX": 600,
"rotateY": 1200
}
}
},
"interactivity": {
"detect_on": "canvas",
"events": {
"onhover": {
"enable": true,
"mode": "grab"
},
"onclick": {
"enable": true,
"mode": "push"
},
"resize": true
},
"modes": {
"grab": {
"distance": 140,
"line_linked": {
"opacity": 1
}
},
"bubble": {
"distance": 400,
"size": 40,
"duration": 2,
"opacity": 8,
"speed": 3
},
"repulse": {
"distance": 200,
"duration": 0.4
},
"push": {
"particles_nb": 4
},
"remove": {
"particles_nb": 2
}
}
},
"retina_detect": true
});


// setup over-drawing
const ctx = document.querySelector('#particles-js > canvas').getContext('2d');
let grad;
onresize();
addEventListener('resize', onresize);

function onresize() {
grad= ctx.createLinearGradient(0,0,ctx.canvas.width,0);
grad.addColorStop(0,'yellow');
grad.addColorStop(1,'green');
}

// must be ran after Particles.js' own anim loop has began
// se we are always pushed after their drawings
requestAnimationFrame( anim );
function anim() {
ctx.fillStyle = grad;
ctx.globalCompositeOperation = "source-atop";
ctx.fillRect(0,0,ctx.canvas.width,ctx.canvas.height);
ctx.globalCompositeOperation = "source-over";
requestAnimationFrame( anim );
}
/* ---- reset ---- */

body {
margin: 0;
font:normal 75% Arial, Helvetica, sans-serif;
}

canvas {
display: block;
vertical-align: bottom;
}

/* ---- particles.js container ---- */

#particles-js {
position: absolute;
width: 100%;
height: 100%;
background-color: #b61924;
background-image: url("");
background-repeat: no-repeat;
background-size: cover;
background-position: 50% 50%;
}
<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
<div id="particles-js"></div>

关于javascript - 如何将统一渐变应用于 (particule.js) Canvas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59221898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com