SvgVexFlowBackend.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import Vex = require("vexflow");
  2. import {VexFlowBackend} from "./VexFlowBackend";
  3. import {VexFlowConverter} from "./VexFlowConverter";
  4. import {FontStyles} from "../../../Common/Enums/FontStyles";
  5. import {Fonts} from "../../../Common/Enums/Fonts";
  6. import {RectangleF2D} from "../../../Common/DataObjects/RectangleF2D";
  7. import {PointF2D} from "../../../Common/DataObjects/PointF2D";
  8. export class SvgVexFlowBackend extends VexFlowBackend {
  9. private ctx: Vex.Flow.SVGContext;
  10. public getBackendType(): number {
  11. return Vex.Flow.Renderer.Backends.SVG;
  12. }
  13. public initialize(container: HTMLElement): void {
  14. this.canvas = document.createElement("div");
  15. this.inner = this.canvas;
  16. this.inner.style.position = "relative";
  17. this.canvas.style.zIndex = "0";
  18. container.appendChild(this.inner);
  19. this.renderer = new Vex.Flow.Renderer(this.canvas, this.getBackendType());
  20. this.ctx = <Vex.Flow.SVGContext>this.renderer.getContext();
  21. }
  22. public getContext(): Vex.Flow.SVGContext {
  23. return this.ctx;
  24. }
  25. public clear(): void {
  26. if (!this.ctx) {
  27. return;
  28. }
  29. const { svg } = this.ctx;
  30. // removes all children from the SVG element,
  31. // effectively clearing the SVG viewport
  32. while (svg.lastChild) {
  33. svg.removeChild(svg.lastChild);
  34. }
  35. }
  36. public scale(k: number): void {
  37. this.ctx.scale(k, k);
  38. }
  39. public translate(x: number, y: number): void {
  40. // TODO: implement this
  41. }
  42. public renderText(fontHeight: number, fontStyle: FontStyles, font: Fonts, text: string,
  43. heightInPixel: number, screenPosition: PointF2D, color: string = undefined): void {
  44. this.ctx.save();
  45. if (color) {
  46. this.ctx.attributes.fill = color;
  47. this.ctx.attributes.stroke = color;
  48. }
  49. this.ctx.setFont("Times New Roman", fontHeight, VexFlowConverter.fontStyle(fontStyle));
  50. // font size is set by VexFlow in `pt`. This overwrites the font so it's set to px instead
  51. this.ctx.attributes["font-size"] = `${fontHeight}px`;
  52. this.ctx.state["font-size"] = `${fontHeight}px`;
  53. this.ctx.fillText(text, screenPosition.x, screenPosition.y + heightInPixel);
  54. this.ctx.restore();
  55. }
  56. public renderRectangle(rectangle: RectangleF2D, styleId: number, alpha: number = 1): void {
  57. this.ctx.save();
  58. this.ctx.attributes.fill = VexFlowConverter.style(styleId);
  59. this.ctx.attributes["fill-opacity"] = alpha;
  60. this.ctx.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
  61. this.ctx.restore();
  62. this.ctx.attributes["fill-opacity"] = 1;
  63. }
  64. public renderLine(start: PointF2D, stop: PointF2D, color: string = "#FF0000FF", lineWidth: number = 2): void {
  65. this.ctx.save();
  66. this.ctx.beginPath();
  67. this.ctx.moveTo(start.x, start.y);
  68. this.ctx.lineTo(stop.x, stop.y);
  69. this.ctx.attributes.stroke = color;
  70. //this.ctx.attributes.strokeStyle = color;
  71. //this.ctx.attributes["font-weight"] = "bold";
  72. //this.ctx.attributes["stroke-linecap"] = "round";
  73. this.ctx.lineWidth = lineWidth;
  74. this.ctx.stroke();
  75. this.ctx.restore();
  76. }
  77. public renderCurve(points: PointF2D[]): void {
  78. this.ctx.beginPath();
  79. this.ctx.moveTo(points[0].x, points[0].y);
  80. this.ctx.bezierCurveTo(
  81. points[1].x,
  82. points[1].y,
  83. points[2].x,
  84. points[2].y,
  85. points[3].x,
  86. points[3].y
  87. );
  88. this.ctx.lineTo(points[7].x, points[7].y);
  89. this.ctx.bezierCurveTo(
  90. points[6].x,
  91. points[6].y,
  92. points[5].x,
  93. points[5].y,
  94. points[4].x,
  95. points[4].y
  96. );
  97. this.ctx.lineTo(points[0].x, points[0].y);
  98. //this.ctx.stroke();
  99. this.ctx.closePath();
  100. this.ctx.fill();
  101. }
  102. }