-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsnakeDesign.js
More file actions
26 lines (22 loc) · 900 Bytes
/
snakeDesign.js
File metadata and controls
26 lines (22 loc) · 900 Bytes
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
// rectangle with border radius
export function roundedRect(ctx, x, y, width, height, radius, color) {
ctx.beginPath();
ctx.moveTo(x, y + radius);
ctx.arcTo(x, y + height, x + radius, y + height, radius);
ctx.arcTo(x + width, y + height, x + width, y + height - radius, radius);
ctx.arcTo(x + width, y, x + width - radius, y, radius);
ctx.arcTo(x, y, x, y + radius, radius);
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
}
// draw snake elements
export function drawSnake(ctx, snakeItemsPos, GRID_SIZE, RECT_RADIUS) {
snakeItemsPos.forEach((segment, idx) => {
if(idx === 0) {
roundedRect(ctx, segment.x*GRID_SIZE, segment.y*GRID_SIZE, GRID_SIZE, GRID_SIZE, RECT_RADIUS, "green");
} else {
roundedRect(ctx, segment.x*GRID_SIZE, segment.y*GRID_SIZE, GRID_SIZE, GRID_SIZE, RECT_RADIUS, "blue");
}
})
}