a***@163.com
a***@163.com
  • 发布:2025-06-20 14:31
  • 更新:2025-07-03 15:47
  • 阅读:34

【报Bug】createCanvasContext在微信小程序中不兼容

分类:uni-app

产品分类: uniapp/小程序/微信

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: 10

HBuilderX类型: 正式

HBuilderX版本号: 4.66

第三方开发者工具版本号: 1.06.24

基础库版本号: 3.8.8

项目创建方式: HBuilderX

示例代码:

<template>
<view class="goodsposter">
<view class="goodsposter_cen">
<view class="goodsposter_cen_t">
<canvas canvasId="goodsci" id="goodsci" type="2d" @error="canvasIdErrorCallback"></canvas>
</view>
<view class="goodsposter_cen_b">
<view class="goodsposter_cen_b_i" @click="onDownloadPoster">
下载海报
</view>
<view class="goodsposter_cen_b_i">
分享海报
</view>
</view>
</view>
</view>
</template>
<script setup>
import { onMounted, ref, reactive, defineProps, getCurrentInstance, nextTick } from 'vue'
const {proxy} = getCurrentInstance()

const props = defineProps({
info: {
type: Object
}
})
onMounted(()=>{
let ctx = uni.createCanvasContext("goodsci",proxy)
console.log("这是canvas",ctx)
ctx.setFillStyle('red')
ctx.fillRect(10, 10, 150, 75)
ctx.draw()

})
const onDownloadPoster=()=>{

}
const canvasIdErrorCallback=(e)=>{
console.error(e.detail.errMsg)
}
</script>
<style scoped lang="scss">
.goodsposter {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 100;

&_cen {  
    position: absolute;  
    top: 50%;  
    left: 50%;  
    transform: translate(-50%, -50%);  
    width: 80%;  
    background-color: #fff;  
    border-radius: 10rpx;  

    &_t {  
        padding: 20rpx;  
        text-align: center;  

        // canvas {  
        //  width: 100%;  
        //  height: auto;  
        // }  
    }  

    &_b {  
        display: flex;  
        justify-content: space-around;  
        padding: 20rpx;  

        &_i {  
            flex-grow: 1;  
            text-align: center;  
            color: $text-act-color;  
        }  
    }  
}  

}
</style>

操作步骤:

在components里面创建一个goods-poster.vue文件,将代码复制进入,然后在页面中引用该组件,然后运行到微信小程序

预期结果:

显示一个红色矩形

实际结果:

组件白屏

bug描述:

从基础库 2.9.0 开始,sx.createCanvasContext接口停止维护,createCanvasContext在微信小程序中有兼容问题,无法显示,解决办法是使用createSelectorQuery代替,这是微信文档:https://developers.weixin.qq.com/miniprogram/dev/api/canvas/wx.createCanvasContext.html

2025-06-20 14:31 负责人:无 分享
已邀请:
DCloud_UNI_yuhe

DCloud_UNI_yuhe

uni 上的方法在微信上就是使用对应的微信方法,如果有问题请参照这个解决方法

要回复问题请先登录注册