書式
コンテキスト.translate(tx,ty)
属性
属性名/パラメーター |
内 容 |
tx |
横の移動量を指定します。 |
ty |
縦の移動量を指定します。 |
説明
キャンバスの座標系を移動させます。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sample</title>
<script>
window.onload = function(){
var canvasObj = document.getElementById("imgCanvas");
var context = canvasObj.getContext("2d");
imgObj = new Image(120,80);
imgObj.src = "photo.jpg";
imgObj.onload = function(){
var tx = Math.random() * 200;
var ty = Math.random() * 100;
context.save();
context.translate(tx,ty);
context.drawImage(imgObj,0,0);
context.restore();
setTimeout(arguments.callee, 100);
}
}
</script>
</head>
<body>
<h1>Sample</h1>
<canvas id="imgCanvas" width="320" height="240" style="border:1px black solid"></canvas>
</body>
</html>