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>
相关推荐
尽欢i几秒前
HTML iframe 详细解读
html
令狐寻欢3 分钟前
HTML中 的 meta 标签常用属性及其作用
前端·html
Stanford_11066 分钟前
关于大数据的基础知识(二)——国内大数据产业链分布结构
大数据·开发语言·物联网·微信小程序·微信公众平台·twitter·微信开放平台
一个何包蛋!!7 分钟前
相关类相关的可视化图像总结
开发语言·python·数据可视化
超级土豆粉18 分钟前
JavaScript 标签加载
开发语言·javascript·ecmascript
love530love20 分钟前
教程:PyCharm 中搭建多级隔离的 Poetry 环境(从 Anaconda 到项目专属.venv)
开发语言·ide·人工智能·windows·python·pycharm
百锦再24 分钟前
Razor编程中@Html的方法使用大全
前端·html
啪叽26 分钟前
JavaScript可选链操作符(?.)的实用指南
前端·javascript
代码搬运媛29 分钟前
【react实战】如何实现监听窗口大小变化
前端·javascript·react.js
前端Hardy44 分钟前
HTML&CSS:产品卡片动画效果
前端·javascript