1. 首页
  2. SEO技术

html5 canvas rotate属性实现图片旋转

gooood个人博客网站

canvas

html5 canvas rotate属性实现图片旋转
<!doctype html> <html> <head> <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> <style> body{ background-color: ivory; } canvas{border:1px solid red;} </style> <script> $(function(){ var canvas=document.getElementById("canvas"); var ctx=canvas.getContext("2d"); var angleInDegrees=0; var image=document.createElement("img"); image.onload=function(){ ctx.drawImage(image,canvas.width/2-image.width/2,canvas.height/2-image.width/2); } image.src="https://www.baidu.com/img/bd_logo1.png?where=super"; $("#clockwise").click(function(){ angleInDegrees+=30; drawRotated(angleInDegrees); }); $("#counterclockwise").click(function(){ angleInDegrees-=30; drawRotated(angleInDegrees); }); function drawRotated(degrees){ ctx.clearRect(0,0,canvas.width,canvas.height); ctx.save(); ctx.translate(canvas.width/2,canvas.height/2); ctx.rotate(degrees*Math.PI/180); ctx.drawImage(image,-image.width/2,-image.width/2); ctx.restore(); } }); // end $(function(){}); </script> </head> <body> <canvas width=300 height=300></canvas><br> <button>Rotate right</button> <button>Rotate left</button> </body> </html>

本文来自SEO排名优化_SEO技术_SEO资讯网,经授权后发布,转载请联系原作者。原文链接:http://www.ylzug.com/js/427.html