zoukankan      html  css  js  c++  java
  • JS 用画布讲多张图、有规律的拼接成一张图

    <view class="canvasBox">
    <canvas canvas-id="shareCanvas" style="610px;height:610px"></canvas>
    </view>

    //生成封面
    createPic(){
    let imgArr = [
    'http://cook.file.chuyitech.com/jpg/20200504/e1845a945683d8368c0f04c53fef0b61.jpg',
    'http://cook.file.chuyitech.com/jpg/20200305/cdf5393cae52c19b02ae8c61017576f8.jpg',
    'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1341118893,346019396&fm=26&gp=0.jpg',
    'http://cook.file.chuyitech.com/jpg/20200504/e1845a945683d8368c0f04c53fef0b61.jpg',
    'http://cook.file.chuyitech.com/jpg/20200305/cdf5393cae52c19b02ae8c61017576f8.jpg',
    'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1341118893,346019396&fm=26&gp=0.jpg',
    'http://cook.file.chuyitech.com/jpg/20200504/e1845a945683d8368c0f04c53fef0b61.jpg',
    'http://cook.file.chuyitech.com/jpg/20200305/cdf5393cae52c19b02ae8c61017576f8.jpg',
    'http://cook.file.chuyitech.com/jpg/20200305/cdf5393cae52c19b02ae8c61017576f8.jpg',
    ];
    const ctx = wx.createCanvasContext('shareCanvas');
    let _this = this;
    this.picture(0,imgArr,ctx,_this);
    },
    //递归画图
    picture: function (i,imgArr,ctx,_this) {
    //获取url图信息
    wx.getImageInfo({
    src: imgArr[i],
    success(res) {
    let globalWidth; //图片目标宽
    let globalHeight; //图片目标高
    let offX ;
    let offY;
    if(imgArr.length >= 9){
    globalWidth = 200;
    globalHeight = 200;
    offX = _this.getX(globalWidth,5,3,i);
    offY = _this.getY(globalHeight,5,3,i);
    }else if(imgArr.length >= 4){
    globalWidth = 305;
    globalHeight = 305;
    offX = _this.getX(globalWidth,5,2,i);
    offY = _this.getY(globalHeight,5,2,i);
    }else{
    globalWidth = 610;
    globalHeight = 610;
    offX = _this.getX(globalWidth,0,1,i);
    offY = _this.getY(globalHeight,0,1,i);
    }
    let obj = _this.getXYWH(parseInt(res.width), parseInt(res.height), parseInt(globalWidth), parseInt(globalHeight));
    //绘制图
    ctx.drawImage(res.path, obj.x, obj.y, obj.w, obj.h, offX, offY, globalWidth, globalHeight);
    if(i<imgArr.length-1){
    _this.picture(i+1,imgArr,ctx,_this);
    }else{
    ctx.stroke();
    ctx.draw(false, _this.drawPicture());
    }
    },error(){
    if(i<imgArr.length-1){
    _this.picture(i+1,imgArr,ctx,_this)
    }else{
    ctx.stroke();
    ctx.draw(false, _this.drawPicture());
    }
    }
    })
    },
    //生成图片
    drawPicture: function () {
    //延时解决 ctx.draw异步不了的坑
    setTimeout(()=>{
    wx.canvasToTempFilePath({
    x: 0,
    y: 0,
    610,
    height: 610,
    destWidth: 610,
    destHeight: 610,
    fileType:'jpg',
    canvasId: 'shareCanvas',
    success: function (res) {
    utils.uploadFile(api.FileUpload, res.tempFilePath, {}).then(function (data) {
    let newRes = JSON.parse(data);
    if (newRes.code === 200) {

    }
    });
    },
    })
    },300)
    },

    //获取原图新宽高偏移量
    getXYWH(width, height, globalWidth, globalHeight){
    let sourceWidth=0;
    let sourceHeight=0;
    //宽大于高
    if(width*globalHeight/globalWidth>height){
    sourceHeight=height;
    sourceWidth=height*globalWidth/globalHeight;
    }else{
    //高大于宽
    sourceHeight=width*globalHeight/globalWidth;
    sourceWidth=width;
    }
    let sourceX=(width-sourceWidth)/2;
    let sourceY=(height-sourceHeight)/2;
    let obj={};
    obj["x"]=sourceX;
    obj["y"]=sourceY;
    obj["w"]=sourceWidth;
    obj["h"]=sourceHeight;
    return obj;
    },

    //获取新图X偏移量
    getX(globalWidth,link,rowSize,index){
    let x = 0.0;
    x = (globalWidth+link) * (index%rowSize);
    return x;
    },
    //获取新图Y偏移量
    getY(globalHeight,link, rowSize, index){
    let y = 0.0;
    y = (globalHeight+link) * parseInt((index/rowSize));
    return y;
    }

  • 相关阅读:
    二分+RMQ/双端队列/尺取法 HDOJ 5289 Assignment
    思维题 HDOJ 5288 OO’s Sequence
    树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
    最大流增广路(KM算法) HDOJ 1853 Cyclic Tour
    最大流增广路(KM算法) HDOJ 1533 Going Home
    最大流增广路(KM算法) HDOJ 2255 奔小康赚大钱
    Complete the Word CodeForces
    Gadgets for dollars and pounds CodeForces
    Vasya and Basketball CodeForces
    Carries SCU
  • 原文地址:https://www.cnblogs.com/77yf/p/13491436.html
Copyright © 2011-2022 走看看