Skip to content

Commit

Permalink
Add Svg.prototype.center getter
Browse files Browse the repository at this point in the history
  • Loading branch information
ericyd committed Jan 18, 2024
1 parent e88e80c commit f5c87a7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/components/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Polyline, LineSegment, polyline } from './polyline.js'
import { Polygon } from './polygon.js'
import { LinearGradient } from './linear-gradient.js'
import { Defs } from './defs.js'
import { Vector2, vec2 } from '../vector2.js'

/**
* @typedef {object} SvgAttributes
Expand Down Expand Up @@ -64,6 +65,11 @@ export class Svg extends Tag {
/** @type {Record<string, string | number> | null} */
this.filenameMetadata = null
}

/** @type {Vector2} */
get center() {
return vec2(this.width / 2, this.height / 2)
}

// TODO: find a more generic way of expressing this "instance or builder" pattern
/**
Expand Down
8 changes: 8 additions & 0 deletions lib/components/svg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ describe('Svg', () => {
)
})
})

describe('center', () => {
it('is a Vector2 at the center of the viewport', () => {
const center = new Svg({ width: 50, height: 50 }).center
assert.strictEqual(center.x, 25)
assert.strictEqual(center.y, 25)
})
})
})

0 comments on commit f5c87a7

Please sign in to comment.