canvas旋转文本
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
canvas{ border: 1px solid black; }
</style>
</head>
<body>
<canvas id="myCanvas" width="500" height="500"></canvas>
<button type="button" onclick="rotate();">旋转文本</button>
<script>
var canvas = document.getElementById("myCanvas");
var ox = canvas.width / 2;
var oy = canvas.height / 2;
var ctx = canvas.getContext("2d");
ctx.font = "42px serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("Hello World", ox, oy);
function rotate() {
ctx.translate(ox, oy); // 将画布的原点移动到正中央
ctx.rotate((Math.PI / 180) * 15); // 弧度 = (Math.PI/180)*角度
ctx.fillText("Hello World", 0, 0);
ctx.translate(-ox, -oy); // 将画布的原点还原
};
</script>
</body>
</html>
原文地址:https://www.cnblogs.com/ye-hcj/p/10356397.html
时间: 2024-10-29 00:33:52