English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
translate() 是 Canvas 2D API 通过在网格中移动 canvas 和 canvas 原点 x 水平方向、原点 y 垂直方向,添加平移变换的方法。
在位置(10,10)绘制一个矩形,将新的(0,0)位置设置为(70,70)。再次绘制相同的矩形(注意,矩形现在从(80,80)位置开始:
<!DOCTYPE html> <html> <head> <meta charset="utf-8> <title>HTML canvas translate()方法使用-基础教程(oldtoolbag.com)</<title> </<head> <body> <canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3> 您的浏览器不支持 HTML5 canvas 표签。 </canvas> <script> var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.fillRect(10,10,100,50); ctx.translate(70,70); ctx.fillRect(10,10,100,50); </script> </body> </html>테스트해 보세요 ‹/›
IEFirefoxOperaChromeSafari
Internet Explorer 9Firefox, Opera, Chrome, Safari와는 translate()를 지원합니다. 메서드.
주의:Internet Explorer 8 이전 버전은 <canvas> 요소를 지원하지 않습니다.
translate() 메서드는 캔버스에서 (0,0) 위치를 재지도합니다.
주의:translate() 함수를 호출한 후 fillRect()와 같은 메서드를 호출할 때, 값이 x와 y 좌표 값에 추가됩니다.
JavaScript 문법: | context.translate(x,y); |
---|
주의: 한 가지 또는 두 가지 파라미터를 지정할 수 있습니다.
파라미터 | 설명 |
---|---|
x | 수평 좌표(x)에 추가할 값. |
y | 수직 좌표(y)에 추가할 값. |