Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

widgets: Fix canvas rendering for widgets that are in the background #1193

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/components/widgets/Compass.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</template>

<script setup lang="ts">
import { useElementSize } from '@vueuse/core'
import { useWindowSize } from '@vueuse/core'
import gsap from 'gsap'
import { computed, nextTick, onBeforeMount, onMounted, reactive, ref, toRefs, watch } from 'vue'

Expand Down Expand Up @@ -85,8 +85,12 @@ onMounted(() => {
renderCanvas()
})

// Make canvas size follows window resizing
const { width: windowWidth, height: windowHeight } = useWindowSize()
const width = computed(() => widget.value.size.width * windowWidth.value)
const height = computed(() => widget.value.size.height * windowHeight.value)

// Calculates the smallest between the widget dimensions, so we can keep the inner content always inside it, without overlays
const { width, height } = useElementSize(compassRoot)
const smallestDimension = computed(() => (width.value < height.value ? width.value : height.value))

// Renders the updated canvas state
Expand Down
7 changes: 6 additions & 1 deletion src/components/widgets/CompassHUD.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</template>

<script setup lang="ts">
import { useWindowSize } from '@vueuse/core'
import { useElementVisibility, useWindowSize } from '@vueuse/core'
import { colord } from 'colord'
import gsap from 'gsap'
import { computed, nextTick, onBeforeMount, onMounted, reactive, ref, toRefs, watch } from 'vue'
Expand Down Expand Up @@ -256,6 +256,11 @@ watch([renderVars, canvasSize, widget.value.options], () => {
if (!widgetStore.isWidgetVisible(widget.value)) return
nextTick(() => renderCanvas())
})

const canvasVisible = useElementVisibility(canvasRef)
watch(canvasVisible, (isVisible, wasVisible) => {
if (isVisible && !wasVisible) renderCanvas()
})
</script>

<style scoped>
Expand Down
7 changes: 6 additions & 1 deletion src/components/widgets/DepthHUD.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</template>

<script setup lang="ts">
import { useWindowSize } from '@vueuse/core'
import { useElementVisibility, useWindowSize } from '@vueuse/core'
import { colord } from 'colord'
import gsap from 'gsap'
import { unit } from 'mathjs'
Expand Down Expand Up @@ -234,6 +234,11 @@ watch([renderVars, canvasSize, widget.value.options], () => {
if (!widgetStore.isWidgetVisible(widget.value)) return
nextTick(() => renderCanvas())
})

const canvasVisible = useElementVisibility(canvasRef)
watch(canvasVisible, (isVisible, wasVisible) => {
if (isVisible && !wasVisible) renderCanvas()
})
</script>

<style scoped>
Expand Down
12 changes: 7 additions & 5 deletions src/components/widgets/VirtualHorizon.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</template>

<script setup lang="ts">
import { useElementSize } from '@vueuse/core'
import { useWindowSize } from '@vueuse/core'
import gsap from 'gsap'
import { computed, nextTick, onMounted, reactive, ref, toRefs, watch } from 'vue'

Expand All @@ -37,14 +37,19 @@ const virtualHorizonRoot = ref()
const canvasRef = ref<HTMLCanvasElement | undefined>()
const canvasContext = ref()

// Make canvas size follows window resizing
const { width: windowWidth, height: windowHeight } = useWindowSize()
const width = computed(() => widget.value.size.width * windowWidth.value)
const height = computed(() => widget.value.size.height * windowHeight.value)

// Calculates the smallest between the widget dimensions, so we can keep the inner content always inside it, without overlays
const { width, height } = useElementSize(virtualHorizonRoot)
const smallestDimension = computed(() => (width.value < height.value ? width.value : height.value))

// Renders the updated canvas state
const renderCanvas = (): void => {
if (canvasRef.value === undefined || canvasRef.value === null) return
if (canvasContext.value === undefined) canvasContext.value = canvasRef.value.getContext('2d')

const ctx = canvasContext.value
resetCanvas(ctx)

Expand Down Expand Up @@ -255,9 +260,6 @@ watch([renderVariables, width, height], () => {
})

onMounted(() => {
if (canvasRef.value === undefined || canvasRef.value === null) return
if (canvasContext.value === undefined) canvasContext.value = canvasRef.value.getContext('2d')

// Set initial values, since 0 or 360 degrees does not render
gsap.to(renderVariables, 0.1, { pitchAngleDegrees: pitchAngleDeg.value })
gsap.to(renderVariables, 0.1, { rollAngleDegrees: -1 * rollAngleDeg.value })
Expand Down
Loading