DrawHelper.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 - 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.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}; 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.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. 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. fillTextShift(text, x, y, font, size) {
  59. return this.fillText(text, x, y + size*this.fontShift, font);
  60. }
  61. fillText(text, x, y, font) {
  62. return `<div style="position: absolute; left: ${x}px; top: ${y}px; font: ${font}">${text}</div>`;
  63. }
  64. fillRect(x, y, w, h, color) {
  65. return `<div style="position: absolute; left: ${x}px; top: ${y}px; ` +
  66. `width: ${w}px; height: ${h}px; background-color: ${color}"></div>`;
  67. }
  68. strokeRect(x, y, w, h, color) {
  69. return `<div style="position: absolute; left: ${x}px; top: ${y}px; ` +
  70. `width: ${w}px; height: ${h}px; border: 1px solid ${color}"></div>`;
  71. }
  72. }