background.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // mode : terrain de vie, proximité, superposition....
  2. // profondeur : atlas, carte
  3. let modeAmount = 0,
  4. curentMode = 1, prevMode,
  5. currentProfondeur = "atlas",
  6. colors = {
  7. bg: {
  8. h: 29,
  9. s: 48,
  10. l: 48
  11. }
  12. };
  13. document.querySelectorAll('.mode').forEach(function(node, index) {
  14. modeAmount++;
  15. node.addEventListener('click', function() {
  16. document.querySelectorAll('.active').forEach(function(el) {
  17. el.classList.remove('active');
  18. if (currentProfondeur == "carte") { el.classList.add('collapsed'); }
  19. });
  20. node.classList.toggle('active');
  21. prevMode = curentMode;
  22. curentMode = index + 1;
  23. if (curentMode != prevMode) {
  24. changeColors(curentMode - prevMode);
  25. }
  26. })
  27. });
  28. function toggleProfondeur(el) {
  29. if (el.classList.contains("selected")) {
  30. return;
  31. } else {
  32. for (let sibling of el.parentNode.children) {
  33. sibling.classList.remove('selected');
  34. }
  35. }
  36. el.classList.add("selected");
  37. if(el.innerHTML == "L'atlas") {
  38. currentProfondeur = "atlas";
  39. document.querySelectorAll('.mode').forEach(function(node) {
  40. if(node.classList.contains("collapsed")) {
  41. node.classList.remove("collapsed");
  42. }
  43. });
  44. } else {
  45. currentProfondeur = "carte";
  46. document.querySelectorAll('.mode').forEach(function(node) {
  47. if(!node.classList.contains("active")) {
  48. node.classList.add("collapsed");
  49. }
  50. });
  51. }
  52. }
  53. // onclick sur les flèches du menu
  54. function toggleMode(direction) {
  55. let modes = [];
  56. let currentIndex;
  57. document.querySelectorAll('.mode').forEach(function(node, index) {
  58. modes.push(node);
  59. if (node.classList.contains("active")) {
  60. currentIndex = index;
  61. node.classList.remove("active");
  62. if (currentProfondeur == "carte") {
  63. node.classList.add("collapsed");
  64. }
  65. }
  66. });
  67. if (direction == "next" && currentIndex + 2 <= modeAmount) {
  68. modes[currentIndex + 1].classList.add('active');
  69. curentMode = curentMode + 1;
  70. changeColors(1);
  71. } else if (direction == "prev" && currentIndex - 1 >= 0) {
  72. modes[currentIndex - 1].classList.add('active');
  73. curentMode = curentMode - 1;
  74. changeColors(-1);
  75. } else if (direction == "next" && currentIndex + 1 == modeAmount) {
  76. modes[0].classList.add('active');
  77. curentMode = 1;
  78. changeColors(-modeAmount + 1);
  79. } else if (direction == "prev" && currentIndex == 0) {
  80. modes[modeAmount - 1].classList.add('active');
  81. curentMode = modeAmount;
  82. changeColors(modeAmount - 1);
  83. }
  84. }
  85. function changeColors(diffAmount) {
  86. let prevPaletteIndex = (prevMode + (prevMode - 1));
  87. let baseColors = [];
  88. baseColors.push(colorPalette[prevPaletteIndex - 1]);
  89. baseColors.push(colorPalette[prevPaletteIndex]);
  90. baseColors.push(colorPalette[prevPaletteIndex + 1] ? colorPalette[prevPaletteIndex + 1] : colorPalette[0]);
  91. let paletteIndex = (curentMode + (curentMode - 1));
  92. let targetColors = [];
  93. targetColors.push(colorPalette[paletteIndex - 1]);
  94. targetColors.push(colorPalette[paletteIndex]);
  95. targetColors.push(colorPalette[paletteIndex + 1 ] ? colorPalette[paletteIndex + 1] : colorPalette[0]);
  96. function hexToRgba(color) {
  97. let colorRgba = {
  98. r : parseInt(color.substr(0, 2), 16),
  99. g : parseInt(color.substr(2, 2), 16),
  100. b : parseInt(color.substr(4, 2), 16),
  101. a : parseInt(color.substr(6, 2), 16)
  102. }
  103. return colorRgba;
  104. }
  105. function rgbaToHex(color) {
  106. let colorHexa = {
  107. r : color.r.toString(16).length != 2 ? 0 + color.r.toString(16) : color.r.toString(16),
  108. g : color.g.toString(16).length != 2 ? 0 + color.g.toString(16) : color.g.toString(16),
  109. b : color.b.toString(16).length != 2 ? 0 + color.b.toString(16) : color.b.toString(16),
  110. a : color.a.toString(16).length != 2 ? 0 + color.a.toString(16) : color.a.toString(16)
  111. }
  112. return colorHexa.r + colorHexa.g + colorHexa.b + colorHexa.a;
  113. }
  114. let steps = 10, // vitesse de la transition
  115. currentFrame = 0;
  116. function animate() {
  117. currentFrame++;
  118. // à chaque frame on vide currentColors
  119. // puis pusher trois couleurs hex de transition
  120. // lorsque le nombre de steps est atteint envoyer les couleurs def
  121. // et ne pas réanimer
  122. currentColors = [];
  123. for (i = 0; i < baseColors.length; i++) {
  124. if (currentFrame == steps) {
  125. currentColors.push(targetColors[i])
  126. } else {
  127. currentColors.push(getTransitionColor(baseColors[i], targetColors[i], currentFrame));
  128. }
  129. }
  130. if (steps == 0) transitionLaunched++;
  131. if (currentFrame != steps) {
  132. window.requestAnimationFrame(() => animate());
  133. } else {
  134. currentFrame = 0;
  135. }
  136. }
  137. animate()
  138. function getTransitionColor(baseColor, targetColor, currentFrame) {
  139. console.log(baseColor, targetColor, currentFrame)
  140. let rgbaBase = hexToRgba(baseColor),
  141. rgbaTarget = hexToRgba(targetColor),
  142. diff = {
  143. r : (rgbaBase.r - rgbaTarget.r) * -1,
  144. g : (rgbaBase.g - rgbaTarget.g) * -1,
  145. b : (rgbaBase.b - rgbaTarget.b) * -1,
  146. a : (rgbaBase.a - rgbaTarget.a) * -1
  147. },
  148. transitionColor = {
  149. r : Math.floor(rgbaBase.r + (diff.r / steps) * currentFrame),
  150. g : Math.floor(rgbaBase.g + (diff.g / steps) * currentFrame),
  151. b : Math.floor(rgbaBase.b + (diff.b / steps) * currentFrame),
  152. a : Math.floor(rgbaBase.a + (diff.a / steps) * currentFrame)
  153. };
  154. transitionColor = rgbaToHex(transitionColor);
  155. return transitionColor;
  156. }
  157. }
  158. // COULEURS DE FOND
  159. // basé sur https://codepen.io/ThreePixDroid/pen/MWeomWp
  160. // ajouter un grain fixe : cf
  161. // https://codepen.io/zadvorsky/pen/PwyoMm
  162. let colorPalette = [
  163. "ff6a0399",
  164. "c9c4ffff",
  165. "0796908c",
  166. "e6ffc4ff",
  167. "e105054d",
  168. "aaffa2ff",
  169. "18b8e64d",
  170. "ffc4f9ff",
  171. "9692074d",
  172. "ffe7c4ff",
  173. "0629ad4d",
  174. "fffdbaff"
  175. ]
  176. let currentColors = [];
  177. currentColors.push(
  178. colorPalette[0],
  179. colorPalette[1],
  180. colorPalette[2],
  181. );
  182. class GradientAnimation {
  183. constructor() {
  184. this.cnv = document.querySelector('canvas');
  185. this.ctx = this.cnv.getContext('2d');
  186. // réglages du fond
  187. this.radius = 1800;
  188. this.speed = 0.001;
  189. (window.onresize = () => {
  190. this.setCanvasSize();
  191. this.createCircles();
  192. })();
  193. this.drawAnimation();
  194. }
  195. setCanvasSize() {
  196. this.w = this.cnv.width = innerWidth * devicePixelRatio;
  197. this.h = this.cnv.height = innerHeight * devicePixelRatio;
  198. this.ctx.scale(devicePixelRatio, devicePixelRatio);
  199. }
  200. createCircles() {
  201. this.circles = [];
  202. // positionnement des cercles directement ici
  203. this.circles.push(new Circle(this.w, this.h, this.radius, 30, 30, 0));
  204. this.circles.push(new Circle(this.w, this.h, this.radius, window.innerWidth / 2, window.innerHeight, 1));
  205. this.circles.push(new Circle(this.w, this.h, this.radius, window.innerWidth - 30, 30, 2));
  206. }
  207. drawCircles() {
  208. this.circles.forEach(circle => circle.draw(this.ctx, this.speed));
  209. }
  210. drawBackground() {
  211. this.ctx.fillStyle = "#aaaaaa";
  212. this.ctx.fillRect(0, 0, this.w, this.h);
  213. }
  214. drawAnimation() {
  215. this.drawBackground();
  216. this.drawCircles();
  217. window.requestAnimationFrame(() => this.drawAnimation());
  218. }
  219. }
  220. class Circle {
  221. constructor(w, h, radius, posX, posY, index) {
  222. this.x = posX;
  223. this.y = posY;
  224. this.angle = Math.random() * Math.PI * 2;
  225. this.radius = radius;
  226. this.index = index;
  227. this.firstColor = "#" + currentColors[index];
  228. this.secondColor = "#ffffff00";
  229. }
  230. draw(ctx, speed) {
  231. this.angle += speed;
  232. const x = this.x + Math.cos(this.angle) * 200;
  233. const y = this.y + Math.sin(this.angle) * 200;
  234. const gradient = ctx.createRadialGradient(x, y, 0, x, y, this.radius);
  235. gradient.addColorStop(0, "#" + currentColors[this.index]);
  236. gradient.addColorStop(1, this.secondColor);
  237. ctx.globalCompositionOperation = "multiply"; // ça marche pas
  238. ctx.fillStyle = gradient;
  239. ctx.beginPath();
  240. ctx.arc(x, y, this.radius, 0, Math.PI * 2);
  241. ctx.fill();
  242. }
  243. }
  244. window.onload = () => {
  245. new GradientAnimation();
  246. }
  247. /* palette :
  248. multiply vers blanc
  249. 0 #ff6a03 45%
  250. 1 TERRAIN DE VIE
  251. 1 #c9c4ff 100%
  252. 2 #079690 30%
  253. 2 PROXIMITE
  254. 3 #e6ffc4 100%
  255. 4 #e10505 30%
  256. 3 SUPPERPOSITION
  257. 5 #aaffa2 100%
  258. 6 #18b8e6 30%
  259. 4 PUISSANCE
  260. 7 #ffc4f9 100%
  261. 8 #969207 30%
  262. 5 ACTION
  263. 9 #ffe7c4 100%
  264. 10 #0629ad 30%
  265. 6 DOLEANCER
  266. 11 #fffdba 100% */
  267. // 1 1, 2 3, 3 5, 4 7, 5 9, 6 11