• 欢迎访问情深深几许的博客网站,这里有免费网络资源信息,WordPress教程,Python、MySQL教程
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏本网站吧

随鼠标变换的动态线条效果

wordPress luckzk 6年前 (2019-07-21) 1194次浏览 0个评论
文章目录[隐藏]

在别人的博客看到一个随鼠标变换的动态线条效果,也就是俗称的蜘蛛网效果,如下图(或者直接看我的博客背景也可以哦)
随鼠标变换的动态线条效果
这个效果的实现其实并不难,有两种方法提供给了大家:

方式一

直接使用网上的js代码

  1. <script type="text/javascript" src="https://cdn.bootcss.com/canvas-nest.js/1.0.1/canvas-nest.min.js"></script> 

将上面的代码直接添加到博客主页面就行了。

方式二

添加到本地js文件:

  1. /**
  2. * Copyright (c) 2016 hustcc
  3. * License: MIT
  4. * Version: v1.0.1
  5. * GitHub: https://github.com/hustcc/canvas-nest.js
  6. **/
  7. !
  8. function() {
  9. function n(n, e, t) {
  10. return n.getAttribute(e) || t
  11. }
  12. function e(n) {
  13. return document.getElementsByTagName(n)
  14. }
  15. function t() {
  16. var t = e("script"),
  17. o = t.length,
  18. i = t[o - 1];
  19. return {
  20. l: o,
  21. z: n(i, "zIndex", -1),
  22. o: n(i, "opacity", .5),
  23. c: n(i, "color", "0,0,0"),
  24. n: n(i, "count", 99)
  25. }
  26. }
  27. function o() {
  28. a = m.width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
  29. c = m.height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
  30. }
  31. function i() {
  32. r.clearRect(0, 0, a, c);
  33. var n, e, t, o, m, l;
  34. s.forEach(function(i, x) {
  35. for (i.x += i.xa, i.y += i.ya, i.xa *= i.x > a || i.x < 0 ? -1 : 1, i.ya *= i.y > c || i.y < 0 ? -1 : 1, r.fillRect(i.x - .5, i.y - .5, 1, 1), e = x + 1; e < u.length; e++) n = u[e],
  36. null !== n.x && null !== n.y && (o = i.x - n.x, m = i.y - n.y, l = o * o + m * m, l < n.max && (n === y && l >= n.max / 2 && (i.x -= .03 * o, i.y -= .03 * m), t = (n.max - l) / n.max, r.beginPath(), r.lineWidth = t / 2, r.strokeStyle = "rgba(" + d.c + "," + (t + .2) + ")", r.moveTo(i.x, i.y), r.lineTo(n.x, n.y), r.stroke()))
  37. }),
  38. x(i)
  39. }
  40. var a, c, u, m = document.createElement("canvas"),
  41. d = t(),
  42. l = "c_n" + d.l,
  43. r = m.getContext("2d"),
  44. x = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
  45. function(n) {
  46. window.setTimeout(n, 1e3 / 45)
  47. },
  48. w = Math.random,
  49. y = {
  50. x: null,
  51. y: null,
  52. max: 2e4
  53. };
  54. m.id = l,
  55. m.style.cssText = "position:fixed;top:0;left:0;z-index:" + d.z + ";opacity:" + d.o,
  56. e("body")[0].appendChild(m),
  57. o(),
  58. window.onresize = o,
  59. window.onmousemove = function(n) {
  60. n = n || window.event,
  61. y.x = n.clientX,
  62. y.y = n.clientY
  63. },
  64. window.onmouseout = function() {
  65. y.x = null,
  66. y.y = null
  67. };
  68. for (var s = [], f = 0; d.n > f; f++) {
  69. var h = w() * a,
  70. g = w() * c,
  71. v = 2 * w() - 1,
  72. p = 2 * w() - 1;
  73. s.push({
  74. x: h,
  75. y: g,
  76. xa: v,
  77. ya: p,
  78. max: 6e3
  79. })
  80. }
  81. u = s.concat([y]),
  82. setTimeout(function() {
  83. i()
  84. },
  85. )
  86. } ();
  87.  

本效果的原作者的GitHub地址是https://github.com/hustcc/canvas-nest.js


情深几许 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:随鼠标变换的动态线条效果
喜欢 (0)
关于作者:
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址