当您使用画布旋转图像时,会被切断 – 如何避免这种情况?我已经使画布元素比图像更大,但它仍然切断边缘.
例:
<html>
<head>
<title>test</title>
<script type="text/javascript">
function startup() {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var img = new Image();
img.src = 'player.gif';
img.onload = function() {
ctx.rotate(5 * Math.PI / 180);
ctx.drawImage(img,64,120);
}
}
</script>
</head>
<body onload='startup();'>
<canvas id="canvas" style="position: absolute; left: 300px; top: 300px;" width="800" height="800"></canvas>
</body>
</html>
解决方法
旋转始终在当前原点周围发生.因此,您可能需要首先使用翻译将画布翻译到要旋转的位置(称为中心),然后旋转.
例如
ctx.translate(85,85); ctx.rotate(5 * Math.PI / 180);
画布现在旋转(85,85).