使用的是正面视角,主要是用 HTML5 + CSS3 来实现,JS只是用来画图。

test.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>地球-太阳自转与公转</title>
<link type="text/css" rel='stylesheet' href="test.css"></link>
</head>
<body>
<div class="box">
<canvas id="sun" width="150px" height="150px"></canvas>
<canvas id="earth" width="50px" height="50px"></canvas>
</div>
<script src="test.js"></script>
</body>
</html>

注意<canvas>是行内置换元素,可以设置宽高和内外边距。

test.css:

*{
margin:;
padding:;
}
.box{
width: 150px;
height: 150px;
margin: 100px auto;
position: relative;
transform-style: preserve-3d;
/*perspective: 10000px;*/
animation: sun linear 365s infinite; /*地球公转*/
}
#sun{
position: absolute;
animation: sun linear 26.7s infinite; /*太阳自转+误差*/
}
#earth{
margin: 50px;
position: absolute;
transform: translateZ(600px);
animation: earth linear 1s infinite; /*地球自转*/
}
@keyframes sun{
from{transform: rotateY(0deg);}
to{transform: rotateY(360deg);}
}
@keyframes earth{
from{transform: translateZ(600px) rotateY(0deg);}
to{transform: translateZ(600px) rotateY(360deg);}
}

其中1 s = 1 天。

.box加上一个较大的 perspective 属性,想象自己漂在太空中较远处某个点观察地球和太阳;不加 perspective 属性相当于站在无穷远处观察。

test.js:

var sun = document.getElementById("sun").getContext('2d'),
earth = document.getElementById('earth').getContext('2d'),
gra1 = sun.createRadialGradient(75,75,0,75,75,75),
gra2 = earth.createRadialGradient(25,25,0,25,25,25);
gra1.addColorStop(0,'#ffff00');
gra1.addColorStop(1,'#ff9900');
sun.arc(75,75,75,0,2 * Math.PI);
sun.fillStyle = gra1;
sun.fill();
gra2.addColorStop(0,'#78b1e8');
gra2.addColorStop(1,'#1c4364');
earth.arc(25,25,25,0,2 * Math.PI);
earth.fillStyle = gra2;
earth.fill();

效果图:

HTML5 + CSS3 实现地球绕太阳公转的更多相关文章

  1. Web大前端时代之:HTML5+CSS3入门系列

    准备来一波新技术,待续.... Old: 联系源码:https://github.com/dunitian/LoTHTML5 文档下载:https://github.com/dunitian/LoTD ...

  2. 07. Web大前端时代之:HTML5+CSS3入门系列~H5 地理位置

    Web大前端时代之:HTML5+CSS3入门系列:http://www.cnblogs.com/dunitian/p/5121725.html 源码:https://github.com/duniti ...

  3. 01.Web大前端时代之:HTML5+CSS3入门系列~初识HTML5

    Web大前端时代之:HTML5+CSS3入门系列:http://www.cnblogs.com/dunitian/p/5121725.html 文档申明 <!--文档类型申明,html代表是ht ...

  4. 02.Web大前端时代之:HTML5+CSS3入门系列~H5结构元素

    Web大前端时代之:HTML5+CSS3入门系列:http://www.cnblogs.com/dunitian/p/5121725.html 1.结构元素 可以理解为语义话标记,比如:以前这么写&l ...

  5. 03.Web大前端时代之:HTML5+CSS3入门系列~H5功能元素

    Web大前端时代之:HTML5+CSS3入门系列:http://www.cnblogs.com/dunitian/p/5121725.html 2.功能元素 1.hgroup 对网页或区段(secti ...

  6. 04. Web大前端时代之:HTML5+CSS3入门系列~HTML5 表单

    Web大前端时代之:HTML5+CSS3入门系列:http://www.cnblogs.com/dunitian/p/5121725.html 一.input新增类型: 1.tel:输入类型用于应该包 ...

  7. 05. Web大前端时代之:HTML5+CSS3入门系列~H5 多媒体系

    Web大前端时代之:HTML5+CSS3入门系列:http://www.cnblogs.com/dunitian/p/5121725.html 1.引入 概述 音频文件或视频文件都可以看做是一个容器文 ...

  8. 06. Web大前端时代之:HTML5+CSS3入门系列~HTML5 画布

    Web大前端时代之:HTML5+CSS3入门系列:http://www.cnblogs.com/dunitian/p/5121725.html 我们先看看画布的魅力: 初始画布 canvas默认是宽3 ...

  9. 08. Web大前端时代之:HTML5+CSS3入门系列~H5 Web存储

    Web大前端时代之:HTML5+CSS3入门系列:http://www.cnblogs.com/dunitian/p/5121725.html  

随机推荐

  1. 数据包式套接字:基于UDP协议的Socket网络编程

    步骤: 1.利用DatagramPacket封装数据包: 2.利用DatagramSocket发送数据包: 3.利用DatagramSocket接收数据包: 4.利用DatagramPacket处理数 ...

  2. iOS 二维码生成 记录一下

    #import <CoreImage/CoreImage.h> @property (nonatomic, nonnull , strong) UIImageView *showImage ...

  3. Java SPI(Service Provider Interface)

    SPI 全称为 (Service Provider Interface) ,是JDK内置的一种服务提供发现机制. 目前有不少框架用它来做服务的扩展发现, 简单来说,它就是一种动态替换发现的机制, 举个 ...

  4. python 装饰器 传递参数简单案例

    def debug(func): def wrapper(*args, **kwargs): # 指定宇宙无敌参数 print "[DEBUG]: enter {}()".form ...

  5. .NET获取IIS7.0及以上版本托管服务信息

    近期写了个扫描IIS托管站点然后定期注册到Consul的小工具,随意网上拷贝了个帮助类,搞完本机测试没问题,扔到服务器发现硕大的一个异常.. System.Runtime.InteropService ...

  6. sys_arch interface for lwIP 2.0.3

    sys_arch interface for lwIP 2.0.3 Author: Adam Dunkels Simon Goldschmidt The operating system emulat ...

  7. 批量kill杀死某些会话session的PL/SQL

    原文:http://blog.itpub.net/9240380/viewspace-666622/ SQL> declare 2 v_sid v$session.sid%type; --定义如 ...

  8. 谈谈对html5的了解

    1.良好的移动性,以移动端设备为主. 2.响应式设计,以适应自动变化的屏幕尺寸. 3.支持离线缓存技术,webStorage本地缓存. 4.新增canvas,video,audio等新.标签元素.新增 ...

  9. laravel when 的用法

    当你在使用where语句有前提条件时,比如某值为1的时候才执行where子句,否则不执行,这个时候,laravel5.5新出了一个简便方法when($arg,fun1[,fun2]). 具体用法如下: ...

  10. goland实现函数式链式编程

    先来看一段代码 package main import ( "fmt" elastic "gopkg.in/olivere/elastic.v2" ) type ...