translate作用就是移动原点,默认的原点(0,0)是在屏幕左上角的,你可以通过translate(x,y)把点(x,y)作为原点。即,translate(float x,float y)函数是将整个canvas水平移动x,垂直移动y距离。

下面有一段很详细的解释:

Translate - Basically do what it says. Just translate the canvas using x,y. If you want to draw two objects and the one is just translation of the other e.g x2 = x1 + 50 for each point . You don't have to make all your calculations again for the second object but you can just translate the canvas and draw again the same object.

Ok,image you have a canvas and you want to draw 2 trees on it and one bird which is above the first tree. The trees have the same y coordinate but different x (let's say for example 50 inches). So what are you going to do ?

You'll draw the first three then you'll move your canvas 50 inches to the right ( which is what canvas.translate do ) and will draw the second tree. In order to restore the original position of the canvas you'll move it again 50 inches but this time to the left. And now when you draw the bird it'll be above the first tree, if you don't restore the canvas original state all object that you draw after the translation will be translated to - in our example the bird will be drawn above the second tree

From:What does canvas.translate do?
Link:http://stackoverflow.com/questions/5789813/what-does-canvas-translate-do




转载于:https://my.oschina.net/jerikc/blog/122329

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