forked from flucoma/flucoma-max
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfluid.jit.plotter.resize.pworld.js
More file actions
77 lines (67 loc) · 2.01 KB
/
Copy pathfluid.jit.plotter.resize.pworld.js
File metadata and controls
77 lines (67 loc) · 2.01 KB
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
// create a Task event loop detecting bpatcher resizing
// and adjusting pworld presentatin_rect accordingly
var task = new Task(getBpatcherRect, this);
// buffers for comparison
var prevWidth, prevHeight;
// Minimum and maximum intervals (in ms) for the Task.
// It starts out at minInterval, and gradually slows
// down to maxInterval by multiplying the current
// Task.interval by intervalMult on every step.
// It keeps ticking at this low rate until a new
// resize is detected.
var minInterval = 10;
var maxInterval = 1000;
var intervalMult = 1.1;
// optionally force square aspect ratio (kind of janky)
var forceSquareBpatcher = true;
// the callback function for the Task
function getBpatcherRect() {
// force square aspect ratio
if (this.patcher.box != null && forceSquareBpatcher) {
forceSquareAR();
}
// get bpatcher width and height
r = this.patcher.box.rect;
width = r[2] - r[0];
height = r[3] - r[1];
// output list for setting the pworld presentation_rect to width & height
outlet(0, 0, 0, width, height);
// slow down Task refresh rate if idle
adjustInterval(width, height);
// save width & height for comparison
prevWidth = width;
prevHeight = height;
}
function forceSquareAR() {
// get bpatcher width and height
r = this.patcher.box.rect;
width = r[2] - r[0];
height = r[3] - r[1];
if (width == prevWidth || height == prevHeight) {
longestSide = width >= height ? width : height;
this.patcher.box.rect = [r[0], r[1], r[0]+longestSide, r[1]+longestSide];
}
}
// slow down Task interval when idle
function adjustInterval(width, height) {
if (width == prevWidth || height == prevHeight) {
task.interval = Math.min(task.interval * intervalMult, maxInterval);
} else {
// reset to fast refresh if width or height changes
task.interval = minInterval;
}
}
// start the task loop
function start() {
// if not in a subpatcher
if (this.patcher.box == null) {
return;
}
task.cancel();
task.interval = minInterval;
task.repeat();
}
// stop the task loop
function stop() {
task.cancel();
}