首页 > 其他分享 >随机运行小球 - html

随机运行小球 - html

时间:2024-10-15 16:24:15浏览次数:1  
标签:小球 random setAttribute height width html 随机 var circle

目的是为了训练眼球运动

直接将如下代码到保存为html文件打开即可

<!DOCTYPE html>
<html>
    
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>随机运动小球</title>
    <style>
        body {
            background-color: black;
            margin: 0;
            height: 100vh;
            width: 100vw;
            overflow: hidden;
        }
    </style>
</head>
<body >
<svg id="mySVG" height="200" width="200">
   <circle id="myCircle1" cx="50" cy="50" r="40" fill="#27202075" />
  <circle id="myCircle" cx="50" cy="50" r="40" fill="#27202075" />
  <text id="myCircle1Text" x="50" y="55" font-size="20px" text-anchor="middle" fill="pink">·</text>
  <text id="myCircleText" x="50" y="55" font-size="20px" text-anchor="middle" fill="pink">·</text>
</svg>

<script>
var svg = document.getElementById('mySVG');
var circle = document.getElementById('myCircle');
var circle1 = document.getElementById('myCircle1');
var text = document.getElementById('myCircleText');
var text1 = document.getElementById('myCircle1Text');


// Function to get random number up to max
var random = function(max) {
  return Math.floor(Math.random() * max);
}

// Set svg and circle size
svg.setAttribute('width', window.innerWidth - 50);
svg.setAttribute('height', window.innerHeight - 50);

// Animate the circle to a new position, and repeat
var animate = function() {
    let width = random(window.innerWidth - 100)
    let height = random(window.innerHeight - 100)
    circle.setAttribute('cx', width);
    circle.setAttribute('cy', height);

    let time = random(5) + 1
    // Apply a random duration for the next animation
    circle.style.transitionDuration = (time) + 's';
    
    setTimeout(animate, (time) * 1000);
    text.setAttribute('x', width);
    text.setAttribute('y', height + 5);
    text.style.transitionDuration = (time) + 's';
}

// Animate the circle to a new position, and repeat
var animate1 = function() {
    let width = random(window.innerWidth - 100)
    let height = random(window.innerHeight - 100)
    circle1.setAttribute('cx', width);
    circle1.setAttribute('cy', height);
    
    // Apply a random duration for the next animation
    let time1 = random(5) + 1
    circle1.style.transitionDuration = (time1) + 's';
    text1.setAttribute('x', width);
    text1.setAttribute('y', height + 5);
    text1.style.transitionDuration = (time1) + 's';
    
    setTimeout(animate1, (time1) * 1000);
  }
  
// Start the animation
animate();
animate1();
</script>

</body>
</html>

  

标签:小球,random,setAttribute,height,width,html,随机,var,circle
From: https://www.cnblogs.com/uoky/p/18467765

相关文章

  • Chromium 中HTML5 WebSocket收发消息分析c++(一)
    一、WebSocket前端接口定义:WebSocket 对象提供了用于创建和管理 WebSocket 连接,以及可以通过该连接发送和接收数据的API。使用 WebSocket() 构造函数来构造一个 WebSocket。构造函数WebSocket(url[,protocols])返回一个 WebSocket 对象。常量ConstantValueWeb......
  • Chromium 中HTML5 WebSocket收发消息分析c++(二)
    看下websocket调用过程:基本定义参考上一篇:Chromium中HTML5WebSocket收发消息分析c++(一)-CSDN博客一、前端测试用例 参考:HTML5WebSocket|菜鸟教程(runoob.com) websocket.html文件如下:<!DOCTYPEHTML><html><head><metacharset="utf-8"><title>Web......
  • 大学生HTML期末大作业——HTML+CSS+JavaScript购物商城
    HTML+CSS+JS【购物商场】网页设计期末课程大作业web前端开发技术web课程设计网页规划与设计......
  • 关于html的20道前端面试题2
    21.请解释HTML5中的新增语义标签,如<header>、<footer>、<section>等。HTML5引入了一些新的语义标签,这些标签旨在帮助开发者更好地描述网页内容的结构。以下是一些常见的新增语义标签及其用途:<header>:表示页面或区域的头部信息,通常包含标题、导航链接和标志等。它通常出......
  • 什么是随机变量
    目录1.从变量引出随机变量2.随机变量的本质3.总结1.从变量引出随机变量首先,让我们考虑这样一个问题:什么是变量?变量表示一个不确定的值,例如变量\(x\),我们不知道\(x\)的值可以取多少,\(x\)有可能是\(1\),有可能是\(2\),有可能是\(0.5\),总之,\(x\)可以是任意一个值。它的值是不确......
  • 机器学习-决策树与随机森林-demo
    ......
  • 学生个人网页制作html
    创建一个简单的个人网页可以按照以下步骤进行:设置基本HTML结构:首先,你需要设置一个基本的HTML文档结构,包括文档类型声明、HTML标签、head部分(包含标题、样式链接等)和body部分。编写头部信息:在<head>标签内,添加页面标题、引入外部CSS文件或直接定义内联样式,还可以引入JavaSc......
  • HTML DOM 教程---操作自定义属性(2)
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=d......
  • 【油猴脚本】00027 案例 Tampermonkey油猴脚本, 仅用于学习,不要乱搞。添加标题为网页数
    前言:哈喽,大家好,今天给大家分享一篇文章!并提供具体代码帮助大家深入理解,彻底掌握!创作不易,如果能帮助到大家或者给大家一些灵感和启发,欢迎收藏+关注哦......
  • html vite modulerpreload 和preload 作用和区别
    <linkrel="modulepreload">和<linkrel="preload">的功能在某种程度上是相似的,主要都是用于优化资源的加载性能,但它们之间有一些关键区别:相似之处预加载资源:两者都允许浏览器提前下载指定的资源,以减少后续使用时的加载延迟。非阻塞加载:都不会阻塞DOM渲染,浏览器......