DrawHelper.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. export default class DrawHelper {
  2. fontBySize(size) {
  3. return `${size}px ${this.fontName}`;
  4. }
  5. drawPercentBar(x, y, w, h, font, fontSize, bookPos, textLength) {
  6. const pad = 3;
  7. const fh = h - 2*pad;
  8. const fh2 = fh/2;
  9. const t1 = `${Math.floor((bookPos + 1)/1000)}k/${Math.floor(textLength/1000)}k`;
  10. const w1 = this.measureTextFont(t1, font) + fh2;
  11. const read = (bookPos + 1)/textLength;
  12. const t2 = `${(read*100).toFixed(2)}%`;
  13. const w2 = this.measureTextFont(t2, font);
  14. let w3 = w - w1 - w2;
  15. let out = '';
  16. if (w1 + w2 <= w)
  17. out += this.fillTextShift(t1, x, y, font, fontSize);
  18. if (w1 + w2 + w3 <= w && w3 > (10 + fh2)) {
  19. const barWidth = w - w1 - w2 - fh2;
  20. out += this.strokeRect(x + w1, y + pad, barWidth, fh - 2, this.statusBarColor);
  21. out += this.fillRect(x + w1 + 2, y + pad + 2, (barWidth - 4)*read, fh - 6, this.statusBarColor);
  22. }
  23. if (w1 <= w)
  24. out += this.fillTextShift(t2, x + w1 + w3, y, font, fontSize);
  25. return out;
  26. }
  27. drawStatusBar(statusBarTop, statusBarHeight, bookPos, textLength, title) {
  28. let out = `<div class="layout" style="` +
  29. `width: ${this.realWidth}px; height: ${statusBarHeight}px; ` +
  30. `color: ${this.statusBarColor}">`;
  31. const fontSize = statusBarHeight*0.75;
  32. const font = 'bold ' + this.fontBySize(fontSize);
  33. out += this.fillRect(0, (statusBarTop ? statusBarHeight : 0), this.realWidth, 1, this.statusBarColor);
  34. const date = new Date();
  35. const time = `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
  36. const timeW = this.measureTextFont(time, font);
  37. out += this.fillTextShift(time, this.realWidth - timeW - fontSize, 2, font, fontSize);
  38. out += this.fillTextShift(this.fittingString(title, this.realWidth/2 - fontSize - 3, font), fontSize, 2, font, fontSize);
  39. out += this.drawPercentBar(this.realWidth/2, 2, this.realWidth/2 - timeW - 2*fontSize, statusBarHeight, font, fontSize, bookPos, textLength);
  40. out += '</div>';
  41. return out;
  42. }
  43. statusBarClickable(statusBarTop, statusBarHeight) {
  44. return `<div class="layout" style="position: absolute; ` +
  45. `left: 0px; top: ${statusBarTop ? 1 : this.realHeight - statusBarHeight + 1}px; ` +
  46. `width: ${this.realWidth/2}px; height: ${statusBarHeight}px; cursor: pointer"></div>`;
  47. }
  48. fittingString(str, maxWidth, font) {
  49. let w = this.measureTextFont(str, font);
  50. const ellipsis = '…';
  51. const ellipsisWidth = this.measureTextFont(ellipsis, font);
  52. if (w <= maxWidth || w <= ellipsisWidth) {
  53. return str;
  54. } else {
  55. let len = str.length;
  56. while (w >= maxWidth - ellipsisWidth && len-- > 0) {
  57. str = str.substring(0, len);
  58. w = this.measureTextFont(str, font);
  59. }
  60. return str + ellipsis;
  61. }
  62. }
  63. fillTextShift(text, x, y, font, size, css) {
  64. return this.fillText(text, x, y + size*this.fontShift, font, css);
  65. }
  66. fillText(text, x, y, font, css) {
  67. css = (css ? css : '');
  68. return `<div style="position: absolute; white-space: pre; left: ${x}px; top: ${y}px; font: ${font}; ${css}">${text}</div>`;
  69. }
  70. fillRect(x, y, w, h, color) {
  71. return `<div style="position: absolute; left: ${x}px; top: ${y}px; ` +
  72. `width: ${w}px; height: ${h}px; background-color: ${color}"></div>`;
  73. }
  74. strokeRect(x, y, w, h, color) {
  75. return `<div style="position: absolute; left: ${x}px; top: ${y}px; ` +
  76. `width: ${w}px; height: ${h}px; box-sizing: border-box; border: 1px solid ${color}"></div>`;
  77. }
  78. }