Skip to content

Commit 6517307

Browse files
authored
Merge pull request #7373 from GregStanton/dev-2.0
Overhaul custom shapes for p5.js 2.0
2 parents 8d5498d + 6f10af2 commit 6517307

File tree

66 files changed

+3909
-3453
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+3909
-3453
lines changed

preview/global/sketch.js

+49-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,55 @@
1-
console.log(p5);
1+
const vertSrc = `#version 300 es
2+
precision mediump float;
3+
uniform mat4 uModelViewMatrix;
4+
uniform mat4 uProjectionMatrix;
25
6+
in vec3 aPosition;
7+
in vec2 aOffset;
8+
9+
void main(){
10+
vec4 positionVec4 = vec4(aPosition.xyz, 1.0);
11+
positionVec4.xy += aOffset;
12+
gl_Position = uProjectionMatrix * uModelViewMatrix * positionVec4;
13+
}
14+
`;
15+
16+
const fragSrc = `#version 300 es
17+
precision mediump float;
18+
out vec4 outColor;
19+
void main(){
20+
outColor = vec4(0.0, 1.0, 1.0, 1.0);
21+
}
22+
`;
23+
24+
let myShader;
325
function setup(){
4-
createCanvas(200, 200);
26+
createCanvas(100, 100, WEBGL);
27+
28+
// Create and use the custom shader.
29+
myShader = createShader(vertSrc, fragSrc);
30+
31+
describe('A wobbly, cyan circle on a gray background.');
532
}
633

7-
async function draw(){
8-
background(0, 50, 50);
9-
circle(100, 100, 50);
34+
function draw(){
35+
// Set the styles
36+
background(125);
37+
noStroke();
38+
shader(myShader);
39+
40+
// Draw the circle.
41+
beginShape();
42+
for (let i = 0; i < 30; i++){
43+
const x = 40 * cos(i/30 * TWO_PI);
44+
const y = 40 * sin(i/30 * TWO_PI);
45+
46+
// Apply some noise to the coordinates.
47+
const xOff = 10 * noise(x + millis()/1000) - 5;
48+
const yOff = 10 * noise(y + millis()/1000) - 5;
1049

11-
fill('white');
12-
textSize(30);
13-
text('hello', 10, 30);
50+
// Apply these noise values to the following vertex.
51+
vertexProperty('aOffset', [xOff, yOff]);
52+
vertex(x, y);
53+
}
54+
endShape(CLOSE);
1455
}

preview/index.html

+16-16
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,30 @@
2020
import p5 from '../src/app.js';
2121

2222
const sketch = function (p) {
23-
let f;
24-
const testWebgl = true
25-
26-
p.setup = async function () {
27-
// TODO: make this work without a name
28-
f = await p.loadFont('font/Lato-Black.ttf', 'Lato')
29-
p.createCanvas(200, 200, testWebgl ? p.WEBGL : undefined);
23+
p.setup = function () {
24+
p.createCanvas(100, 100, p.WEBGL);
3025
};
3126

3227
p.draw = function () {
33-
p.background(0, 50, 50);
34-
if (testWebgl) p.translate(-p.width/2, -p.height/2);
35-
36-
p.fill('white');
37-
p.textSize(60);
38-
p.textAlign(p.RIGHT, p.CENTER)
39-
p.textFont(f)
40-
p.text('hello, world!', 0, p.height/2, p.width);
28+
p.background(200);
29+
p.strokeCap(p.SQUARE);
30+
p.strokeJoin(p.MITER);
31+
p.translate(-p.width/2, -p.height/2);
32+
p.noStroke();
33+
p.beginShape();
34+
p.bezierOrder(2);
35+
p.fill('red');
36+
p.vertex(10, 10);
37+
p.fill('lime');
38+
p.bezierVertex(40, 25);
39+
p.fill('blue');
40+
p.bezierVertex(10, 40);
41+
p.endShape();
4142
};
4243
};
4344

4445
new p5(sketch);
4546
</script>
46-
<p style="font-family: Lato">hello, world!</p>
4747
</body>
4848

4949
</html>

src/color/p5.Color.js

+4
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,10 @@ class Color {
466466
}
467467

468468
get _array() {
469+
return this.array();
470+
}
471+
472+
array() {
469473
return [...this.color.coords, this.color.alpha];
470474
}
471475

src/color/setting.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,7 @@ function setting(p5, fn){
12691269
* </div>
12701270
*/
12711271
fn.noFill = function() {
1272-
this._renderer.states.doFill = false;
1272+
this._renderer.noFill();
12731273
return this;
12741274
};
12751275

@@ -1325,7 +1325,7 @@ function setting(p5, fn){
13251325
* </div>
13261326
*/
13271327
fn.noStroke = function() {
1328-
this._renderer.states.doStroke = false;
1328+
this._renderer.states.strokeColor = null;
13291329
return this;
13301330
};
13311331

src/core/constants.js

+41-1
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,18 @@ export const QUAD_STRIP = 'quad_strip';
768768
* @final
769769
*/
770770
export const TESS = 'tess';
771+
/**
772+
* @typedef {0x0007} EMPTY_PATH
773+
* @property {EMPTY_PATH} EMPTY_PATH
774+
* @final
775+
*/
776+
export const EMPTY_PATH = 0x0007;
777+
/**
778+
* @typedef {0x0008} PATH
779+
* @property {PATH} PATH
780+
* @final
781+
*/
782+
export const PATH = 0x0008;
771783
/**
772784
* @typedef {'close'} CLOSE
773785
* @property {CLOSE} CLOSE
@@ -1330,4 +1342,32 @@ export const HALF_FLOAT = 'half-float';
13301342
* @property {RGBA} RGBA
13311343
* @final
13321344
*/
1333-
export const RGBA = 'rgba';
1345+
export const RGBA = 'rgba';
1346+
1347+
/**
1348+
* The `splineEnds` mode where splines curve through
1349+
* their first and last points.
1350+
* @typedef {unique symbol} INCLUDE
1351+
* @property {INCLUDE} INCLUDE
1352+
* @final
1353+
*/
1354+
export const INCLUDE = Symbol('include');
1355+
1356+
/**
1357+
* The `splineEnds` mode where the first and last points in a spline
1358+
* affect the direction of the curve, but are not rendered.
1359+
* @typedef {unique symbol} EXCLUDE
1360+
* @property {EXCLUDE} EXCLUDE
1361+
* @final
1362+
*/
1363+
export const EXCLUDE = Symbol('exclude');
1364+
1365+
/**
1366+
* The `splineEnds` mode where the spline loops back to its first point.
1367+
* Only used internally.
1368+
* @typedef {unique symbol} JOIN
1369+
* @property {JOIN} JOIN
1370+
* @final
1371+
* @private
1372+
*/
1373+
export const JOIN = Symbol('join');

src/core/main.js

-3
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,6 @@ class p5 {
414414

415415
this._styles = [];
416416

417-
this._bezierDetail = 20;
418-
this._curveDetail = 20;
419-
420417
this._colorMode = constants.RGB;
421418
this._colorMaxes = {
422419
rgb: [255, 255, 255, 255],

0 commit comments

Comments
 (0)