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

206 lines
4.2 KiB

  1. const canvas = document.getElementById ('canvas');
  2. const ctx = canvas.getContext ('2d');
  3. const bg = (
  4. function ()
  5. {
  6. const _ = document.createElement ('canvas');
  7. const img = new Image ();
  8. _.width = 1920;
  9. _.height = 1080;
  10. const ctx = _.getContext ('2d');
  11. img.onload = (
  12. function ()
  13. {
  14. ctx.drawImage (img, 0, 0);
  15. });
  16. img.src = './assets/bg.jpg';
  17. return _;
  18. }) ();
  19. const nizikaList = Array (24).fill ().map (
  20. function (__, i)
  21. {
  22. const _ = document.createElement ('canvas');
  23. const img = new Image ();
  24. _.width = 300;
  25. _.height = 300;
  26. const ctx = _.getContext ('2d');
  27. img.onload = (
  28. function ()
  29. {
  30. ctx.drawImage (img, 0, 0);
  31. replaceColorWithTransparent (_);
  32. });
  33. img.src = `./assets/nizika/${String (i + 1).padStart (3, '0')}.png`;
  34. return _;
  35. });
  36. const kitaList = Array (40).fill ().map (
  37. function (__, i)
  38. {
  39. const _ = document.createElement ('canvas');
  40. const img = new Image ();
  41. _.width = 428;
  42. _.height = 240;
  43. const ctx = _.getContext ('2d');
  44. img.onload = (
  45. function ()
  46. {
  47. ctx.drawImage (img, 0, 0);
  48. });
  49. img.src = `./assets/kita/${String (i + 41).padStart (3, '0')}.png`;
  50. return _;
  51. });
  52. const upperKita = document.createElement ('canvas');
  53. const runawayGuitar = new Audio ('./assets/kita.wav');
  54. let canvasX;
  55. let canvasY;
  56. let frame = 0;
  57. let nizikaData = [];
  58. let kitaData = [];
  59. const nizikaVel = 5;
  60. let runawayFlag = false;
  61. let runawayFrame = 0;
  62. preprocess ();
  63. window.setInterval (() => reDraw (), 16);
  64. function
  65. preprocess ()
  66. {
  67. canvas.addEventListener ('click',
  68. function f (e)
  69. {
  70. this.removeEventListener ('click', f);
  71. kitaData.push ({x: e.clientX,
  72. y: e.clientY});
  73. runawayFlag = true;
  74. runawayFrame = frame;
  75. runawayGuitar.play ();
  76. });
  77. upperKita.width = 428;
  78. upperKita.height = 240;
  79. reDraw ();
  80. }
  81. function
  82. reDraw ()
  83. {
  84. canvasX = window.innerWidth;
  85. canvasY = window.innerHeight;
  86. canvas.width = canvasX;
  87. canvas.height = canvasY;
  88. ctx.drawImage (bg, 0, 0, canvasX, canvasY);
  89. if (!(runawayFlag))
  90. {
  91. nizikaData.push ({x: Math.random () * canvasX,
  92. y: Math.random () * canvasY});
  93. }
  94. if (nizikaData.length > 100)
  95. nizikaData.shift ();
  96. kitaData.forEach (
  97. function (kita)
  98. {
  99. ctx.drawImage (kitaList[frame % 40], kita.x - 214, kita.y - 120);
  100. });
  101. nizikaData.forEach (
  102. function (nizika)
  103. {
  104. ctx.drawImage (nizikaList[frame % 24], nizika.x - 150, nizika.y - 150);
  105. if (runawayFlag)
  106. {
  107. const l = Math.sqrt (Math.pow (kitaData[0].x - nizika.x, 2)
  108. + Math.pow (kitaData[0].y - nizika.y, 2));
  109. const t = Math.atan2 (kitaData[0].y - nizika.y,
  110. kitaData[0].x - nizika.x);
  111. nizika.x += nizikaVel * Math.cos (t);
  112. nizika.y += nizikaVel * Math.sin (t);
  113. }
  114. });
  115. if (runawayFlag)
  116. {
  117. changeAlpha (upperKita, kitaList[frame % 40],
  118. Math.max (runawayFrame - frame + 85, 0) * 3);
  119. ctx.drawImage (upperKita, kitaData[0].x - 214, kitaData[0].y - 120);
  120. }
  121. ++frame;
  122. }
  123. function
  124. replaceColorWithTransparent (canvas)
  125. {
  126. const context = canvas.getContext ('2d');
  127. const imageData = context.getImageData (0, 0, canvas.width, canvas.height);
  128. const pixels = imageData.data;
  129. for (let i = 0; i < pixels.length; i += 4)
  130. {
  131. const r = pixels[i];
  132. const g = pixels[i + 1];
  133. const b = pixels[i + 2];
  134. if (r < 64 && g >= 96 && b < 96)
  135. pixels[i + 3] = 0;
  136. }
  137. context.putImageData (imageData, 0, 0);
  138. }
  139. function
  140. changeAlpha (toCanvas, canvas, alpha)
  141. {
  142. const toCtx = toCanvas.getContext ('2d');
  143. const ctx = canvas.getContext ('2d');
  144. const imageData = ctx.getImageData (0, 0, canvas.width, canvas.height);
  145. const pxs = imageData.data;
  146. for (let i = 0; i < pxs.length; i += 4)
  147. pxs[i + 3] = alpha;
  148. toCtx.putImageData (imageData, 0, 0);
  149. }