Path.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import {Object2D} from "../Object2D.js";
  2. import {Vector2} from "../math/Vector2.js";
  3. import {Box2} from "../math/Box2.js";
  4. import {ColorStyle} from "./style/ColorStyle";
  5. import {Style} from "./style/Style";
  6. /**
  7. * Path object can be used to draw paths build from commands into the canvas.
  8. *
  9. * These paths can be also obtained from SVG files as a SVG command list.
  10. *
  11. * @class
  12. * @extends {Object2D}
  13. */
  14. function Path(path)
  15. {
  16. Object2D.call(this);
  17. /**
  18. * Path2D object containing the commands to draw the shape into the canvas.
  19. *
  20. * Check https://developer.mozilla.org/en-US/docs/Web/API/Path2D/Path2D for more details.
  21. */
  22. this.path = path !== undefined ? path : new Path2D("M10 10 h 80 v 80 h -80 Z");
  23. /**
  24. * Style of the object border line.
  25. *
  26. * If set null it is ignored.
  27. */
  28. this.strokeStyle = new ColorStyle("#000000");
  29. /**
  30. * Line width, only used if a valid strokeStyle is defined.
  31. */
  32. this.lineWidth = 1;
  33. /**
  34. * Background color of the path.
  35. *
  36. * If set null it is ignored.
  37. *
  38. * @param {Style}
  39. */
  40. this.fillStyle = new ColorStyle("#FFFFFF");
  41. }
  42. Path.prototype = Object.create(Object2D.prototype);
  43. Path.prototype.constructor = Path;
  44. Path.prototype.type = "Path";
  45. Object2D.register(Path, "Path");
  46. Path.prototype.draw = function(context, viewport, canvas)
  47. {
  48. if(this.fillStyle !== null)
  49. {
  50. context.fillStyle = this.fillStyle.get(context);
  51. context.fill(this.path);
  52. }
  53. if(this.strokeStyle !== null)
  54. {
  55. context.lineWidth = this.lineWidth;
  56. context.strokeStyle = this.strokeStyle.get(context);
  57. context.stroke(this.path);
  58. }
  59. };
  60. Path.prototype.serialize = function(recursive)
  61. {
  62. var data = Object2D.prototype.serialize.call(this, recursive);
  63. data.strokeStyle = this.strokeStyle !== null ? this.strokeStyle.serialize() : null;
  64. data.lineWidth = this.lineWidth;
  65. data.fillStyle = this.fillStyle !== null ? this.fillStyle.serialize() : null;
  66. return data;
  67. };
  68. Path.prototype.parse = function(data, root)
  69. {
  70. Object2D.prototype.parse.call(this, data, root);
  71. this.strokeStyle = data.strokeStyle !== null ? Style.parse(data.strokeStyle) : null;
  72. this.lineWidth = data.lineWidth;
  73. this.fillStyle = data.fillStyle !== null ? Style.parse(data.fillStyle) : null;
  74. };
  75. export {Path};