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 64 65 66 67 68
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Canvas 状态操作应用实例</title> </head> <body> <canvas id="myCanvas" width="400" height="300" style="border:1px solid #000;"></canvas>
<script> const canvas = document.getElementById('myCanvas'); const ctx = canvas.getContext('2d');
ctx.fillStyle = '#f0f0f0'; ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.save();
ctx.fillStyle = 'red'; ctx.beginPath(); ctx.arc(100, 100, 50, 0, Math.PI * 2, false); ctx.fill();
ctx.save();
ctx.translate(150, 0); ctx.scale(0.5, 0.5);
ctx.fillStyle = 'blue'; ctx.beginPath(); ctx.arc(100, 100, 50, 0, Math.PI * 2, false); ctx.fill();
ctx.restore();
ctx.fillStyle = 'green'; ctx.beginPath(); ctx.arc(100, 100, 50, 0, Math.PI * 2, false); ctx.fill();
ctx.restore();
ctx.fillStyle = 'yellow'; ctx.beginPath(); ctx.arc(300, 100, 50, 0, Math.PI * 2, false); ctx.fill();
ctx.save(); ctx.translate(200, 150); ctx.rotate(Math.PI / 4); ctx.fillStyle = 'purple'; ctx.fillRect(-50, -25, 100, 50); ctx.restore(); </script> </body> </html>
|