HTML JavaScript 随机游走

html 复制代码
<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>随机游走</title>  
    <style>  
        /* 使用百分比或vw/vh单位设置画布大小 */  
        #myCanvas {  
            border: 2px solid black;  
            width: 100vw; /* 使得画布宽度等于视口宽度 */  
            height: 100vh; /* 使得画布高度等于视口高度 */  
        }  
    </style>  
</head>  
<body>  
    <canvas id="myCanvas"></canvas> <!-- 移除内联的宽度和高度 -->  
    <script>  
        document.addEventListener('DOMContentLoaded', (event) => {  
            const canvas = document.getElementById('myCanvas');  
            const ctx = canvas.getContext('2d');  
  
            let count = 0;  
            let x, y; // 初始化x和y  
            let points = []; // 初始化点的数组  
  
            function resizeCanvas() {  
                // 动态设置画布的大小  
                canvas.width = window.innerWidth;  
                canvas.height = window.innerHeight;  
  
                // 重置x和y到新的画布中心  
                x = canvas.width / 2;  
                y = canvas.height / 2;  
  
                // 如果已经有点,则重置第一个点为新的中心  
                if (points.length > 0) {  
                    points = [{ x, y }];  
                }  
            }  
  
            // 监听窗口大小变化  
            window.addEventListener('resize', resizeCanvas);  
  
            // 初始化画布大小  
            resizeCanvas();  
  
            function draw() {  
                ctx.clearRect(0, 0, canvas.width, canvas.height);  
  
                // Draw the trace  
                ctx.beginPath();  
                if (points.length > 0) {  
                    ctx.moveTo(points[0].x, points[0].y);  
                    for (let i = 1; i < points.length; i++) {  
                        ctx.lineTo(points[i].x, points[i].y);  
                    }  
                }  
                ctx.stroke();  
  
                count = getRandomInt(0, 50);  
  
                // Update the position  
                const dx = Math.random() * count - count / 2; // 使移动平均分布在两侧  
                const dy = Math.random() * count - count / 2;  
  
                // 根据count的值改变方向  
                if (count % 4 == 0) {  
                    x += dx;  
                    y += dy;  
                } else if (count % 4 == 1) {  
                    x += dx;  
                    y -= dy;  
                } else if (count % 4 == 2) {  
                    x -= dx;  
                    y += dy;  
                } else if (count % 4 == 3) {  
                    x -= dx;  
                    y -= dy;  
                }  
  
                // Keep the point within the canvas  
                if (x < 0 || x > canvas.width) {  
                    x = Math.max(0, Math.min(canvas.width, x - dx));  
                }  
                if (y < 0 || y > canvas.height) {  
                    y = Math.max(0, Math.min(canvas.height, y - dy));  
                }  
  
                // Add the new point to the array  
                points.push({ x, y });  
  
                // Draw the current point  
                ctx.beginPath();  
                ctx.arc(x, y, 2, 0, Math.PI * 2);  
                ctx.fill();  
            }  
  
            function getRandomInt(min, max) {  
                min = Math.ceil(min);  
                max = Math.floor(max);  
                return Math.floor(Math.random() * (max - min + 1)) + min;  
            }  
  
            setInterval(draw, 200); // Update every 200ms  
        });  
    </script>  
</body>  
</html>
相关推荐
蜡笔小马26 分钟前
10.Boost.Geometry R-tree 空间索引详解
开发语言·c++·算法·r-tree
IOsetting27 分钟前
金山云主机添加开机路由
运维·服务器·开发语言·网络·php
Hilaku30 分钟前
不要在简历上写精通 Vue3?来自面试官的真实劝退
前端·javascript·vue.js
三小河36 分钟前
前端视角详解 Agent Skill
前端·javascript·后端
林开落L41 分钟前
从零开始学习Protobuf(C++实战版)
开发语言·c++·学习·protobuffer·结构化数据序列化机制
牛奔1 小时前
Go 是如何做抢占式调度的?
开发语言·后端·golang
颜酱1 小时前
二叉树遍历思维实战
javascript·后端·算法
鹏多多1 小时前
移动端H5项目,还需要react-fastclick解决300ms点击延迟吗?
前端·javascript·react.js
符哥20081 小时前
C++ 进阶知识点整理
java·开发语言·jvm
小猪咪piggy1 小时前
【Python】(4) 列表和元组
开发语言·python