dom 元素生成海報(bào) 附帶二維碼

安裝

npm i qrcodejs2
npm i html2canva

引入

import saveQRcode from "@/utils/saveQRcode";  
import QRCode from "qrcode";
import html2canvas from "html2canvas";

saveQRcode 方法請(qǐng)前往畫布(canvas)圖像保存成本地圖片

使用

樣式圖片


樣式圖片

html

<template>
  <div class="main">
    <!-- 海報(bào)html元素 -->
    <div class="content">
      <div id="posterHtml" class="poster">
        <div class="product-pic">
          <img src="@/assets/images/lxsc.jpg" alt />
        </div>
        <div class="product">
          <!-- 底部標(biāo)題 -->
          <div class="product-info">
            <p class="product-info-sign">餐飲食材供應(yīng)商</p>
          </div>
          <!-- 底部紅色提示 -->
          <div class="product-info">
            <p class="product-info-name">{{ product.detailTitle }}</p>
          </div>
          <!-- 二維碼 -->
          <div class="qrcode">
            <canvas ref="qrcodeImg"></canvas>
          </div>
        </div>
      </div>
      <van-button class="btn-down" size="small" round @click="createPoster">點(diǎn)擊保存</van-button>
    </div>
  </div>
</template>

js

data() {
    return {
      posterContent: "冷鮮食品商城",
      product: {}
    };
  },
  mounted() {
      this.createQrcode();
  },
  methods: {
    createQrcode(text) {
      // 生成二維碼
      let codeLink = window.location.protocol + "http://" + window.location.host;
      var canvas = this.$refs.qrcodeImg;
      QRCode.toCanvas(canvas, codeLink, error => {
        if (error) console.error(error);
      });
    },

    createPoster() {
      // 生成海報(bào)
      const domObj = document.getElementById("posterHtml");
      html2canvas(domObj, {
        useCORS: true, // 防止跨域,效果并不好
        allowTaint: false,
        logging: false,
        letterRendering: true,
        onclone(doc) {
          let e = doc.querySelector("#posterHtml");
          e.style.display = "block";
        }
      }).then(function(canvas) {
        // 保存 canvas
        saveQRcode(canvas.toDataURL("image/png"));
      });
    },
  }

css

.main {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, .7);
  overflow: hidden;
  .content {
    position: fixed;
    left: 50%;
    top: 50%;
    width: 75%;
    transform: translate(-50%, -50%);
    .poster {
      width: 100%;
      border-radius: 8px;
      overflow: hidden;
      background-color: white;
      .product-pic {
        width: 100%;
        max-height: 280px;
        min-height: 150px;
        overflow: hidden;
        img {
          width: 100%;
        }
      }
      .product {
        display: flex;
        justify-content: space-between;
        padding: 0 20px 10px 20px;
        .product-info {
          display: flex;
          flex-direction: column;
          justify-content: space-around;
          .product-info-name {
            font-size: 14px;
            color: #333;
          }
          .product-info-price {
            padding: 0 10px;
            background-color: #f33;
            font-size: 18px;
            color: white;
            text-align: center;
            border-radius: 10px;
          }
          .product-info-sign {
            padding: 0 4px;
            background-color: #f33;
            font-size: 12px;
            color: white;
            text-align: center;
          }
        }
      }
    }
    .btn-down {
      position: absolute;
      left: 50%;
      bottom: -50px;
      transform: translateX(-50%);
    }
  }
}

.order-share {
  border: 1px solid #ff6f6f;
  height: 30px;
  padding: 0 8px;
  margin-top: 5px;
  background: white;
  color: #ff6f6f;
  line-height: 30px;
  border-radius: 4px;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容