console
var spaceX = 30;
var spaceY = 30;
var PI = Math.PI;
var radius = 150;
var radian = PI / 180;
var speedX = 150;
var speedY = 50;
var offsetX = 250;
var offsetY = 250;
var spheres = new Array();
var canvas;
var context;
var focalLength = 250;
var start = true;
var sx = 0;
var cx = 0;
var sy = 0;
var cy = 0;
var sz = 0;
var cz = 0;
var innerStaColor = "rgba(234, 97, 44, 0.5)";
var outerStaColor = "rgba(230, 76, 52, 0.5)";
var objectRadius = 8;
var scaleRatio = 0;
var cameraView = {
x: 0,
y: 0,
z: 0,
rotX: 0,
rotY: 0,
rotZ: 0
};
function initCanvas() {
try{
canvas = document.getElementById("sphere");
context = canvas.getContext("2d");
}catch(e){
document.getElementById("tip_info").innerHTML = "您的浏览器不支持!";
}
}
function initSphere() {
for (var i = spaceY; i < 180; i += spaceY) {
for (var angle = 0; angle < 360; angle += spaceX) {
var object = {};
var x = Math.sin(radian * i) * radius;
object.x = Math.cos(angle * radian) * x;
object.y = Math.cos(radian * i) * radius;
object.z = Math.sin(angle * radian) * x;
object.glow = .5;
spheres.push(object);
}
}
}
function init() {
initCanvas();
initSphere();
setInterval(this.update, 10, this);
}
function update() {
if (start) {
setParam();
}
}
function setParam() {
var rotYstep = speedX / 10000;
var rotXstep = speedY / 10000;
cameraView.rotY = rotYstep;
cameraView.rotX = -rotXstep;
sx = Math.sin(cameraView.rotX);
cx = Math.cos(cameraView.rotX);
sy = Math.sin(cameraView.rotY);
cy = Math.cos(cameraView.rotY);
sz = Math.sin(cameraView.rotZ);
cz = Math.cos(cameraView.rotZ);
context.fillStyle = 'rgba(0, 0, 0, 0.1)';
context.fillRect(0, 0, canvas.width, canvas.height);
var l = spheres.length - 1;
for (var i = l,
obj; obj = spheres[i]; i--) {
render(obj);
}
}
function render(object) {
var xy, xz, yx, yz, zx, zy;
var x = object.x - cameraView.x;
var y = object.y - cameraView.y;
var z = object.z - cameraView.z;
xy = cx * y - sx * z;
xz = sx * y + cx * z;
yz = cy * xz - sy * x;
yx = sy * xz + cy * x;
zx = cz * yx - sz * xy;
zy = sz * yx + cz * xy;
object.x = zx;
object.y = zy;
object.z = yz;
scaleRatio = focalLength / (focalLength + yz);
scale = scaleRatio;
if (object.glow > .5) {
object.glow -= .02;
}
var sphereStyle = context.createRadialGradient(offsetX + object.x * scaleRatio, offsetY + object.y * scaleRatio,
scaleRatio * .5, offsetX + object.x * scaleRatio, offsetY + object.y * scaleRatio, scaleRatio * objectRadius * .5);
sphereStyle.addColorStop(0, innerStaColor);
sphereStyle.addColorStop(object.glow, outerStaColor);
sphereStyle.addColorStop(1, 'rgba(0,0,0,0)');
context.fillStyle = sphereStyle;
context.fillRect(offsetX + object.x * scaleRatio - scaleRatio * objectRadius * .5,
offsetY + object.y * scaleRatio - scaleRatio * objectRadius * .5, scaleRatio * objectRadius, scaleRatio * objectRadius);
document.getElementById("tip_info").innerHTML = "当前速度:"+speedX+" "+ speedY+" 小球半径:"+objectRadius;
}
window.addEventListener("load", init, true);
<canvas id="sphere" width="500" height="500"></canvas>
<a id="tip_info"></a>
* { margin: 0; padding: 0; }
#sphere { width: 500px; display: block; }