5***@qq.com
5***@qq.com
  • 发布:2022-02-18 11:31
  • 更新:2022-02-18 15:46
  • 阅读:440

【报Bug】救命,H5的canvas获取不到上下文对象

分类:uni-app

产品分类: uniapp/H5

PC开发环境操作系统: Windows

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

HBuilderX类型: 正式

HBuilderX版本号: 3.3.11

浏览器平台: Chrome

浏览器版本: 79.0.3945.130(正式版本) (64 位)

项目创建方式: HBuilderX

示例代码:
<template>  
    <view class="hot-box">  
        <view class="hot-tips">  
            <image src="../static/img/hot-like.png" mode="" style="width: 306rpx;height:76rpx"></image>  
        </view>  
        <view class="hot-bg">  
            <canvas id="ec-heatmp-mask" canvas-id="ec-heatmp-mask" style="width: 340px; height: 340px"></canvas>  
        </view>  
    </view>  
</template>  
<script>  
    export default {  
        data() {  
            return {  
            };  

        },  
        computed: {  
        },  
        created() {  
            this.initCanvas()  
        },  
        mounted() {  
        },  
        methods: {  
            initCanvas() {  
                const ctx = uni.createCanvasContext("ec-heatmp-mask", this)  
                uni.downloadFile({  
                    url: "https://piccd.zzss.com/manager/attach/srp/20200319/190527.png",  
                    success: (res) => {  
                        console.log(res)  
                        ctx.drawImage(res.tempFilePath, 0, 0, 325, 325)  
                        ctx.draw(true, function() {  
                            uni.canvasGetImageData({  
                                canvasId: "ec-heatmp-mask",  
                                x: 0,  
                                y: 0,  
                                width: 325,  
                                height: 325,  
                                success(res) {  
                                    let pixels = res.data  
                                    console.log(pixels instanceof Uint8ClampedArray)  
                                    for (let index = 0; index < res.data.length; index += 4) {  
                                        let r = pixels[index]  
                                        let g = pixels[index + 1]  
                                        let b = pixels[index + 2]  
                                        let a = pixels[index + 3]  
                                        if (a == 0) {  
                                            pixels[index] = 255  
                                            pixels[index + 1] = 255  
                                            pixels[index + 2] = 255  
                                            pixels[index + 3] = 255  
                                        } else {  
                                            pixels[index] = 0  
                                            pixels[index + 1] = 255  
                                            pixels[index + 2] = 255  
                                            pixels[index + 3] = 0  
                                        }  
                                    }  
                                    uni.canvasPutImageData({  
                                        canvasId: "ec-heatmp-mask",  
                                        x: 0,  
                                        y: 0,  
                                        width: 325,  
                                        data: pixels,  
                                        success(res) {  
                                            console.log(res);  
                                        },  
                                        completeres(res) {  
                                            console.log(res);  
                                        },  
                                    })  
                                }  
                            })  
                        })  
                    }  
                })  
            }  
        }  
    }  
</script>  

操作步骤:

const ctx = uni.createCanvasContext("ec-heatmp-mask", this)
ctx获取不到
为什么获取不到canvas的上下文对象,this也有.............

预期结果:

const ctx = uni.createCanvasContext("ec-heatmp-mask", this)
ctx获取不到
为什么获取不到canvas的上下文对象,this也有.............

实际结果:

const ctx = uni.createCanvasContext("ec-heatmp-mask", this)
ctx获取不到
为什么获取不到canvas的上下文对象,this也有.............

bug描述:

const ctx = uni.createCanvasContext("ec-heatmp-mask", this)
ctx获取不到
为什么获取不到canvas的上下文对象,this也有.............

2022-02-18 11:31 负责人:无 分享
已邀请:
DCloud_UNI_Anne

DCloud_UNI_Anne

未复现此问题,建议新建空项目测试下排查具体问题

该问题目前已经被锁定, 无法添加新回复