用html5如何编写星球运动,使用HTML5里的Canvas标签构建粒子星球

阅读: 评论:0

用html5如何编写星球运动,使用HTML5里的Canvas标签构建粒子星球

用html5如何编写星球运动,使用HTML5里的Canvas标签构建粒子星球

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

questAnimationFrame ||

window.webkitRequestAnimationFrame ||

function(callback) {

window.setTimeout(callback, 1000 / 60);

};

})();

var canvas = ElementById("canvas");

var ctx = Context("2d");

var P = [];

var part = function(angle, angleZ, x, y, z, range, angVel, angVelZ, r, red, green, blue, alpha) {

this.angle = angle;

this.angleZ = angleZ;

this.x = x;

this.y = y;

this.z = z;

this.range = range;

this.angVel = angVel;

this.angVelZ = angVelZ;

this.r = r;

< = green;

this.blue = blue;

this.alpha = alpha;

}

var partNum = 1000;

var zMax = (canvas.width + canvas.height) / 10;

var zMin = -zMax;

function size() {

canvas.width = window.innerWidth;

canvas.height = window.innerHeight;

}

function bg() {

ctx.fillStyle = "rgb(40,40,40)";

ctx.fillRect(0, 0, canvas.width, canvas.height);

}

function init() {

for (var i = 0; i < partNum; i++) {

var angle = Math.random() * (2 * Math.PI),

angleZ = Math.random() * (2 * Math.PI),

x = (canvas.width / 2) + range * s(angle),

y = (canvas.height / 2) + range * Math.sin(angle),

z = Math.random() * zMax,

range = zMax / 2,

angVel = Math.random() * (Math.PI / 720),

angVelZ = Math.random() * (Math.PI / 720),

r = 3 * (z / zMax) + .5,

red = und(Math.random() * 155 + 100),

green = und(Math.random() * 155 + 100),

blue = und(Math.random() * 155 + 100),

alpha = (Math.abs(z / zMax));

P.push(new part(angle, angleZ, x, y, z, range, angVel, angVelZ, r, red, green, blue, alpha));

}

}

function move(p) {

// i really have no idea of what i'm doing here, lol

// if anyone knows how to align the particles in z axis

// so that this thing actually look like a sphere,

// please help me a bit!

var dz = (zMax - p.z);

p.angle += p.angVel;

p.angleZ += p.angVelZ;

p.x = (canvas.width / 2) + p.range * Math.sin(p.angle);

p.y = (canvas.height / 2) + p.range * s(p.angle);

p.z = (zMax) * s(p.angleZ);

p.range = 220 * (dz / zMax) * Math.sin(p.angleZ);

p.r = dz / 50 + 1;

p.alpha = und(100 * (p.r / 3)) / 100;

}

function drawParts() {

var p;

for (var i = 0; i < P.length; i++) {

p = P[i];

ctx.fillStyle = "rgba(" + p.red + "," + p.green + "," + p.blue + "," + p.alpha + ")";

ctx.beginPath();

ctx.arc(p.x, p.y, p.r, 0, 2 * Math.PI);

ctx.fill();

move(p);

}

}

function loop() {

bg();

drawParts();

requestAnimFrame(loop);

}

size();

init();

loop();

}

本文发布于:2024-01-27 22:39:49,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17063663883073.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:星球   粒子   标签   Canvas
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23