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
| <!DOCTYPE html> <html>
<head> <title>变形操作(translate)</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;"> <div style="display: inline-block;"> <canvas id="canvas" width="800" height="400" style="border:solid 1px #CCCCCC;"></canvas> </div> </body> <script> let canvas = document.getElementById('canvas'); let ctx = canvas.getContext('2d');
drawGrid('lightgray', 10, 10, ctx);
ctx.save(); ctx.textBaseline = "bottom"; ctx.font = "30px Verdana"; ctx.fillStyle = "gold"; ctx.fillText("随机平移的星星", 550, 100); ctx.restore();
let num = 18; for (let i = 0; i < num; i++) { ctx.save(); let x = getRandomNum(10, 750); let y = getRandomNum(10, 350); ctx.translate(x, y); drawStar(); ctx.restore(); }
</script>
</html>
|