图片旋转

 html 5 canvas  tag 
  canvas. getContext("2d");
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"></meta>
<script type="text/javascript">
	function rotate(direction){
		var img = document.getElementById('img');
		if(!img || !direction)return false;
		var stepNum = img.getAttribute('step');
		if(stepNum == null)stepNum=0;
		if(direction=='left'){//逆时针减一
			stepNum==0 ? stepNum=3 : stepNum--;
		}else if(direction=='right'){//顺时针加
			stepNum==3?stepNum=0:stepNum++;
		}
		img.setAttribute('step',stepNum);
		var canv = document.getElementById('canv');
		var ctx = canv.getContext('2d');
		if(canv)img.style.visibility = 'hidden'; img.style.position='absolute';
		switch(stepNum) {
			default:
			case 0 :
				canv.setAttribute('width',img.width);
				canv.setAttribute('height',img.height);
				ctx.rotate(0 * Math.PI / 180);
				ctx.drawImage(img,0,0);
				break;
			case 1:
				canv.setAttribute('width',img.height);
				canv.setAttribute('height',img.width);
				ctx.rotate( 90 * Math.PI / 180);
				ctx.drawImage(img,0,-img.height);
				break;
			case 2:
				canv.setAttribute('width',img.width);
				canv.setAttribute('height',img.height);
				ctx.rotate( 180 * Math.PI / 180);
				ctx.drawImage(img,-img.width,-img.height);
				break;
			case 3:
				canv.setAttribute('width',img.height);
				canv.setAttribute('height',img.width);
				ctx.rotate( 270 * Math.PI / 180);
				ctx.drawImage(img,-img.width,0);
				break;
		}
	}
</script>
</head>
<body>
	<ul>
	  <li>
		<div >
			<button οnclick="rotate('left')">rotate left</button>
			<button οnclick="rotate('right')">rotate right</button>
		</div>
		<div >
			<img src="123.jpg" alt="ddddddd" id="img" style="width:130px;height:150px;"/>
			<canvas id="canv" style="width:130px;height:150px;" />
		</div>
	  </li>
	</ul>
</body>

</html>
<!--       IE 8   filter : -->
function rotate(o,p){ 
    var img = document.getElementById(o); 
    if(!img || !p) return false; 
    var n = img.getAttribute('step'); 
    if(n== null) n=0; 
    if(p=='right'){ 
        (n==3)? n=0:n++; 
    }else if(p=='left'){ 
        (n==0)? n=3:n--; 
    } 
    img.setAttribute('step',n); 
    //MSIE 
    if(document.all) { 
        img.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation='+ n +')'; 
        //HACK FOR MSIE 8 
        switch(n){ 
            case 0: 
                img.parentNode.style.height = img.height; 
                break; 
            case 1: 
                img.parentNode.style.height = img.width; 
                break; 
            case 2: 
                img.parentNode.style.height = img.height; 
                break; 
            case 3: 
                img.parentNode.style.height = img.width; 
                break; 
        } 
    //DOM 
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值