Skip to content

Commit 29781c3

Browse files
committed
Avoid recalculation of pattern on every draw iteration
1 parent 5d2846e commit 29781c3

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/js/env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var env = {
2828
"gcode": {
2929
"command": "G0"
3030
},
31-
"recalculate_pattern": true,
31+
"recalculate_pattern": false,
3232
"mouse": {
3333
"pressed": false,
3434
"x": null,

src/js/sand_table_pattern_maker.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ var pattern_select;
5050
// is selected this is used to save the configuration of the previous Pattern
5151
var previous_pattern;
5252

53-
var path;
53+
var path = [];
5454

55-
let path_preview;
55+
let path_preview = [];
5656

5757
// Flag for setting whether the pattern coordinates should be recalculated
5858
var recalculate_pattern = env.recalculate_pattern;
@@ -139,6 +139,9 @@ new p5((sketch) => {
139139

140140
// Initialize.
141141
patternSelectEvent(false);
142+
143+
const selected_pattern = pattern_select.value();
144+
path = Patterns[selected_pattern].draw();
142145
}
143146

144147
// Processing standard function that loops forever
@@ -150,6 +153,8 @@ new p5((sketch) => {
150153
// Save the selected pattern to a local variable
151154
var selected_pattern = pattern_select.value();
152155

156+
path_preview = path;
157+
153158
// Some patterns, like a free-drawing, must be recalculated on every draw loop
154159
if (selected_pattern == "draw") {
155160
recalculate_pattern = true;
@@ -162,7 +167,9 @@ new p5((sketch) => {
162167
// Recalculate the pattern if required (depending on env.recalculate_pattern)
163168
if (recalculate_pattern) {
164169
path = Patterns[selected_pattern].draw();
165-
path_preview = PathHelp.dividePathComplete(path, 10);
170+
if (path.length > 0) {
171+
path_preview = PathHelp.dividePathComplete(path, 10);
172+
}
166173
recalculate_pattern = env.recalculate_pattern;
167174
}
168175

@@ -191,7 +198,9 @@ new p5((sketch) => {
191198
drawTable(path_exceeds_plotter(path));
192199

193200
// Draw the path [path, path width, connected path, animated]
194-
drawPath(path_preview, 2, false, true, coordinate_overlay);
201+
if (path_preview.length > 0) {
202+
drawPath(path_preview, 2, false, true, coordinate_overlay);
203+
}
195204

196205
// Calculate path length
197206
distance = 0;
@@ -250,7 +259,7 @@ new p5((sketch) => {
250259
sketch.select('#pattern-controls').html('');
251260

252261
// Save the selected pattern to a local variable
253-
var selected_pattern = pattern_select.value();
262+
const selected_pattern = pattern_select.value();
254263
localStorage.setItem('lastPattern', selected_pattern);
255264

256265
// Load Pattern State
@@ -355,9 +364,12 @@ new p5((sketch) => {
355364
document.title = 'Sand Pattern | ' + Patterns[selected_pattern].name;
356365

357366
// Update the URL
358-
if (Patterns[pattern_select.value()] !== undefined) {
359-
updateURL(pattern_select.value())
367+
if (Patterns[selected_pattern] !== undefined) {
368+
updateURL(selected_pattern)
360369
}
370+
371+
// Recalculate the pattern
372+
path = Patterns[selected_pattern].draw();
361373
}
362374

363375
function drawTable(plotter_exceeded = false) {
@@ -733,6 +745,10 @@ new p5((sketch) => {
733745
function path_exceeds_plotter(path)
734746
{
735747

748+
if (!path) {
749+
return false;
750+
}
751+
736752
// Define function to extract column from multidimensional array
737753
const arrayColumn = (arr, n) => arr.map(a => a[n]);
738754

0 commit comments

Comments
 (0)