Skip to content

Commit

Permalink
add push method and cursor prop to polyline
Browse files Browse the repository at this point in the history
  • Loading branch information
ericyd committed Jan 11, 2024
1 parent de1743c commit d1054ac
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/components/polyline.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ import { Tag } from './tag.js'
*/

export class Polyline extends Tag {
/** @type {Vector2} */
cursor = new Vector2(0, 0)
/** @type {Vector2[]} */
points = []
/**
* Initialize to "empty" rectangle
* @type {Rectangle}
*/
#boundingBox = new Rectangle({ x: 0, y: 0, width: 0, height: 0 })

/**
* @param {PolylineAttributes} attributes
*/
constructor({ points = [], ...attributes } = { points: [] }) {
super('polyline', attributes)
this.points = points
this.cursor = points[points.length - 1] ?? new Vector2(0, 0)
}

/** @returns {Rectangle} */
Expand All @@ -43,6 +47,15 @@ export class Polyline extends Tag {
return this.#boundingBox
}

/**
* Adds a point to the points of the polyline.
* @param {Vector2} point
*/
push(point) {
this.points.push(point)
this.cursor = point
}

render() {
if (!Array.isArray(this.points) || this.points.length === 0) {
throw new Error('Cannot render a Polyline without points')
Expand Down

0 comments on commit d1054ac

Please sign in to comment.