Skip to content

Add splineProperties #7528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2025
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
10 changes: 10 additions & 0 deletions src/core/p5.Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ class Renderer {
this.updateShapeProperties();
}

splineProperties(values) {
if (values) {
for (const key in values) {
this.splineProperty(key, values[key]);
}
} else {
return { ...this.states.splineProperties };
}
}

splineVertex(x, y, z = 0, u = 0, v = 0) {
const position = new Vector(x, y, z);
const textureCoordinates = this.getSupportedIndividualVertexProperties().textureCoordinates
Expand Down
20 changes: 19 additions & 1 deletion src/shape/custom_shapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,16 @@ class Shape {
this._splineProperties[key] = value;
}

splineProperties(values) {
if (values) {
for (const key in values) {
this.splineProperty(key, values[key]);
}
} else {
return this._splineProperties;
}
}

/*
To-do: Maybe refactor #createVertex() since this has side effects that aren't advertised
in the method name?
Expand Down Expand Up @@ -1597,12 +1607,20 @@ function customShapes(p5, fn) {
/**
* TODO: documentation
* @param {String} key
* @param value
* @param [value]
*/
fn.splineProperty = function(key, value) {
return this._renderer.splineProperty(key, value);
};

/**
* TODO: documentation
* @param {Object} [values]
*/
fn.splineProperties = function(values) {
return this._renderer.splineProperties(values);
};

/**
* Adds a vertex to a custom shape.
*
Expand Down
18 changes: 16 additions & 2 deletions test/unit/core/rendering.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import p5 from '../../../src/app.js';
import { vi } from 'vitest';
import { vi, expect } from 'vitest';

suite('Rendering', function() {
var myp5;
Expand Down Expand Up @@ -185,7 +185,7 @@ suite('Rendering', function() {
try {
expect(function() {
myp5[webglMethod].call(myp5);
}).to.throw(Error, /is only supported in WEBGL mode/);
}).toThrow(Error, /is only supported in WEBGL mode/);
} finally {
myp5.validateParameters = validateParamters;
}
Expand All @@ -194,4 +194,18 @@ suite('Rendering', function() {
);
}
});

suite('spline functions', function() {
test('setting via splineProperty()', function() {
myp5.splineProperty('tightness', 2);
expect(myp5.splineProperty('tightness')).toEqual(2);
expect(myp5.splineProperties()).toMatchObject({ tightness: 2 });
});

test('setting via splineProperties()', function() {
myp5.splineProperties({ tightness: 2 });
expect(myp5.splineProperty('tightness')).toEqual(2);
expect(myp5.splineProperties()).toMatchObject({ tightness: 2 });
});
});
});