搜集H5 页面特效(canvas实现)

新增蜘蛛网效果:

效果图

提供源代码:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            body {
                background: #99CCFF;
                margin: 0;
                padding: 0;
            }
        </style>
    </head>

    <body>
        <div style="float:right;" id="hub_iframe"></div>
        <script>
            ! function() {
                //封装方法,压缩之后减少文件大小
                function get_attribute(node, attr, default_value) {
                    return node.getAttribute(attr) || default_value;
                }
                //封装方法,压缩之后减少文件大小
                function get_by_tagname(name) {
                    return document.getElementsByTagName(name);
                }
                //获取配置参数
                function get_config_option() {
                    var scripts = get_by_tagname("script"),
                        script_len = scripts.length,
                        script = scripts[script_len - 1]; //当前加载的script
                    return {
                        l: script_len, //长度,用于生成id用
                        z: get_attribute(script, "zIndex", -1), //z-index
                        o: get_attribute(script, "opacity", 0.5), //opacity
                        c: get_attribute(script, "color", "255,0,204"), //color
                        n: get_attribute(script, "count", 99) //count
                    };
                }
                //设置canvas的高宽
                function set_canvas_size() {
                    canvas_width = the_canvas.width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
                        canvas_height = the_canvas.height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
                }

                //绘制过程
                function draw_canvas() {
                    context.clearRect(0, 0, canvas_width, canvas_height);
                    //随机的线条和当前位置联合数组
                    var e, i, d, x_dist, y_dist, dist; //临时节点
                    //遍历处理每一个点
                    random_lines.forEach(function(r, idx) {
                        r.x += r.xa,
                            r.y += r.ya, //移动
                            r.xa *= r.x > canvas_width || r.x < 0 ? -1 : 1,
                            r.ya *= r.y > canvas_height || r.y < 0 ? -1 : 1, //碰到边界,反向反弹
                            context.fillRect(r.x - 0.5, r.y - 0.5, 1, 1); //绘制一个宽高为1的点
                        //从下一个点开始
                        for(i = idx + 1; i < all_array.length; i++) {
                            e = all_array[i];
                            //不是当前点
                            if(null !== e.x && null !== e.y) {
                                x_dist = r.x - e.x, //x轴距离 l
                                    y_dist = r.y - e.y, //y轴距离 n
                                    dist = x_dist * x_dist + y_dist * y_dist; //总距离, m
                                dist < e.max && (e === current_point && dist >= e.max / 2 && (r.x -= 0.03 * x_dist, r.y -= 0.03 * y_dist), //靠近的时候加速
                                    d = (e.max - dist) / e.max,
                                    context.beginPath(),
                                    context.lineWidth = d / 2,
                                    context.strokeStyle = "rgba(" + config.c + "," + (d + 0.2) + ")",
                                    context.moveTo(r.x, r.y),
                                    context.lineTo(e.x, e.y),
                                    context.stroke());
                            }
                        }
                    }), frame_func(draw_canvas);
                }
                //创建画布,并添加到body中
                var the_canvas = document.createElement("canvas"), //画布
                    config = get_config_option(), //配置
                    canvas_id = "c_n" + config.l, //canvas id
                    context = the_canvas.getContext("2d"),
                    canvas_width, canvas_height,
                    frame_func = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(func) {
                        window.setTimeout(func, 1000 / 45);
                    },
                    random = Math.random,
                    current_point = {
                        x: null, //当前鼠标x
                        y: null, //当前鼠标y
                        max: 20000
                    },
                    all_array;
                the_canvas.id = canvas_id;
                the_canvas.style.cssText = "position:fixed;top:0;left:0;z-index:" + config.z + ";opacity:" + config.o;
                get_by_tagname("body")[0].appendChild(the_canvas);
                //初始化画布大小

                set_canvas_size(), window.onresize = set_canvas_size;
                //当时鼠标位置存储,离开的时候,释放当前位置信息
                window.onmousemove = function(e) {
                    e = e || window.event, current_point.x = e.clientX, current_point.y = e.clientY;
                }, window.onmouseout = function() {
                    current_point.x = null, current_point.y = null;
                };
                //随机生成config.n条线位置信息
                for(var random_lines = [], i = 0; config.n > i; i++) {
                    var x = random() * canvas_width, //随机位置
                        y = random() * canvas_height,
                        xa = 2 * random() - 1, //随机运动方向
                        ya = 2 * random() - 1;
                    random_lines.push({
                        x: x,
                        y: y,
                        xa: xa,
                        ya: ya,
                        max: 6000 //沾附距离
                    });
                }
                all_array = random_lines.concat([current_point]);
                //0.1秒后绘制
                setTimeout(function() {
                    draw_canvas();
                }, 100);
            }();
        </script>
    </body>

</html>

来源于: http://www.jq22.com/webqd212

1. 3D特效一,效果如下:

3D特效一

附:代码一

