From 35d100674a86503274e4ce2ad28d6d8f1786f250 Mon Sep 17 00:00:00 2001 From: Dave Pagurek Date: Wed, 5 Feb 2025 16:35:28 -0500 Subject: [PATCH] Add splineProperties --- src/core/p5.Renderer.js | 10 ++++++++++ src/shape/custom_shapes.js | 20 +++++++++++++++++++- test/unit/core/rendering.js | 18 ++++++++++++++++-- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/core/p5.Renderer.js b/src/core/p5.Renderer.js index e5ba194d63..f7d55b0c68 100644 --- a/src/core/p5.Renderer.js +++ b/src/core/p5.Renderer.js @@ -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 diff --git a/src/shape/custom_shapes.js b/src/shape/custom_shapes.js index cb53761d19..80aa77bb6b 100644 --- a/src/shape/custom_shapes.js +++ b/src/shape/custom_shapes.js @@ -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? @@ -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. * diff --git a/test/unit/core/rendering.js b/test/unit/core/rendering.js index 47b2fc2240..2e6e721f0a 100644 --- a/test/unit/core/rendering.js +++ b/test/unit/core/rendering.js @@ -1,5 +1,5 @@ import p5 from '../../../src/app.js'; -import { vi } from 'vitest'; +import { vi, expect } from 'vitest'; suite('Rendering', function() { var myp5; @@ -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; } @@ -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 }); + }); + }); });