-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfillMap.js
146 lines (120 loc) · 4.33 KB
/
fillMap.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const SVG_WIDTH = 500
const SVG_HEIGHT = 500
const svg = document.querySelector('svg')
svg.setAttribute('width', SVG_WIDTH)
svg.setAttribute('height', SVG_HEIGHT)
const { top: SVG_TOP, left: SVG_LEFT } = svg.getBoundingClientRect()
const ns = 'http://www.w3.org/2000/svg'
const getWithAttributes = (obj, attrs) => {
Object.entries(attrs).forEach(([key, value]) => {
obj.setAttribute(key, value);
})
return obj;
}
svg.append(
getWithAttributes(
document.createElementNS(ns, 'rect'),
{
x: 0,
y: 0,
width: SVG_WIDTH,
height: SVG_HEIGHT,
stroke: 'black',
'stroke-width': 1,
fill: 'rgb(164, 164, 164)'
}
)
)
const ROAD_WIDTH = 120
const grassRectanglesGroup = document.createElementNS(ns, 'g');
svg.append(grassRectanglesGroup)
const grassWidth = (SVG_WIDTH - ROAD_WIDTH) / 2
const grassHeight = (SVG_HEIGHT - ROAD_WIDTH) / 2
const jsonGrassRectangles = [
{ 'x': 0, 'y': 0, 'width': grassWidth, 'height': grassHeight, fill: 'green' },
{ 'x': grassWidth + ROAD_WIDTH + 1, 'y': 0, 'width': grassWidth, 'height': grassHeight, fill: 'green' },
{ 'x': 0, 'y': grassHeight + ROAD_WIDTH + 1, 'width': grassWidth, 'height': grassHeight, fill: 'green' },
{ 'x': grassWidth + ROAD_WIDTH + 1, 'y': grassHeight + ROAD_WIDTH + 1, 'width': grassWidth, 'height': grassHeight, fill: 'green' }
]
jsonGrassRectangles.forEach((grassRectangle) => {
grassRectanglesGroup.append(
getWithAttributes(
document.createElementNS(ns, 'rect'),
grassRectangle
)
)
})
const horizontalRoadMarkingGroup = document.createElementNS(ns, 'g');
svg.append(horizontalRoadMarkingGroup)
const jsonHorizontalLines = [
{ 'x1': 0, 'y1': grassHeight + ROAD_WIDTH / 2, 'x2': grassWidth, 'y2': grassHeight + ROAD_WIDTH / 2, stroke: 'white' },
{ 'x1': grassWidth + ROAD_WIDTH + 1, 'y1': grassHeight + ROAD_WIDTH / 2, 'x2': SVG_WIDTH, 'y2': grassHeight + ROAD_WIDTH / 2, stroke: 'white' }
]
jsonHorizontalLines.forEach((horizontalLine) => {
horizontalRoadMarkingGroup.append(
getWithAttributes(
document.createElementNS(ns, 'line'),
horizontalLine
)
)
})
const verticalRoadMarkingGroup = document.createElementNS(ns, 'g');
svg.append(verticalRoadMarkingGroup)
const jsonVerticalLines = [
{ 'x1': grassWidth + ROAD_WIDTH / 2, 'y1': 0, 'x2': grassWidth + ROAD_WIDTH / 2, 'y2': grassHeight, stroke: 'white' },
{ 'x1': grassWidth + ROAD_WIDTH / 2, 'y1': grassHeight + ROAD_WIDTH + 1, 'x2': grassWidth + ROAD_WIDTH / 2, 'y2': SVG_HEIGHT, stroke: 'white' }
]
jsonVerticalLines.forEach((verticalLine) => {
horizontalRoadMarkingGroup.append(
getWithAttributes(
document.createElementNS(ns, 'line'),
verticalLine
)
)
})
const trafficLightGroup = document.createElementNS(ns, 'g');
svg.append(trafficLightGroup)
const trafficLightWidth = 40
const trafficLightHeight = 40
trafficLightGroup.append(
getWithAttributes(
document.createElementNS(ns, 'rect'),
{
x: SVG_WIDTH / 2 - trafficLightWidth / 2 + 1,
y: SVG_HEIGHT / 2 - trafficLightHeight / 2 + 1,
transform: `rotate(45 ${SVG_WIDTH / 2} ${SVG_HEIGHT / 2})`,
width: trafficLightWidth,
height: trafficLightHeight,
fill: 'rgb(132, 132, 132)'
}
)
)
const trafficLightPartRadius = 5
const trafficLightHorizontalGroup = document.createElementNS(ns, 'g');
const trafficLightVerticalGroup = document.createElementNS(ns, 'g');
svg.append(trafficLightHorizontalGroup)
svg.append(trafficLightVerticalGroup)
const jsonTrafficLightHorizontalParts = [
{ 'cx': SVG_WIDTH / 2 - trafficLightWidth / 3, 'cy': SVG_HEIGHT / 2 + 1, r: trafficLightPartRadius, fill: 'white' },
{ 'cx': SVG_WIDTH / 2 + trafficLightWidth / 3, 'cy': SVG_HEIGHT / 2 + 1, r: trafficLightPartRadius, fill: 'white' }
]
jsonTrafficLightHorizontalParts.forEach((part) => {
trafficLightHorizontalGroup.append(
getWithAttributes(
document.createElementNS(ns, 'circle'),
part
)
)
})
const jsonTrafficLightVerticalParts = [
{ 'cx': SVG_WIDTH / 2, 'cy': SVG_HEIGHT / 2 - trafficLightHeight / 3, r: trafficLightPartRadius, fill: 'white' },
{ 'cx': SVG_WIDTH / 2, 'cy': SVG_HEIGHT / 2 + trafficLightHeight / 3, r: trafficLightPartRadius, fill: 'white' }
]
jsonTrafficLightVerticalParts.forEach((part) => {
trafficLightVerticalGroup.append(
getWithAttributes(
document.createElementNS(ns, 'circle'),
part
)
)
})