前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >uniapp生成海报完整示例

uniapp生成海报完整示例

作者头像
发布2023-10-18 10:26:42
5730
发布2023-10-18 10:26:42
举报
文章被收录于专栏:IT杂症
CAN.jpg
CAN.jpg

CAN.jpg

uniapp-canvas示例

代码语言:javascript
复制
<template>
    <view style="width: 80vw; height: 80vh">
        <image v-if="showposter" show-menu-by-longpress="true" class="poster" mode="aspectFill" style="width: 100%; height: 100%" :src="canvasimg"></image>
        <canvas   v-else  canvas-id="myCanvas" id="myCanvas" style="width: 100%; height: 100%"></canvas>        
        <view class="shareTip">长按保存或分享</view>
    </view>
</template>

<script>
const app = getApp();

export default {
    props: {
        begindraw: {
            type: Boolean,
            default: false
        }
    },
    watch: {
        begindraw: function (o, n) {
            console.log('nnnnnn', n);

            this.dotest();
            //  this.doposter()
        }
    },
    data() {
        return {
            showposter: false,
            canvasimg: '',
            canvaImgW: '',
            canvaImgH: '',
            qrcode: '',
            imagePath: '',
            canvaImgUrl: ''
        };
    },
    onLoad() {
        // this.getShareQr()
    },
    beforeMount() {},
    created() {
        // this.doposter()
    },
    onShow() {},
    onReady() {},
    methods: {
        drawBackground() {
            const canvasId = 'myCanvas';
            const ctx = uni.createCanvasContext(canvasId, this);
            let that = this;
            let schoolInfo = uni.getStorageSync('schoolInfo');
            let sc = schoolInfo.customInfo.customName || '';
            let schphone = schoolInfo.customInfo.phone || '';
            let address = schoolInfo.customInfo.address || '';
            uni.getSystemInfo({
                success: function (res) {
                    if (res) {
                        that.cwidth = res.windowWidth;
                        that.cheight = res.windowHeight;
                    }
                }
            });
             
            this.qrcode = app.globalData.baseResUrl;//二维码地址
         
            if (schoolInfo && schoolInfo.customInfo && schoolInfo.customInfo.imgs) {
                this.canvaImgUrl = app.globalData.baseResUrl ;//机构背景图地址
            }  

            uni.downloadFile({
                url: this.canvaImgUrl,
                success(res) {
                    uni.getImageInfo({
                        src: that.canvaImgUrl,
                        success: function (image) {
                            that.canvaImgW = image.width;
                            that.canvaImgH = image.height;
                        }
                    });
                    ctx.drawImage(res.tempFilePath, 0, 0, that.canvaImgW, that.canvaImgH, 0, 0, that.cwidth * 0.81, that.cheight * 0.8);
                    ctx.rect(0, that.cheight * 0.65, that.cwidth * 0.81, 400);
                    ctx.setFillStyle('white');
                    ctx.fill();
                    ctx.setFillStyle('black');
                    ctx.font = 'normal bold 12px Arial,sans-serif ';
                    ctx.fillText(sc, 16, that.cheight * 0.65 + 30);
                    ctx.fillText('报名找我:' + schphone, 16, that.cheight * 0.65 + 50);
                    ctx.fillText('地址:' + address, 16, that.cheight * 0.65 + 70);
                    
                    uni.getImageInfo({
                        src: that.qrcode,
                        success: (res) => {
                            // console.log('imagePath', res);
                            ctx.drawImage(res.path, that.cwidth * 0.8 - 100, that.cheight * 0.65 + 10, 80, 80);
                            console.log('ctx',ctx)
                            ctx.draw(false, () => {
                                
                                 
                                    console.log('ctx2',ctx)
                                    uni.canvasToTempFilePath({
                                         canvasId: canvasId,
                                        success: (res) => {
                                             
                                            console.log('临时图片路径:', res.tempFilePath);
                                            that.canvasimg = res.tempFilePath;
                                            that.showposter=true;
                                            uni.hideLoading()
                                         
                                        },
                                        fail: (error) => {
                                            console.error('转化图片失败:', error);
                                        }
                                    },that);
                                    
                         
                                
                            });
                        },
                        fail: (err) => {
                            console.error(err);
                        }
                    });

                     
                }
            });
        },
        async doposter() {
            uni.showLoading({
                title: '正在生成海报'
            });
            await this.drawBackground();
        }
    }
};
</script>

<style lang="scss" scoped>
.shareTip {
    background: #fff;
    text-align: center;
    padding: 5px;
    margin-top:-5px;
}
</style>

需要注意的是this关键字

uni.canvasToTempFilePath(object, component) 组件形式需要加指向component也就是this uni.createCanvasContext(canvasId, this)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-07-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 需要注意的是this关键字
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档