1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| <!DOCTYPE html> <html>
<head> <title>动画(旋转的矩形)</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content="图形系统开发实战:基础篇 示例"> <meta name="author" content="hjq"> <meta name="keywords" content="canvas,ladder,javascript"> <script src="/examples/canvas-qa/canvas_1b/js/helper.js"></script> </head>
<body style="overflow: hidden; margin:10px;"> <canvas id="canvas" width="500" height="500" style="border:solid 1px #CCCCCC;"></canvas> </body> <script> let canvas = document.getElementById('canvas'), ctx = canvas.getContext('2d'); let angle = 0; ctx.fillStyle = "green"; ctx.font = "40px 黑体"; ctx.textAlign = "center"; ctx.textBaseline = "middle";
function drawProcess() { ctx.save(); ctx.translate(250, 250); ctx.rotate(toRadians(angle)); ctx.fillRect(-150, -60, 300, 120); ctx.restore();
ctx.save(); ctx.fillStyle = "gold"; ctx.fillText(angle + "°", 250, 250); ctx.restore(); }
function frame() { ctx.clearRect(0, 0, canvas.width, canvas.height); drawGrid('lightgray', 10, 10, ctx); drawProcess();
if (angle < 360) { angle += 1; animationFrame = window.requestAnimationFrame(frame); } }
window.requestAnimationFrame(frame); </script>
</html>
|