<canvas id="canvas" width="1423" height="404"></canvas>
        <script>
            function project3D(x, y, z, vars) {
                var p, d;
                x -= vars.camX;
                y -= vars.camY - 8;
                z -= vars.camZ;
                p = Math.atan2(x, z);
                d = Math.sqrt(x * x + z * z);
                x = Math.sin(p - vars.yaw) * d;
                z = Math.cos(p - vars.yaw) * d;
                p = Math.atan2(y, z);
                d = Math.sqrt(y * y + z * z);
                y = Math.sin(p - vars.pitch) * d;
                z = Math.cos(p - vars.pitch) * d;
                var rx1 = -1000;
                var ry1 = 1;
                var rx2 = 1000;
                var ry2 = 1;
                var rx3 = 0;
                var ry3 = 0;
                var rx4 = x;
                var ry4 = z;
                var uc = (ry4 - ry3) * (rx2 - rx1) - (rx4 - rx3) * (ry2 - ry1);
                var ua = ((rx4 - rx3) * (ry1 - ry3) - (ry4 - ry3) * (rx1 - rx3)) / uc;
                var ub = ((rx2 - rx1) * (ry1 - ry3) - (ry2 - ry1) * (rx1 - rx3)) / uc;
                if(!z) z = 0.000000001;
                if(ua > 0 && ua < 1 && ub > 0 && ub < 1) {
                    return {
                        x: vars.cx + (rx1 + ua * (rx2 - rx1)) * vars.scale,
                        y: vars.cy + y / z * vars.scale,
                        d: (x * x + y * y + z * z)
                    }
                } else {
                    return {
                        d: -1
                    }
                }
            }

            function elevation(x, y, z) {
                var dist = Math.sqrt(x * x + y * y + z * z);
                if(dist && z / dist >= -1 && z / dist <= 1) return Math.acos(z / dist);
                return 0.00000001
            }

            function rgb(col) {
                col += 0.000001;
                var r = parseInt((0.5 + Math.sin(col) * 0.5) * 16);
                var g = parseInt((0.5 + Math.cos(col) * 0.5) * 16);
                var b = parseInt((0.5 - Math.sin(col) * 0.5) * 16);
                return "#" + r.toString(16) + g.toString(16) + b.toString(16)
            }

            function interpolateColors(RGB1, RGB2, degree) {
                var w2 = degree;
                var w1 = 1 - w2;
                return [w1 * RGB1[0] + w2 * RGB2[0], w1 * RGB1[1] + w2 * RGB2[1], w1 * RGB1[2] + w2 * RGB2[2]]
            }

            function rgbArray(col) {
                col += 0.000001;
                var r = parseInt((0.5 + Math.sin(col) * 0.5) * 256);
                var g = parseInt((0.5 + Math.cos(col) * 0.5) * 256);
                var b = parseInt((0.5 - Math.sin(col) * 0.5) * 256);
                return [r, g, b]
            }

            function colorString(arr) {
                var r = parseInt(arr[0]);
                var g = parseInt(arr[1]);
                var b = parseInt(arr[2]);
                return "#" + ("0" + r.toString(16)).slice(-2) + ("0" + g.toString(16)).slice(-2) + ("0" + b.toString(16)).slice(-2)
            }

            function process(vars) {
                if(vars.points.length < vars.initParticles)
                    for(var i = 0; i < 5; ++i) spawnParticle(vars);
                var p, d, t;
                p = Math.atan2(vars.camX, vars.camZ);
                d = Math.sqrt(vars.camX * vars.camX + vars.camZ * vars.camZ);
                d -= Math.sin(vars.frameNo / 80) / 25;
                t = Math.cos(vars.frameNo / 300) / 165;
                vars.camX = Math.sin(p + t) * d;
                vars.camZ = Math.cos(p + t) * d;
                vars.camY = -Math.sin(vars.frameNo / 220) * 15;
                vars.yaw = Math.PI + p + t;
                vars.pitch = elevation(vars.camX, vars.camZ, vars.camY) - Math.PI / 2;
                var t;
                for(var i = 0; i < vars.points.length; ++i) {
                    x = vars.points[i].x;
                    y = vars.points[i].y;
                    z = vars.points[i].z;
                    d = Math.sqrt(x * x + z * z) / 1.0075;
                    t = .1 / (1 + d * d / 5);
                    p = Math.atan2(x, z) + t;
                    vars.points[i].x = Math.sin(p) * d;
                    vars.points[i].z = Math.cos(p) * d;
                    vars.points[i].y += vars.points[i].vy * t * ((Math.sqrt(vars.distributionRadius) - d) * 2);
                    if(vars.points[i].y > vars.vortexHeight / 2 || d < .25) {
                        vars.points.splice(i, 1);
                        spawnParticle(vars)
                    }
                }
            }

            function drawFloor(vars) {
                var x, y, z, d, point, a;
                for(var i = -25; i <= 25; i += 1) {
                    for(var j = -25; j <= 25; j += 1) {
                        x = i * 2;
                        z = j * 2;
                        y = vars.floor;
                        d = Math.sqrt(x * x + z * z);
                        point = project3D(x, y - d * d / 85, z, vars);
                        if(point.d != -1) {
                            size = 1 + 15000 / (1 + point.d);
                            a = 0.15 - Math.pow(d / 50, 4) * 0.15;
                            if(a > 0) {
                                vars.ctx.fillStyle = colorString(interpolateColors(rgbArray(d / 26 - vars.frameNo / 40), [0, 128, 32], .5 + Math.sin(d / 6 - vars.frameNo / 8) / 2));
                                vars.ctx.globalAlpha = a;
                                vars.ctx.fillRect(point.x - size / 2, point.y - size / 2, size, size)
                            }
                        }
                    }
                }
                vars.ctx.fillStyle = "#82f";
                for(var i = -25; i <= 25; i += 1) {
                    for(var j = -25; j <= 25; j += 1) {
                        x = i * 2;
                        z = j * 2;
                        y = -vars.floor;
                        d = Math.sqrt(x * x + z * z);
                        point = project3D(x, y + d * d / 85, z, vars);
                        if(point.d != -1) {
                            size = 1 + 15000 / (1 + point.d);
                            a = 0.15 - Math.pow(d / 50, 4) * 0.15;
                            if(a > 0) {
                                vars.ctx.fillStyle = colorString(interpolateColors(rgbArray(-d / 26 - vars.frameNo / 40), [32, 0, 128], .5 + Math.sin(-d / 6 - vars.frameNo / 8) / 2));
                                vars.ctx.globalAlpha = a;
                                vars.ctx.fillRect(point.x - size / 2, point.y - size / 2, size, size)
                            }
                        }
                    }
                }
            }

            function sortFunction(a, b) {
                return b.dist - a.dist
            }

            function draw(vars) {
                vars.ctx.globalAlpha = .15;
                vars.ctx.fillStyle = "#000";
                vars.ctx.fillRect(0, 0, canvas.width, canvas.height);
                drawFloor(vars);
                var point, x, y, z, a;
                for(var i = 0; i < vars.points.length; ++i) {
                    x = vars.points[i].x;
                    y = vars.points[i].y;
                    z = vars.points[i].z;
                    point = project3D(x, y, z, vars);
                    if(point.d != -1) {
                        vars.points[i].dist = point.d;
                        size = 1 + vars.points[i].radius / (1 + point.d);
                        d = Math.abs(vars.points[i].y);
                        a = .8 - Math.pow(d / (vars.vortexHeight / 2), 1000) * .8;
                        vars.ctx.globalAlpha = a >= 0 && a <= 1 ? a : 0;
                        vars.ctx.fillStyle = rgb(vars.points[i].color);
                        if(point.x > -1 && point.x < vars.canvas.width && point.y > -1 && point.y < vars.canvas.height) vars.ctx.fillRect(point.x - size / 2, point.y - size / 2, size, size)
                    }
                }
                vars.points.sort(sortFunction)
            }

            function spawnParticle(vars) {
                var p, ls;
                pt = {};
                p = Math.PI * 2 * Math.random();
                ls = Math.sqrt(Math.random() * vars.distributionRadius);
                pt.x = Math.sin(p) * ls;
                pt.y = -vars.vortexHeight / 2;
                pt.vy = vars.initV / 20 + Math.random() * vars.initV;
                pt.z = Math.cos(p) * ls;
                pt.radius = 200 + 800 * Math.random();
                pt.color = pt.radius / 1000 + vars.frameNo / 250;
                vars.points.push(pt)
            }

            function frame(vars) {
                if(vars === undefined) {
                    var vars = {};
                    vars.canvas = document.querySelector("canvas");
                    vars.ctx = vars.canvas.getContext("2d");
                    vars.canvas.width = document.body.clientWidth;
                    vars.canvas.height = document.body.clientHeight;
                    window.addEventListener("resize", function() {
                        vars.canvas.width = document.body.clientWidth;
                        vars.canvas.height = document.body.clientHeight;
                        vars.cx = vars.canvas.width / 2;
                        vars.cy = vars.canvas.height / 2
                    }, true);
                    vars.frameNo = 0;
                    vars.camX = 0;
                    vars.camY = 0;
                    vars.camZ = -14;
                    vars.pitch = elevation(vars.camX, vars.camZ, vars.camY) - Math.PI / 2;
                    vars.yaw = 0;
                    vars.cx = vars.canvas.width / 2;
                    vars.cy = vars.canvas.height / 2;
                    vars.bounding = 10;
                    vars.scale = 500;
                    vars.floor = 26.5;
                    vars.points = [];
                    vars.initParticles = 1000;
                    vars.initV = .01;
                    vars.distributionRadius = 800;
                    vars.vortexHeight = 25
                }
                vars.frameNo++;
                requestAnimationFrame(function() {
                    frame(vars)
                });
                process(vars);
                draw(vars)
            }
            frame();
        </script>

