关于微信小程序 canvas 2d 的问题,感觉这块内容好少 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xiaohantx
V2EX    微信

关于微信小程序 canvas 2d 的问题,感觉这块内容好少

  •  
  •   xiaohantx 2024-05-16 09:13:21 +08:00 1640 次点击
    这是一个创建于 517 天前的主题,其中的信息可能已经有所发展或是发生改变。
    canvasToTempFilePath 这个 Api 在 Canvas 里转的文件不能显示吗?
    ios 安卓开发者工具都正常,真机模拟(pc 小程序)和 pc 小程序时候都空白,看显示 wxfile://temp/地址

    还有网页
    https://cdnjson.com/image/image.8Guvk
    小程序
    https://cdnjson.com/image/image.8G2LB

    为啥一套代码网页会大那么多。。。

    非 2d 的好像还好,type="2d"这块感觉内容很少,canvasToTempFilePath 有 canvas 和 canvasId 两个参数,我看网页好像不支持 canvasid
    12 条回复    2024-05-16 13:47:39 +08:00
    icandoallthings
        1
    icandoallthings  
       2024-05-16 09:17:43 +08:00
    一切以真机为准
    xiaohantx
        2
    xiaohantx  
    OP
       2024-05-16 09:22:17 +08:00
    @YorkWong 就是真机下不统一呀....ios,安卓目前一样,pc 真机目前不显示,网页真机目前不知道为什么倍数放大
    HTML001
        3
    HTML001  
       2024-05-16 09:31:32 +08:00
    小程序就这样,某些 API 在 IOS 和安卓表现不一样,在真机和开发者工具不一样,在不同版本的微信上面表现不一样,一些冷门 API 最容易出现这些问题
    Grapevine
        4
    Grapevine  
       2024-05-16 09:33:51 +08:00
    小程序真的是毒瘤。。
    toesbieya
        5
    toesbieya  
       2024-05-16 09:41:41 +08:00
    canvas 2d 用 toDataURL 获取 base64 然后自己写入本地文件
    大很多可能移动设备 dpi 较高,自己缩放 canvas
    xiaohantx
        6
    xiaohantx  
    OP
       2024-05-16 09:44:27 +08:00
    @toesbieya 我输出 devicePixelRatio 的值我看完全一样,width,height 也都一样就很疑惑,写入本地文件的时候是否会向用户申请写入权限。。
    xiaohantx
        7
    xiaohantx  
    OP
       2024-05-16 09:49:21 +08:00
    ```
    const drawCanvas = (canvasId, data) => {
    uni.createSelectorQuery().select(canvasId).fields({
    node: true,
    size: true
    }).exec(res => {
    // 处理数据
    demoList[0].value = data.levelOne ?? 0
    demoList[1].value = data.levelTwo ?? 0
    demoList[2].value = data.levelThree ?? 0
    const {
    devicePixelRatio,
    screenWidth
    } = uni.getSystemInfoSync()
    const rpx = devicePixelRatio
    const canvas = res[0].node
    let ctx = canvas.getContext('2d')
    // 初始化画布大小
    canvas.width = res[0].width * rpx
    canvas.height = res[0].height * rpx
    // 获取结束
    const x = res[0].width / 2 * rpx;
    const y = res[0].height / 2 * rpx;
    const lineWidth = 20 * rpx;
    const radius = (res[0].height / 2) * rpx - (lineWidth / 2) // 半径
    const sumResult = canvasId === '#chart' ? data.errorTotal : data.alertTotal;
    let startAngle = 0;
    demoList.map(item => {
    // 绘制数组中的百分比
    ctx.beginPath();
    console.log('sum', sumResult)
    let angle = (item.value / sumResult) * 2 * Math.PI
    ctx.arc(x, y, radius, startAngle, startAngle + angle);
    ctx.lineWidth = lineWidth;
    ctx.strokeStyle = item.color;
    ctx.stroke();
    startAngle += angle
    })
    // 文字颜色
    ctx.fillStyle = "#1D2129"
    ctx.fOnt= `${30 * rpx}px sans-serif`
    ctx.textAlign = 'center'
    // // 文字位置
    // // 插入文字
    ctx.fillText(sumResult, canvas.width / 2, y)
    // // 文字大小
    ctx.fillStyle = "#86909C"
    ctx.fOnt= `${12 * rpx}px sans-serif`
    ctx.textAlign = 'center'
    // // 文字颜色
    // // 插入文字
    ctx.fillText('故障总计(台)', canvas.width / 2, y + (20 * rpx))
    uni.canvasToTempFilePath({
    canvas: canvas,
    success: function(res) {
    if (canvasId === '#chart') {
    canvasSrc1.value = res.tempFilePath;
    } else {
    canvasSrc2.value = res.tempFilePath;
    }
    },
    fail: function(res) {
    console.log(222, res)
    }
    })
    })
    }
    ```
    lukesy
        8
    lukesy  
       2024-05-16 10:26:56 +08:00
    这个 api 没有问题的
    lukesy
        9
    lukesy  
       2024-05-16 10:30:47 +08:00
    你如果要做海报 你直接用这个 https://github.com/Kujiale-Mobile/Painter
    Ashore
        10
    Ashore  
       2024-05-16 11:43:20 +08:00
    我用的这个 https://kiccer.github.io/wx-canvas-2d/ 感觉还可以。
    uniapp 可以用这个 https://ext.dcloud.net.cn/plugin?id=2389
    toesbieya
        11
    toesbieya  
       2024-05-16 12:05:43 +08:00
    uniapp 啊 他那个 canvas 自己有做缩放,h5 上自己创建 canvas 元素应该就正常了
    yuyuf
        12
    yuyuf  
       2024-05-16 13:47:39 +08:00
    空白问题可能是 canvas 还没有绘制完成。可以调用 cavnas.draw() 后,暂停 200ms, 再调 canvasToTempFilePath
    分辨率问题,尽量手动设置 canvas 的大小,还有 destWidth ,destHeight 这两个参数
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3163 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 10:56 PVG 18:56 LAX 03:56 JFK 06:56
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86