思路:vue中图片合并

  首先准备好要合并的背景图,和请求后得到的二维码,

canvas画图,将两张背景图和一张二维码用canvas画出来,

将canvas再转为img

注意canvas和图片的清晰图和图片的尺寸位置

开始时canvas是隐藏的,两张背景图时显示的,当canvas画完后再转为img的时候,隐藏canvas和背景图,显示canvas转完的图片(也就是合并后的图片)

这个适配方式可能有些瑕疵,所以会加了很多设备的判断

代码:

  html

    

<div class="wap-poster" ref="imageWrapper" id="target" style="width: 100%;">
<canvas id="mycanvas" width="100%" height="100%" v-if="!infactQrCode" style="transform: scale(2);display: none"></canvas>
<img v-if="!infactQrCode" src="../../../assets/imgs/posterbg.jpg" id="bgImg" alt="" style="width: 100%;height:100%;">
<img v-if="!infactQrCode" src="../../../assets/imgs/poster0.png" id="poster" alt="" style="position: absolute;">
<img v-if="!infactQrCode" id="qrcode" :src="qrCodeImg" alt="" style="position: absolute;top:247px;width: 140px;height: 140px;display: none">
<img :src="infactQrCode" alt="" v-if="infactQrCode" style="width: 100%;height:100%">
</div>

js<script>

  import html2canvas from  'html2canvas'
export default {
name: 'Poster',
data() {
return {
qrCodeImg: "",
infactQrCode:"",
}
},
created(){
    
//二维码图片后台返回
this.qrCodeImg="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1568183940&di=49b7187fdb7fee3e30c20583150f09b6&imgtype=jpg&er=1&src=http%3A%2F%2Fdown.admin5.com%2Fuploads%2Fallimg%2F170630%2F2160_170630144656_1.jpg";
},
methods: {
drawImg(){
let _self=this;
var canvas = document.getElementById("mycanvas");
var a = setInterval(() =>{
// 重复获取
canvas = document.getElementById("mycanvas");
if(!canvas){
return false
} else {
clearInterval(a);
var context = canvas.getContext('2d');
context.scale(,) //图片清晰度解决办法,缩放2倍后,其他图片的宽高也要比ui设计的宽高乘以2
var img = new Image();
img.setAttribute('crossOrigin', 'anonymous');
let bgUrl=document.getElementById("bgImg").src;
img.src=bgUrl;
img.onload = function(){
if(img.complete){
          //画第一张背景图,图片的宽高撑满整个屏幕  
context.drawImage(img,,,window.screeWidth,window.screeHeight);
var img1 = new Image();
let bgUrl1=document.getElementById("poster");
img1.src=bgUrl1.src;
img1.setAttribute('crossOrigin', 'anonymous');
img1.onload = function(){
            //img1为第2张背景图,开始画第2张图,图片的的定位,left为整个屏幕-ui设计图片的宽*图片的适配比例,最后除以2保证图片居中,
            //图片的left top width height都是这样计算的,
if(img1.complete){
var left = (window.screeWidth-*window.rateWidth)/;
if(window.screeHeight>=&&window.screeWidth<){ //iphonx的计算方式
context.drawImage(img1,left,*window.rateWidth,*window.rateWidth,*window.rateHeight);
}else{
context.drawImage(img1,left,*window.rateWidth,*window.rateWidth,*window.rateHeight);
} var img2 = new Image();
img2.src=document.getElementById("qrcode").src;
img2.crossOrigin="*";
img2.onload = function(){
if(img2.complete){
              //二维码图片的画图left top widht height
var left = (window.screeWidth-*window.rateWidth)/;
if(window.screeWidth==&&window.screeHeight==){ //ipad
var left = (window.screeWidth-*window.rateWidth)/;
context.drawImage(img2,left,*window.rateHeight,*window.rateWidth,*window.rateWidth);
var image = new Image();
_self.infactQrCode=canvas.toDataURL("image/png");
return
}
if(window.screeWidth>=&&window.screeHeight>){ //其他设备
var left = (window.screeWidth-*window.rateWidth)/;
context.drawImage(img2,left,*window.rateHeight,*window.rateWidth,*window.rateWidth);
}else if (window.screeHeight>=&&window.screeWidth<){
context.drawImage(img2,left,*window.rateHeight,*window.rateWidth,*window.rateWidth);
}else{
context.drawImage(img2,left,*window.rateHeight,*window.rateWidth,*window.rateWidth);
}               //以上为2张背景图和一张二维码画合并后图画到canvas的结果,下面为将canvas转为图片的方法
var image = new Image();
_self.infactQrCode=canvas.toDataURL("image/png");
}
}
}
}
}
}
}
},)
},
},
mounted(){
this.drawImg(); //主要代码再该方法,将图片转为canvas,再将canvas转为图片,因为canvas是无法长按保存和识别二维码的
const that = this;
window.screeWidth=window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
window.screeHeight= window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
var left = (window.screeWidth-)/;
var qrcode=document.getElementById("qrcode");
qrcode.style.left=left+'px';
this.screeWidth=window.screeWidth;
var canvas = document.getElementById("mycanvas");
//适配
window.rateWidth = window.screeWidth/ ;
window.rateHeight = window.screeHeight/;
//清晰度解决办法
canvas.setAttribute("width",window.screeWidth*);
canvas.setAttribute("height",window.screeHeight*); let bgUrl1=document.getElementById("poster");
var left = (window.screeWidth-*window.rateWidth)/;
if(window.screeHeight>=&&window.screeWidth<){
bgUrl1.style.left=left+"px";
bgUrl1.style.top=*window.rateWidth+"px";
bgUrl1.style.width=*window.rateWidth+"px";
bgUrl1.style.height=*window.rateHeight+"px";
}else{
bgUrl1.style.left=left+"px";
bgUrl1.style.top=*window.rateWidth+"px";
bgUrl1.style.width=*window.rateWidth+"px";
bgUrl1.style.height=*window.rateHeight+"px";
} },
}
</script>

