前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >js之H5画布不显示图片的问题解决

js之H5画布不显示图片的问题解决

原创
作者头像
IT工作者
发布2022-05-16 21:22:38
1.2K0
发布2022-05-16 21:22:38
举报
文章被收录于专栏:程序技术知识程序技术知识

在onReady 执行

代码语言:javascript
复制
<template>
    <view class="">
        <canvas style="" canvas-id="myCanvas" id="myCanvas"></canvas>
        <!-- <view class="container">
            <img :src="tempFilePath" />
        </view> -->
    </view>
 
</template>
 
<script>
    var that
    export default {
        data() {
            return {
                tempFilePath: ''
            }
        },
        onReady(option) {
            that = this;
            const ctx = uni.createCanvasContext('myCanvas')
            // uni.chooseImage({
            //   success: function(res){
                ctx.drawImage('../../static/sharePoster_bg.png', 0, 0, 150, 100)
                ctx.draw()
              // }
            // })
        },
        methods: {
        }
    }
</script>
 
<style lang="scss" scoped>
</style>

 

   

<template>
    <view class="">
        <canvas style="" canvas-id="myCanvas" id="myCanvas"></canvas>
        <view class="container">
            <image :src="tempFilePath" mode="widthFix"></image>
        </view>
    </view>
 
</template>
 
<script>
    var that
    export default {
        data() {
            return {
                tempFilePath: ''
            }
        },
        onReady(option) {
            that = this;
            const ctx = uni.createCanvasContext('myCanvas')
 
            ctx.drawImage('../../static/sharePoster_bg.png', 0, 0, 200, 380)
            ctx.fillStyle="#005B8C";
            ctx.font = "bold 60px Arial";
            // ctx.setFontStyle('color','#005B8C')
            ctx.fillText('A+', 15, 126)
            ctx.draw(true, () => {
                uni.canvasToTempFilePath({
                    x: 0,
                    y: 0,
                    width: 200,
                    height: 380,
                    destWidth: getApp().windowWidth,
                    destHeight: getApp().windowWidth/200*380,
                    canvasId: 'myCanvas',
                    fileType: "jpg",
                    quality: 1,
                    success(res) {
                        console.log('绘制成功-------', res)
                        uni.hideLoading();
                        that.tempFilePath = res.tempFilePath;
                    },
                    fail(err) {
                        console.log('绘制失败', err)
                    }
                });
            });
        },
        methods: {}
    }
</script>
 
<style lang="scss" scoped>
    canvas {
            background-color: #fff;
            border: 1px solid #d0d0d0;
            width: 100vw;
            height: 100vh;
            position: absolute;
            left: 100%;
        }
     
        .container {
            width: 100vw;
            align-items: center;
            overflow: auto;
            background: #fefefe;
        }
     
        .container image {
            width: 100%;
            position: absolute;
            top: 0;
        }
</style>

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档