移動端 canvas插入多張圖片生成一張可保存到手機圖片


第一次寫隨筆,想把開發中遇到的問題與大家分享,可能會讓您少走一步彎路。

先看下效果圖:

代碼分三部分為大家展示:

1、html 部分

<div id="myQrcontainer">
  <canvas id="canvas_box"></canvas>
  <img src="" id="imgShow"/>
</div>

2、css 部分

body,html{
  width: 100%;
  height: 100%;
}
#myQrcontainer,#canvas_box,#imgShow{
  width: 100%;
  height: 100%;
}

移動端基於微信公眾號開發,生成的圖片可以喚起“保存圖片”功能,但是點擊“保存圖片”沒有反應;

解決方法:

1、canvas畫完圖之后要生成base64圖片;動態添加到 img 的src中

    toDataURL();

2、跨域問題,一定給img對象添加

     img.crossOrigin="anonymous";

js部分:

 

//封裝畫矩形的方法
var _that =this;
function roundedRect(ctx,x,y,width,height,radius){
  ctx.beginPath();
  ctx.moveTo(x,y+radius);
  ctx.lineTo(x,y+height-radius);
  ctx.quadraticCurveTo(x,y+height,x+radius,y+height);
  ctx.lineTo(x+width-radius,y+height);
  ctx.quadraticCurveTo(x+width,y+height,x+width,y+height-radius);
  ctx.lineTo(x+width,y+radius);
  ctx.quadraticCurveTo(x+width,y,x+width-radius,y);
  ctx.lineTo(x+radius,y);
  ctx.quadraticCurveTo(x,y,x,y+radius);
  ctx.stroke();
};
function drawQrcode(){
  var nWidth = document.body.clientWidth,//屏幕可視區域 寬度
  nHeight = document.body.clientHeight,//屏幕可視區域 高度
  _canvasWidth = document.body.clientWidth*2,//畫布 寬度
  _canvasHeight = document.body.clientHeight*2;//畫布 高度
  //開始畫圖,獲取上下文;
  var _canvas = document.getElementById("canvas_box");
  _canvas.width = _canvasWidth;
  _canvas.height = _canvasHeight;
  var _context = _canvas.getContext('2d');
  //背景
  _context.fillStyle="#f3af4c";
  _context.fillRect(0,0,nWidth*2,nHeight*2);
  //白色矩形部分
  _context.moveTo(40,203);
  _context.strokeStyle = 'rgba(255,255,255,1)';
  _context.fillStyle = 'rgba(255,255,255,1)';
  _that.roundedRect(_context,40,70*2,335*2,489*2,10*2);
  _context.fill();
  _context.closePath();
  var _imagehead = new Image();//頭像
  //如果有跨域問題,請給img對象添加如下屬性
  _imagehead.crossOrigin="anonymous";
  _imagehead.src = 'http://img1.imgtn.bdimg.com/it/u=3664893832,2142990655&fm=26&gp=0.jpg';
  _imagehead.onload = function(){
    _context.save(); // 保存當前_context的狀態
    _context.beginPath();
    _context.moveTo(((nWidth)/2+40/375*nWidth)*2,70.28/603*nHeight*2);
    _context.lineWidth="20";
    //畫出圓
    _context.arc(nWidth,70.28/603*nHeight*2,40/375*nWidth*2,0,2*Math.PI,true);
    //圓有個邊框
    _context.lineWidth=20;
    _context.strokeStyle = 'rgba(255,197,108,14)';
    _context.fill();
    _context.stroke();
    //裁剪上面的圓形
    _context.clip();
    // 在剛剛裁剪的園上畫圖
    _context.drawImage(_imagehead, (nWidth/2-40)*2, 30*2, 90*2, 90*2);
    _context.restore();
    _context.stroke();
    //頭像下面的文字
    _context.beginPath();
    _context.textAlign = "center";
    //設置字體
    _context.font = '30px Arial';
    _context.lineWidth = 1.0;
    _context.fillStyle = 'rgb(73,73,73)';
    _context.fillText("任小超", nWidth, 150*2);

 

    //onload是異步加載,所以要等第一個onload 加載完畢再畫第二張圖片
    //代言文字圖片
    var _imagetext = new Image();
    //解決跨域,如果有跨域錯誤信息一定要加此屬性;
    _imagetext.crossOrigin="anonymous";
    _imagetext.src ='https://cdn.kaishuhezi.com/kstory/activity_flow/image/a0364809-6289-474e-a5da-4aca336541cb.png';
    _imagetext.onload =function(){
      _context.save(); // 保存當前_context的狀態
      _context.drawImage(_imagetext, (nWidth-200)/2*2, 170*2,200*2,25*2);
      _context.stroke();//
      _context.closePath();

      //canvas 畫完圖 一定要生成圖片流,作為img 的src屬性值,同時隱藏canvas,只展示img 就ok了,在手機上試試長按保存功能吧

      var _imgSrc = _canvas.toDataURL("image/png",1);
      _canvas.style.display="none";
      var imgShow = document.getElementById('imgShow');
      imgShow.setAttribute('src', _imgSrc);
    }
  }
}
drawQrcode();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM