view嵌套canvas,overflow:hidden不生效,在开发者工具上展示正常,真机展示失效。
<template>
<view class="container">
<view class="wrapper">
<canvas class="canvas" canvas-id="canvas" style="width: 200px; height: 200px;"></canvas>
</view>
</view>
</template>
<script setup lang="ts">
import { getCurrentInstance, onMounted } from 'vue';
const _this = getCurrentInstance()
onMounted(() => {
const canvas = uni.createCanvasContext('canvas', _this)
canvas.setFillStyle('red')
canvas.fillRect(0, 0, 200, 200)
canvas.draw(false, (res) => {
console.log(res)
})
})
</script>
<style scoped lang="scss">
.container {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.wrapper {
width: 100px;
height: 100px;
overflow: hidden;
}
</style>
用cover-view包一层,这里的canvas 是原生的,渲染层级高 <cover-view class="wrapper"> <canvas class="canvas" canvas-id="canvas" style="width: 200px; height: 200px;"></canvas> </cover-view>
<template>
<view class="container">
<cover-view class="wrapper">
<canvas class="canvas" canvas-id="canvas" style="width: 200px; height: 200px;"></canvas>
</cover-view>
</view>
</template>
<script setup lang="ts">
import { getCurrentInstance, onMounted } from 'vue';
const _this = getCurrentInstance()
onMounted(() => {
const canvas = uni.createCanvasContext('canvas', _this)
canvas.setFillStyle('red')
canvas.fillRect(0, 0, 200, 200)
canvas.draw(false, (res) => {
console.log(res)
})
})
</script>
<style scoped lang="scss">
.container {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.wrapper {
width: 100px;
height: 100px;
overflow: hidden;
}
</style>