地铁二期项目正式开始

canvas01.js 11KB

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