background.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. function changeColors(diffAmount) {
  54. let prevPaletteIndex = (prevMode + (prevMode - 1)); // cf commentaire palette à la fin
  55. let baseColors = [];
  56. baseColors.push(
  57. colorPalette[prevPaletteIndex - 1],
  58. colorPalette[prevPaletteIndex],
  59. colorPalette[prevPaletteIndex + 1] ? colorPalette[prevPaletteIndex + 1] : colorPalette[0]
  60. );
  61. let paletteIndex = (curentMode + (curentMode - 1));
  62. let targetColors = [];
  63. targetColors.push(
  64. colorPalette[paletteIndex - 1],
  65. colorPalette[paletteIndex],
  66. colorPalette[paletteIndex + 1 ] ? colorPalette[paletteIndex + 1] : colorPalette[0]
  67. );
  68. function hexToRgba(color) {
  69. let colorRgba = {
  70. r : parseInt(color.substr(0, 2), 16),
  71. g : parseInt(color.substr(2, 2), 16),
  72. b : parseInt(color.substr(4, 2), 16),
  73. a : parseInt(color.substr(6, 2), 16)
  74. }
  75. return colorRgba;
  76. }
  77. function rgbaToHex(color) {
  78. let colorHexa = {
  79. r : color.r.toString(16).length != 2 ? 0 + color.r.toString(16) : color.r.toString(16),
  80. g : color.g.toString(16).length != 2 ? 0 + color.g.toString(16) : color.g.toString(16),
  81. b : color.b.toString(16).length != 2 ? 0 + color.b.toString(16) : color.b.toString(16),
  82. a : color.a.toString(16).length != 2 ? 0 + color.a.toString(16) : color.a.toString(16)
  83. }
  84. return colorHexa.r + colorHexa.g + colorHexa.b + colorHexa.a;
  85. }
  86. let steps = 10, // vitesse de la transition
  87. currentFrame = 0;
  88. function animate() {
  89. currentFrame++;
  90. // à chaque frame on vide currentColors
  91. // puis pusher trois couleurs hex de transition
  92. // lorsque le nombre de steps est atteint envoyer les couleurs def
  93. // et ne pas réanimer
  94. currentColors = [];
  95. for (i = 0; i < baseColors.length; i++) {
  96. if (currentFrame == steps) {
  97. currentColors.push(targetColors[i])
  98. } else {
  99. currentColors.push(getTransitionColor(baseColors[i], targetColors[i], currentFrame));
  100. }
  101. }
  102. if (steps == 0) transitionLaunched++;
  103. if (currentFrame != steps) {
  104. window.requestAnimationFrame(() => animate());
  105. } else {
  106. currentFrame = 0;
  107. }
  108. }
  109. animate()
  110. function getTransitionColor(baseColor, targetColor, currentFrame) {
  111. let rgbaBase = hexToRgba(baseColor),
  112. rgbaTarget = hexToRgba(targetColor),
  113. diff = {
  114. r : (rgbaBase.r - rgbaTarget.r) * -1,
  115. g : (rgbaBase.g - rgbaTarget.g) * -1,
  116. b : (rgbaBase.b - rgbaTarget.b) * -1,
  117. a : (rgbaBase.a - rgbaTarget.a) * -1
  118. },
  119. transitionColor = {
  120. r : Math.floor(rgbaBase.r + (diff.r / steps) * currentFrame),
  121. g : Math.floor(rgbaBase.g + (diff.g / steps) * currentFrame),
  122. b : Math.floor(rgbaBase.b + (diff.b / steps) * currentFrame),
  123. a : Math.floor(rgbaBase.a + (diff.a / steps) * currentFrame)
  124. };
  125. transitionColor = rgbaToHex(transitionColor);
  126. return transitionColor;
  127. }
  128. }
  129. // COULEURS DE FOND
  130. // basé sur https://codepen.io/ThreePixDroid/pen/MWeomWp
  131. // ajouter un grain fixe : cf
  132. // https://codepen.io/zadvorsky/pen/PwyoMm
  133. let colorPalette = [
  134. "ff6a0399",
  135. "c9c4ffff",
  136. "0796908c",
  137. "e6ffc4ff",
  138. "e105054d",
  139. "aaffa2ff",
  140. "18b8e64d",
  141. "ffc4f9ff",
  142. "9692074d",
  143. "ffe7c4ff",
  144. "0629ad4d",
  145. "fffdbaff"
  146. ]
  147. let currentColors = [];
  148. currentColors.push(
  149. colorPalette[0],
  150. colorPalette[1],
  151. colorPalette[2],
  152. );
  153. class GradientAnimation {
  154. constructor() {
  155. this.cnv = document.querySelector('canvas');
  156. this.ctx = this.cnv.getContext('2d');
  157. // réglages du fond
  158. this.radius = 1800;
  159. this.speed = 0.01;
  160. // variables du grain
  161. this.patternSize = 512;
  162. this.patternAlpha = 10; // int between 0 and 255,
  163. this.patternPixelDataLength = this.patternSize * this.patternSize * 4;
  164. this.patternCanvas;
  165. this.patternCtx;
  166. this.patternData;
  167. this.frame = 0;
  168. (window.onresize = () => {
  169. this.setCanvasSize();
  170. this.createCircles();
  171. })();
  172. this.initGrain();
  173. this.drawAnimation();
  174. }
  175. setCanvasSize() {
  176. this.w = this.cnv.width = innerWidth * devicePixelRatio;
  177. this.h = this.cnv.height = innerHeight * devicePixelRatio;
  178. this.ctx.scale(devicePixelRatio, devicePixelRatio);
  179. }
  180. createCircles() {
  181. this.circles = [];
  182. // positionnement des cercles directement ici
  183. this.circles.push(new Circle(this.w, this.h, this.radius, 30, 30, 0));
  184. this.circles.push(new Circle(this.w, this.h, this.radius, window.innerWidth / 2, window.innerHeight, 1));
  185. this.circles.push(new Circle(this.w, this.h, this.radius, window.innerWidth - 30, 30, 2));
  186. }
  187. drawCircles() {
  188. this.circles.forEach(circle => circle.draw(this.ctx, this.speed));
  189. }
  190. drawBackground() {
  191. this.ctx.fillStyle = "#aaaaaa";
  192. this.ctx.fillRect(0, 0, this.w, this.h);
  193. }
  194. initGrain() {
  195. let patternSize = 128; // ça marche pas this.patternSize
  196. this.patternCanvas = document.createElement('canvas');
  197. this.patternCanvas.width = patternSize;
  198. this.patternCanvas.height = patternSize;
  199. this.patternCtx = this.patternCanvas.getContext('2d');
  200. this.patternData = this.patternCtx.createImageData(patternSize, patternSize);
  201. let value;
  202. for (let i = 0; i < this.patternPixelDataLength; i += 4) {
  203. value = (Math.random() * 255) | 0;
  204. this.patternData.data[i ] = value;
  205. this.patternData.data[i + 1] = value;
  206. this.patternData.data[i + 2] = value;
  207. this.patternData.data[i + 3] = this.patternAlpha;
  208. }
  209. this.patternCtx.putImageData(this.patternData, 0, 0);
  210. }
  211. drawAnimation() {
  212. this.drawBackground();
  213. this.drawCircles();
  214. this.ctx.fillStyle = this.ctx.createPattern(this.patternCanvas, 'repeat');
  215. this.ctx.fillRect(0, 0, window.innerWidth, window.innerHeight);
  216. window.requestAnimationFrame(() => this.drawAnimation());
  217. }
  218. }
  219. class Circle {
  220. constructor(w, h, radius, posX, posY, index) {
  221. this.x = posX;
  222. this.y = posY;
  223. this.angle = Math.random() * Math.PI * 2;
  224. this.radius = radius;
  225. this.index = index;
  226. this.firstColor = "#" + currentColors[index];
  227. this.secondColor = "#ffffff00";
  228. }
  229. draw(ctx, speed) {
  230. this.angle += speed;
  231. const x = this.x + Math.cos(this.angle) * 200;
  232. const y = this.y + Math.sin(this.angle) * 200;
  233. const gradient = ctx.createRadialGradient(x, y, 0, x, y, this.radius);
  234. gradient.addColorStop(0, "#" + currentColors[this.index]);
  235. gradient.addColorStop(1, this.secondColor);
  236. ctx.fillStyle = gradient;
  237. ctx.beginPath();
  238. ctx.arc(x, y, this.radius, 0, Math.PI * 2);
  239. ctx.fill();
  240. }
  241. }
  242. window.onload = () => {
  243. new GradientAnimation();
  244. }
  245. /* palette :
  246. multiply vers blanc
  247. 0 #ff6a03 45%
  248. 1 TERRAIN DE VIE
  249. 1 #c9c4ff 100%
  250. 2 #079690 30%
  251. 2 PROXIMITE
  252. 3 #e6ffc4 100%
  253. 4 #e10505 30%
  254. 3 SUPPERPOSITION
  255. 5 #aaffa2 100%
  256. 6 #18b8e6 30%
  257. 4 PUISSANCE
  258. 7 #ffc4f9 100%
  259. 8 #969207 30%
  260. 5 ACTION
  261. 9 #ffe7c4 100%
  262. 10 #0629ad 30%
  263. 6 DOLEANCER
  264. 11 #fffdba 100% */
  265. // 1 1, 2 3, 3 5, 4 7, 5 9, 6 11
  266. // curentMode + (curentMode - 1)