canvas 旋转图片并展示图片中的小部分

canvas 旋转图片并展示图片中的小部分

月光魔力鸭

2019-09-23 11:08 阅读 1003 喜欢 0 canvas 图片旋转

需求如下:有一张大图,需要显示大图中的一小部分,目前能知道的时候小图的宽高和坐标,同时大图有一个旋转角度可以知道,目标就是把小图正确的显示出来。

实际上,如果只针对大图的显示和旋转,可以通过css就能搞定,但是这里面有个问题,旋转后实际上他原来的空间不变,会导致并不美观,最终还是决定通过canvas来实现。

大图的旋转显示

image.onload = function(){
    var img = this;
    var width = img.width;
    var height = img.height;
    
    //针对角度来算
    var canvas = document.createElement('canvas');
    var ctx = canvas.getContext('2d');
    var direction = ((parseInt(rotate,10) + 360) % 360) / 90;
    switch (direction) {
      case 1: {
        canvas.width = height;
        canvas.height = width;
        ctx.rotate(90*Math.PI/180);
        ctx.drawImage(img, 0, -height,width,height);
        break;
      }
      case 2: {
        canvas.width = width;
        canvas.height = height;
        ctx.rotate(180*Math.PI/180);
        ctx.drawImage(img, -width, -height,width,height);
        break;
      }
      case 3: {
        canvas.width = height;
        canvas.height = width;
        ctx.rotate(270*Math.PI/180);
        ctx.drawImage(img, -width, 0,width,height);
        break;
      }
      case 0:
      case 4:
      default: {
        canvas.width = width;
        canvas.height = height;
        ctx.rotate(0);
        ctx.drawImage(img, 0, 0,width,height);
        break;
      }
    }
    $item.get(0).appendChild(canvas);
}

大图旋转和小图展示旋转其实是一样的,只是在canvas drawImage 的时候多几个参数而已。

小图展示旋转

image.onload = (function(direction,filePath,div,showwidth,showheight,sshowwidth,sshowheight,x,y,imgIndex){
    return function(){
    var img = this;
    var thiz = img;
    var width = img.width;
    var height = img.height;
    var canvas = document.createElement('canvas');
    canvas.style.position='relative';
    var ctx = canvas.getContext('2d');
    console.log(direction);
    var canvasX = 0,canvasY = 0;
    switch (direction) {
      case 1: {
        canvas.width = showheight;
        canvas.height = showwidth;
        ctx.rotate(90*Math.PI/180);
        ctx.drawImage(thiz,x,y,showwidth,showheight,0,-showheight,showwidth,showheight);
        break;
      }
      case 2: {
        canvas.width = showwidth;
        canvas.height = showheight;
        ctx.rotate(180*Math.PI/180);
        ctx.drawImage(thiz,x,y,showwidth,showheight,-showwidth,-showheight,showwidth,showheight);
        break;
      }
      case 3: {
        canvas.width = showheight;
        canvas.height = showwidth;
        ctx.rotate(270*Math.PI/180);
        ctx.drawImage(thiz,x,y,showwidth,showheight,-showwidth,0,showwidth,showheight);
        break;
      }
      case 0:
      case 4:
      default: {
        canvas.width = showwidth;
        canvas.height = showheight;
        ctx.rotate(0);
        ctx.drawImage(thiz,x,y,showwidth,showheight,0,0,finalWidth,finalHeight);
        break;
      }
    }
    div.appendChild(canvas);

    div.setAttribute('index',imgIndex);//设置索引
    div.style.width = sshowwidth+'px';
    div.style.height = sshowheight+'px';
    //根据index 查找比对前后插入。
    var sd = document.getElementById('showDetail');
    var cn = sd.childNodes;
    if(cn.length == 0){
        sd.appendChild(div);
    }else{
        //循环找到
        var diff = 10,nearNode = null,isBefore = false;
        cn.forEach(function(tempNode,nodeIndexL){
            var nodeIndex = parseInt(tempNode.getAttribute('index'),10);
            //比对哪个更接近imgIndex
            var currentDiff = Math.abs(nodeIndex - imgIndex);
            if(currentDiff < diff && nodeIndex > imgIndex){
                diff = currentDiff;
                nearNode = tempNode;
                isBefore = true;
            }
        })
        if(isBefore){
            sd.insertBefore(div,nearNode);
        }else{
            sd.appendChild(div);
        }
    }
    
    }
})(direction,filePath,div,showwidth,showheight,sshowwidth,sshowheight,x,y,i);
                        

我这里参数比较多,主要就看switch 里面的那一部分就可以。 我这边的需求是需要显示多个大图的里面的小图,有小图的坐标,然后根据小图坐标展示每个小图图片,并正常显示。

最终的原理就是,旋转canvas,然后将图片渲染并显示部分坐标的内容,最终旋转回来展示。 具体可以查看drawImage的参数说明。

参数说明

转载请注明出处: https://chrunlee.cn/article/canvas-pic-rotate-show.html


感谢支持!

赞赏支持
提交评论
评论信息 (请文明评论)
暂无评论,快来快来写想法...
推荐
当一些业务必须通过横屏来实现,但是又没有原生来做,只能通过h5的时候怎么办?
问题是由一个BUG引起的,功能中有使用国际化组件,用的是jquery.i18n,在chrome上、ff上都没有什么问题,问题出在了IE上。万恶的IE..
最近做个nodejs的项目,使用了thinkjs 3.0 的框架,编辑器为vs code ,之前用的好好的,每次 . 后都有提示的,可是使用了多模块后发现.. model的提示没有了..
this 是 JavaScript 的一大难点,多年经验的前端程序员都可能对这方面模糊。this 在大量的函数、类库中都有使用,理清显式绑定和隐式绑定有助于理解或书写这类函数。
先记录下,不定哪天就查了..防止找不到或不全
在通过chrome浏览器来调用摄像头的时候发现getUserMedia报错,但是本地开发却没有问题,主要原因是https环境的问题。chrome 不允许在非https和非localhost下的非安全环境进行调用。
项目中需要使用treegrid,找了下easyui 和 ext都有,但是项目用的框架是 byyui,如果为了treegrid 就把这些都加载的话,感觉不太合算。找了大家常用的基于jquery的treegrid.
通过jspdf ,我们可以将页面或图片生成pdf下载下来,如果是一些复杂的页面,我们可以将页面转成图片,然后把图片加入到pdf中,生成并下载。