v2.0.0-beta.1
Pre-releaseWhat's Changed 🎊
In addition to lots of behind-the-scenes refactoring, here are some new features that we'd love your help testing out!
Async loading (thanks to @limzykenneth)
Rather than having a preload
function, p5 2.0 has async setup
!
1.x | 2.0 |
---|---|
let img;
function preload() {
img = loadImage('cat.jpg');
}
function setup() {
createCanvas(200, 200);
} |
let img;
async function setup() {
createCanvas(200, 200);
img = await loadImage('cat.jpg');
} |
Support for loading fonts via CSS (thanks to @dhowe)
In 2D mode, try loading fonts via a a path to a CSS font file, such as a Google Fonts link!
loadFont("https://fonts.googleapis.com/css2?family=Bricolage+Grotesque:opsz,[email protected],200..800&display=swap")
loadFont(`@font-face { font-family: "Bricolage Grotesque", serif; font-optical-sizing: auto; font-weight: <weight> font-style: normal; font-variation-settings: "wdth" 100; }`);
loadFont({
fontFamily: '"Bricolage Grotesque", serif',
fontOpticalSizing: 'auto',
fontWeight: '<weight>',
fontStyle: 'normal',
fontVariationSettings: '"wdth" 100',
});
loadFont("https://fonts.gstatic.com/s/inter/v18/UcCO3FwrK3iLTeHuS_nVMrMxCp50SjIw2boKoduKmMEVuLyfMZhrib2Bg-4.ttf");
loadFont("path/to/localFont.ttf");
loadFont("system-font-name");
Support for variable font weight (thanks to contributions by @dhowe)
In 2D mode, if you've loaded a variable font, try changing its weight!
async function setup() {
createCanvas(100, 100);
const font = await loadFont(
'https://fonts.googleapis.com/css2?family=Sniglet:wght@400;800&display=swap'
);
}
function draw() {
background(255);
textFont(font);
textAlign(LEFT, TOP);
textSize(35);
textWeight(sin(millis() * 0.002) * 200 + 400);
text('p5*js', 0, 10);
}
More ways to draw and manipulate text (thanks to @davepagurek)
Like how textToPoints()
gives you points on text, the new textToContours()
function lets you edit the points on text and then draw them with fills!
createCanvas(100, 100);
const font = await loadFont('myFont.ttf');
background(200);
strokeWeight(2);
textSize(50);
const contours = font.textToContours('p5*js', 0, 50, { sampleFactor: 0.5 });
beginShape();
for (const pts of contours) {
beginContour();
for (const pt of pts) {
vertex(x + 20*sin(y*0.01), y + 20*sin(x*0.01));
}
endContour(CLOSE);
}
endShape();
In WebGL, you can use textToModel
to extrude a 3D model out of your text:
createCanvas(100, 100, WEBGL);
const font = await loadFont('myFont.ttf');
background(200);
textSize(50);
const geom = font.textToModel('p5*js', 0, 50, { sampleFactor: 2, extrude: 20 });
orbitControl();
model(geom);
A new pointer event handling system (thanks to @diyaayay)
Instead of having separate methods for mouse and touch, we now use the browser's pointer API to handle both simultaneously. Try defining mouse functions as usual and accessing the global touches
array to see what pointers are active for multitouch support!
Custom shader attributes (thanks to @lukeplowden)
If you are using a shader and want custom per-vertex properties in addition to uniform
s, which are the same across the whole shape, you can now call vertexProperty(name, value)
before vertices.
const vertSrc = `#version 300 es
precision mediump float;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
in vec3 aPosition;
in vec2 aOffset;
void main(){
vec4 positionVec4 = vec4(aPosition.xyz, 1.0);
positionVec4.xy += aOffset;
gl_Position = uProjectionMatrix * uModelViewMatrix * positionVec4;
}
`;
const fragSrc = `#version 300 es
precision mediump float;
out vec4 outColor;
void main(){
outColor = vec4(0.0, 1.0, 1.0, 1.0);
}
`;
function setup(){
createCanvas(100, 100, WEBGL);
// Create and use the custom shader.
const myShader = createShader(vertSrc, fragSrc);
shader(myShader);
describe('A wobbly, cyan circle on a gray background.');
}
function draw(){
// Set the styles
background(125);
noStroke();
// Draw the circle.
beginShape();
for (let i = 0; i < 30; i++){
const x = 40 * cos(i/30 * TWO_PI);
const y = 40 * sin(i/30 * TWO_PI);
// Apply some noise to the coordinates.
const xOff = 10 * noise(x + millis()/1000) - 5;
const yOff = 10 * noise(y + millis()/1000) - 5;
// Apply these noise values to the following vertex.
vertexProperty('aOffset', [xOff, yOff]);
vertex(x, y);
}
endShape(CLOSE);
}
Updated bezier and curve drawing functions (thanks to @GregStanton)
First off: you can combine multiple types of curves in one begin/endShape()
block now!
Long cubic and quadratic bezier vertex calls are now split up into their individual control points. Both cubic and quadratic curves are done with bezierVertex
now, and you can set bezierOrder()
to change from cubic (order 3) to quadratic (order 2). For WebGL mode, this also means you can also specify texture coordinates per control point, or change the fill, stroke, normal, and more between control points.
1.x | 2.0 |
---|---|
beginShape();
vertex(10, 10);
vertex(30, 10);
bezierVertex(35, 10, 40, 15, 40, 20);
vertex(40, 30);
quadraticVertex(40, 40, 30, 40);
vertex(10, 40);
endShape(CLOSE); |
beginShape();
vertex(10, 10);
vertex(30, 10);
// Default cubic
bezierVertex(35, 10);
bezierVertex(40, 15);
bezierVertex(40, 20);
vertex(40, 30);
bezierOrder(2);
bezierVertex(40, 40);
bezierVertex(30, 40);
vertex(10, 40);
endShape(p5.CLOSE); |
We've renamed curveVertex
to splineVertex
and have given it more options. By default, it will now go through every splineVertex
, so you no longer have to double up the first/last point to get it to go through it:
1.x | 2.0 |
---|---|
beginShape();
curveVertex(10, 10);
curveVertex(10, 10);
curveVertex(15, 40);
curveVertex(40, 35);
curveVertex(25, 15);
curveVertex(15, 25);
curveVertex(15, 25);
endShape(); |
beginShape();
splineVertex(10, 10);
splineVertex(15, 40);
splineVertex(40, 35);
splineVertex(25, 15);
splineVertex(15, 25);
endShape(); |
Similarly, endShape(CLOSE)
(or endContour(CLOSE)
if you're in a contour) will cause a spline to smoothly loop back on itself so you no longer need to double up any points:
1.x | 2.0 |
---|---|
beginShape();
curveVertex(15, 25);
curveVertex(10, 10);
curveVertex(15, 40);
curveVertex(40, 35);
curveVertex(25, 15);
curveVertex(15, 25);
curveVertex(10, 10);
curveVertex(15, 40);
endShape(); |
beginShape();
splineVertex(10, 10);
splineVertex(15, 40);
splineVertex(40, 35);
splineVertex(25, 15);
splineVertex(15, 25);
endShape(CLOSE); |
A new simple lines mode for WebGL (thanks to contributions from @perminder-17)
If you are drawing lots of shapes, and don't need stroke caps or joins, you can use a simple lines mode for increased performance in WebGL. You can activate this mode by calling linesMode(SIMPLE)
in your sketch.
Custom shaders for fills, strokes, images (thanks to @Garima3110 and @perminder-17)
You can now create your own shaders for fills, strokes, and images, and have them all applied at once! Use shader()
to set a fill shader, strokeShader()
to set a stroke shader, and imageShader()
to set an image shader. Try using baseMaterialShader().modify(...)
and baseStrokeShader().modify(...)
to create custom shaders.
let myFillShader = baseMaterialShader.modify({
'vec4 getFinalColor': `(vec4 color) {
return vec4(1., 1., 0., 1.);
}`
});
let myStrokeShader = baseStrokeShader.modify({
'vec4 getFinalColor': `(vec4 color) {
return vec4(1., 0., 1., 1.);
}`
});
let myImageShader = baseMaterialShader.modify({
'vec4 getFinalColor': `(vec4 color) {
return vec4(0., 1., 1., 1.);
}`
});
shader(myFillShader);
strokeShader(myStrokeShader);
imageShader(myImageShader);
sphere(); // Draws with the custom stroke and image shaders
image(myImg, 0, 0); // Draws with the custom image shader
A new public p5.Matrix
class (thanks to @holomorfo)
TODO add examples
Support for more color spaces in p5.Color
(thanks to @limzykenneth and @dianamgalindo)
TODO add examples
Support for wide-gamut colors with the display-p3
mode (thanks to @limzykenneth)
TODO add examples
Full List
- modified stroke vertex shader by @JordanSucher in #7064
- Fix #7049: P5.Graphics.remove() doesn't release all related resources by @iambiancafonseca in #7060
- Separate Model and View Matrices. by @deveshidwivedi in #6761
- Switch camera keep transform by @davepagurek in #7067
- Check the element type before calling
createRadio
fromselect()
by @lindapaiste in #6838 - Use requestAnimationFrame timestamp for less noise in deltaTime by @davepagurek in #6785
- fix: framebuffer cameras uses the size from main canvas by @Vishal2002 in #7075
- Fix code snippet formatting in references by @nickmcintyre in #7089
- Fix more <code>s in reference by @davepagurek in #7092
- Update clearDepth() example to restore trails by @davepagurek in #7095
- Changed point class documentation description text by @Chaitanya1672 in #7096
- Adds geometry to downloadable obj files functionality by @diyaayay in #6812
- Fix vector reflect() mutating surface normal arg (#7088) by @nbogie in #7103
- Implemented roll function by @rohanjulka19 in #7093
- Fix clipped multiline font rendering by @davepagurek in #7109
- Update: FES Messages in Japanese. by @shibomb in #7074
- Addresses issue #7077 by @Garima3110 in #7102
- Corrects the circle() description by @JulioGitLab in #7110
- Add initial conversion script from Documentation.js to old format by @davepagurek in #6777
- Fix broken JsDoc comments by @Orasund in #7114
- Resolves issue #7118 - Update README.md & Changed the path for contributor docs by @ravixalgorithm in #7119
- fix: Correct default parameters for quad in y-direction by @Abhinavcode13 in #7129
- Fix 'Learn' and 'Get Started' links in README.md by @NicholasGillen in #7126
- Fix: corrected variable name to reflect height instead of width by @Abhinavcode13 in #7132
- Skip out of canvas shapes for gridOutput() table by @limzykenneth in #7135
- Added window size check upon p5 instantiation by @bensgilbert in #7134
- Adds
webp
mime type tosaveCanvas
. by @starzonmyarmz in #7140 - fixed: Broken link to dev_notes.md in the documentation by @Shahmaz0 in #7117
- "Fix Broken 'Get Started for Developers' Link in README" by @Souvik-Cyclic in #7151
- Update readme copy by @davepagurek in #6944
- Standardize all parameter types by @sproutleaf in #7179
- Modify convert.js and finish standardizing parameters by @sproutleaf in #7183
- Start developing a new param validator using Zod by @sproutleaf in #7186
- Update to param validator + test file by @sproutleaf in #7194
- [p5.js 2.0] Refactor and modular build by @limzykenneth in #7203
- Fix most WebGL tests by @davepagurek in #7210
- Get visual tests running in 2.0 by @davepagurek in #7251
- Small fixes to help get dev-2.0 branch running for other contributors by @lukeplowden in #7215
- Module syntax conversion by @limzykenneth in #7257
- More test fixes by @davepagurek in #7266
- Update param validator and test file by @sproutleaf in #7268
- Shader hooks for dev-2.0 by @davepagurek in #7267
- Final changes for parameter validation by @sproutleaf in #7291
- setAttribute() function for defining custom shader attributes by @lukeplowden in #7276
- Initial modules refactor by @davepagurek in #7295
- [p5.js 2.0] WebGL module syntax by @limzykenneth in #7296
- Add new sketch verifier to FES by @sproutleaf in #7293
- Make shaders define what they get used for. by @Garima3110 in #7256
- Shape tests by @davepagurek in #7323
- Split contents of dom.js among four(4) files by @SilasVM in #7316
- Solves issue #7059 by @Garima3110 in #7113
- Add more shape visual tests by @davepagurek in #7350
- [p5.js 2.0] State machines and renderer refactoring by @limzykenneth in #7270
- WebGL mode refactor by @davepagurek in #7355
- 2.0 modules clean up by @limzykenneth in #7361
- Add custom_shapes.js, which will eventually replace vertex.js by @GregStanton in #7368
- Fix issues with stroke depth ordering by @davepagurek in #7369
- [p5.js 2.0] IO refactoring by @limzykenneth in #7365
- Fix issues with retained mode rendering and image light by @davepagurek in #7384
- [p5.js 2.0] Fix CSV parsing by @limzykenneth in #7391
- Adding strokeMode() to access SIMPLE lines. by @perminder-17 in #7390
- Event Ordering for touch and mouse events by @diyaayay in #7378
- [p5.js 2.0] Unit tests fix/conversion by @limzykenneth in #7393
- Type 2.0 by @dhowe in #7356
- Refactor 2.0 Typography code to work with WebGL by @davepagurek in #7417
- Overhaul custom shapes for p5.js 2.0 by @GregStanton in #7373
- Add tests for font weight + fix Google Font loading by @davepagurek in #7426
- Add visual tests for textToPoints, addTextToContours by @davepagurek in #7427
- textToPoints perf improvement by @davepagurek in #7428
- FilterRenderer2D for a 2d-Build by @perminder-17 in #7409
- [p5.js 2.0] Color module rewrite by @limzykenneth in #7406
- [p5.js 2.0] Vector n-dimentional and Matrix Interface by @holomorfo in #7405
- [dev-2.0] Benchmark setup and sample report for render and batch by @holomorfo in #7421
- Changed mouse button to object by @diyaayay in #7404
- Updates to shader hooks for 2.0 by @davepagurek in #7432
- More typography updates by @davepagurek in #7434
- Type: implement #7147 and minor refactors by @dhowe in #7440
- Fix MTL color loading when vertices are shared across different faces by @davepagurek in #7441
- Fix noErase() breaking in WebGL by @davepagurek in #7443
- added loadFilterShader function by @Rishab87 in #7445
- [dev-2.0] WOFF font support by @dhowe in #7449
New Contributors
- @JordanSucher made their first contribution in #7064
- @iambiancafonseca made their first contribution in #7060
- @Chaitanya1672 made their first contribution in #7096
- @rohanjulka19 made their first contribution in #7093
- @Orasund made their first contribution in #7114
- @ravixalgorithm made their first contribution in #7119
- @NicholasGillen made their first contribution in #7126
- @bensgilbert made their first contribution in #7134
- @starzonmyarmz made their first contribution in #7140
- @Shahmaz0 made their first contribution in #7117
- @Souvik-Cyclic made their first contribution in #7151
- @sproutleaf made their first contribution in #7179
- @lukeplowden made their first contribution in #7215
- @holomorfo made their first contribution in #7405
Full Changelog: v1.9.4...v2.0.0-beta.1