vue-shovel/src/Background.vue
2023-04-25 09:09:49 +02:00

35 lines
808 B
Vue

<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import useBackground from './util/useBackground'
import { BLOCK_SIZE, STAGE_WIDTH, STAGE_HEIGHT } from './level/def'
export interface Props {
time: number
x: number
}
const props = defineProps<Props>()
const canvas = ref<HTMLCanvasElement | null>(null)
const p = Math.PI / -10
const sunY = computed(() => Math.sin(props.time * p))
onMounted(() => {
const canvasEl = canvas.value
if (canvasEl === null) return
const drawBackground = useBackground(
canvasEl,
~~(STAGE_WIDTH * BLOCK_SIZE / 2.0),
~~(STAGE_HEIGHT * BLOCK_SIZE / 2.0),
)
watch(props, () => drawBackground(props.x, sunY.value), { immediate: true })
})
</script>
<template>
<canvas ref="canvas" id="background"></canvas>
</template>