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

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

月光魔力鸭

2019-09-23 11:08 阅读 1163 喜欢 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


感谢支持!

赞赏支持
提交评论
评论信息 (请文明评论)
暂无评论,快来快来写想法...
推荐
最近折腾的少了,实在没的写了,大约三四个月没更新了,先水一篇。
在web开发过程中,现在JSON 已经到了俯拾皆是的地步了,操作JSON对于JS来说非常简单,那么我们对于JSON的转化是如何应对的呢?
在通过chrome浏览器来调用摄像头的时候发现getUserMedia报错,但是本地开发却没有问题,主要原因是https环境的问题。chrome 不允许在非https和非localhost下的非安全环境进行调用。
在开发过程中经常会碰到跨frame 去操作的需求,那么如何获得这个frame呢,这里写了一个工具类,用来通过name获得frame的jq对象
通过canvas可以进行画图实现一些动画效果等,今天练习下通过canvas来实现一个简易的电子画板,可以在白板上进行画画,然后指定不同的颜色、线条粗细,加载不同的背景以及擦除效果。
偶尔练习下canvas,这里简单记录下常用API,防止遗忘..加深记忆..努力提高..争取突破...daydayup
在页面中不同的frame之间进行相互调用的话,我们可以通过frame获取对应的window然后进行调用,但是如果是浏览器不同的tab之间呢?
之前的时候都是在各大主机厂商手动进行申请免费的,直到阿里的免费期限变更为3个月.. 我就开始觉的有些麻烦了,还不如使用这个let's encrypt进行部署呢。