Skip to content
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

Build base classes and visitors for custom shapes in p5.js 2.0 #7373

Draft
wants to merge 43 commits into
base: dev-2.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
62567a9
commit message
GregStanton Nov 9, 2024
2107ecb
Add custom_shapes.js to eventually replace vertex.js
GregStanton Nov 9, 2024
e38b3b9
Set up files for custom shape refactor
GregStanton Nov 12, 2024
cff8461
Merge branch 'dev-2.0' of https://github.com/processing/p5.js into de…
GregStanton Nov 12, 2024
b778784
Add inline documentation for Shape class
GregStanton Nov 12, 2024
19e8c48
Fix documentation of default parameter for Shape constructor
GregStanton Nov 12, 2024
8eb74e6
Update inline documentation for p5.Shape
GregStanton Nov 13, 2024
f0fc908
Document p5.Vertex
GregStanton Nov 14, 2024
1a05d51
Fix code snippet in p5.Vertex documentation
GregStanton Nov 14, 2024
45591b0
Revise p5.Vertex documentation and add todo comment about a new imple…
GregStanton Nov 15, 2024
37c2db4
Move TODO comments out of the code (and into project timeline)
GregStanton Nov 15, 2024
9663eda
Document p5.Contour
GregStanton Nov 15, 2024
5cd71a2
Document p5.ShapePrimitive (and revise some other documentation).
GregStanton Nov 15, 2024
84a0fde
Revise docs for clarity.
GregStanton Nov 15, 2024
0b117cc
Add TODO comment about Vertex interface
GregStanton Nov 22, 2024
2fcc2b0
Describe custom vertex properties in Shape docs
GregStanton Nov 22, 2024
d862591
Revise p5.Contour docs based on new design
GregStanton Nov 22, 2024
8ea67c5
Remove @constructor tags (YUIDoc syntax)
GregStanton Nov 22, 2024
1443b53
Fix typo in Shape docs
GregStanton Nov 22, 2024
78940ff
Update p5.ShapePrimitive docs with new design
GregStanton Nov 22, 2024
4dac39e
Update p5.Vertex docs to reflect new design
GregStanton Nov 22, 2024
567ff25
Make p5.Vertex and p5.Shape constructors consistent
GregStanton Nov 22, 2024
dbff7a2
Merge branch 'dev-2.0' of https://github.com/processing/p5.js into de…
GregStanton Nov 22, 2024
727ff60
Move fill and stroke into the base renderer
davepagurek Nov 18, 2024
c756172
Add vertex state getter
davepagurek Nov 18, 2024
b4e43e5
Fix usage of false instead of null
davepagurek Nov 22, 2024
64e319b
Add properties to current shape
davepagurek Nov 22, 2024
290f5c7
Update vertexProperties to make setters
davepagurek Nov 23, 2024
cd156bf
Refactor old vertex() into legacyVertex() and start making new vertex()
davepagurek Nov 23, 2024
bc252e3
Implement Vertex constructor
GregStanton Nov 23, 2024
0c61aaf
Implement ShapePrimitive, add some doc tags
GregStanton Nov 23, 2024
2326ff5
Add PATH to constants.js
GregStanton Nov 23, 2024
75030a5
Implement Contour
GregStanton Nov 23, 2024
70a6fa6
Add EMPTY_PATH to constants.js
GregStanton Nov 23, 2024
d7aec76
Add kind getter to Contour to handle EMPTY_PATH
GregStanton Nov 23, 2024
27f0b26
Add tag to Anchor docs
GregStanton Nov 23, 2024
4eaa56f
Implement Anchor, add vertices to ShapePrimitive
GregStanton Nov 23, 2024
13f21d9
Implement Segment, add tags to docs
GregStanton Nov 24, 2024
787ca3c
Implement addToShape on ShapePrimitive (base class)
GregStanton Nov 24, 2024
dbf64f6
Changed initial value of index in Segment
GregStanton Nov 24, 2024
65c2d32
Add note in Segment docs
GregStanton Nov 24, 2024
20922c7
Move index out of base Segment class
GregStanton Nov 24, 2024
9468d2e
Add at() method on Shape, revise Segment & Anchor, implement LineSegm…
GregStanton Nov 24, 2024
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
4 changes: 2 additions & 2 deletions src/color/setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ function setting(p5, fn){
* </div>
*/
fn.noFill = function() {
this._renderer.states.doFill = false;
this._renderer.noFill();
return this;
};

Expand Down Expand Up @@ -1325,7 +1325,7 @@ function setting(p5, fn){
* </div>
*/
fn.noStroke = function() {
this._renderer.states.doStroke = false;
this._renderer.states.strokeColor = null;
return this;
};

Expand Down
12 changes: 12 additions & 0 deletions src/core/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,18 @@ export const BOTTOM = 'bottom';
* @final
*/
export const BASELINE = 'alphabetic';
/**
Copy link
Collaborator Author

@GregStanton GregStanton Nov 23, 2024

Choose a reason for hiding this comment

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

@davepagurek Does this look okay? I remembered from the 2.0 RFC that the plan is to use Symbol when the underlying value of a constant isn't needed. I'm guessing we just haven't gotten around to changing all the constants that fit that description?

Update: I added both PATH and EMPTY_PATH as Symbol values.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think so!

* @typedef {unique symbol} PATH
* @property {PATH} PATH
* @final
*/
export const PATH = Symbol('path');
/**
* @typedef {unique symbol} EMPTY_PATH
* @property {EMPTY_PATH} EMPTY_PATH
* @final
*/
export const EMPTY_PATH = Symbol('empty_path');
/**
* @typedef {0x0000} POINTS
* @property {POINTS} POINTS
Expand Down
82 changes: 75 additions & 7 deletions src/core/p5.Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
* @for p5
*/

import { Color } from '../color/p5.Color';
import * as constants from '../core/constants';
import { Image } from '../image/p5.Image';
import { Vector } from '../math/p5.Vector';
import { Shape } from '../shape/custom_shapes';

class Renderer {
constructor(pInst, w, h, isMainCanvas) {
Expand All @@ -25,9 +28,9 @@ class Renderer {

// Renderer state machine
this.states = {
doStroke: true,
strokeColor: new Color([0, 0, 0]),
strokeSet: false,
doFill: true,
fillColor: new Color([255, 255, 255]),
fillSet: false,
tint: null,
imageMode: constants.CORNER,
Expand All @@ -49,6 +52,8 @@ class Renderer {
this._clipping = false;
this._clipInvert = false;
this._curveTightness = 0;

this.currentShape = new Shape(this.vertexProperties());
}

remove() {
Expand Down Expand Up @@ -91,6 +96,45 @@ class Renderer {
pop() {
this._pushPopDepth--;
Object.assign(this.states, this._pushPopStack.pop());
this.updateShapeVertexProperties();
}

beginShape(...args) {
this.currentShape.reset();
this.currentShape.beginShape(...args);
}

endShape(...args) {
this.currentShape.endShape(...args);
this.drawShape(this.currentShape);
}

drawShape(shape) {
throw new Error('Unimplemented')
}

vertex(x, y) {
let z, u, v;

// default to (x, y) mode: all other arguments assumed to be 0.
z = u = v = 0;

if (arguments.length === 3) {
// (x, y, z) mode: (u, v) assumed to be 0.
z = arguments[2];
} else if (arguments.length === 4) {
// (x, y, u, v) mode: z assumed to be 0.
u = arguments[2];
v = arguments[3];
} else if (arguments.length === 5) {
// (x, y, z, u, v) mode
z = arguments[2];
u = arguments[3];
v = arguments[4];
}
const position = new Vector(x, y, z);
const textureCoordinates = new Vector(u, v);
this.currentShape.vertex(position, textureCoordinates);
}

beginClip(options = {}) {
Expand Down Expand Up @@ -153,14 +197,38 @@ class Renderer {

}

fill() {
fill(...args) {
this.states.fillSet = true;
this.states.doFill = true;
this.states.fillColor = this._pInst.color(...args);
this.updateShapeVertexProperties();
}

stroke() {
noFill() {
this.states.fillColor = null;
}

stroke(...args) {
this.states.strokeSet = true;
this.states.doStroke = true;
this.states.strokeColor = this._pInst.color(...args);
this.updateShapeVertexProperties();
}

noStroke() {
this.states.strokeColor = null;
}

vertexProperties() {
return {
stroke: this.states.strokeColor,
fill: this.states.fillColor,
}
}

updateShapeVertexProperties() {
const props = this.vertexProperties();
for (const key in props) {
this.currentShape[key](props[key]);
}
}

textSize(s) {
Expand Down Expand Up @@ -254,7 +322,7 @@ class Renderer {
// fix for #5785 (top of bounding box)
let finalMinHeight = y;

if (!(this.states.doFill || this.states.doStroke)) {
if (!(this.states.fillColor || this.states.strokeColor)) {
return;
}

Expand Down
Loading
Loading