六边形酷科技特效源码

简介: 基于canvas画布绘制多个六边形跟随鼠标,科技感的几何图形酷炫动画特效,

源码介绍

基于canvas画布绘制多个六边形跟随鼠标,科技感的几何图形酷炫动画特效,

单页html源码,可以做网站动态背景,喜欢的朋友可以拿去

效果截图

liubianxing.gif

完整源码

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>canvas六角型</title>
<style>
body {
    
  background: black;
  overflow: hidden;
  /* cursor: none; */
}
</style>

</head>
<body>

<canvas id='canvas-club'></canvas>

<script>
window.requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame;

var c = document.getElementById("canvas-club");
var w = c.width = window.innerWidth;
var h = c.height = window.innerHeight;
var ctx = c.getContext("2d");

var maxParticles = 30;
var particles = [];
var hue = 183;

mouse = {
    };
mouse.size = 200;
mouse.x = mouse.tx = w/2;
mouse.y = mouse.ty = h/2;

var clearColor = "rgba(0, 0, 0, .2)";

function random(min, max){
    
    return Math.random() * (max - min) + min
}

function distance(x1, y1, x2, y2){
    
    return Math.sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
}

function P(){
    }

P.prototype = {
    
    init: function(){
    
        this.size = this.origSize = random(10, 100);
        this.x = random(0, w);
        this.y = Math.random() > .5 ? -this.size : h + this.size;
        this.speed = this.origSpeed = random(.01, .03);
    },

    draw: function(){
    
        this.distanceFromMouse = distance(this.x, this.y, mouse.x, mouse.y);
        ctx.strokeStyle = "hsla("+hue+", 90%, 50%, 1)";
        ctx.shadowColor = "hsla("+hue+", 100%, 55%, 1)";
        ctx.shadowBlur = this.size * 2;
        ctx.beginPath();
        ctx.moveTo(this.x + this.size * Math.cos(0), this.y + this.size *  Math.sin(0));

        for(var i=0; i<6; i++){
    
            ctx.lineTo(this.x + this.size * Math.cos(i * 2 * Math.PI / 6), this.y + this.size * Math.sin(i * 2 * Math.PI / 6));
        }   

        ctx.closePath();
        ctx.lineWidth = 3;
        ctx.stroke();
        this.update();
    },

    update: function(){
    
        if(this.distanceFromMouse > 20){
    
            this.x += (mouse.x - this.x) * this.speed;
            this.y += (mouse.y - this.y) * this.speed;
            if(this.distanceFromMouse < mouse.size){
    
                this.size += (0 - this.size) * this.speed;
                this.speed += .01;
            } else {
    
                this.size += (this.origSize - this.size) * this.speed;
            }
        } else {
    
                this.init();
        }
    }
}


mouse.move = function(){
    
    if(!distance(mouse.x, mouse.y, mouse.tx, mouse.ty) <= .1){
    
      mouse.x += (mouse.tx - mouse.x) * .2;
        mouse.y += (mouse.ty - mouse.y) * .2;
    } 
};

mouse.touches = function(e) {
    
    var touches = e.touches;
    if(touches){
    
        mouse.tx = touches[0].clientX;
        mouse.ty = touches[0].clientY;
    } else {
    
        mouse.tx = e.clientX;
      mouse.ty = e.clientY;
    }
    e.preventDefault();
};

mouse.mouseleave = function(e){
    
    mouse.tx = w/2;
    mouse.ty = h/2;
};

window.addEventListener("mousemove", mouse.touches);
window.addEventListener("touchstart", mouse.touches);
window.addEventListener("touchmove", mouse.touches)

c.addEventListener("mouseleave", mouse.mouseleave)

window.addEventListener("resize", function(){
    
    w = c.width = window.innerWidth;
    h = c.height = window.innerHeight;
});

for(var i=1; i<=maxParticles; i++) {
    
    setTimeout(function(){
    
        var p = new P();
        p.init();
        particles.push(p);
    }, i * 50);
}



function anim(){
    
    ctx.fillStyle = clearColor;
    ctx.shadowColor = clearColor;
    ctx.shadowBlur = 0;
    ctx.globalCompositeOperation = "source-over";
    ctx.fillRect(0,0,w, h);
    mouse.move();

    for(var i in particles){
    
        var p = particles[i];
        p.draw();
    }
    hue++;
    requestAnimationFrame(anim);
}

anim();

</script>
<script>
var _hmt = _hmt || [];
(function() {
    
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?c8249987b56687e1763167bfea967a61";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>
<!--QQ沐编程 www.qqmu.com 学习群:290987565 -->
</body>
</html>
相关文章
|
1月前
|
JavaScript
【UI】 欢快畅游的小鱼特效
【UI】 欢快畅游的小鱼特效
17 1
|
2月前
网页星光闪耀背景动画特效
网页星光闪耀背景动画特效
14 0
网页星光闪耀背景动画特效
|
5月前
|
前端开发
前端必学——实现电商图片放大镜效果(附代码)
放大镜可以说是前端人必须学会的程序之一,今天的案例为大家展示一下怎么实现放大镜的效果! 效果图展示   整个效果就是当鼠标放到展示图上的时候,会出现一个遮罩层以及弹出来一个框展示一个详情图,并且鼠标移动的时候详情图跟着移动,鼠标离开详情图消失。
|
6月前
|
程序员
技术文章精美配图模板网站推荐-创客贴
技术文章精美配图模板网站推荐-创客贴
29 0
|
6月前
|
安全 iOS开发
让人惊艳的高级配色,让你的色彩灵感爆棚!
让人惊艳的高级配色,让你的色彩灵感爆棚!
47 0
|
10月前
|
前端开发 JavaScript
前端代码分享——星空背景特效(内含源码)
前端代码分享——星空背景特效(内含源码)
当个快乐的WebGL炫酷背景搬运工!
看到shadertoy里面的那么多炫酷的背景是不是很心动?那我们就来把人家的shader搬过来用一下吧!
当个快乐的WebGL炫酷背景搬运工!
创意代码之文字特效合集
因为写的几个创意代码的特效都十分的简单,就合并到这篇文章里了,文章会从实现思路和遇到的问题两方面讲解实现如何现实这些简单的特效。
295 0
创意代码之文字特效合集
|
C#
图像滤镜艺术---流行艺术风滤镜特效PS实现
原文:图像滤镜艺术---流行艺术风滤镜特效PS实现 今天,本人给大家介绍一款新滤镜:流行艺术风效果,先看下效果吧! 原图 流行艺术风效果图 上面的这款滤镜效果是不是很赞,呵呵,按照本人以往的逻辑,我会先介绍PS实现过...
1259 0