-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
145 lines (115 loc) · 3.67 KB
/
sketch.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
P5Capture.setDefaultOptions({
format: "gif",
framerate: 60,
quality: 0.5,
width: 320,
disableUi: true,
});
let renderer;
//Some basic app variables we need
let canvasWidth, canvasHeight, fps;
//Default variables for UI elements
let defaultLineWidth = 0.6;
let defaultExtruderSpeed = 1.25;
let defaultBGColor;
let defaultInitialColor;
//Extruder variables
let extruder; //Extruder object drawing lines
let interpreter; //Interprets GCode instructions
let gcode; //Array of gcode instructions to be executed
let currentInstruction = 0; //Index of the current instruction being executed
let padding = 50;
let boundingBox; //Bounding box of the print
let scaleFactor = 1; //Scale factor of the print (based on the size of the print and the size of the canvas)
let xOffset = 0; //X offset of the print (based on the size of the print and the size of the canvas)
let yOffset = 0; //Y offset of the print (based on the size of the print and the size of the canvas)
function setup() {
//Setup some basic app variables
renderer = WEBGL;
canvasWidth = windowWidth;
canvasHeight = windowHeight;
fps = 60;
defaultBGColor = color(0);
defaultInitialColor = color(255);
createCanvas(canvasWidth, canvasHeight, renderer);
frameRate(fps);
//Setup the UI
setupUI();
//Setup cameras
setupCameras();
//Create the extruder and the interpreter
extruder = new Extruder(0, 0, 0);
interpreter = new GcodeInterpreter();
//Make a nice background
background(bgColorPicker.value());
//Load the sample file
loadStrings("gcode-samples/vulkazalogo.gcode", handleGCode);
}
function draw() {
let timeRemaining = 1 / fps;
//While we have gcode instructions to execute, execute one and if the return value is true (it was executed fully), execute another one until the return value becomes false (partial fulfillment)
while (gcode && currentInstruction < gcode.length) {
timeRemaining = interpreter.parseLine(
gcode[currentInstruction],
timeRemaining
);
if (timeRemaining > 0) {
currentInstruction++;
} else {
break;
}
}
//Draw all the shapes stored in the extruder object
if (renderer == WEBGL) {
lerpTowardsFixedCamera(1 / fps);
//If the space key is pressed, rotate the camera
if (keyIsDown(32)) {
orbitControl();
}
background(bgColorPicker.value());
extruder.drawShapes();
}
//Make any UI updates that are needed
updateUI();
}
function handleGCode(lines) {
//Reset the extruder
extruder.reset();
//Reset the instruction pointer
currentInstruction = 0;
//Get the list of currently supported instructions
let supportedInstructions = Object.keys(interpreter.instructions);
//Remove all unsupported lines
lines = lines.filter((line) => {
//Get the instruction from the line
const [instruction] = line.split(" ");
//Check if the instruction is supported
return supportedInstructions.includes(instruction);
});
//Set the gcode to the filtered list of lines
gcode = lines;
//Calculate the bounding box of the print
boundingBox = interpreter.calculateBoundingBox(gcode);
//Calculate the scale factor
scaleFactor = calculateScaleFactor();
//Calculate the offsets
let offsets = calculateOffsets();
xOffset = offsets.xOffset;
yOffset = offsets.yOffset;
}
/*
Keyboard controls:
If the R key is pressed, reset the extruder
If the H key is pressed and shift is held, hide the UI
1-6: different camera angles
*/
function keyPressed() {
if (key == "r") {
currentInstruction = 0;
extruder.reset();
} else if (parseInt(key) > 0 && parseInt(key) < fixedCameras.length + 1) {
targetCameraAngle = parseInt(key) - 1;
} else if (key == "h") {
toggleUI();
}
}