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

117 lines
2.6 KiB

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