Web前端H5 canvas好玩的网站背景

国内外主流前端开发框架对比 适合手机前端开发的框架 微信前端用什么框架开发的

代码片段 1

<!DOCTYPE html>
<head>
    <meta charset=”utf-8″>
    <title>particle nets</title>
    <style>
        html,
        body {
            margin: 0;
            padding: 0;
            height: 100%;
            overflow: hidden;
        }
    </style>
</head>
<body>
    <canvas id=”cas”></canvas>
    <script>
        var canvas = document.getElementById(“cas”),
            ctx = canvas.getContext(“2d”);
        function resize() {
            canvas.width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
            canvas.height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
            console.log(“canvasWidth:” + canvas.width, “canvasHeight:” + canvas.height);
        }
        resize();
        window.onresize = resize;
        var RAF = (function () {
            return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.setTimeout(callback, 1000 / 60);
        })();
        var warea = { x: null, y: null, max: 20000 };
        window.onmousemove = function (e) {
            e = e || event;
            warea.x = e.clientX;
            warea.y = e.clientY;
        };
        window.onmouseout = function (e) {
            e = e || event;
            warea.x = null;
            warea.y = null;
        }
        var dots = [];
        for (var i = 0; i < 300; i++) {
            var x = Math.random() * canvas.width,
                y = Math.random() * canvas.height;
            var xa = Math.random() * 2 – 1,
                ya = Math.random() * 2 – 1;
            dots.push({ “x”: x, “y”: y, “xa”: xa, “ya”: ya, “max”: 6000 });
        }
        setTimeout(function () { animate(); }, 100);
        function animate() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            var ndots = [warea].concat(dots);
            dots.forEach(function (dot) {
                dot.x += dot.xa;
                dot.y += dot.ya;
                dot.xa *= (dot.x > canvas.width || dot.x < 0) ? -1 : 1;
                dot.ya *= (dot.y > canvas.height || dot.y < 0) ? -1 : 1;
                ctx.fillRect(dot.x – .5, dot.y – .5, 1, 1);
                for (var i = 0; i < ndots.length; i++) {
                    var d2 = ndots[i];
                    if (dot === d2 || d2.x === null || d2.y === null) continue;
                    var xc = dot.x – d2.x,
                        yc = dot.y – d2.y;
                    var dis = xc * xc + yc * yc;
                    var ratio;
                    if (dis < d2.max) {
                        if (d2 === warea && dis > (d2.max / 2)) {
                            dot.x -= xc * 0.03;
                            dot.y -= yc * 0.03;
                        }
                        ratio = (d2.max – dis) / d2.max;
                        ctx.beginPath();
                        ctx.lineWidth = ratio / 2;
                        ctx.strokeStyle = “rgba(0,0,0,” + (ratio + .2) + “)”;
                        ctx.moveTo(dot.x, dot.y);
                        ctx.lineTo(d2.x, d2.y);
                        ctx.stroke();
                    }
                }
                ndots.splice(ndots.indexOf(dot), 1);
            });
            RAF(animate);
        }
    </script>
</body>
</html>

微信web前端开发框架 电商的前端开发框架 前端跨平台开发框架

» 本文来自:前端开发者 » 《Web前端H5 canvas好玩的网站背景》
» 本文链接地址:https://www.rokub.com/5922.html
» 您也可以订阅本站:https://www.rokub.com
赞(0)
64K

评论 抢沙发

评论前必须登录!