logo
当前位置:首 页 > 编程技术 >前端开发 >HTML/CSS > 查看文章

自己的博客网站背景都是就静止的太没有动感了,今天介绍一个新发现的背景插件ribbons

 

下面是主要代码 ,加入到页面或者做成js文件引入都行

  1. (function (name, factory) {
  2. if (typeof window === "object") {
  3. window[name] = factory();
  4. }
  5. })
  6. ("Ribbons", function () {
  7. var _w = window, _b = document.body, _d = document.documentElement;
  8.  
  9. //随机函数
  10. var random = function () {
  11. if (arguments.length === 1) {
  12. if (Array.isArray(arguments[0])) {
  13. var index = Math.round(random(0, arguments[0].length - 1));
  14. return arguments[0][index];
  15. }
  16. return random(0, arguments[0]);
  17. } else
  18. if (arguments.length === 2) {
  19. return Math.random() * (arguments[1] - arguments[0]) + arguments[0];
  20. }
  21. return 0;
  22. };
  23.  
  24. //屏幕信息
  25. var screenInfo = function (e) {
  26. var width = Math.max(0, _w.innerWidth || _d.clientWidth || _b.clientWidth || 0),
  27. height = Math.max(0, _w.innerHeight || _d.clientHeight || _b.clientHeight || 0),
  28. scrollx = Math.max(0, _w.pageXOffset || _d.scrollLeft || _b.scrollLeft || 0) - (_d.clientLeft || 0),
  29. scrolly = Math.max(0, _w.pageYOffset || _d.scrollTop || _b.scrollTop || 0) - (_d.clientTop || 0);
  30. return {
  31. width: width,
  32. height: height,
  33. ratio: width / height,
  34. centerx: width / 2,
  35. centery: height / 2,
  36. scrollx: scrollx,
  37. scrolly: scrolly
  38. };
  39. };
  40.  
  41. var mouseInfo = function (e) {
  42. var screen = screenInfo(e),
  43. mousex = e ? Math.max(0, e.pageX || e.clientX || 0) : 0,
  44. mousey = e ? Math.max(0, e.pageY || e.clientY || 0) : 0;
  45.  
  46. return {
  47. mousex: mousex,
  48. mousey: mousey,
  49. centerx: mousex - screen.width / 2,
  50. centery: mousey - screen.height / 2
  51. };
  52. };
  53.  
  54. //点
  55. var Point = function (x, y) {
  56. this.x = 0;
  57. this.y = 0;
  58. this.set(x, y);
  59. };
  60. //点运算
  61. Point.prototype = {
  62. constructor: Point,
  63. set: function (x, y) {
  64. this.x = x || 0; this.y = y || 0;
  65. },
  66. copy: function (point) {
  67. this.x = point.x || 0; this.y = point.y || 0; return this;
  68. },
  69. multiply: function (x, y) {
  70. this.x *= x || 1; this.y *= y || 1; return this;
  71. },
  72. divide: function (x, y) {
  73. this.x /= x || 1; this.y /= y || 1; return this;
  74. },
  75. add: function (x, y) {
  76. this.x += x || 0; this.y += y || 0; return this;
  77. },
  78. subtract: function (x, y) {
  79. this.x -= x || 0; this.y -= y || 0; return this;
  80. },
  81. clampX: function (min, max) {
  82. this.x = Math.max(min, Math.min(this.x, max)); return this;
  83. },
  84. clampY: function (min, max) {
  85. this.y = Math.max(min, Math.min(this.y, max)); return this;
  86. },
  87. flipX: function () {
  88. this.x *= -1; return this;
  89. },
  90. flipY: function () {
  91. this.y *= -1; return this;
  92. }
  93. };
  94.  
  95. //丝带画板
  96. var Factory = function (options) {
  97. this._canvas = null;
  98. this._context = null;
  99. this._sto = null;
  100. this._width = 0;
  101. this._height = 0;
  102. this._scroll = 0;
  103. this._ribbons = [];
  104. this._options = {
  105. id: "bgCanvas",//画板Id
  106. backgroundColor: "#1f1f1f",//画板背景
  107. colorSaturation: "80%",//纯度
  108. colorBrightness: "60%",//亮度
  109. colorAlpha: 0.65,//透明度
  110. colorCycleSpeed: 6,//丝带不同块之间的色彩变化量
  111. verticalPosition: "center",//丝带相对于屏幕的初始位置:top/min 屏幕最上方,middle|center 中间,bottom|max 屏幕最下面
  112. horizontalSpeed: 200,//丝带水平方向移动速度参数(会乘以一个随机值)
  113. ribbonCount: 3,//同一时间丝带总条数
  114. strokeSize: 0,//公共边路径样式
  115. parallaxAmount: -0.5,//滚动偏移参数,-1表示不偏移,0表示基于丝带只出现在页面最上面
  116. animateSections: true//丝带块是否偏移,显得有动感
  117. };
  118. this._onDraw = this._onDraw.bind(this);
  119. this._onResize = this._onResize.bind(this);
  120. this._onScroll = this._onScroll.bind(this);
  121. this.setOptions(options);
  122. this.init();
  123. };
  124. Factory.prototype = {
  125. constructor: Factory,
  126. setOptions: function (options) {
  127. if (typeof options === "object") {
  128. for (var key in options) {
  129. if (options.hasOwnProperty(key)) {
  130. this._options[key] = options[key];
  131. }
  132. }
  133. }
  134. },
  135. //初始化
  136. init: function () {
  137. //初始化画板
  138. try {
  139. this._canvas = document.createElement("canvas");
  140. this._canvas.style["display"] = "block";
  141. this._canvas.style["position"] = "fixed";
  142. this._canvas.style["margin"] = "0";
  143. this._canvas.style["padding"] = "0";
  144. this._canvas.style["border"] = "0";
  145. this._canvas.style["outline"] = "0";
  146. this._canvas.style["left"] = "0";
  147. this._canvas.style["top"] = "0";
  148. this._canvas.style["width"] = "100%";
  149. this._canvas.style["height"] = "100%";
  150. this._canvas.style["z-index"] = "-1";
  151. this._canvas.style["background-color"] = this._options.backgroundColor;
  152. this._canvas.id = this._options.id;
  153. this._onResize();
  154. this._context = this._canvas.getContext("2d");
  155. this._context.clearRect(0, 0, this._width, this._height);
  156. this._context.globalAlpha = this._options.colorAlpha;
  157. window.addEventListener("resize", this._onResize);
  158. window.addEventListener("scroll", this._onScroll);
  159. document.body.appendChild(this._canvas);
  160. }
  161. catch (e) {
  162. console.warn("Canvas Context Error: " + e.toString());
  163. return;
  164. }
  165. //开始绘画
  166. this._onDraw();
  167. },
  168. //生成一条丝带
  169. addRibbon: function () {
  170. var dir = Math.round(random(1, 9)) > 5 ? "right" : "left",//丝带延伸方向
  171. stop = 1000,
  172. hide = 200,
  173. min = 0 - hide,
  174. max = this._width + hide,
  175. movex = 0,
  176. movey = 0,
  177. startx = dir === "right" ? min : max,//起始点x左边
  178. starty = Math.round(random(0, this._height));//起始点y左边
  179.  
  180. //丝带生成的位置
  181. if (/^(top|min)$/i.test(this._options.verticalPosition)) {//最上方
  182. starty = 0 + hide;
  183. } else if (/^(middle|center)$/i.test(this._options.verticalPosition)) {//中间
  184. starty = this._height / 2;
  185. } else if (/^(bottom|max)$/i.test(this._options.verticalPosition)) {//最下方
  186. starty = this._height - hide;
  187. }
  188.  
  189. if (this._options.parallaxAmount !== 0) {
  190. starty += this._scroll;//加上滚动
  191. }
  192.  
  193. var ribbon = [],
  194. point1 = new Point(startx, starty),
  195. point2 = new Point(startx, starty),
  196. point3 = null,
  197. color = Math.round(random(0, 360)),
  198. delay = 0;
  199.  
  200. //从起始位置开始生成一条丝带
  201. //丝带每个分部都是一个三角形,三点确定一个三角形,相邻三角形有一条公共边
  202. while (true) {
  203. if (stop <= 0) break;
  204. stop--;
  205. movex = Math.round((Math.random() * 1 - 0.2) * this._options.horizontalSpeed);
  206. movey = Math.round((Math.random() * 1 - 0.5) * (this._height * 0.25));
  207. point3 = new Point();
  208. point3.copy(point2);
  209. if (dir === "right") {
  210. point3.add(movex, movey);
  211. if (point2.x >= max) break;
  212. } else if (dir === "left") {
  213. point3.subtract(movex, movey);
  214. if (point2.x <= min) break;
  215. }
  216. ribbon.push({
  217. //三点
  218. point1: new Point(point1.x, point1.y),
  219. point2: new Point(point2.x, point2.y),
  220. point3: point3,
  221. color: color,//丝带颜色
  222. delay: delay,//延迟消失
  223. dir: dir,//方向
  224. alpha: 0,//透明度
  225. phase: 0 //随机位移有关参数
  226. });
  227. //公共边
  228. point1.copy(point2);
  229. point2.copy(point3);
  230. delay += 4;
  231. color += this._options.colorCycleSpeed;
  232. }
  233. this._ribbons.push(ribbon);
  234. },
  235. //绘制一个三角形方块
  236. _drawRibbonSection: function (section) {
  237. if (section) {
  238. if (section.phase >= 1 && section.alpha <= 0) {
  239. return true;
  240. }
  241. if (section.delay <= 0) {
  242. section.phase += 0.02;
  243. section.alpha = Math.sin(section.phase) * 1;
  244. section.alpha = section.alpha <= 0 ? 0 : section.alpha;
  245. section.alpha = section.alpha >= 1 ? 1 : section.alpha;
  246. if (this._options.animateSections) {
  247. var mod = Math.sin(1 + section.phase * Math.PI / 2) * 0.1;
  248. if (section.dir === "right") {
  249. section.point1.add(mod, 0);
  250. section.point2.add(mod, 0);
  251. section.point3.add(mod, 0);
  252. } else {
  253. section.point1.subtract(mod, 0);
  254. section.point2.subtract(mod, 0);
  255. section.point3.subtract(mod, 0);
  256. }
  257. section.point1.add(0, mod);
  258. section.point2.add(0, mod);
  259. section.point3.add(0, mod);
  260. }
  261. } else {
  262. section.delay -= 0.5;
  263. }
  264. var s = this._options.colorSaturation,
  265. l = this._options.colorBrightness,
  266. c = "hsla(" + section.color + ", " + s + ", " + l + ", " + section.alpha + " )";
  267.  
  268. //绘制一个方块
  269. this._context.save();
  270. if (this._options.parallaxAmount !== 0) {
  271. this._context.translate(0, this._scroll * this._options.parallaxAmount);
  272. }
  273. this._context.beginPath();
  274. this._context.moveTo(section.point1.x, section.point1.y);
  275. this._context.lineTo(section.point2.x, section.point2.y);
  276. this._context.lineTo(section.point3.x, section.point3.y);
  277. this._context.fillStyle = c;
  278. this._context.fill();
  279. if (this._options.strokeSize > 0) {
  280. this._context.lineWidth = this._options.strokeSize;
  281. this._context.strokeStyle = c;
  282. this._context.lineCap = "round";
  283. this._context.stroke();
  284. }
  285. this._context.restore();
  286. }
  287. return false;
  288. },
  289. //绘制丝带
  290. _onDraw: function () {
  291. //清空已经绘制过的丝带
  292. for (var i = 0, t = this._ribbons.length; i < t; ++i) {
  293. if (!this._ribbons[i]) {
  294. this._ribbons.splice(i, 1);
  295. }
  296. }
  297. this._context.clearRect(0, 0, this._width, this._height);//清空画板
  298. for (var a = 0; a < this._ribbons.length; ++a) {
  299. var ribbon = this._ribbons[a],
  300. numSections = ribbon ? ribbon.length : 0,
  301. numDone = 0;
  302.  
  303. //绘制整条丝带
  304. for (var b = 0; b < numSections; ++b) {
  305. if (this._drawRibbonSection(ribbon[b])) {
  306. numDone++;
  307. }
  308. }
  309. //丝带已经全部飘过屏幕,设置为null,函数前面会自动清理
  310. if (numDone >= numSections) {
  311. this._ribbons[a] = null;
  312. }
  313. }
  314. //随机生成一条丝带
  315. if (this._ribbons.length < this._options.ribbonCount && Math.random() > 0.99) {
  316. this.addRibbon();
  317. }
  318.  
  319. //调度交给系统,当需要刷新画板时调用指定的回调函数,用于提高性能
  320. requestAnimationFrame(this._onDraw);
  321. },
  322. //重新设置窗体大小时需要获取窗体大小
  323. _onResize: function (e) {
  324. var screen = screenInfo(e);
  325. this._width = screen.width;
  326. this._height = screen.height;
  327. if (this._canvas) {
  328. this._canvas.width = this._width;
  329. this._canvas.height = this._height;
  330. if (this._context) {
  331. this._context.globalAlpha = this._options.colorAlpha;
  332. }
  333. }
  334. },
  335. //滚动时获取滚动距离
  336. _onScroll: function (e) {
  337. var screen = screenInfo(e);
  338. this._scroll = screen.scrolly;
  339. }
  340. }; return Factory;
  341. });
  342.  
  343. //初始化并绘制
  344. new Ribbons({
  345. ribbonCount: 5,
  346. parallaxAmount: -0.99
  347. });

