ニジカもんすたぁ!! トップ・ページ https://nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.js 2.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const canvas = document.getElementById ('canvas');
  2. const ctx = canvas.getContext ('2d');
  3. const bg = (function ()
  4. {
  5. const _ = document.createElement('canvas');
  6. const img = new Image ();
  7. _.width = 1920;
  8. _.height = 1080;
  9. const ctx = _.getContext ('2d');
  10. img.onload = (function ()
  11. {
  12. ctx.drawImage (img, 0, 0);
  13. });
  14. img.src = './assets/bg.jpg';
  15. return _;
  16. }) ();
  17. const nizikaList = Array (24).fill ().map (function (__, i)
  18. {
  19. const _ = document.createElement('canvas');
  20. const img = new Image ();
  21. _.width = 300;
  22. _.height = 300;
  23. const ctx = _.getContext ('2d');
  24. img.onload = (function ()
  25. {
  26. ctx.drawImage (img, 0, 0);
  27. replaceColorWithTransparent (_);
  28. });
  29. img.src = `./assets/nizika/${String (i + 1).padStart (3, '0')}.png`;
  30. return _;
  31. });
  32. const kitaList = Array (40).fill ().map (function (__, i)
  33. {
  34. const _ = document.createElement('canvas');
  35. const img = new Image ();
  36. _.width = 428;
  37. _.height = 240;
  38. const ctx = _.getContext ('2d');
  39. img.onload = (function ()
  40. {
  41. ctx.drawImage (img, 0, 0);
  42. });
  43. img.src = `./assets/kita/${String (i + 41).padStart (3, '0')}.png`;
  44. return _;
  45. });
  46. let canvasX;
  47. let canvasY;
  48. let frame = 0;
  49. let nizikaData = [];
  50. preprocess ();
  51. window.setInterval (() => reDraw (), 16);
  52. function
  53. preprocess ()
  54. {
  55. reDraw ();
  56. }
  57. function
  58. reDraw ()
  59. {
  60. canvasX = window.innerWidth;
  61. canvasY = window.innerHeight;
  62. canvas.width = canvasX;
  63. canvas.height = canvasY;
  64. ctx.drawImage (bg, 0, 0, canvasX, canvasY);
  65. nizikaData.push ({x: Math.random () * canvasX - 150,
  66. y: Math.random () * canvasY - 150});
  67. if (nizikaData.length > 100)
  68. nizikaData.shift ();
  69. nizikaData.forEach (function (nizika)
  70. {
  71. ctx.drawImage (nizikaList[frame % 24], nizika.x, nizika.y);
  72. });
  73. ++frame;
  74. }
  75. function
  76. replaceColorWithTransparent (canvas)
  77. {
  78. const context = canvas.getContext ('2d');
  79. const imageData = context.getImageData (0, 0, canvas.width, canvas.height);
  80. const pixels = imageData.data;
  81. for (let i = 0; i < pixels.length; i += 4)
  82. {
  83. const r = pixels[i];
  84. const g = pixels[i + 1];
  85. const b = pixels[i + 2];
  86. if (r < 64 && g >= 96 && b < 96)
  87. pixels[i + 3] = 0;
  88. }
  89. context.putImageData (imageData, 0, 0);
  90. }