2. 星空特效,效果如下:

星空特效

附:代码如下

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="jquery.min.js"></script>
        <style>
            body {
                background: #000;
                margin: 0;
                padding: 0;
            }
        </style>
    </head>

    <body>
        <canvas id="canvas" width="1423" height="404"></canvas>
        <script>
            $(function() {
                var canvas = document.querySelector('canvas'),
                    ctx = canvas.getContext('2d')
                canvas.width = window.innerWidth;
                canvas.height = window.innerHeight;
                ctx.lineWidth = .3;
                ctx.strokeStyle = (new Color(150)).style;

                var mousePosition = {
                    x: 30 * canvas.width / 100,
                    y: 30 * canvas.height / 100
                };

                var dots = {
                    nb: 150,
                    distance: 50,
                    d_radius: 100,
                    array: []
                };

                function colorValue(min) {
                    return Math.floor(Math.random() * 255 + min);
                }

                function createColorStyle(r, g, b) {
                    return 'rgba(' + r + ',' + g + ',' + b + ', 0.8)';
                }

                function mixComponents(comp1, weight1, comp2, weight2) {
                    return(comp1 * weight1 + comp2 * weight2) / (weight1 + weight2);
                }

                function averageColorStyles(dot1, dot2) {
                    var color1 = dot1.color,
                        color2 = dot2.color;

                    var r = mixComponents(color1.r, dot1.radius, color2.r, dot2.radius),
                        g = mixComponents(color1.g, dot1.radius, color2.g, dot2.radius),
                        b = mixComponents(color1.b, dot1.radius, color2.b, dot2.radius);
                    return createColorStyle(Math.floor(r), Math.floor(g), Math.floor(b));
                }

                function Color(min) {
                    min = min || 0;
                    this.r = colorValue(min);
                    this.g = colorValue(min);
                    this.b = colorValue(min);
                    this.style = createColorStyle(this.r, this.g, this.b);
                }

                function Dot() {
                    this.x = Math.random() * canvas.width;
                    this.y = Math.random() * canvas.height;

                    this.vx = -.5 + Math.random();
                    this.vy = -.5 + Math.random();

                    this.radius = Math.random() * 2;

                    this.color = new Color();
                    console.log(this);
                }

                Dot.prototype = {
                    draw: function() {
                        ctx.beginPath();
                        ctx.fillStyle = this.color.style;
                        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
                        ctx.fill();
                    }
                };

                function createDots() {
                    for(i = 0; i < dots.nb; i++) {
                        dots.array.push(new Dot());
                    }
                }

                function moveDots() {
                    for(i = 0; i < dots.nb; i++) {

                        var dot = dots.array[i];

                        if(dot.y < 0 || dot.y > canvas.height) {
                            dot.vx = dot.vx;
                            dot.vy = -dot.vy;
                        } else if(dot.x < 0 || dot.x > canvas.width) {
                            dot.vx = -dot.vx;
                            dot.vy = dot.vy;
                        }
                        dot.x += dot.vx;
                        dot.y += dot.vy;
                    }
                }

                function connectDots() {
                    for(i = 0; i < dots.nb; i++) {
                        for(j = 0; j < dots.nb; j++) {
                            i_dot = dots.array[i];
                            j_dot = dots.array[j];

                            if((i_dot.x - j_dot.x) < dots.distance && (i_dot.y - j_dot.y) < dots.distance && (i_dot.x - j_dot.x) > -dots.distance && (i_dot.y - j_dot.y) > -dots.distance) {
                                if((i_dot.x - mousePosition.x) < dots.d_radius && (i_dot.y - mousePosition.y) < dots.d_radius && (i_dot.x - mousePosition.x) > -dots.d_radius && (i_dot.y - mousePosition.y) > -dots.d_radius) {
                                    ctx.beginPath();
                                    ctx.strokeStyle = averageColorStyles(i_dot, j_dot);
                                    ctx.moveTo(i_dot.x, i_dot.y);
                                    ctx.lineTo(j_dot.x, j_dot.y);
                                    ctx.stroke();
                                    ctx.closePath();
                                }
                            }
                        }
                    }
                }

                function drawDots() {
                    for(i = 0; i < dots.nb; i++) {
                        var dot = dots.array[i];
                        dot.draw();
                    }
                }

                function animateDots() {
                    ctx.clearRect(0, 0, canvas.width, canvas.height);
                    moveDots();
                    connectDots();
                    drawDots();

                    requestAnimationFrame(animateDots);
                }

                $('canvas').on('mousemove', function(e) {
                    mousePosition.x = e.pageX;
                    mousePosition.y = e.pageY;
                });

                $('canvas').on('mouseleave', function(e) {
                    mousePosition.x = canvas.width / 2;
                    mousePosition.y = canvas.height / 2;
                });

                createDots();
                requestAnimationFrame(animateDots);
            });
        </script>
    </body>

</html>

3.星空特效二,效果如下:

星空特效二

