如何水平移动旋转的画布
how to move rotated canvas horizontally?
我想水平移动html画布,然后旋转它,然后再次水平移动它。问题是,在我旋转它并想再次移动它之后,旋转就消失了。我做错了什么?谢谢,我的代码低于
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title></title>
</head>
<body>
<style type="text/css">
#canvas {
width:400px;
height:400px;
border:1px solid red;
}
</style>
<canvas id="canvas"></canvas>
<script type="text/javascript">
var c=document.getElementById("canvas");
var ctx=c.getContext("2d");
var counterx = 0;
var canvasWidth = 400;
var canvasHeight = 400;
var imageObj = new Image();
imageObj.onload = function() {
ctx.drawImage(imageObj, counterx,0 ,69, 50);
};
imageObj.src = 'test.png';
function moveRight() {
ctx.save();
counterx += 5;
ctx.clearRect(0 ,0 ,canvasWidth, canvasHeight);
ctx.drawImage(imageObj, counterx,0 ,69, 50);
ctx.restore();
}
function rotate() {
ctx.save();
ctx.translate(counterx, 0);
ctx.rotate(Math.PI / 4);
ctx.clearRect(0 ,0 ,canvasWidth, canvasHeight);
ctx.drawImage(imageObj, counterx,0 ,69, 50);
ctx.restore();
}
</script>
<a onclick="moveRight(); return false;" href="#">move right</a>
<a onclick="rotate(); return false;" href="#">rotate</a>
</body>
</html>
我建议累积这些值,然后只在需要时进行绝对变换。一点重构也可以使跟踪这些更改变得更容易,在这里,这将使用一个通用的更新方法。
例如:
var rotation = 0;
var counterx = 0;
function moveRight() {
counterx += 5;
update();
}
function rotate() {
rotation += Math.PI / 4
update();
}
function update() {
ctx.clearRect(0 ,0 ,canvasWidth, canvasHeight);
ctx.translate(counterx, 0); // absolute translate
ctx.rotate(rotation); // absolute rotation
ctx.drawImage(imageObj, 0, 0, 69, 50); // we are already at counterx
ctx.setTransform(1, 0, 0, 1, 0, 0); // reset all transformations
}
如果你想按图像的中心旋转,只需将上面的drawImage行替换为:
ctx.drawImage(imageObj, -69/2, -50/2, 69, 50);
现场演示
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
var canvasWidth = 400;
var canvasHeight = 400;
var imageObj = new Image();
var rotation = 0;
var counterx = 0;
c.width = canvasWidth;
c.height = canvasHeight;
imageObj.onload = update;
imageObj.src = 'http://i.imgur.com/mP58PXJ.png';
function moveRight() {
counterx += 5;
update();
}
function rotate() {
rotation += Math.PI / 4
update();
}
function update() {
var iw = imageObj.width, ih = imageObj.height;
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
ctx.translate(counterx, ih); // absolute translate
ctx.rotate(rotation); // absolute rotation
ctx.drawImage(imageObj, -iw*0.5, -ih*0.5); // we are already at counterx
ctx.setTransform(1, 0, 0, 1, 0, 0); // reset all transformations
}
#canvas {
width: 400px;
height: 400px;
border: 1px solid red;
}
<canvas id="canvas"></canvas>
<a onclick="moveRight(); return false;" href="#">move right</a>
<a onclick="rotate(); return false;" href="#">rotate</a>
相关文章:
- 使用KineticJS变换(移动/缩放/旋转)形状
- 如何水平移动旋转的画布
- 如何使我的功能360度旋转并上下移动
- 猫头鹰旋转木马-自动高度移动问题
- 如何在Javascript中移动元素的旋转点
- 如何旋转rect,并且它仍然根据其旋转沿正确的方向移动
- 用户键入时旋转的内容可编辑移动
- 在 HTML5 画布中沿路径旋转移动对象
- 三.js一个物体可以同时旋转和移动吗?
- 如何锁定在类固醇中旋转的场景.js - AppGyver移动应用程序
- 想要使用 html5 的画布优化捕获的图像调整大小和旋转的代码,现在在移动设备上发出内存不足警告
- 如何在phase.js中移动时旋转汽车
- OWL旋转木马移动+/-5张幻灯片
- 我可以使用旋转功能绕时钟移动一条线吗
- Three.js-鼠标移动时沿(0,0,0)旋转对象
- 使用JavaScript和setTimeout旋转HTML元素以模拟不断移动的对象
- IE10中鼠标移动时的图像旋转
- 在HTML画布中移动旋转对象
- 在JQuery不工作的情况下旋转和移动
- 变换:旋转移动视图