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

137 lines
3.5 KiB

  1. import CommonModule from './common_module.js';
  2. class
  3. Talk
  4. {
  5. static
  6. main ()
  7. {
  8. const canvas = new Canvas;
  9. window.onresize = () => canvas.resize ();
  10. }
  11. }
  12. class
  13. Canvas
  14. {
  15. constructor ()
  16. {
  17. this.canvas = $ ('#canvas');
  18. this.ctx = this.canvas[0].getContext ('2d');
  19. (this.bg = new Image ()).src = './assets/bg.jpg';
  20. this.bg.onload = () => this.resize ();
  21. (this.nizika = new Image ()).src = './assets/nizika.png';
  22. this.nizika.onload = () => this.resize ();
  23. (this.talking = new Image ()).src = './assets/talking.png';
  24. this.talking.onload = () => this.resize ();
  25. (new FontFace ('Nikumaru', 'url(./assets/nikumaru.otf)')).load ().then (
  26. () => this.resize ());
  27. }
  28. resize ()
  29. {
  30. this.canvas[0].width = $ (window).width ();
  31. this.canvas[0].height = $ (window).height ();
  32. this.redraw ();
  33. }
  34. redraw ()
  35. {
  36. this.putBG ();
  37. this.putText (0, 0, 15, $ ('#dt').val ());
  38. this.putImage (this.nizika, 370, 260, 1.1);
  39. this.putImage (this.talking, 0, 0, 640 / 1024);
  40. this.putText (75, 43.75, 20,
  41. ('> ' + ((CommonModule.lenByFull ($ ('#chat').val ()) <= 21)
  42. ? $ ('#chat').val ()
  43. : (CommonModule.midByFull ($ ('#chat').val (), 0, 19.5)
  44. + '...'))),
  45. undefined, undefined,
  46. true);
  47. this.putText (62.5, 93.75, 31.25,
  48. ((CommonModule.lenByFull ($ ('#answer').val ()) <= 16)
  49. ? $ ('#answer').val ()
  50. : CommonModule.midByFull ($ ('#answer').val (), 0, 16)),
  51. 'Nikumaru', '#c00000');
  52. if (CommonModule.lenByFull ($ ('#answer').val ()) > 16)
  53. {
  54. this.putText (62.5, 125, 31.25,
  55. ((CommonModule.lenByFull ($ ('#answer').val ()) <= 32)
  56. ? CommonModule.midByFull ($ ('#answer').val (), 16, 16)
  57. : (CommonModule.midByFull ($ ('#answer').val (), 16, 14.5)
  58. + '...')),
  59. 'Nikumaru', '#c00000');
  60. }
  61. }
  62. putBG ()
  63. {
  64. const [x, y, zoom] = this.convertPosition (0, 0);
  65. const width = this.bg.height * 4 / 3;
  66. const height = this.bg.height;
  67. this.ctx.drawImage (this.bg,
  68. (852 - 640) / 2, 0, width, height,
  69. x, y, width * zoom, height * zoom);
  70. }
  71. putImage (image, x, y, zoom = 1)
  72. {
  73. let zoom2
  74. [x, y, zoom2] = this.convertPosition (x, y);
  75. zoom *= zoom2
  76. this.ctx.drawImage (image,
  77. 0, 0, image.width, image.height,
  78. x, y, image.width * zoom, image.height * zoom);
  79. }
  80. putText (x, y, size, text, style = 'sans-serif', colour = 'black',
  81. italic = false)
  82. {
  83. let zoom;
  84. [x, y, zoom] = this.convertPosition (x, y);
  85. size *= zoom;
  86. this.ctx.font = `${italic ? 'italic ' : ''}${Math.trunc (size)}px ${style}`;
  87. this.ctx.fillStyle = colour;
  88. this.ctx.textBaseline = 'top';
  89. this.ctx.fillText (text, x, y);
  90. }
  91. convertPosition (x, y)
  92. {
  93. const sizeX = this.canvas.width ();
  94. const sizeY = this.canvas.height ();
  95. const width = this.bg.height * 4 / 3;
  96. const height = this.bg.height;
  97. const vertical = sizeY / sizeX > height / width;
  98. const zoom = vertical ? (sizeX / width) : (sizeY / height);
  99. const baseX = vertical ? 0 : ((sizeX - width * zoom) / 2);
  100. const baseY = vertical ? ((sizeY - height * zoom) / 2) : 0;
  101. return [baseX + x * zoom, baseY + y * zoom, zoom];
  102. }
  103. }
  104. $ (() => Talk.main ());