index.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <html><head>
  2. <meta charset="utf-8">
  3. <title>www.husonghe.com</title>
  4. <style>
  5. html {
  6. height: 100%;
  7. background-image: -webkit-radial-gradient(ellipse farthest-corner at center center, #1b44e4 0%, #020f3a 100%);
  8. background-image: radial-gradient(ellipse farthest-corner at center center, #1b44e4 0%, #020f3a 100%);
  9. cursor: move;
  10. }
  11. body {
  12. width: 100%;
  13. margin: 0;
  14. overflow: hidden;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <canvas id="canv" width="1920" height="572"></canvas>
  20. <script>
  21. var num = 200;
  22. var w = window.innerWidth;
  23. var h = window.innerHeight;
  24. var max = 100;
  25. var _x = 0;
  26. var _y = 0;
  27. var _z = 150;
  28. var dtr = function(d) {
  29. return d * Math.PI / 180;
  30. };
  31. var rnd = function() {
  32. return Math.sin(Math.floor(Math.random() * 360) * Math.PI / 180);
  33. };
  34. var dist = function(p1, p2, p3) {
  35. return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2) + Math.pow(p2.z - p1.z, 2));
  36. };
  37. var cam = {
  38. obj: {
  39. x: _x,
  40. y: _y,
  41. z: _z
  42. },
  43. dest: {
  44. x: 0,
  45. y: 0,
  46. z: 1
  47. },
  48. dist: {
  49. x: 0,
  50. y: 0,
  51. z: 200
  52. },
  53. ang: {
  54. cplane: 0,
  55. splane: 0,
  56. ctheta: 0,
  57. stheta: 0
  58. },
  59. zoom: 1,
  60. disp: {
  61. x: w / 2,
  62. y: h / 2,
  63. z: 0
  64. },
  65. upd: function() {
  66. cam.dist.x = cam.dest.x - cam.obj.x;
  67. cam.dist.y = cam.dest.y - cam.obj.y;
  68. cam.dist.z = cam.dest.z - cam.obj.z;
  69. cam.ang.cplane = -cam.dist.z / Math.sqrt(cam.dist.x * cam.dist.x + cam.dist.z * cam.dist.z);
  70. cam.ang.splane = cam.dist.x / Math.sqrt(cam.dist.x * cam.dist.x + cam.dist.z * cam.dist.z);
  71. cam.ang.ctheta = Math.sqrt(cam.dist.x * cam.dist.x + cam.dist.z * cam.dist.z) / Math.sqrt(cam.dist.x * cam.dist.x + cam.dist.y * cam.dist.y + cam.dist.z * cam.dist.z);
  72. cam.ang.stheta = -cam.dist.y / Math.sqrt(cam.dist.x * cam.dist.x + cam.dist.y * cam.dist.y + cam.dist.z * cam.dist.z);
  73. }
  74. };
  75. var trans = {
  76. parts: {
  77. sz: function(p, sz) {
  78. return {
  79. x: p.x * sz.x,
  80. y: p.y * sz.y,
  81. z: p.z * sz.z
  82. };
  83. },
  84. rot: {
  85. x: function(p, rot) {
  86. return {
  87. x: p.x,
  88. y: p.y * Math.cos(dtr(rot.x)) - p.z * Math.sin(dtr(rot.x)),
  89. z: p.y * Math.sin(dtr(rot.x)) + p.z * Math.cos(dtr(rot.x))
  90. };
  91. },
  92. y: function(p, rot) {
  93. return {
  94. x: p.x * Math.cos(dtr(rot.y)) + p.z * Math.sin(dtr(rot.y)),
  95. y: p.y,
  96. z: -p.x * Math.sin(dtr(rot.y)) + p.z * Math.cos(dtr(rot.y))
  97. };
  98. },
  99. z: function(p, rot) {
  100. return {
  101. x: p.x * Math.cos(dtr(rot.z)) - p.y * Math.sin(dtr(rot.z)),
  102. y: p.x * Math.sin(dtr(rot.z)) + p.y * Math.cos(dtr(rot.z)),
  103. z: p.z
  104. };
  105. }
  106. },
  107. pos: function(p, pos) {
  108. return {
  109. x: p.x + pos.x,
  110. y: p.y + pos.y,
  111. z: p.z + pos.z
  112. };
  113. }
  114. },
  115. pov: {
  116. plane: function(p) {
  117. return {
  118. x: p.x * cam.ang.cplane + p.z * cam.ang.splane,
  119. y: p.y,
  120. z: p.x * -cam.ang.splane + p.z * cam.ang.cplane
  121. };
  122. },
  123. theta: function(p) {
  124. return {
  125. x: p.x,
  126. y: p.y * cam.ang.ctheta - p.z * cam.ang.stheta,
  127. z: p.y * cam.ang.stheta + p.z * cam.ang.ctheta
  128. };
  129. },
  130. set: function(p) {
  131. return {
  132. x: p.x - cam.obj.x,
  133. y: p.y - cam.obj.y,
  134. z: p.z - cam.obj.z
  135. };
  136. }
  137. },
  138. persp: function(p) {
  139. return {
  140. x: p.x * cam.dist.z / p.z * cam.zoom,
  141. y: p.y * cam.dist.z / p.z * cam.zoom,
  142. z: p.z * cam.zoom,
  143. p: cam.dist.z / p.z
  144. };
  145. },
  146. disp: function(p, disp) {
  147. return {
  148. x: p.x + disp.x,
  149. y: -p.y + disp.y,
  150. z: p.z + disp.z,
  151. p: p.p
  152. };
  153. },
  154. steps: function(_obj_, sz, rot, pos, disp) {
  155. var _args = trans.parts.sz(_obj_, sz);
  156. _args = trans.parts.rot.x(_args, rot);
  157. _args = trans.parts.rot.y(_args, rot);
  158. _args = trans.parts.rot.z(_args, rot);
  159. _args = trans.parts.pos(_args, pos);
  160. _args = trans.pov.plane(_args);
  161. _args = trans.pov.theta(_args);
  162. _args = trans.pov.set(_args);
  163. _args = trans.persp(_args);
  164. _args = trans.disp(_args, disp);
  165. return _args;
  166. }
  167. };
  168. (function() {
  169. "use strict";
  170. var threeD = function(param) {
  171. this.transIn = {};
  172. this.transOut = {};
  173. this.transIn.vtx = (param.vtx);
  174. this.transIn.sz = (param.sz);
  175. this.transIn.rot = (param.rot);
  176. this.transIn.pos = (param.pos);
  177. };
  178. threeD.prototype.vupd = function() {
  179. this.transOut = trans.steps(
  180. this.transIn.vtx,
  181. this.transIn.sz,
  182. this.transIn.rot,
  183. this.transIn.pos,
  184. cam.disp
  185. );
  186. };
  187. var Build = function() {
  188. this.vel = 0.04;
  189. this.lim = 360;
  190. this.diff = 200;
  191. this.initPos = 100;
  192. this.toX = _x;
  193. this.toY = _y;
  194. this.go();
  195. };
  196. Build.prototype.go = function() {
  197. this.canvas = document.getElementById("canv");
  198. this.canvas.width = window.innerWidth;
  199. this.canvas.height = window.innerHeight;
  200. this.$ = canv.getContext("2d");
  201. this.$.globalCompositeOperation = 'source-over';
  202. this.varr = [];
  203. this.dist = [];
  204. this.calc = [];
  205. for (var i = 0, len = num; i < len; i++) {
  206. this.add();
  207. }
  208. this.rotObj = {
  209. x: 0,
  210. y: 0,
  211. z: 0
  212. };
  213. this.objSz = {
  214. x: w / 5,
  215. y: h / 5,
  216. z: w / 5
  217. };
  218. };
  219. Build.prototype.add = function() {
  220. this.varr.push(new threeD({
  221. vtx: {
  222. x: rnd(),
  223. y: rnd(),
  224. z: rnd()
  225. },
  226. sz: {
  227. x: 0,
  228. y: 0,
  229. z: 0
  230. },
  231. rot: {
  232. x: 20,
  233. y: -20,
  234. z: 0
  235. },
  236. pos: {
  237. x: this.diff * Math.sin(360 * Math.random() * Math.PI / 180),
  238. y: this.diff * Math.sin(360 * Math.random() * Math.PI / 180),
  239. z: this.diff * Math.sin(360 * Math.random() * Math.PI / 180)
  240. }
  241. }));
  242. this.calc.push({
  243. x: 360 * Math.random(),
  244. y: 360 * Math.random(),
  245. z: 360 * Math.random()
  246. });
  247. };
  248. Build.prototype.upd = function() {
  249. cam.obj.x += (this.toX - cam.obj.x) * 0.05;
  250. cam.obj.y += (this.toY - cam.obj.y) * 0.05;
  251. };
  252. Build.prototype.draw = function() {
  253. this.$.clearRect(0, 0, this.canvas.width, this.canvas.height);
  254. cam.upd();
  255. this.rotObj.x += 0.1;
  256. this.rotObj.y += 0.1;
  257. this.rotObj.z += 0.1;
  258. for (var i = 0; i < this.varr.length; i++) {
  259. for (var val in this.calc[i]) {
  260. if (this.calc[i].hasOwnProperty(val)) {
  261. this.calc[i][val] += this.vel;
  262. if (this.calc[i][val] > this.lim) this.calc[i][val] = 0;
  263. }
  264. }
  265. this.varr[i].transIn.pos = {
  266. x: this.diff * Math.cos(this.calc[i].x * Math.PI / 180),
  267. y: this.diff * Math.sin(this.calc[i].y * Math.PI / 180),
  268. z: this.diff * Math.sin(this.calc[i].z * Math.PI / 180)
  269. };
  270. this.varr[i].transIn.rot = this.rotObj;
  271. this.varr[i].transIn.sz = this.objSz;
  272. this.varr[i].vupd();
  273. if (this.varr[i].transOut.p < 0) continue;
  274. var g = this.$.createRadialGradient(this.varr[i].transOut.x, this.varr[i].transOut.y, this.varr[i].transOut.p, this.varr[i].transOut.x, this.varr[i].transOut.y, this.varr[i].transOut.p * 2);
  275. this.$.globalCompositeOperation = 'lighter';
  276. g.addColorStop(0, 'hsla(255, 255%, 255%, 1)');
  277. g.addColorStop(.5, 'hsla(' + (i + 2) + ',85%, 40%,1)');
  278. g.addColorStop(1, 'hsla(' + (i) + ',85%, 40%,.5)');
  279. this.$.fillStyle = g;
  280. this.$.beginPath();
  281. this.$.arc(this.varr[i].transOut.x, this.varr[i].transOut.y, this.varr[i].transOut.p * 2, 0, Math.PI * 2, false);
  282. this.$.fill();
  283. this.$.closePath();
  284. }
  285. };
  286. Build.prototype.anim = function() {
  287. window.requestAnimationFrame = (function() {
  288. return window.requestAnimationFrame ||
  289. function(callback, element) {
  290. window.setTimeout(callback, 1000 / 60);
  291. };
  292. })();
  293. var anim = function() {
  294. this.upd();
  295. this.draw();
  296. window.requestAnimationFrame(anim);
  297. }.bind(this);
  298. window.requestAnimationFrame(anim);
  299. };
  300. Build.prototype.run = function() {
  301. this.anim();
  302. window.addEventListener('mousemove', function(e) {
  303. this.toX = (e.clientX - this.canvas.width / 2) * -0.8;
  304. this.toY = (e.clientY - this.canvas.height / 2) * 0.8;
  305. }.bind(this));
  306. window.addEventListener('touchmove', function(e) {
  307. e.preventDefault();
  308. this.toX = (e.touches[0].clientX - this.canvas.width / 2) * -0.8;
  309. this.toY = (e.touches[0].clientY - this.canvas.height / 2) * 0.8;
  310. }.bind(this));
  311. window.addEventListener('mousedown', function(e) {
  312. for (var i = 0; i < 100; i++) {
  313. this.add();
  314. }
  315. }.bind(this));
  316. window.addEventListener('touchstart', function(e) {
  317. e.preventDefault();
  318. for (var i = 0; i < 100; i++) {
  319. this.add();
  320. }
  321. }.bind(this));
  322. };
  323. var app = new Build();
  324. app.run();
  325. })();
  326. window.addEventListener('resize', function() {
  327. canvas.width = w = window.innerWidth;
  328. canvas.height = h = window.innerHeight;
  329. }, false);
  330. </script>
  331. </body></html>