JavaScript 是浏览器端脚本语言,有许多非常实用的第三方 JS 类库,我们可以借助它来实现各种各样的特效,今天为大家分享一下基于 sketch.js 实现鼠标点击特效的方法。

基于 sketch.js 实现非常酷炫的鼠标点击特效

实现方法

实现的方法很简单,只需要以下三步就可以完成。

引入sketch.js

在 functions.php 文件中引入 sketch.min.js,文件下载地址:

https://www.bootcdn.cn/sketch.js。

添加 html 代码

然后在 body 标签中任意位置加入下面这段 HTML 代码。

  1. <?php !wp_is_mobile()): //排除移动设备 ?>
  2. <canvas id="mineCanvas" style="position:fixed;left:0;top:0;z-index:99999999;pointer-events:none;"></canvas>
  3. <?php endif; ?>

添加 js 代码

将下面的 JS 代码添加到 footer.php 中,也可以将内容添加到 sketch.min.js 文件末尾。

  1. <script type='text/javascript'>
  2. if(document.getElementById("clickCanvas")) {
  3. function Particle(x, y, radius) {
  4. this.init(x, y, radius);
  5. }
  6. Particle.prototype = {
  7. init : function(x, y, radius) {
  8. this.alive = true;
  9. this.radius = radius || 10;
  10. this.wander = 0.15;
  11. this.theta = random(TWO_PI);
  12. this.drag = 0.92;
  13. this.color = '#ffeb3b';
  14.  
  15. this.x = x || 0.0;
  16. this.y = y || 0.0;
  17. this.vx = 0.0;
  18. this.vy = 0.0;
  19. },
  20. move : function() {
  21. this.x += this.vx;
  22. this.y += this.vy;
  23. this.vx *= this.drag;
  24. this.vy *= this.drag;
  25. this.theta += random(-0.5, 0.5) * this.wander;
  26. this.vx += sin(this.theta) * 0.1;
  27. this.vy += cos(this.theta) * 0.1;
  28. this.radius *= 0.96;
  29. this.alive = this.radius > 0.5;
  30. },
  31. draw : function(ctx) {
  32. ctx.beginPath();
  33. ctx.arc(this.x, this.y, this.radius, 0, TWO_PI);
  34. ctx.fillStyle = this.color;
  35. ctx.fill();
  36. }
  37. };
  38. var MAX_PARTICLES = 50;
  39. //圆点颜色库
  40. var COLOURS = [ "#5ee4ff", "#f44033", "#ffeb3b", "#F38630", "#FA6900", "#f403e8", "#F9D423" ];
  41. var particles = [];
  42. var pool = [];
  43. var clickparticle = Sketch.create({
  44. container : document.getElementById('clickCanvas')
  45. });
  46. clickparticle.spawn = function(x, y) {
  47. if (particles.length >= MAX_PARTICLES)
  48. pool.push(particles.shift());
  49. particle = pool.length ? pool.pop() : new Particle();
  50. particle.init(x, y, random(5, 20));//圆点大小范围
  51. particle.wander = random(0.5, 2.0);
  52. particle.color = random(COLOURS);
  53. particle.drag = random(0.9, 0.99);
  54. theta = random(TWO_PI);
  55. force = random(1, 5);
  56. particle.vx = sin(theta) * force;
  57. particle.vy = cos(theta) * force;
  58. particles.push(particle);
  59. };
  60. clickparticle.update = function() {
  61. var i, particle;
  62. for (i = particles.length - 1; i >= 0; i--) {
  63. particle = particles[i];
  64. if (particle.alive)
  65. particle.move();
  66. else
  67. pool.push(particles.splice(i, 1)[0]);
  68. }
  69. };
  70. clickparticle.draw = function() {
  71. clickparticle.globalCompositeOperation = 'lighter';
  72. for ( var i = particles.length - 1; i >= 0; i--) {
  73. particles[i].draw(clickparticle);
  74. }
  75. };
  76. //按下时显示效果,mousedown 换成 click 为点击时显示效果
  77. document.addEventListener("mousedown", function(e) {
  78. var max, j;
  79. //排除一些元素
  80. "TEXTAREA" !== e.target.nodeName && "INPUT" !== e.target.nodeName && "A" !== e.target.nodeName && "I" !== e.target.nodeName && "IMG" !== e.target.nodeName
  81. && function() {
  82. for (max = random(15, 20), j = 0; j < max; j++)
  83. clickparticle.spawn(e.clientX, e.clientY);
  84. }();
  85. });
  86. }
  87. </script>

结束语

本文简单分享了一下基于 sketch.js 实现的鼠标点击特效的方法,如果大家喜欢的话可以添加到自己的网站上。引入额外的 JS 代码会影响网页的加载速度,大家可以有选择性地使用。另外,大家如果有更好的建议,欢迎在下方评论处分享。

本文已通过「原本」原创作品认证,转载请注明文章出处及链接。

WEB最后更新:2022-11-30
夏日阳光
  • 本文由 夏日阳光 发表于 2020年11月14日
  • 本文为夏日阳光原创文章,转载请务必保留本文链接:https://www.pieruo.com/159.html
    • 演员
      演员 2

      学习了!

    匿名

    发表评论

    匿名网友
    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
    确定

    拖动滑块以完成验证
    加载中...