一个基础的编辑器背板

2D图形编辑器的基础背景板

代码实现

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>design</title>
<style>
body {
text-align: center;
font-family: monospace;
}
canvas {
background-color: black;
}
</style>
</head>
<body>
<canvas id="asteroids" width = "800" height = "800"></canvas>
<script>
let canvas = document.getElementById('asteroids');
let ctx = canvas.getContext('2d');
drawGrid(ctx, 10, 100)
function drawGrid(ctx, minor, major, stroke, fill) {
minor = minor || 10;
major = major || minor * 5;
stroke = stroke || '#00FF00';
fill = fill || '#009900';
ctx.save();
ctx.strokeStyle = stroke;
ctx.fillStyle = fill;
let width = ctx.canvas.width;
let height = ctx.canvas.height;
for (let x = 0; x < width; x += minor) {
ctx.beginPath();
ctx.strokeStyle = '#00FF00';
ctx.moveTo(x, 0);
ctx.lineTo(x, height);
ctx.lineWidth = (x % major == 0) ? 0.5 : 0.25;
ctx.stroke();
if(x % major == 0 ) {
ctx.fillText(x, x, 10);
}
}
for (let y = 0; y < height; y += minor) {
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(width, y);
ctx.lineWidth = (y % major == 0) ? 0.5 : 0.25;
ctx.stroke();
if(y % major == 0 ) {
ctx.fillText(y, 0, y + 10);
}
}
}
</script>
</body>
</html>

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!