漂亮的动态气泡背景效果bubbly-bg.js

小天天天天    前端    999+ 次    2019-01-23 18:56:54


bubbly-bg.js是一款漂亮的动态气泡背景js插件。它是基于HTML5 canvas,压缩后的版本小于1kb,但是它能制作出各种漂亮的动态气泡背景效果,非常强大。

使用

可以通过npm来安装bubbly-bg.js动态气泡背景插件。

npm install bubbly-bg --save

可以在页面中引入bubbly-bg.js文件。

<script type="text/javascript" src="path/to/js/bubbly-bg.js"></script> 

HTML结构

如果你不指定<canvas>元素作为容器,而是直接在body中初始化插件,那么插件会在body之后创建一个<canvas>元素,这个元素具有position: fixedz-index: -1属性,并且它的宽度和高度始终会等于视口的宽度和高度。例如:

<body>
  ...
  <script src="js/bubbly-bg.js"></script>
  <script>bubbly();</script>
</body>

你也可以指定一个<canvas>作为动态气泡背景的容器。例如:

<canvas id="demo" width="400" height="300"></canvas>

然后通过下面的方法来初始化。

bubbly({
  canvas: document.getElementById("demo")
});

配置参数

bubbly-bg.js动态气泡背景插件的可用配置参数有:

bubbly({
    animate: false, // default is true
    blur: 1, // default is 4
    bubbleFunc: () => `hsla(${Math.random() * 360}, 100%, 50%, ${Math.random() * 0.25})`, // default is () => `hsla(0, 0%, 100%, ${r() * 0.1})`)
    bubbles: 100, // default is Math.floor((canvas.width + canvas.height) * 0.02);
    canvas: document.querySelector("#background"), // default is created and attached
    colorStart: "#4c004c", // default is blue-ish
    colorStop: "#1a001a",// default is blue-ish
    compose: "lighter", // default is "lighter"
    shadowColor: "#0ff", // default is #fff
});

示例,黄色/粉色带红色/橙色/黄色气泡效果:

bubbly({
    colorStart: "#fff4e6",
    colorStop: "#ffe9e4",
    blur: 1,
    compose: "source-over",
    bubbleFunc: () => `hsla(${Math.random() * 50}, 100%, 50%, .3)`
});

该项目的github地址:https://github.com/tipsy/bubbly-bg.


如果你觉得本篇文章对您有帮助,请打赏作者

上一篇: Swoole实践:7-使用Websocket上传文件(1)

下一篇: PHP RSA加解密示例

最新评论

暂无评论

热门文章

最新评论

网站数据

网站文章数:481

今日UV/PV/IP:13/15/13

昨日UV/PV/IP:33/37 /33

TOP