js多张图片合成一张图,canvas(海报图,将二维码和背景图合并) -----vue
思路: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的更多相关文章
- .NET生成小程序码,并合自定义背景图生成推广小程序二维码
前言: 对于小程序大家可能都非常熟悉了,随着小程序的不断普及越来越多的公司都开始推广使用起来了.今天接到一个需求就是生成小程序码,并且于运营给的推广图片合并在一起做成一张漂亮美观的推广二维码,扫码这种 ...
- javascript利用canvas解析图片中的二维码
本方法两种应用方式:一种使用canvas解析本站图片中的二维码,canvas有同源策略限制,只能处理本站图片.另一种处理文件选择中的图片二维码. 第一种使用场景可以换成像微信中一样,长按图片识别二维码 ...
- C#/.net 通过js调用系统相机进行拍照,图片无损压缩后进行二维码识别
这两天撸了一个需求,通过 JS 调用手机后置相机,进行拍照扫码.前台实现调用手机相机,然后截取图片并上传到后台的功能.后台接收传过来的图片后,通过调用开源二维码识别库 ZXing 进行二维码数据解析 ...
- 使用js生成二维码和条形码
1.生成二维码 使用github开源项目qrcode. 1.引入方式一(js cdn引入): ①.引入qrcode cdn: 自行下载..没有合适的cdn,地址 <script src=&quo ...
- 利用phpqrcode二维码生成类库合成带logo的二维码并且用合成的二维码生成海报(二)
前期准备 引入phpqrcode类库(下载地址:https://download.csdn.net/download/weixin_37557729/11891240:支持彩色二维码的下载地址:htt ...
- jquery.qrcode.min.js(支持中文转化二维码)
详情请看:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/jqueryqrcodeminjs/ 今天还是要讲一下关于二维码的知识,前 ...
- js将网址转为二维码并下载图片
将一个网址转为二维码, 下面可以添加文字, 还提供下载功能 利用的是 GitHub上面的qrcode.js 和canvas <!DOCTYPE html> <html> < ...
- node 无脑生成小程序二维码图
RT 新建createwxaqrcode.js: const request = require('request') const fs = require('fs') // eg:生成购物车列表圆形 ...
- jquery.qrcode.min.js生成二维码
jquery.qrcode.min.js是一款可以生成二维码的插件,使用前提是先引入jquery,因为jquery.qrcode.min.js依赖jquery. 基本用法 1.引入js <scr ...
随机推荐
- qt5---QMainWindow
QMainWindow是一个为用户提供主窗口程序的类,包含一个菜单栏(menu bar).多个工具栏(tool bars).多个锚接部件(dock widgets)[就是浮动窗口].一个状态栏(sta ...
- ubuntu安装wireshark
$sudo apt-add-repository ppa:wireshark-dev/stable$sudo apt update$sudo apt install wireshark 出于安全方面的 ...
- 科普TPF知识
https://tieba.baidu.com/p/4926092734?see_lz=1&pn=1 707680700 https://tieba.baidu.com/p/492609273 ...
- pdf转换
namespace Utilities { public static class PDFHelper { /// <summary> /// Html转Pdf /// </summ ...
- Mybatis-Plus的BaseMapper的用法
1.如何使用BaseMapper进行数据库的操作. 2.使用BaseMapper进行插入实体时如何让UUID的主键自动生成. Student实体类,其中id属性主键为UUID package com. ...
- Codeforces 1213C Book Reading
cf题面 中文题意 多组数据,每组给一个n给一个m,要求正整数\(1\)~\(n\)中,所有能被m整除的数的个位之和. 解题思路 首先,能被m整除的数的数量是\(\lfloor\frac{n}{m}\ ...
- 「TJOI2019」甲苯先生的滚榜
题目链接 问题分析 参照数据范围,我们需要一个能够在\(O(n\log n)\)复杂度内维护有序数列的数据结构.那么平衡树是很好的选择.参考程序中使用带旋Treap. 参考程序 #pragma GCC ...
- 高性能JavaScript之加载和执行
JS在浏览器中的性能,可以认为是开发者所面临的最重要的可行性问题.这个问题因JS的阻塞特性变得复杂,也就是说当浏览器在执行JS代码时,不能同时做其他任何事情.事实上,大多数浏览器都使用单一进程来处理U ...
- C++入门经典-例2.3-在print函数中使用附加符号
1:代码如下: // 2.3.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" int main() { ; /*定义长整型变量,为其赋值*/ ...
- HTML userAgent
<html> <body> <script type="text/javascript"> document.write("<p ...