-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathframe.js
99 lines (95 loc) · 2.96 KB
/
frame.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
"use strict";
function tick() {
if (pause) {
if (Input.drag) {
if (
Input.gridSnapping ||
(Input.ctrl && Input.gridCreation === false)
) {
const shift = Input.pointer.sub(Input.downPos);
const currentCell = new Vector(
Math.round(shift.x / cellSize),
Math.round(shift.y / cellSize)
);
if (
currentCell.x !== Input.downCellIndex.x ||
currentCell.y !== Input.downCellIndex.y
) {
const diff = currentCell.sub(Input.downCellIndex);
selectedPoints.map((p) => {
p.pos.addMut(diff.scale(cellSize));
});
Input.downCellIndex.addMut(diff);
}
}
}
} else {
if (Input.drag) {
activePoint.pos = Input.pointer.add(grabFix);
}
points.map((p) => p.update());
lines.map((l) => l.update());
}
}
function render() {
ctx.fillStyle = pause ? "#ddb" : "#ccc";
ctx.fillRect(0, 0, width, height);
ctx.save();
ctx.fillStyle = "grey";
ctx.fillRect(0, floor, width, height / FLOOR_FACTOR);
ctx.fillText("FPS: " + FPS, 10, 15);
ctx.strokeStyle = "blue";
ctx.lineWidth = 2;
if (Input.drag && Input.gridCreation) {
let gridWidth =
cellSize *
Math.round((Input.pointer.x - Input.downPos.x) / cellSize);
let gridHeight =
cellSize *
Math.round((Input.pointer.y - Input.downPos.y) / cellSize);
ctx.strokeRect(Input.downPos.x, Input.downPos.y, gridWidth, gridHeight);
} else if (Input.downState && Input.boxSelection) {
ctx.strokeStyle = "grey";
ctx.setLineDash([5, 10]);
const selectionBox = Input.pointer.sub(Input.downPos);
ctx.strokeRect(
Input.downPos.x,
Input.downPos.y,
selectionBox.x,
selectionBox.y
);
} else if (
Input.downState &&
(Input.drawConnections || Input.alt) &&
activePoint
) {
ctx.beginPath();
ctx.moveTo(activePoint.pos.x, activePoint.pos.y);
ctx.lineTo(Input.pointer.x, Input.pointer.y);
ctx.stroke();
}
ctx.restore();
if (hoverLine) hoverLine.highlight();
if (pause === true) {
selectedPoints.map((sp) => sp.highlight());
selectedLines.map((sl) => sl.highlight());
if (activePoint) activePoint.activeHighlight();
}
lines.map((l) => l.draw());
points.map((p) => p.draw());
if (hoverPoint) hoverPoint.highlight();
}
function frame() {
frames++;
if (performance.now() - lastTime > 1000) {
lastTime += 1000;
FPS = frames;
frames = 0;
}
alreadyRequestedFrame = false;
tick();
render();
if (pause === false) {
requestAnimationFrame(frame);
}
}