VexFlowMusicSheetDrawer.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import Vex = require("vexflow");
  2. import {MusicSheetDrawer} from "../MusicSheetDrawer";
  3. import {RectangleF2D} from "../../../Common/DataObjects/RectangleF2D";
  4. import {VexFlowMeasure} from "./VexFlowMeasure";
  5. import {ITextMeasurer} from "../../Interfaces/ITextMeasurer";
  6. import {PointF2D} from "../../../Common/DataObjects/PointF2D";
  7. import {GraphicalLabel} from "../GraphicalLabel";
  8. /**
  9. * Created by Matthias on 22.06.2016.
  10. */
  11. export class VexFlowMusicSheetDrawer extends MusicSheetDrawer {
  12. private renderer: Vex.Flow.Renderer;
  13. private ctx: Vex.Flow.CanvasContext;
  14. constructor(canvas: HTMLCanvasElement, textMeasurer: ITextMeasurer, isPreviewImageDrawer: boolean = false) {
  15. super(textMeasurer, isPreviewImageDrawer);
  16. this.renderer = new Vex.Flow.Renderer(canvas, Vex.Flow.Renderer.Backends.CANVAS);
  17. this.ctx = this.renderer.getContext();
  18. }
  19. public scale(k: number): void {
  20. this.ctx.scale(k, k);
  21. }
  22. public resize(x: number, y: number): void {
  23. this.renderer.resize(x, y);
  24. }
  25. /**
  26. * Converts a distance from unit to pixel space.
  27. * @param unitDistance the distance in units
  28. * @returns {number} the distance in pixels
  29. */
  30. public calculatePixelDistance(unitDistance: number): number {
  31. // ToDo: implement!
  32. return unitDistance * 10.0;
  33. }
  34. protected drawMeasure(measure: VexFlowMeasure): void {
  35. measure.setAbsoluteCoordinates(
  36. measure.PositionAndShape.AbsolutePosition.x * (measure as VexFlowMeasure).unit,
  37. measure.PositionAndShape.AbsolutePosition.y * (measure as VexFlowMeasure).unit
  38. );
  39. return measure.draw(this.ctx);
  40. }
  41. /**
  42. * Renders a Label to the screen (e.g. Title, composer..)
  43. * @param graphicalLabel holds the label string, the text height in units and the font parameters
  44. * @param layer is the current rendering layer. There are many layers on top of each other to which can be rendered. Not needed for now.
  45. * @param bitmapWidth Not needed for now.
  46. * @param bitmapHeight Not needed for now.
  47. * @param heightInPixel the height of the text in screen coordinates
  48. * @param screenPosition the position of the lower left corner of the text in screen coordinates
  49. */
  50. protected renderLabel(graphicalLabel: GraphicalLabel, layer: number, bitmapWidth: number,
  51. bitmapHeight: number, heightInPixel: number, screenPosition: PointF2D): void {
  52. // ToDo: implement!
  53. }
  54. /**
  55. * Renders a rectangle with the given style to the screen.
  56. * It is given in screen coordinates.
  57. * @param rectangle the rect in screen coordinates
  58. * @param layer is the current rendering layer. There are many layers on top of each other to which can be rendered. Not needed for now.
  59. * @param styleId the style id
  60. */
  61. protected renderRectangle(rectangle: RectangleF2D, layer: number, styleId: number): void {
  62. // ToDo: implement!
  63. }
  64. /**
  65. * Converts a point from unit to pixel space.
  66. * @param point
  67. * @returns {PointF2D}
  68. */
  69. protected applyScreenTransformation(point: PointF2D): PointF2D {
  70. // ToDo: implement!
  71. return point;
  72. }
  73. /**
  74. * Converts a rectangle from unit to pixel space.
  75. * @param rectangle
  76. * @returns {RectangleF2D}
  77. */
  78. protected applyScreenTransformationForRect(rectangle: RectangleF2D): RectangleF2D {
  79. // ToDo: implement!
  80. return rectangle;
  81. }
  82. }