直接使用就可以了不需要在页面做html埋点之类的

参数自己编辑就可以了:

  1. options = {
  2. id: "bgCanvas",//画板Id
  3. backgroundColor: "#1f1f1f",//画板背景
  4. colorSaturation: "80%",//纯度
  5. colorBrightness: "60%",//亮度
  6. colorAlpha: 0.65,//透明度
  7. colorCycleSpeed: 6,//丝带不同块之间的色彩变化量
  8. verticalPosition: "center",//丝带相对于屏幕的初始位置:top/min 屏幕最上方,middle|center 中间,bottom|max 屏幕最下面
  9. horizontalSpeed: 200,//丝带水平方向移动速度参数(会乘以一个随机值)
  10. ribbonCount: 3,//同一时间丝带总条数
  11. strokeSize: 0,//公共边路径样式
  12. parallaxAmount: -0.5,//滚动偏移参数,-1表示不偏移,0表示基于丝带只出现在页面最上面
  13. animateSections: true//丝带块是否偏移,显得有动感
  14. };

 

效果自己看,页面已经上上去了

 

 

 

 

 

说说梦想,谈谈感悟 ,聊聊技术,有啥要说的来github留言吧 https://github.com/cjx2328

—— 陈 建鑫

陈建鑫
footer logo
未经许可请勿自行使用、转载、修改、复制、发行、出售、发表或以其它方式利用本网站之内容。站长联系:cjx2328#126.com(修改#为@)
Copyright ©ziao design Studio All Rights Reserved.沪ICP备14052271号-3