Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions src/strands/strands_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,75 @@ function installBuiltinGlobalAccessors(strandsContext) {

for (const name of Object.keys(BUILTIN_GLOBAL_SPECS)) {
const spec = BUILTIN_GLOBAL_SPECS[name]
Object.defineProperty(window, name, {
get: () => {
const backingKey = `_strands_${name}`

// Define on window for global mode only
const inst = getRuntimeP5Instance()
if (inst?._isGlobal) {
Object.defineProperty(window, name, {
get: () => {
if (strandsContext.active) {
return getBuiltinGlobalNode(strandsContext, name);
}
const inst = getRuntimeP5Instance()
return spec.get(inst);
},
configurable: true,
})
}

// Capture original descriptor (held in closure for the getter to delegate to)
const originalProtoDesc = Object.getOwnPropertyDescriptor(strandsContext.p5.prototype, name);

// Define on p5.prototype for instance mode
Object.defineProperty(strandsContext.p5.prototype, name, {
get: function() {
if (strandsContext.active) {
return getBuiltinGlobalNode(strandsContext, name);
}
const inst = getRuntimeP5Instance()
return spec.get(inst);
// If our setter stored a value on this instance, return it
if (Object.prototype.hasOwnProperty.call(this, backingKey)) {
return this[backingKey];
}
// Delegate to original getter (e.g. width -> this._renderer?.width)
if (originalProtoDesc?.get) {
return originalProtoDesc.get.call(this);
}
// Fall back to original value for data properties (like mouseX)
return originalProtoDesc?.value;
},
set: function(val) {
this[backingKey] = val;
},
configurable: true,
})

// Define on p5.Graphics.prototype for graphics mode
const GraphicsProto = strandsContext.p5?.Graphics?.prototype;
if (GraphicsProto) {
const originalDesc = Object.getOwnPropertyDescriptor(GraphicsProto, name);

Object.defineProperty(GraphicsProto, name, {
get: function() {
if (strandsContext.active) {
return getBuiltinGlobalNode(strandsContext, name);
}
// Delegate to original getter if it exists (class-level getters like width, deltaTime)
if (originalDesc?.get) {
return originalDesc.get.call(this);
}
return this[backingKey];
},
set: function(val) {
if (originalDesc?.set) {
originalDesc.set.call(this, val);
} else {
this[backingKey] = val;
}
},
configurable: true,
})
}
}
strandsContext._builtinGlobalsAccessorsInstalled = true
}
Expand Down
14 changes: 6 additions & 8 deletions test/unit/visual/cases/webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1172,16 +1172,14 @@ visualTest('randomGaussian() in a fragment loop averages to the mean', (p5, scre
});

visualTest('uses width/height in getFinalColor', (p5, screenshot) => {
let firstShader;
function firstShaderCallback() {
getFinalColor((color) => {
color = [width / 60, height / 60, 0, 1];
return color;
});
}
p5.createCanvas(60, 60, p5.WEBGL);
p5.pixelDensity(1);
firstShader = p5.baseColorShader().modify(firstShaderCallback);
const firstShader = p5.baseColorShader().modify(() => {
p5.getFinalColor((color) => {
color = [p5.width / 60, p5.height / 60, 0, 1];
return color;
});
}, { p5 });
p5.background(0);
p5.shader(firstShader);
p5.noStroke();
Expand Down
9 changes: 4 additions & 5 deletions test/unit/webgl/p5.Shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,23 +516,22 @@ test('returns numbers for builtin globals outside hooks and a strandNode when ca
myp5.createCanvas(5, 5, myp5.WEBGL);
myp5.baseMaterialShader().modify(() => {
myp5.getPixelInputs(inputs => {
const mxInHook = window.mouseX;
const wInHook = window.width;
const mxInHook = myp5.mouseX;
const wInHook = myp5.width;
assert.isTrue(mxInHook.isStrandsNode);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

assert.isTrue(wInHook.isStrandsNode);
inputs.color = [1, 0, 0, 1];
return inputs;
});
}, { myp5 });

const mx = window.mouseX;
const w = window.width;
const mx = myp5.mouseX;
const w = myp5.width;
assert.isNumber(mx);
assert.isNumber(w);
assert.strictEqual(w, myp5.width);
});


test('map() works inside a strands modify callback', () => {
myp5.createCanvas(50, 50, myp5.WEBGL);
const testShader = myp5.baseMaterialShader().modify(() => {
Expand Down
Loading