【问题标题】:How to apply a uniform gradient to a (particule.js) canvas?如何将均匀渐变应用于(particule.js)画布?
【发布时间】:2020-03-31 23:08:08
【问题描述】:

我一直在寻找一种简单有效的方法来将单个渐变应用到来自particles.js 库的移动粒子。理想情况下,我想要只使用 CSS 的东西,但我现在很确定即使我已经阅读了关于剪辑路径的有趣内容,例如 explanations on clipping and masking using CSS

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

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

我不使用带有particle.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.

【问题讨论】:

    标签: javascript css html5-canvas gradient


    【解决方案1】:

    也许可以通过 CSS 和 mix-blend-mode 做到这一点,但这也会影响底层元素(这里是红色背景),所以你也必须使用过滤器,最后你会得到使用简单的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>

    【讨论】:

    • 好的,它工作得很好,你是个英雄,祝你有美好的一天。总结一下,here 我们可以找到更多关于线性渐变以及如何使用它们的信息。祝你有美好的一天;)
    猜你喜欢
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 2011-04-04
    • 2013-03-25
    • 1970-01-01
    • 2011-04-29
    相关资源
    最近更新 更多