ニジカもんすたぁ!! トップ・ページ 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.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. const runawayGuitar = new Audio ('./assets/kita.wav');
  47. let canvasX;
  48. let canvasY;
  49. let frame = 0;
  50. let nizikaData = [];
  51. preprocess ();
  52. window.setInterval (() => reDraw (), 16);
  53. function
  54. preprocess ()
  55. {
  56. canvas.addEventListener ('click', function ()
  57. {
  58. runawayGuitar.play ();
  59. });
  60. reDraw ();
  61. }
  62. function
  63. reDraw ()
  64. {
  65. canvasX = window.innerWidth;
  66. canvasY = window.innerHeight;
  67. canvas.width = canvasX;
  68. canvas.height = canvasY;
  69. ctx.drawImage (bg, 0, 0, canvasX, canvasY);
  70. nizikaData.push ({x: Math.random () * canvasX - 150,
  71. y: Math.random () * canvasY - 150});
  72. if (nizikaData.length > 100)
  73. nizikaData.shift ();
  74. nizikaData.forEach (function (nizika)
  75. {
  76. ctx.drawImage (nizikaList[frame % 24], nizika.x, nizika.y);
  77. });
  78. ++frame;
  79. }
  80. function
  81. replaceColorWithTransparent (canvas)
  82. {
  83. const context = canvas.getContext ('2d');
  84. const imageData = context.getImageData (0, 0, canvas.width, canvas.height);
  85. const pixels = imageData.data;
  86. for (let i = 0; i < pixels.length; i += 4)
  87. {
  88. const r = pixels[i];
  89. const g = pixels[i + 1];
  90. const b = pixels[i + 2];
  91. if (r < 64 && g >= 96 && b < 96)
  92. pixels[i + 3] = 0;
  93. }
  94. context.putImageData (imageData, 0, 0);
  95. }