vue生成海报(vue-canvas-poster)

安装 vue-canvas-poster 插件

npm i --save vue-canvas-poster 

封装成组件(微信长按保存图片)

<template>
    <div class="talent_poster">
        <vue-canvas-poster
            :widthPixels="1000"
            :painting="painting"
            @success="canvasSuccess"
            @fail="canvasFail"
        ></vue-canvas-poster>
        <img :src="posterImg" />
        <div class="tipTxt">长按保存或分享</div>
        <!--关闭-->
        <div style="width: 100%; text-align: center">
            <div class="cancleBtn" @click="$emit('hideposter')">
                <span id="close"></span>
            </div>
        </div>
    </div>
</template>
<script>
import wx from "weixin-js-sdk";
export default {
    middleware: "",
    props: {},
    components: {},
    data() {
        return {
            showPoster: true,
            posterImg: "", //生成的海报
            painting: {
                width: "750px",
                height: "1168px",
                background: "",
                views: [
                    {
                        type: "qrcode",
                        content: "",
                        background: "transparent",
                        css: {
                            bottom: "350px",
                            left: "0",
                            right: "275px",
                            color: "#000",
                            background: "#fff",
                            width: "0px",
                            height: "0px",
                            borderWidth: "10px",
                            borderColor: "#fff",
                        },
                    },
                    {
                        type: "text",
                        text: "",
                        css: {
                            top: "36%",
                            right: "13.5%",
                            width: "325px",
                            maxLines: 1,
                            textAlign: "center",
                            fontSize: "70px",
                            color: "#0068B7",
                        },
                    },
                    {
                        type: "text",
                        text: "",
                        css: {
                            top: "44%",
                            right: "13.5%",
                            width: "325px",
                            maxLines: 1,
                            textAlign: "center",
                            fontSize: "70px",
                            color: "#0068B7",
                        },
                    },
                ],
            },
        };
    },
    created() {},
    methods: {
        open(item) {
            if (item.paintingWidth || item.paintingHeight) {
                this.painting.width = item.paintingWidth;
                this.painting.height = item.paintingHeight;
            }
            this.painting.background = item.bgImg;
            this.painting.views[0].content = item.codeUrl;
            this.painting.views[0].css = item.css;
            if (item.textObj) {
                this.painting.views[1].text = item.textObj.txt1;
                this.painting.views[2].text = item.textObj.txt2;
            }
        },
        canvasSuccess(src) {
            this.posterImg = src;
        },
        canvasFail(err) {
            alert(err);
        },
        saveImg() {
            wx.saveImageToPhotosAlbum({
                filePath: this.posterImg,
                success(res) {},
                fail(err) {
                    console.log(err);
                },
            });
        },
    },
};
</script>
<style lang="less" scoped>
.talent_poster {
    position: fixed;
    width: 80%;
    top: 50%;
    left: 50%;
    z-index: 999;
    transform: translate(-50%, -50%);
    margin: 0 auto;

    img {
        width: 100%;
        height: 100%;
        margin-bottom: 10px;
    }

    .tipTxt {
        text-align: center;
        color: #ccc;
    }
}
</style>
 

使用组件

let data = {
    paintingWidth: '816px',
    paintingHeight: '1219px',
    bgImg:‘’,
    codeUrl:‘’,//网页地址
    css: {
       bottom: '14.5%',
       left: '0',
       right: '34.5%',
       width: '260px',
       height: '260px',
   },
}
this.$refs.invitePoster.open(data)

效果

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值