TS_凤凤
TS_凤凤
  • 发布:2024-01-12 08:48
  • 更新:2024-01-12 10:50
  • 阅读:281

canvas在 vue3 环境保存为图片到相册报错

分类:uni-app

这段代码在 vue2 中能够正常运行,也能保存为图片到相册,但是 vue3 环境报错:

"canvasToTempFilePath:fail Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas or VideoFrame)'"

是在微信小程序模拟器中出现的

代码如下:


<template>  
    <view>  
        <canvas style="width: 300px; height: 400px;"  
            canvas-id="firstCanvas" id="firstCanvas"></canvas>  

        <button @click="saveImage11">保存</button>  
    </view>  

</template>  

<script>  
    export default {  
        data() {  
            return {  
                selectId: 'firstCanvas',  
                scrollLeft: 0,  
                canvas: null  
            }  
        },  
        props: ['content'],  
        watch: {  
            content(value) {  
                if (value) {  
                    this.createCanvas11()  
                }  
            }  
        },  
        methods: {  
            // 生成canvas  
            createCanvas11() {  
                const ctx = uni.createCanvasContext('firstCanvas', this);  
                ctx.clearRect(0, 0, 300, 400)  
                ctx.fillStyle = "#FFFFFF";  
                ctx.fillRect(0, 0, 300, 400);  

                function findBreakPoint(text, width, context) {  
                    var min = 0;  
                    var max = text.length - 1;  
                    while (min <= max) {  
                        var middle = Math.floor((min   max) / 2);  
                        var middleWidth = context.measureText(text.substr(0, middle)).width;  
                        var oneCharWiderThanMiddleWidth = context.measureText(text.substr(0, middle   1)).width;  
                        if (middleWidth <= width 
2024-01-12 08:48 负责人:无 分享
已邀请:
DCloud_UNI_OttoJi

DCloud_UNI_OttoJi - 日常回复 uni-app/x 问题,如果艾特我没看到,请主动私信

你给的代码不够完整,不能运行,能提供更多内容吗,或者你进行删减代码,看看具体报错是哪一行有问题。
切换成 type=2d 的新版写法有没有问题?

要回复问题请先登录注册