下面代码 canvas rotate旋转 旋转的坐标点在哪,为什么不在元素的中心点?

于娜 2015-04-28 09:32:43
下面代码 canvas rotate旋转 旋转的坐标点在哪,为什么不在元素的中心点?

=================================

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
</style>
<script>

function init()
{
var canvas = document.getElementById('canvas');
var cxt = canvas.getContext('2d');
cxt.strokeStyle = "red";
cxt.strokeRect(300,300,100,100);
cxt.strokeStyle = "blue";
cxt.rotate(10*Math.PI/180);
cxt.strokeRect(300,300,100,100);
cxt.strokeStyle = "green";
cxt.rotate(20*Math.PI/180);
cxt.strokeRect(300,300,100,100);
}
</script>
</head>
<body onload="init()">
<canvas id="canvas" style="border:1px solid red" width="1200" height="600"></canvas>
</body>
</html>
...全文
372 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
白莲河观音山 2015-04-30
  • 打赏
  • 举报
回复
先要移动原点 ctx.translate(canvas.width / 2, canvas.height / 2);
x556549 2015-04-30
  • 打赏
  • 举报
回复
旋转并不是以图像的中心点为对照点的,一般而言,如果你没有移动过坐标原点的话,都是以坐标原点(就是canvas控件的左上角的那个点)为参照点的,如果想要改变参照点,需要移动!

39,087

社区成员

发帖
与我相关
我的任务
社区描述
HTML5是构建Web内容的一种语言描述方式。HTML5是互联网的下一代标准,是构建以及呈现互联网内容的一种语言方式.被认为是互联网的核心技术之一。
社区管理员
  • HTML5社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