• canvas_31 动画-星空连线


    <template>
        <view class="zcvs">
            <canvas canvas-id="cvs" id="cvs" :style="setCanvasStyle" />
        </view>
    </template>
    
    <script>
        export default {
            data() {
                return {};
            },
            onReady() {
                this.drawCvs();
            },
            computed: {
                setCanvasStyle() {
                    let retStyle = "background: #000;"
                    const info = uni.getSystemInfoSync();
                    retStyle += "" + info.windowWidth + "px;"
                    retStyle += "height:" + info.windowHeight + "px;"
                    return retStyle;
                }
            },
            methods: {
                drawCvs() {
                    const ctx = uni.createCanvasContext('cvs');
                    const info = uni.getSystemInfoSync();
                    const canvas = {
                         info.windowWidth,
                        height: info.windowHeight
                    };
    
                    let warea = {
                        x: null,
                        y: null,
                        max: 20000
                    };
    
                    window.onmousemove = e => {
                        warea.x = e.clientX;
                        warea.y = e.clientY;
                    };
    
                    window.onmouseout = e => { //鼠标移出界面时清空
                        warea.x = null;
                        warea.y = null;
                    };
    
                    let dots = []; // 所有粒子`
                    for (var i = 0; i < 500; i++) {
                        dots.push({
                            x: Math.random() * canvas.width, // x  , y  为 粒子坐标
                            y: Math.random() * canvas.height,
                            xa: Math.random() * 2 - 1, // xa , ya 为 粒子 xy 轴加速度
                            ya: Math.random() * 2 - 1,
                            max: 6000 //max为 连线的最大距离
                        });
                    };
    
                    var animate = () => {
                        ctx.clearRect(0, 0, canvas.width, canvas.height);
                        var ndots = [warea].concat(dots); // 数组合并
                        dots.forEach(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 - 1, dot.y - 1, 2, 2)
                            ctx.setFillStyle("#d6b816");
                            // 循环比对粒子间的距离
                            for (var i = 0; i < ndots.length; i++) {
                                var d2 = ndots[i]
                                if (dot === d2 || d2.x === null || d2.y === null) continue
                                let [xc, yc, dis, ratio] = [dot.x - d2.x, dot.y - d2.y, '', '']
                                // 两个粒子之间的距离
                                dis = xc * xc + yc * yc
                                // 如果两个粒子之间的距离小于粒子对象的max值
                                //则在两个粒子间画线
                                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.setLineWidth(ratio / 2);
                                    ctx.setStrokeStyle("#d6b816");
                                    ctx.moveTo(dot.x, dot.y)
                                    ctx.lineTo(d2.x, d2.y)
                                    ctx.stroke();
    
                                    ctx.draw(true);
                                }
                            }
                            ndots.splice(ndots.indexOf(dot), 1) // 从数组中删除已经计算过的粒子
                        })
                        window.requestAnimationFrame(animate);
                    };
                    animate();
                },
            }
        }
    </script>
    
    <style lang="scss" scoped>
        .zcvs {
            flex: 1;
            background: "#000";
        }
    </style>
  • 相关阅读:
    QFramework 使用指南 2020(二):下载与版本介绍
    QFramework 使用指南 2020 (一): 概述
    Unity 游戏框架搭建 2018 (二) 单例的模板与最佳实践
    Unity 游戏框架搭建 2018 (一) 架构、框架与 QFramework 简介
    Unity 游戏框架搭建 2017 (二十三) 重构小工具 Platform
    Unity 游戏框架搭建 2017 (二十二) 简易引用计数器
    Unity 游戏框架搭建 2017 (二十一) 使用对象池时的一些细节
    你确定你会写 Dockerfile 吗?
    小白学 Python 爬虫(8):网页基础
    老司机大型车祸现场
  • 原文地址:https://www.cnblogs.com/luwei0915/p/15320357.html
Copyright © 2020-2023  润新知