用javascript伪造太阳系模型系统

2024-01-02 02:38

本文主要是介绍用javascript伪造太阳系模型系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这个东西,是我在读在职研的课堂上无聊的时候随便弄的,实现了椭圆和圆形的轨道运转。这些东西吧,其实都是很简单的,在游戏领域,轨道运动是很有意义的,我就顺便把代码po出来,做备忘吧。

由于是无聊时的即兴之作,并没有考虑性能优化和使用html5的canvas或者svg,只是用了最简单的div而已,要想用html5请自行实现了。

<!DOCTYPE html>
<html>
<head><title></title>
</head>
<body style="width: 100%;height: 100%;margin: 0;padding: 0;overflow: hidden; background-color: rgb(42,42,42);"></body>
<script type="text/javascript">
function id(x){return document.getElementById(x);}function print(x){console.log(x);}function random(lower,upper){return lower+Math.floor(Math.random()*(upper-lower+1));}Array.prototype.filter = function(){var tmp = [];for(var i=0;i<this.length;i++){if(typeof(this[i]) !== 'undefined'){tmp.push(this[i]);}}return tmp;
}var w = window.innerWidth;
var h = window.innerHeight;
var Planets = [];
function Planet(x,y,radius,color){var v = this;v.x = x;v.y = y;v.color = color || 'skyblue';v.radius = radius || 10;v.rotateangle = 0;v.body = document.createElement('div');v.body.style = "position:absolute;top:"+ (v.y-v.radius) +"px;left:"+ (v.x-v.radius) +"px;background-color:"+v.color+";border-radius:" +(v.radius*2)+"px;width:"+(v.radius*2)+"px;height:"+(v.radius*2)+"px;";document.body.appendChild(v.body);v.runType = false;v.target = null;v.rotateradius = null;v.longradius = null;v.shortradius = null;v.offsetx = null;v.offsety = null;v.anglespeed = 0;v.isRotated = false;v.rotatetheta = null;v.rotateanglespeed = 0;v.strange = false;var vx,vy;v.update = function(lerp){if(v.rotatetheta>Math.PI*2) v.rotatetheta -= Math.PI*2;if(v.runType == "circle"){if(!v.rotateangle){v.rotateangle = Math.random()*Math.PI*2;}v.rotateangle += v.anglespeed/10000 * lerp;if(v.rotateangle>Math.PI*2) v.rotateangle -= Math.PI*2;v.x =  Math.cos(v.rotateangle)*v.rotateradius;v.y =  Math.sin(v.rotateangle)*v.rotateradius;}else if(v.runType == "eclipse"){if(!v.rotateangle){v.rotateangle = Math.random()*Math.PI*2;}v.rotateangle += v.anglespeed/10000 * lerp;if(v.rotateangle>Math.PI*2) v.rotateangle -= Math.PI*2;v.x = Math.cos(v.rotateangle)*v.longradius + v.offsetx;v.y = Math.sin(v.rotateangle)*v.shortradius + v.offsety;}if(v.isRotated){v.rotatetheta += v.rotateanglespeed/10000 * lerp;if (v.strange){v.x =  v.x*Math.cos(v.rotatetheta) - v.y*Math.sin(v.rotatetheta);v.y =  v.x*Math.sin(v.rotatetheta) + v.y*Math.cos(v.rotatetheta);}else{vx = v.x;vy = v.y; //如果不用两个vx vy临时变量保存的话,则和上面的圆形环绕或椭圆环绕连用的时候会出现诡异的变扁现象,相当有立体感v.x =  vx*Math.cos(v.rotatetheta) - vy*Math.sin(v.rotatetheta);v.y =  vx*Math.sin(v.rotatetheta) + vy*Math.cos(v.rotatetheta);}}if(v.target){v.x += v.target.x;v.y += v.target.y;}v.body.style.left = (v.x-v.radius) + "px";v.body.style.top = (v.y-v.radius) + "px";}v.circleRun = function(target,rotateradius,anglespeed){v.runType = "circle";v.target = target;v.rotateradius = rotateradius;v.anglespeed = anglespeed * (Math.random()>0.5?-1:1);}v.eclipseRun = function(target,longradius,shortradius,offsetx,offsety,anglespeed){v.runType = "eclipse";v.target = target;v.longradius = longradius;v.shortradius = shortradius;v.offsetx = offsetx;v.offsety = offsety;v.anglespeed = anglespeed * (Math.random()>0.5?-1:1);}v.rotate = function(target,rotatetheta,rotateanglespeed,strange){v.isRotated = true;v.target = target;v.rotatetheta = rotatetheta;v.rotateanglespeed = rotateanglespeed;v.strange = strange;}v.suicide = function(){document.body.removeChild(v.avator);delete v;}Planets.push(v);
}var center = new Planet(w/2,h/2,1,'black');for(var i=0;i<1000;i++){var star = new Planet(0,0,0.5,"white");star.eclipseRun(center,random(5,10)/10*w,Math.random()*h,0,0,-1);star.rotate(center,0,-Math.random());
}var sun = new Planet(w/2,h/2,50,'orange');
sun.circleRun(center,8,10);var mercury = new Planet(0,0,7,'blue');
var venus = new Planet(0,0,8,'rgb(255,100,0)');
var earth = new Planet(0,0,10,"deepskyblue");var moon = new Planet(0,0,5,'yellow');
var mars = new Planet(0,0,9,'red');
var jupter = new Planet(0,0,24,'rgb(243,174,129)');
var saturn = new Planet(0,0,22,'rgb(191,149,21)');
var uranus = new Planet(0,0,20,'deepskyblue');
var neptune = new Planet(0,0,17,'rgb(16,201,175)');
var pluto = new Planet(0,0,8,'rgb(112,146,190)');mercury.circleRun(sun,80,9.4);
venus.circleRun(sun,110,8);
earth.circleRun(sun,160,4.8);moon.circleRun(earth,20,-15.2);
mars.circleRun(sun,200,5.4);
jupter.circleRun(sun,270,5.1);
saturn.circleRun(sun,340,4.8);
uranus.circleRun(sun,400,5.2);
neptune.circleRun(sun,450,4.6);
pluto.circleRun(sun,520,5.21);for(var i=0;i<50;i++){var star = new Planet(0,0,1,"white");star.eclipseRun(jupter,Math.random()*2+40,Math.random()*2,0,0,10);star.rotate(jupter,0,2);
}for(var i=0;i<50;i++){var star = new Planet(0,0,1,"white");star.eclipseRun(saturn,Math.random()*2+30,Math.random()*2,0,0,10);star.rotate(saturn,2.5,1);
}for(var i=0;i<50;i++){var star = new Planet(0,0,1,"rgb(209,238,245)");star.eclipseRun(uranus,Math.random()*2+30,Math.random()*2,0,0,10);star.rotate(uranus,2.5,0.1);
}for(var i=0;i<50;i++){var star = new Planet(0,0,1,"rgb(192,250,221)");star.eclipseRun(neptune,Math.random()*2+25,Math.random()*2,0,0,10);star.rotate(neptune,-2.5,-0.1);
}var halley = new Planet(0,0,5,"white");
halley.eclipseRun(sun,460,130,-150,0,20);
halley.rotate(sun,Math.random()*5,Math.random()*1);for(var i=0;i<100;i++){var star = new Planet(0,0,1,"white");star.eclipseRun(sun,Math.random()*10+230,Math.random()*10+200,0,0,10);star.rotate(sun,0,2,true);
}var lastTime = new Date();
var thisTime = lastTime;
var lerp = 0;
function update(){thisTime = new Date();lerp = thisTime - lastTime;for(var i=0,j=Planets.length;i<j;i++){Planets[i].update(lerp);}requestAnimationFrame(update);lastTime = thisTime;
}update();
</script>
</html>

 

 

 

这篇关于用javascript伪造太阳系模型系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/561115

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

基于人工智能的图像分类系统

目录 引言项目背景环境准备 硬件要求软件安装与配置系统设计 系统架构关键技术代码示例 数据预处理模型训练模型预测应用场景结论 1. 引言 图像分类是计算机视觉中的一个重要任务,目标是自动识别图像中的对象类别。通过卷积神经网络(CNN)等深度学习技术,我们可以构建高效的图像分类系统,广泛应用于自动驾驶、医疗影像诊断、监控分析等领域。本文将介绍如何构建一个基于人工智能的图像分类系统,包括环境