需求如下:有一张大图,需要显示大图中的一小部分,目前能知道的时候小图的宽高和坐标,同时大图有一个旋转角度可以知道,目标就是把小图正确的显示出来。
实际上,如果只针对大图的显示和旋转,可以通过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