Source: draw/primitive.mjs

  1. import { DrawCommand } from "./drawcommand.mjs";
  2. import { SetColor, SetRotation } from "./state.mjs";
  3. import { Color } from "../color.mjs";
  4. /**
  5. * Base class of all primitives.
  6. * Handles position, rotation, color and stroke weight.
  7. * Creates the command list for those properties.
  8. */
  9. export class Primitive extends DrawCommand {
  10. pos;
  11. weight;
  12. rotation;
  13. fillCol;
  14. strokeCol;
  15. noFill;
  16. noStroke;
  17. constructor() {
  18. super();
  19. this.pos = { x: 0, y: 0 };
  20. this.strokeCol = Color.Black;
  21. this.fillCol = Color.Black;
  22. this.weight = 1;
  23. this.rotation = 0;
  24. this.noFill = false;
  25. this.noStroke = false;
  26. }
  27. /**
  28. * Set the color of the primitive.
  29. * @param {Color} fillColor - the color to use
  30. * @param {Color} strokeColor - the stroke color to use, may be null then it uses the fill color.
  31. * @returns {Primitive} - the primitive
  32. */
  33. color(fillColor, strokeColor) {
  34. this.fillCol = fillColor;
  35. this.strokeCol = strokeColor || fillColor;
  36. return this;
  37. }
  38. /**
  39. * Set the X coordinate of the primitive.
  40. * @param {number} - the x coordinate
  41. * @returns {Primitive} - the primitive
  42. */
  43. x(x) {
  44. this.pos.x = x;
  45. return this;
  46. }
  47. /**
  48. * Set the Y coordinate of the primitive.
  49. * @param {number} y - the y coordinate
  50. * @returns {Primitive} - the primitive
  51. */
  52. y(y) {
  53. this.pos.y = y;
  54. return this;
  55. }
  56. /**
  57. * Set the X and Y coordinates of the primitive.
  58. * @param {number} x - the x coordinate
  59. * @param {number} y - the y coordinate
  60. * @returns {Primitive} - the primitive
  61. */
  62. xy(x, y) {
  63. this.x(x);
  64. this.y(y);
  65. return this;
  66. }
  67. /**
  68. * Specify the rotation of the primitive. It gets rotated
  69. * around the point of the position.
  70. * @param {number} rotation - the rotation in radians
  71. * @returns {Primitive}
  72. */
  73. rotation(rotation) {
  74. this.rotation = rotation;
  75. return this;
  76. }
  77. /**
  78. * Set the stroke weight (aka width) of the
  79. * stroke lines.
  80. * @param {number} weight - the stroke weight
  81. * @returns {Primitive} - the primitive
  82. */
  83. strokeWeight(weight) {
  84. this.weight = weight;
  85. return this;
  86. }
  87. /**
  88. * Specify if the primitive shape should be filled with the
  89. * fill color.
  90. * @param {boolean} fill should fill the shape
  91. * @returns {Primitive}
  92. */
  93. dofill(fill = true) {
  94. this.noFill = !fill;
  95. return this;
  96. }
  97. /**
  98. * Don't fill the shape.
  99. * Shorthand for dofill(false)
  100. * @returns {Primitive}
  101. */
  102. nofill() {
  103. this.noFill = true;
  104. return this;
  105. }
  106. /**
  107. * Specify if the primitive shape should be stroked with the
  108. * stroke color.
  109. * @param {boolean} stroke should stroke the shape
  110. * @returns {Primitive}
  111. */
  112. dostroke(stroke = true) {
  113. this.noStroke = !stroke;
  114. return this;
  115. }
  116. /**
  117. * Don't stroke the shape.
  118. * Shorthand for dostroke(false)
  119. * @returns {Primitive}
  120. */
  121. nostroke() {
  122. this.noStroke = true;
  123. return this;
  124. }
  125. /**
  126. * --- INTERNAL ---
  127. * Generate the command list for the primitive
  128. * @returns {Array}
  129. */
  130. genCmdList() {
  131. let list = super.genCmdList();
  132. if(!(this.noStroke && this.noFill)) {
  133. list.push(new SetColor(this.fillCol, this.strokeCol));
  134. }
  135. list.push(new SetRotation(this.rotation, this.pos.x, this.pos.y));
  136. return list;
  137. }
  138. }