index.html 8.9 KB

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