附:代码如下

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8">
        <title>星空特效2</title>
        <style>
            html {
                height: 100%;
                background-image: -webkit-radial-gradient(ellipse farthest-corner at center top, #000d4d 0%, #000105 100%);
                background-image: radial-gradient(ellipse farthest-corner at center top, #000d4d 0%, #000105 100%);
                cursor: move;
            }
            
            body {
                width: 100%;
                margin: 0;
                overflow: hidden;
            }
            
            .inner>.add {
                display: none;
                position: absolute;
                top: -1000000px;
                visibility: hidden
            }
            
            .fdad,
            .adsbygoogle {
                display: none;
                position: absolute;
                top: -1000000px;
                visibility: hidden
            }
        </style>
 
    </head>

    <body>

        <canvas id="canv" width="1423" height="404"></canvas>
        <script>
            var num = 200;
            var w = window.innerWidth;
            var h = window.innerHeight;
            var max = 100;
            var _x = 0;
            var _y = 0;
            var _z = 150;
            var dtr = function(d) {
                return d * Math.PI / 180;
            };

            var rnd = function() {
                return Math.sin(Math.floor(Math.random() * 360) * Math.PI / 180);
            };
            var dist = function(p1, p2, p3) {
                return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2) + Math.pow(p2.z - p1.z, 2));
            };

            var cam = {
                obj: {
                    x: _x,
                    y: _y,
                    z: _z
                },
                dest: {
                    x: 0,
                    y: 0,
                    z: 1
                },
                dist: {
                    x: 0,
                    y: 0,
                    z: 200
                },
                ang: {
                    cplane: 0,
                    splane: 0,
                    ctheta: 0,
                    stheta: 0
                },
                zoom: 1,
                disp: {
                    x: w / 2,
                    y: h / 2,
                    z: 0
                },
                upd: function() {
                    cam.dist.x = cam.dest.x - cam.obj.x;
                    cam.dist.y = cam.dest.y - cam.obj.y;
                    cam.dist.z = cam.dest.z - cam.obj.z;
                    cam.ang.cplane = -cam.dist.z / Math.sqrt(cam.dist.x * cam.dist.x + cam.dist.z * cam.dist.z);
                    cam.ang.splane = cam.dist.x / Math.sqrt(cam.dist.x * cam.dist.x + cam.dist.z * cam.dist.z);
                    cam.ang.ctheta = Math.sqrt(cam.dist.x * cam.dist.x + cam.dist.z * cam.dist.z) / Math.sqrt(cam.dist.x * cam.dist.x + cam.dist.y * cam.dist.y + cam.dist.z * cam.dist.z);
                    cam.ang.stheta = -cam.dist.y / Math.sqrt(cam.dist.x * cam.dist.x + cam.dist.y * cam.dist.y + cam.dist.z * cam.dist.z);
                }
            };

            var trans = {
                parts: {
                    sz: function(p, sz) {
                        return {
                            x: p.x * sz.x,
                            y: p.y * sz.y,
                            z: p.z * sz.z
                        };
                    },
                    rot: {
                        x: function(p, rot) {
                            return {
                                x: p.x,
                                y: p.y * Math.cos(dtr(rot.x)) - p.z * Math.sin(dtr(rot.x)),
                                z: p.y * Math.sin(dtr(rot.x)) + p.z * Math.cos(dtr(rot.x))
                            };
                        },
                        y: function(p, rot) {
                            return {
                                x: p.x * Math.cos(dtr(rot.y)) + p.z * Math.sin(dtr(rot.y)),
                                y: p.y,
                                z: -p.x * Math.sin(dtr(rot.y)) + p.z * Math.cos(dtr(rot.y))
                            };
                        },
                        z: function(p, rot) {
                            return {
                                x: p.x * Math.cos(dtr(rot.z)) - p.y * Math.sin(dtr(rot.z)),
                                y: p.x * Math.sin(dtr(rot.z)) + p.y * Math.cos(dtr(rot.z)),
                                z: p.z
                            };
                        }
                    },
                    pos: function(p, pos) {
                        return {
                            x: p.x + pos.x,
                            y: p.y + pos.y,
                            z: p.z + pos.z
                        };
                    }
                },
                pov: {
                    plane: function(p) {
                        return {
                            x: p.x * cam.ang.cplane + p.z * cam.ang.splane,
                            y: p.y,
                            z: p.x * -cam.ang.splane + p.z * cam.ang.cplane
                        };
                    },
                    theta: function(p) {
                        return {
                            x: p.x,
                            y: p.y * cam.ang.ctheta - p.z * cam.ang.stheta,
                            z: p.y * cam.ang.stheta + p.z * cam.ang.ctheta
                        };
                    },
                    set: function(p) {
                        return {
                            x: p.x - cam.obj.x,
                            y: p.y - cam.obj.y,
                            z: p.z - cam.obj.z
                        };
                    }
                },
                persp: function(p) {
                    return {
                        x: p.x * cam.dist.z / p.z * cam.zoom,
                        y: p.y * cam.dist.z / p.z * cam.zoom,
                        z: p.z * cam.zoom,
                        p: cam.dist.z / p.z
                    };
                },
                disp: function(p, disp) {
                    return {
                        x: p.x + disp.x,
                        y: -p.y + disp.y,
                        z: p.z + disp.z,
                        p: p.p
                    };
                },
                steps: function(_obj_, sz, rot, pos, disp) {
                    var _args = trans.parts.sz(_obj_, sz);
                    _args = trans.parts.rot.x(_args, rot);
                    _args = trans.parts.rot.y(_args, rot);
                    _args = trans.parts.rot.z(_args, rot);
                    _args = trans.parts.pos(_args, pos);
                    _args = trans.pov.plane(_args);
                    _args = trans.pov.theta(_args);
                    _args = trans.pov.set(_args);
                    _args = trans.persp(_args);
                    _args = trans.disp(_args, disp);
                    return _args;
                }
            };

            (function() {
                "use strict";
                var threeD = function(param) {
                    this.transIn = {};
                    this.transOut = {};
                    this.transIn.vtx = (param.vtx);
                    this.transIn.sz = (param.sz);
                    this.transIn.rot = (param.rot);
                    this.transIn.pos = (param.pos);
                };

                threeD.prototype.vupd = function() {
                    this.transOut = trans.steps(

                        this.transIn.vtx,
                        this.transIn.sz,
                        this.transIn.rot,
                        this.transIn.pos,
                        cam.disp
                    );
                };

                var Build = function() {
                    this.vel = 0.04;
                    this.lim = 360;
                    this.diff = 200;
                    this.initPos = 100;
                    this.toX = _x;
                    this.toY = _y;
                    this.go();
                };

                Build.prototype.go = function() {
                    this.canvas = document.getElementById("canv");
                    this.canvas.width = window.innerWidth;
                    this.canvas.height = window.innerHeight;
                    this.$ = canv.getContext("2d");
                    this.$.globalCompositeOperation = 'source-over';
                    this.varr = [];
                    this.dist = [];
                    this.calc = [];

                    for(var i = 0, len = num; i < len; i++) {
                        this.add();
                    }

                    this.rotObj = {
                        x: 0,
                        y: 0,
                        z: 0
                    };
                    this.objSz = {
                        x: w / 5,
                        y: h / 5,
                        z: w / 5
                    };
                };

                Build.prototype.add = function() {
                    this.varr.push(new threeD({
                        vtx: {
                            x: rnd(),
                            y: rnd(),
                            z: rnd()
                        },
                        sz: {
                            x: 0,
                            y: 0,
                            z: 0
                        },
                        rot: {
                            x: 20,
                            y: -20,
                            z: 0
                        },
                        pos: {
                            x: this.diff * Math.sin(360 * Math.random() * Math.PI / 180),
                            y: this.diff * Math.sin(360 * Math.random() * Math.PI / 180),
                            z: this.diff * Math.sin(360 * Math.random() * Math.PI / 180)
                        }
                    }));
                    this.calc.push({
                        x: 360 * Math.random(),
                        y: 360 * Math.random(),
                        z: 360 * Math.random()
                    });
                };

                Build.prototype.upd = function() {
                    cam.obj.x += (this.toX - cam.obj.x) * 0.05;
                    cam.obj.y += (this.toY - cam.obj.y) * 0.05;
                };

                Build.prototype.draw = function() {
                    this.$.clearRect(0, 0, this.canvas.width, this.canvas.height);
                    cam.upd();
                    this.rotObj.x += 0.1;
                    this.rotObj.y += 0.1;
                    this.rotObj.z += 0.1;

                    for(var i = 0; i < this.varr.length; i++) {
                        for(var val in this.calc[i]) {
                            if(this.calc[i].hasOwnProperty(val)) {
                                this.calc[i][val] += this.vel;
                                if(this.calc[i][val] > this.lim) this.calc[i][val] = 0;
                            }
                        }

                        this.varr[i].transIn.pos = {
                            x: this.diff * Math.cos(this.calc[i].x * Math.PI / 180),
                            y: this.diff * Math.sin(this.calc[i].y * Math.PI / 180),
                            z: this.diff * Math.sin(this.calc[i].z * Math.PI / 180)
                        };
                        this.varr[i].transIn.rot = this.rotObj;
                        this.varr[i].transIn.sz = this.objSz;
                        this.varr[i].vupd();
                        if(this.varr[i].transOut.p < 0) continue;
                        var g = this.$.createRadialGradient(this.varr[i].transOut.x, this.varr[i].transOut.y, this.varr[i].transOut.p, this.varr[i].transOut.x, this.varr[i].transOut.y, this.varr[i].transOut.p * 2);
                        this.$.globalCompositeOperation = 'lighter';
                        g.addColorStop(0, 'hsla(255, 255%, 255%, 1)');
                        g.addColorStop(.5, 'hsla(' + (i + 2) + ',85%, 40%,1)');
                        g.addColorStop(1, 'hsla(' + (i) + ',85%, 40%,.5)');
                        this.$.fillStyle = g;
                        this.$.beginPath();
                        this.$.arc(this.varr[i].transOut.x, this.varr[i].transOut.y, this.varr[i].transOut.p * 2, 0, Math.PI * 2, false);
                        this.$.fill();
                        this.$.closePath();
                    }
                };
                Build.prototype.anim = function() {
                    window.requestAnimationFrame = (function() {
                        return window.requestAnimationFrame ||
                            function(callback, element) {
                                window.setTimeout(callback, 1000 / 60);
                            };
                    })();
                    var anim = function() {
                        this.upd();
                        this.draw();
                        window.requestAnimationFrame(anim);
                    }.bind(this);
                    window.requestAnimationFrame(anim);
                };

                Build.prototype.run = function() {
                    this.anim();

                    window.addEventListener('mousemove', function(e) {
                        this.toX = (e.clientX - this.canvas.width / 2) * -0.8;
                        this.toY = (e.clientY - this.canvas.height / 2) * 0.8;
                    }.bind(this));
                    window.addEventListener('touchmove', function(e) {
                        e.preventDefault();
                        this.toX = (e.touches[0].clientX - this.canvas.width / 2) * -0.8;
                        this.toY = (e.touches[0].clientY - this.canvas.height / 2) * 0.8;
                    }.bind(this));
                    window.addEventListener('mousedown', function(e) {
                        for(var i = 0; i < 100; i++) {
                            this.add();
                        }
                    }.bind(this));
                    window.addEventListener('touchstart', function(e) {
                        e.preventDefault();
                        for(var i = 0; i < 100; i++) {
                            this.add();
                        }
                    }.bind(this));
                };
                var app = new Build();
                app.run();
            })();
            window.addEventListener('resize', function() {
                canvas.width = w = window.innerWidth;
                canvas.height = h = window.innerHeight;
            }, false);
        </script>

    </body>

