SvgVexFlowBackend.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. public getBackendType(): number {
  10. return Vex.Flow.Renderer.Backends.SVG;
  11. }
  12. public initialize(container: HTMLElement): void {
  13. this.canvas = document.createElement("div");
  14. this.inner = this.canvas;
  15. this.inner.style.position = "relative";
  16. this.canvas.style.zIndex = "0";
  17. container.appendChild(this.inner);
  18. this.renderer = new Vex.Flow.Renderer(this.canvas, this.getBackendType());
  19. this.ctx = <Vex.Flow.SVGContext>this.renderer.getContext();
  20. }
  21. public getContext(): Vex.Flow.SVGContext {
  22. return this.ctx;
  23. }
  24. public clear(): void {
  25. const { svg } = this.ctx;
  26. if (!svg) {
  27. return;
  28. }
  29. // removes all children from the SVG element,
  30. // effectively clearing the SVG viewport
  31. while (svg.lastChild) {
  32. svg.removeChild(svg.lastChild);
  33. }
  34. }
  35. public scale(k: number): void {
  36. this.ctx.scale(k, k);
  37. }
  38. public translate(x: number, y: number): void {
  39. // TODO: implement this
  40. }
  41. public renderText(fontHeight: number, fontStyle: FontStyles, font: Fonts, text: string,
  42. heightInPixel: number, screenPosition: PointF2D): void {
  43. this.ctx.save();
  44. this.ctx.setFont("Times New Roman", fontHeight, VexFlowConverter.fontStyle(fontStyle));
  45. // font size is set by VexFlow in `pt`. This overwrites the font so it's set to px instead
  46. this.ctx.attributes["font-size"] = `${fontHeight}px`;
  47. this.ctx.state["font-size"] = `${fontHeight}px`;
  48. this.ctx.fillText(text, screenPosition.x, screenPosition.y + heightInPixel);
  49. this.ctx.restore();
  50. }
  51. public renderRectangle(rectangle: RectangleF2D, styleId: number, alpha: number = 1): void {
  52. this.ctx.save();
  53. this.ctx.attributes.fill = VexFlowConverter.style(styleId);
  54. this.ctx.attributes["fill-opacity"] = alpha;
  55. this.ctx.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
  56. this.ctx.restore();
  57. this.ctx.attributes["fill-opacity"] = 1;
  58. }
  59. public renderLine(start: PointF2D, stop: PointF2D, color: string = "#FF0000FF", lineWidth: number = 2): void {
  60. this.ctx.save();
  61. this.ctx.beginPath();
  62. this.ctx.moveTo(start.x, start.y);
  63. this.ctx.lineTo(stop.x, stop.y);
  64. this.ctx.attributes.stroke = color;
  65. //this.ctx.attributes.strokeStyle = color;
  66. //this.ctx.attributes["font-weight"] = "bold";
  67. //this.ctx.attributes["stroke-linecap"] = "round";
  68. this.ctx.lineWidth = lineWidth;
  69. this.ctx.stroke();
  70. this.ctx.restore();
  71. }
  72. private ctx: Vex.Flow.SVGContext;
  73. }