DrawHelper.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. export default class DrawHelper {
  2. fontBySize(size) {
  3. return `${size}px ${this.fontName}`;
  4. }
  5. drawPercentBar(x, y, w, h, font, 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.fillText(t1, x, y, font);
  18. if (w1 + w2 + w3 <= w && w3 > (10 + fh2)) {
  19. const barWidth = w - w1 - w2 - fh2;
  20. out += this.strokeRect(x + w1, y + pad - 2, barWidth, fh - 4, this.statusBarColor);
  21. out += this.fillRect(x + w1 + 2, y + pad, (barWidth - 4)*read, fh - 6, this.statusBarColor);
  22. }
  23. if (w1 <= w)
  24. out += this.fillText(t2, x + w1 + w3, y, font);
  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}; background-color: ${this.backgroundColor}">`;
  31. const fontSize = statusBarHeight - 6;
  32. const font = 'bold ' + this.fontBySize(fontSize);
  33. out += this.fillRect(0, (statusBarTop ? statusBarHeight : 1), 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.fillText(time, this.realWidth - timeW - fontSize, 3, font);
  38. out += this.fillText(this.fittingString(title, this.realWidth/2 - fontSize - 3, font), fontSize, 3, font);
  39. out += this.drawPercentBar(this.realWidth/2, 3, this.realWidth/2 - timeW - 2*fontSize, statusBarHeight, font, bookPos, textLength);
  40. out += '</div>';
  41. return out;
  42. }
  43. fittingString(str, maxWidth, font) {
  44. let w = this.measureTextFont(str, font);
  45. const ellipsis = '…';
  46. const ellipsisWidth = this.measureTextFont(ellipsis, font);
  47. if (w <= maxWidth || w <= ellipsisWidth) {
  48. return str;
  49. } else {
  50. let len = str.length;
  51. while (w >= maxWidth - ellipsisWidth && len-- > 0) {
  52. str = str.substring(0, len);
  53. w = this.measureTextFont(str, font);
  54. }
  55. return str + ellipsis;
  56. }
  57. }
  58. fillText(text, x, y, font) {
  59. return `<div style="position: absolute; border: 1px solid black; left: ${x}px; top: ${y}px; font: ${font}">${text}</div>`;
  60. }
  61. fillRect(x, y, w, h, color) {
  62. return `<div style="margin: 0; padding: 0; position: absolute; left: ${x}px; top: ${y}px; ` +
  63. `width: ${w}px; height: ${h}px; background-color: ${color}"></div>`;
  64. }
  65. strokeRect(x, y, w, h, color) {
  66. return `<div style="margin: 0; padding: 0; position: absolute; left: ${x}px; top: ${y}px; ` +
  67. `width: ${w}px; height: ${h}px; border: 1px solid ${color}"></div>`;
  68. }
  69. }