</html>

3. 黑夜绽放的花朵,效果如下:

黑夜绽放的花朵

附:代码如下

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8">
        <title>黑夜绽放花朵</title>
        <style>
            html {
                background: black;
            }
            
            body {
                width: 100%;
                height: 100%;
                position: absolute;
                left: 0;
                top: 0;
                overflow: hidden;
                margin: 0;
                background: black;
                -webkit-filter: invert(0);
                filter: invert(0);
            }
            
            #canvas {
                position: absolute;
                z-index: -1;
                -webkit-filter: hue-rotate(100deg) brightness(1);
                filter: hue-rotate(100deg) brightness(1);
                mix-blend-mode: difference;
            }
            
            #canv {
                position: absolute;
                z-index: -2;
                mix-blend-mode: lighter;
            }
            
            canvas {
                position: absolute;
                top: 50%;
                left: 50%;
                z-index: -1;
                -webkit-transform: translate(-50%, -50%);
                transform: translate(-50%, -50%);
            }
            
            .inner>.add {
                display: none;
                position: absolute;
                top: -1000000px;
                visibility: hidden
            }
            
            .fdad,
            .adsbygoogle {
                display: none;
                position: absolute;
                top: -1000000px;
                visibility: hidden
            }
        </style>
 
    </head>

    <body>
        <canvas id="canvas" height="400" width="400"></canvas>
        <canvas id="canv" width="1423" height="404"></canvas>
        <script>
            var c = document.getElementById('canv'),
                $ = c.getContext('2d'),
                w = c.width = window.innerWidth,
                h = c.height = window.innerHeight,
                t = 0,
                num = 950,
                s, a, b, u = 0,
                x, y, _x, _y,
                _t = 1 / 16;

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

            var anim = function() {
                $.globalCompositeOperation = 'multiply';
                $.fillStyle = 'hsla(258,20%,50%,1)';
                $.fillRect(0, 0, w, h);
                $.globalCompositeOperation = 'lighter';
                for(var i = 0; i < .5; i++) {
                    x = 0;
                    _u = (u) + i * 2, col = u + (_u * 8);
                    $.beginPath();
                    for(var j = 0; j < num; j++) {
                        x -= .312 * Math.sin(15);
                        y = x * Math.sin(i + 3.05 * t + x / 7) / 12;
                        _x = x * Math.cos(b) + y * Math.sin(b);
                        _y = x * Math.sin(b) - y * Math.cos(b);
                        b = (j * 2.1102) * Math.PI / -.1008;
                        $.arc(w / 2 - _x, h / 2 - _y, random(.001, .6), 300, Math.PI * 2 + .1);
                        $.lineWidth = .2;
                    }
                    var g = $.createLinearGradient(w / 2 + _x, h / 2 + _y,
                        1, w / 2 + _x, h / 2 + _y);
                    g.addColorStop(0.2, 'hsla(' + col + ',90%,50%,.2)');
                    g.addColorStop(0.9, 'hsla(' + _u + ',95%,50%,.3)');
                    g.addColorStop(1, 'hsla(0,0%,100%,.4)');
                    $.strokeStyle = g;
                    $.stroke();
                }
                t += _t / 2;
                u -= .2;
                window.requestAnimationFrame(anim);
            };
            anim();

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

            var canvas, ctx, tim;
            canvas = document.getElementsByTagName('canvas')[0];
            ctx = canvas.getContext('2d');
            canvas.width = canvas.height = 400;
            baum();

            function baum() {
                var a, b, c, d, e, x, y, r;
                ctx.globalCompositeOperation = "source-over";
                ctx.fillStyle = "hsla(26,100%,0%,1)";
                ctx.fillRect(0, 0, canvas.width, canvas.height);
                ctx.globalCompositeOperation = "color-burn";
                tim = new Date().getTime() / Math.PI / 72;

                c = [];
                r = tim / 32;
                for(a = 0; a < 18; a++) {
                    b = 160;
                    if(a % 2 == 1) b = 100;
                    x = Math.cos(r) * b;
                    y = Math.sin(r) * b;
                    c.push([200 + x, 200 + y]);
                    r += Math.PI * 2 / 14;
                }

                for(a = 0; a < 7; a++) {
                    b = c[a * 2];
                    d = c[a * 2 + 1];
                    e = c[(a * 2 + 13) % 14];
                    tri([
                        [200, 200], b, d
                    ], 0);
                    tri([
                        [200, 200], b, e
                    ], 0);
                }
                requestAnimationFrame(baum);
            }

            function tri(p, ban) {
                var a, b, c, d, e, f, x, y, han, r1, r2;
                x = y = 0;
                for(a = 0; a < 3; a++) {
                    x += p[a][0];
                    y += p[a][1];
                }
                x = x / 3 - canvas.width / 2;
                y = y / 3 - canvas.height / 2;
                han = Math.pow(x * x + y * y, 0.5);
                c = 0.2 + Math.sin(tim / 13) * 0.15;
                r1 = 0.5 + Math.sin(han / 20 * (1 + Math.sin(tim / 19) * 0.7) + tim / 41) * c;
                r2 = 1 - r1;
                c = p[0][0] * (p[1][1] - p[2][1]);
                c += p[1][0] * (p[2][1] - p[0][1]);
                c += p[2][0] * (p[0][1] - p[1][1]);
                c = Math.abs(c);

                if(c < 100) {
                    if(ban % 17 == 1) {
                        a = ((han + tim * 3) % 360) | 0;
                        b = 0.4;
                        if(ban % 12 > 8) b = 1;

                        ctx.fillStyle = ctx.strokeStyle = "hsla(" + a * 2 + ",60%,40%,0.53)";
                        ctx.beginPath();
                        for(a = 0; a < p.length; a++) {
                            b = p[a];
                            ctx.globalCompositeOperation = "lighter";
                            ctx.lineTo(b[0], b[1]);
                        }
                        ctx.fill();
                        if(Math.random() < 0.2) return;
                    }
                    if(ban % 50 > 28) return;
                    if(c < 20) return;
                }

                d = 0;
                for(a = 0; a < p.length; a++) {
                    b = p[a];
                    c = p[(a + 1) % p.length];
                    x = b[0] - c[0];
                    y = b[1] - c[1];
                    e = Math.pow(x * x + y * y, 0.5);
                    if(e > d) {
                        d = e;
                        f = a;
                    }
                }

                a = p[f];
                b = p[(f + 1) % p.length];
                c = p[(f + 2) % p.length];
                x = a[0] * r1 + b[0] * r2;
                y = a[1] * r1 + b[1] * r2;
                tri([b, c, [x, y]], ban + 1);
                tri([c, a, [x, y]], ban + 2);
            }
        </script>

    </body>

