2018-04-03 13:38:37 970浏览
这篇文章主要介绍了通过HTML5培训之Canvas实现图片的平移及旋转变化的方法,其中旋转讲解的是配合平移坐标系确定圆心位置而进行的顺时针旋转,需要的朋友可以参考下 XML/HTMLCode
>
<htmllang="zh">
<head>
<metacharset="UTF-8">
<title>平移变换title>
<style>
body{background:url("./images/bg3.jpg")repeat;}
#canvas{border:1pxsolid#aaaaaa;display:block;margin:50pxauto;}
style>
head>
<body>
<pid="canvas-warp">
<canvasid="canvas">
你的浏览器居然不支持Canvas?!赶快换一个吧!!
canvas>
p>
<script>
window.onload=function(){
varcanvas=document.getElementById("canvas");
canvas.width=800;
canvas.height=600;
varcontext=canvas.getContext("2d");
context.fillStyle="#FFF";
context.fillRect(0,0,800,600);
context.fillStyle="#00AAAA";
context.fillRect(100,100,200,100);
context.fillStyle="red";
context.translate(100,100);
context.fillRect(100,100,200,100);
};
script>
body>
html>
"zh"> "UTF-8"> "canvas-warp">
你的浏览器居然不支持Canvas?!赶快换一个吧!!
《script》
window.onload=function(){
varcanvas=document.getElementById("canvas");
canvas.width=800;
canvas.height=600;
varcontext=canvas.getContext("2d");
context.fillStyle="#FFF";
context.fillRect(0,0,800,600);
context.fillStyle="#00AAAA";
context.fillRect(100,100,200,100);
context.fillStyle="red";
context.translate(100,100);
context.fillRect(100,100,200,100);
context.fillStyle="green";
context.translate(200,200);
context.fillRect(100,100,200,100);
};
《script》
JavaScriptCode "zh"> "UTF-8"> "canvas-warp">
《script》
window.onload=function(){
varcanvas=document.getElementById("canvas");
canvas.width=800;
canvas.height=600;
varcontext=canvas.getContext("2d");
context.fillStyle="#FFF";
context.fillRect(0,0,800,600);
context.fillStyle="#00AAAA";
context.fillRect(100,100,200,100);
context.save();
context.fillStyle="red";
context.translate(100,100);
context.fillRect(100,100,200,100);
context.restore();
context.save();
context.fillStyle="green";
context.translate(200,200);
context.fillRect(100,100,200,100);
context.restore();
};
《script》
"zh">
"UTF-8">
"canvas-warp">
你的浏览器居然不支持Canvas?!赶快换一个吧!!
《script》
window.onload=function(){
varcanvas=document.getElementById("canvas");
canvas.width=800;
canvas.height=600;
varcontext=canvas.getContext("2d");
context.fillStyle="#FFF";
context.fillRect(0,0,800,600);
for(vari=0;i<=12;i++){
context.save();
context.translate(70+i*50,50+i*40);
context.fillStyle="#00AAAA";
context.fillRect(0,0,20,20);
context.restore();
context.save();
context.translate(70+i*50,50+i*40);
context.rotate(i*30*Math.PI/180);
context.fillStyle="red";
context.fillRect(0,0,20,20);
context.restore();
}
};
《script》
这里用for循环绘制了14对正方形,其中蓝色是旋转前的正方形,红色是旋转后的正方形。每次旋转都以正方形左上角顶点为原点进行旋转。每次绘制都被save()与restore()包裹起来,每次旋转前都移动了坐标系。
最后想要工作不累就要不断的提升自己的技能,请关注扣丁学堂HTML5培训官网、微信等平台,扣丁学堂IT职业在线学习教育平台为您提供权威的HTML5视频教程系统,通过千锋扣丁学堂金牌讲师在线录制的第一套自适应HTML5在线视频课程系统,让你快速掌握HTML5从入门到精通开发实战技能。扣丁学堂H5技术交流群:559883758
【关注微信公众号获取更多学习资料】