js多张图片合成一张图,canvas(海报图,将二维码和背景图合并) -----vue的更多相关文章

  1. .NET生成小程序码,并合自定义背景图生成推广小程序二维码

    前言: 对于小程序大家可能都非常熟悉了,随着小程序的不断普及越来越多的公司都开始推广使用起来了.今天接到一个需求就是生成小程序码,并且于运营给的推广图片合并在一起做成一张漂亮美观的推广二维码,扫码这种 ...

  2. javascript利用canvas解析图片中的二维码

    本方法两种应用方式:一种使用canvas解析本站图片中的二维码,canvas有同源策略限制,只能处理本站图片.另一种处理文件选择中的图片二维码. 第一种使用场景可以换成像微信中一样,长按图片识别二维码 ...

  3. C#/.net 通过js调用系统相机进行拍照,图片无损压缩后进行二维码识别

    这两天撸了一个需求,通过 JS  调用手机后置相机,进行拍照扫码.前台实现调用手机相机,然后截取图片并上传到后台的功能.后台接收传过来的图片后,通过调用开源二维码识别库 ZXing 进行二维码数据解析 ...

  4. 使用js生成二维码和条形码

    1.生成二维码 使用github开源项目qrcode. 1.引入方式一(js cdn引入): ①.引入qrcode cdn: 自行下载..没有合适的cdn,地址 <script src=&quo ...

  5. 利用phpqrcode二维码生成类库合成带logo的二维码并且用合成的二维码生成海报(二)

    前期准备 引入phpqrcode类库(下载地址:https://download.csdn.net/download/weixin_37557729/11891240:支持彩色二维码的下载地址:htt ...

  6. jquery.qrcode.min.js(支持中文转化二维码)

    详情请看:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/jqueryqrcodeminjs/ 今天还是要讲一下关于二维码的知识,前 ...

  7. js将网址转为二维码并下载图片

    将一个网址转为二维码, 下面可以添加文字, 还提供下载功能 利用的是 GitHub上面的qrcode.js 和canvas <!DOCTYPE html> <html> < ...

  8. node 无脑生成小程序二维码图

    RT 新建createwxaqrcode.js: const request = require('request') const fs = require('fs') // eg:生成购物车列表圆形 ...

  9. jquery.qrcode.min.js生成二维码

    jquery.qrcode.min.js是一款可以生成二维码的插件,使用前提是先引入jquery,因为jquery.qrcode.min.js依赖jquery. 基本用法 1.引入js <scr ...

随机推荐

  1. uniapp开发微信小程序跳转出现navigateTo:fail page "pages/user/pages/user/address/address" is not found

    在app.json文件中pages中: ,{ "path" : "pages/user/address/address", "style" ...

  2. [转载]blktrace分析IO

    前言 上篇博客介绍了iostat的一些输出,这篇介绍blktrace这个神器.上一节介绍iostat的时候,我们心心念念希望得到块设备处理io的service time,而不是service time ...

  3. input输入框如何只能输入非零开头的正整数

    input输入框如何只能输入非零开头的正整数 ********* 废话不多说,先来代码 ********* case1: 原生html + javascript <body> <!- ...

  4. Linux压缩工具

    一.gzip/gunzip/zcat gzip, gunzip, zcat - compress or expand files gzip [ option .... ] [ filenames .. ...

  5. VMware Guest customization fails on Linux

    1.1  症状现象 登录Guest OS,在/var/log/vmware-imc/toolsDeployPkg.log文件中,您会看到以下条目: Customization command fail ...

  6. Java连接MQTT服务-wss方式

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  7. webpack学习之路--demo1

    1.不使用框架建立webpack项目时 (1).npm init -y 生成package.json文件 (2).npm install --save-dev webpack 在当前项目下安装webp ...

  8. What is the !! (not not) operator in JavaScript?

    What is the !! (not not) operator in JavaScript? 解答1 Coerces强制 oObject to boolean. If it was falsey ...

  9. Cross-Multimedia dataset

    Wikipedia: http://www.svcl.ucsd.edu/projects/crossmodal/ PKU Xmedia: https://github.com/yeqinglee/mv ...

  10. 如何解决错误【selenium.common.exceptions.SessionNotCreatedException】

    如何解决错误[selenium.common.exceptions.SessionNotCreatedException]   [问题起因] 2018年12月26日晚,启动我的pycharm准备学习s ...