</html>

4.花火效果,如下:

![花火]$8VP7YX7``5650H%M.png](http://upload-images.jianshu.io/upload_images/5328006-efb50059f6c8f112.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

附代码如下:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8">
        <title>花火</title>
        <style>
            body {
                background: #000;
                overflow: hidden;
            }
            
            canvas {
                display: block;
            }
            
            .inner>.add {
                display: none;
                position: absolute;
                top: -1000000px;
                visibility: hidden
            }
            
            .fdad,
            .adsbygoogle {
                display: none;
                position: absolute;
                top: -1000000px;
                visibility: hidden
            }
        </style> 
 
    </head>

    <body>
        <canvas id="canvas" width="1424" height="404"></canvas>
        <script>
            var canvas,
                ctx,
                width,
                height,
                size,
                lines,
                tick;

            function line() {
                this.path = [];
                this.speed = rand(10, 20);
                this.count = randInt(10, 30);
                this.x = width / 2, +1;
                this.y = height / 2 + 1;
                this.target = {
                    x: width / 2,
                    y: height / 2
                };
                this.dist = 0;
                this.angle = 0;
                this.hue = tick / 5;
                this.life = 1;
                this.updateAngle();
                this.updateDist();
            }

            line.prototype.step = function(i) {
                this.x += Math.cos(this.angle) * this.speed;
                this.y += Math.sin(this.angle) * this.speed;

                this.updateDist();

                if(this.dist < this.speed) {
                    this.x = this.target.x;
                    this.y = this.target.y;
                    this.changeTarget();
                }

                this.path.push({
                    x: this.x,
                    y: this.y
                });
                if(this.path.length > this.count) {
                    this.path.shift();
                }

                this.life -= 0.001;

                if(this.life <= 0) {
                    this.path = null;
                    lines.splice(i, 1);
                }
            };

            line.prototype.updateDist = function() {
                var dx = this.target.x - this.x,
                    dy = this.target.y - this.y;
                this.dist = Math.sqrt(dx * dx + dy * dy);
            }

            line.prototype.updateAngle = function() {
                var dx = this.target.x - this.x,
                    dy = this.target.y - this.y;
                this.angle = Math.atan2(dy, dx);
            }

            line.prototype.changeTarget = function() {
                var randStart = randInt(0, 3);
                switch(randStart) {
                    case 0: // up
                        this.target.y = this.y - size;
                        break;
                    case 1: // right
                        this.target.x = this.x + size;
                        break;
                    case 2: // down
                        this.target.y = this.y + size;
                        break;
                    case 3: // left
                        this.target.x = this.x - size;
                }
                this.updateAngle();
            };

            line.prototype.draw = function(i) {
                ctx.beginPath();
                var rando = rand(0, 10);
                for(var j = 0, length = this.path.length; j < length; j++) {
                    ctx[(j === 0) ? 'moveTo' : 'lineTo'](this.path[j].x + rand(-rando, rando), this.path[j].y + rand(-rando, rando));
                }
                ctx.strokeStyle = 'hsla(' + rand(this.hue, this.hue + 30) + ', 80%, 55%, ' + (this.life / 3) + ')';
                ctx.lineWidth = rand(0.1, 2);
                ctx.stroke();
            };

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

            function randInt(min, max) {
                return Math.floor(min + Math.random() * (max - min + 1));
            };

            function init() {
                canvas = document.getElementById('canvas');
                ctx = canvas.getContext('2d');
                size = 30;
                lines = [];
                reset();
                loop();
            }

            function reset() {
                width = Math.ceil(window.innerWidth / 2) * 2;
                height = Math.ceil(window.innerHeight / 2) * 2;
                tick = 0;

                lines.length = 0;
                canvas.width = width;
                canvas.height = height;
            }

            function create() {
                if(tick % 10 === 0) {
                    lines.push(new line());
                }
            }

            function step() {
                var i = lines.length;
                while(i--) {
                    lines[i].step(i);
                }
            }

            function clear() {
                ctx.globalCompositeOperation = 'destination-out';
                ctx.fillStyle = 'hsla(0, 0%, 0%, 0.1';
                ctx.fillRect(0, 0, width, height);
                ctx.globalCompositeOperation = 'lighter';
            }

            function draw() {
                ctx.save();
                ctx.translate(width / 2, height / 2);
                ctx.rotate(tick * 0.001);
                var scale = 0.8 + Math.cos(tick * 0.02) * 0.2;
                ctx.scale(scale, scale);
                ctx.translate(-width / 2, -height / 2);
                var i = lines.length;
                while(i--) {
                    lines[i].draw(i);
                }
                ctx.restore();
            }

            function loop() {
                requestAnimationFrame(loop);
                create();
                step();
                clear();
                draw();
                tick++;
            }

            function onresize() {
                reset();
            }

            window.addEventListener('resize', onresize);

            init();
        </script>

    </body>

</html>

5.红色火焰,效果如下:

红色火焰

代码如下:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <meta name="google" value="notranslate">
        <meta name="robots" content="noindex">
        <title>红色火焰</title>
        <style>
            canvas {
                position: absolute;
                top: 0;
                left: 0;
            }
            
            .inner>.add {
                display: none;
                position: absolute;
                top: -1000000px;
                visibility: hidden
            }
            
            .fdad,
            .adsbygoogle {
                display: none;
                position: absolute;
                top: -1000000px;
                visibility: hidden
            }
        </style>
 
    </head>

    <body>
        <canvas id="c" width="1423" height="404"></canvas>
        <script>
            var w = c.width = window.innerWidth,
                h = c.height = window.innerHeight,
                ctx = c.getContext('2d'),
                opts = {
                    len: 20,
                    count: 50,
                    baseTime: 10,
                    addedTime: 10,
                    dieChance: .05,
                    spawnChance: 1,
                    sparkChance: .1,
                    sparkDist: 10,
                    sparkSize: 2,
                    color: 'hsl(hue,100%,light%)',
                    baseLight: 50,
                    addedLight: 10,
                    shadowToTimePropMult: 6,
                    baseLightInputMultiplier: .01,
                    addedLightInputMultiplier: .02,
                    cx: w / 2,
                    cy: h / 2,
                    repaintAlpha: .04,
                    hueChange: .1
                },
                tick = 0,
                lines = [],
                dieX = w / 2 / opts.len,
                dieY = h / 2 / opts.len,
                baseRad = Math.PI * 2 / 6;
            ctx.fillStyle = 'black';
            ctx.fillRect(0, 0, w, h);

            function loop() {
                window.requestAnimationFrame(loop);
                ++tick;
                ctx.globalCompositeOperation = 'source-over';
                ctx.shadowBlur = 0;
                ctx.fillStyle = 'rgba(0,0,0,alp)'.replace('alp', opts.repaintAlpha);
                ctx.fillRect(0, 0, w, h);
                ctx.globalCompositeOperation = 'lighter';
                if(lines.length < opts.count && Math.random() < opts.spawnChance) lines.push(new Line);
                lines.map(function(line) {
                    line.step()
                })
            }

            function Line() {
                this.reset()
            }
            Line.prototype.reset = function() {
                this.x = 0;
                this.y = 0;
                this.addedX = 0;
                this.addedY = 0;
                this.rad = 0;
                this.lightInputMultiplier = opts.baseLightInputMultiplier + opts.addedLightInputMultiplier * Math.random();
                this.color = opts.color.replace('hue', tick * opts.hueChange);
                this.cumulativeTime = 0;
                this.beginPhase()
            }
            Line.prototype.beginPhase = function() {
                this.x += this.addedX;
                this.y += this.addedY;
                this.time = 0;
                this.targetTime = (opts.baseTime + opts.addedTime * Math.random()) | 0;
                this.rad += baseRad * (Math.random() < .5 ? 1 : -1);
                this.addedX = Math.cos(this.rad);
                this.addedY = Math.sin(this.rad);
                if(Math.random() < opts.dieChance || this.x > dieX || this.x < -dieX || this.y > dieY || this.y < -dieY) this.reset()
            }
            Line.prototype.step = function() {
                ++this.time;
                ++this.cumulativeTime;
                if(this.time >= this.targetTime) this.beginPhase();
                var prop = this.time / this.targetTime,
                    wave = Math.sin(prop * Math.PI / 2),
                    x = this.addedX * wave,
                    y = this.addedY * wave;
                ctx.shadowBlur = prop * opts.shadowToTimePropMult;
                ctx.fillStyle = ctx.shadowColor = this.color.replace('light', opts.baseLight + opts.addedLight * Math.sin(this.cumulativeTime * this.lightInputMultiplier));
                ctx.fillRect(opts.cx + (this.x + x) * opts.len, opts.cy + (this.y + y) * opts.len, 2, 2);
                if(Math.random() < opts.sparkChance) ctx.fillRect(opts.cx + (this.x + x) * opts.len + Math.random() * opts.sparkDist * (Math.random() < .5 ? 1 : -1) - opts.sparkSize / 2, opts.cy + (this.y + y) * opts.len + Math.random() * opts.sparkDist * (Math.random() < .5 ? 1 : -1) - opts.sparkSize / 2, opts.sparkSize, opts.sparkSize)
            }
            loop();
            window.addEventListener('resize',
                function() {
                    w = c.width = window.innerWidth;
                    h = c.height = window.innerHeight;
                    ctx.fillStyle = 'black';
                    ctx.fillRect(0, 0, w, h);
                    opts.cx = w / 2;
                    opts.cy = h / 2;
                    dieX = w / 2 / opts.len;
                    dieY = h / 2 / opts.len
                });
        </script>

    </body>

</html>

6.烟花效果

烟花效果

附代码如下:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8">
        <title>烟花效果</title>
        <style>
            canvas {
                position: absolute;
                top: 0;
                left: 0;
            }
            
            .inner>.add {
                display: none;
                position: absolute;
                top: -1000000px;
                visibility: hidden
            }
            
            .fdad,
            .adsbygoogle {
                display: none;
                position: absolute;
                top: -1000000px;
                visibility: hidden
            }
        </style>
 
    </head>

    <body>
        <canvas id="c" width="1423" height="404"></canvas>
        <script>
            var w = c.width = window.innerWidth,
                h = c.height = window.innerHeight,
                ctx = c.getContext('2d'),
                opts = {
                    baseBaseSize: 15,
                    addedBaseSize: 5,
                    baseVel: 2,
                    addedVel: 1,
                    baseTime: 60,
                    addedTime: 20,
                    overTime: 5,
                    sliding: .99,
                    particleChance: .9,
                    particles: 100,
                    templateParticleColor: 'hsla(hue,80%,40%,alp)',
                    repaintAlpha: 'rgba(0,0,0,.1)',
                    startColor: .2,
                    fullColor: .5,
                    stopColor: .6,
                    timeToColorChange: 3
                },
                particles = [],
                tick = 0;

            function Particle() {
                this.reset();
            }
            Particle.prototype.reset = function() {
                this.x = Math.pow(Math.random(), 1 / 4);
                this.y = h / 2;
                var color = opts.templateParticleColor.replace('hue', this.x * 360 * 2 + tick * opts.timeToColorChange);
                this.baseSize = (Math.random() + this.x) / 2 * (opts.baseBaseSize + opts.addedBaseSize * Math.random());
                this.gradient = ctx.createRadialGradient(0, 0, 0, 0, 0, this.baseSize / 2);
                this.gradient.addColorStop(opts.startColor, color.replace('alp', 0));
                this.gradient.addColorStop(opts.fullColor, color.replace('alp', 1));
                this.gradient.addColorStop(opts.stopColor, color.replace('alp', 1));
                this.gradient.addColorStop(1, color.replace('alp', 0));

                this.vx = -(1 + Math.random() / 10 - this.x) * (opts.baseVel + Math.random() * opts.addedVel);
                this.vy = Math.pow(this.x, 4) * (opts.baseVel + Math.random() * opts.addedVel) * (Math.random() < .5 ? -1 : 1);

                this.x *= w / 2;
                if(Math.random() < .5) {
                    this.x = w - this.x;
                    this.vx *= -1;
                }

                this.time = opts.baseTime + opts.addedTime * Math.random();
                this.tick = this.time + opts.overTime;

            }
            Particle.prototype.step = function() {
                var size;
                if(this.tick <= this.time) {
                    this.x += this.vx *= opts.sliding;
                    this.y += this.vy *= opts.sliding;
                    size = Math.pow(this.tick / this.time, 1 / 2)
                } else size = 1 - ((this.tick - this.time) / opts.overTime) + .000001;

                --this.tick;

                ctx.translate(this.x, this.y);
                ctx.scale(size, size);
                ctx.fillStyle = this.gradient;
                ctx.fillRect(-this.baseSize / 2, -this.baseSize / 2, this.baseSize, this.baseSize);
                ctx.scale(1 / size, 1 / size);
                ctx.translate(-this.x, -this.y);

                if(this.tick <= 0)
                    this.reset();
            }

            function anim() {
                window.requestAnimationFrame(anim);

                ctx.globalCompositeOperation = 'source-over';
                ctx.fillStyle = opts.repaintAlpha;
                ctx.fillRect(0, 0, w, h);

                ctx.globalCompositeOperation = 'lighter';

                ++tick;

                if(particles.length < opts.particles && Math.random() < opts.particleChance)
                    particles.push(new Particle);

                particles.map(function(particle) {
                    particle.step();
                });
            }
            ctx.fillStyle = '#222';
            ctx.fillRect(0, 0, w, h);
            anim();

            window.addEventListener('resize', function() {

                w = c.width = window.innerWidth;
                h = c.height = window.innerHeight;

                ctx.fillStyle = '#222';
                ctx.fillRect(0, 0, w, h);
            })
        </script>

    </body>

</html>

7.星空特效,效果如下:

星空效果

附代码如下:


 <!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="jquery.min.js"></script>
        <style>
            body {
                background: #000;
                margin: 0;
                padding: 0;
            }
        </style>
    </head>

    <body>
        <canvas id="canvas" width="1423" height="404"></canvas>
        <script>
            $(function() {
                var canvas = document.querySelector('canvas'),
                    ctx = canvas.getContext('2d')
                canvas.width = window.innerWidth;
                canvas.height = window.innerHeight;
                ctx.lineWidth = .3;
                ctx.strokeStyle = (new Color(150)).style;

                var mousePosition = {
                    x: 30 * canvas.width / 100,
                    y: 30 * canvas.height / 100
                };

                var dots = {
                    nb: 150,
                    distance: 50,
                    d_radius: 100,
                    array: []
                };

                function colorValue(min) {
                    return Math.floor(Math.random() * 255 + min);
                }

                function createColorStyle(r, g, b) {
                    return 'rgba(' + r + ',' + g + ',' + b + ', 0.8)';
                }

                function mixComponents(comp1, weight1, comp2, weight2) {
                    return(comp1 * weight1 + comp2 * weight2) / (weight1 + weight2);
                }

                function averageColorStyles(dot1, dot2) {
                    var color1 = dot1.color,
                        color2 = dot2.color;

                    var r = mixComponents(color1.r, dot1.radius, color2.r, dot2.radius),
                        g = mixComponents(color1.g, dot1.radius, color2.g, dot2.radius),
                        b = mixComponents(color1.b, dot1.radius, color2.b, dot2.radius);
                    return createColorStyle(Math.floor(r), Math.floor(g), Math.floor(b));
                }

                function Color(min) {
                    min = min || 0;
                    this.r = colorValue(min);
                    this.g = colorValue(min);
                    this.b = colorValue(min);
                    this.style = createColorStyle(this.r, this.g, this.b);
                }

                function Dot() {
                    this.x = Math.random() * canvas.width;
                    this.y = Math.random() * canvas.height;

                    this.vx = -.5 + Math.random();
                    this.vy = -.5 + Math.random();

                    this.radius = Math.random() * 2;

                    this.color = new Color();
                    console.log(this);
                }

                Dot.prototype = {
                    draw: function() {
                        ctx.beginPath();
                        ctx.fillStyle = this.color.style;
                        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
                        ctx.fill();
                    }
                };

                function createDots() {
                    for(i = 0; i < dots.nb; i++) {
                        dots.array.push(new Dot());
                    }
                }

                function moveDots() {
                    for(i = 0; i < dots.nb; i++) {

                        var dot = dots.array[i];

                        if(dot.y < 0 || dot.y > canvas.height) {
                            dot.vx = dot.vx;
                            dot.vy = -dot.vy;
                        } else if(dot.x < 0 || dot.x > canvas.width) {
                            dot.vx = -dot.vx;
                            dot.vy = dot.vy;
                        }
                        dot.x += dot.vx;
                        dot.y += dot.vy;
                    }
                }

                function connectDots() {
                    for(i = 0; i < dots.nb; i++) {
                        for(j = 0; j < dots.nb; j++) {
                            i_dot = dots.array[i];
                            j_dot = dots.array[j];

                            if((i_dot.x - j_dot.x) < dots.distance && (i_dot.y - j_dot.y) < dots.distance && (i_dot.x - j_dot.x) > -dots.distance && (i_dot.y - j_dot.y) > -dots.distance) {
                                if((i_dot.x - mousePosition.x) < dots.d_radius && (i_dot.y - mousePosition.y) < dots.d_radius && (i_dot.x - mousePosition.x) > -dots.d_radius && (i_dot.y - mousePosition.y) > -dots.d_radius) {
                                    ctx.beginPath();
                                    ctx.strokeStyle = averageColorStyles(i_dot, j_dot);
                                    ctx.moveTo(i_dot.x, i_dot.y);
                                    ctx.lineTo(j_dot.x, j_dot.y);
                                    ctx.stroke();
                                    ctx.closePath();
                                }
                            }
                        }
                    }
                }

                function drawDots() {
                    for(i = 0; i < dots.nb; i++) {
                        var dot = dots.array[i];
                        dot.draw();
                    }
                }

                function animateDots() {
                    ctx.clearRect(0, 0, canvas.width, canvas.height);
                    moveDots();
                    connectDots();
                    drawDots();

                    requestAnimationFrame(animateDots);
                }

                $('canvas').on('mousemove', function(e) {
                    mousePosition.x = e.pageX;
                    mousePosition.y = e.pageY;
                });

                $('canvas').on('mouseleave', function(e) {
                    mousePosition.x = canvas.width / 2;
                    mousePosition.y = canvas.height / 2;
                });

                createDots();
                requestAnimationFrame(animateDots);
            });
        </script>
    </body>

</html>

[来源于:http://www.jq22.com/]http://www.jq22.com/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容

  • # 一、框架概述 # 课程概述 1. laravel 4天(之前TP框架还是很大的区别)(国外框架) 2. 在线教...
    关进一阅读 357评论 0 0
  • # 为什么要学习composer? 由于我们要学习的laravel框架底层是syfomy框架。syfomy框架底层...
    Yu_b29b阅读 646评论 0 0
  • 本章节主要介绍jenkins的使用 在前一章节中已经介绍可jenkins安装步骤,不了解的可以参考前文:[jenk...
    haishuiaa阅读 3,218评论 0 3
  • <!DOCTYPE html> Document /* 标签样式初始化 */body{margin:0;p...
    三叶松阅读 763评论 0 2
  • 大家好。我叫小雨。2003年12月出生。目前是一名初一的学生‍。 我想用简单的笔墨,来记录我生活。 首先先申明一下...
    汐1225阅读 155评论 0 1