advent22/ui/src/components/rects/RectText.vue

56 lines
1.2 KiB
Vue
Raw Normal View History

2023-01-24 11:35:45 +00:00
<template>
<text
2023-01-24 23:11:49 +00:00
:x="Math.round(parent_aspect_ratio * rect_middle.x)"
:y="Math.round(rect_middle.y)"
:style="`transform: scaleX(${1 / parent_aspect_ratio})`"
2023-01-24 11:35:45 +00:00
text-anchor="middle"
dominant-baseline="middle"
>
{{ text }}
</text>
</template>
<script lang="ts">
import { Vue, Options } from "vue-class-component";
2023-01-24 23:11:49 +00:00
import { Rectangle, Vector2D } from "./rectangles";
2023-01-24 11:35:45 +00:00
@Options({
props: {
rectangle: Rectangle,
text: String,
},
})
export default class RectText extends Vue {
private rectangle!: Rectangle;
private text!: string;
2023-01-24 23:11:49 +00:00
private refreshKey = 0;
private get rect_middle(): Vector2D {
return this.rectangle.origin.plus(this.rectangle.size.scale(0.5));
}
private get parent_aspect_ratio(): number {
this.refreshKey; // read it just to force recompute on change
if (!(this.$el instanceof Element) || this.$el.parentElement === null) {
return 1;
}
const parent = this.$el.parentElement;
return parent.clientWidth / parent.clientHeight;
}
public mounted() {
this.refreshKey++;
}
2023-01-24 11:35:45 +00:00
}
</script>
<style lang="scss" scoped>
text {
fill: red;
font-weight: bold;
2023-01-24 23:11:49 +00:00
font-size: 50px;
2023-01-24 11:35:45 +00:00
}
</style>