Skip to content

Commit df56c80

Browse files
authored
Merge pull request #6453 from nickmcintyre/sod-p5-font
Edit docs for p5.Font
2 parents 0bffa48 + a28ad4e commit df56c80

File tree

1 file changed

+132
-64
lines changed

1 file changed

+132
-64
lines changed

src/typography/p5.Font.js

Lines changed: 132 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,30 @@ import p5 from '../core/main';
1111
import * as constants from '../core/constants';
1212

1313
/**
14-
* Base class for font handling
14+
* A class to describe fonts.
1515
* @class p5.Font
1616
* @constructor
17-
* @param {p5} [pInst] pointer to p5 instance
17+
* @param {p5} [pInst] pointer to p5 instance.
18+
* @example
19+
* <div>
20+
* <code>
21+
* let font;
22+
*
23+
* function preload() {
24+
* // Creates a p5.Font object.
25+
* font = loadFont('assets/inconsolata.otf');
26+
* }
27+
*
28+
* function setup() {
29+
* fill('deeppink');
30+
* textFont(font);
31+
* textSize(36);
32+
* text('p5*js', 10, 50);
33+
*
34+
* describe('The text "p5*js" written in pink on a white background.');
35+
* }
36+
* </code>
37+
* </div>
1838
*/
1939
p5.Font = class {
2040
constructor(p){
@@ -23,51 +43,103 @@ p5.Font = class {
2343
this.cache = {};
2444

2545
/**
26-
* Underlying opentype font implementation
46+
* Underlying
47+
* <a href="https://opentype.js.org/" target="_blank">opentype.js</a>
48+
* font object.
2749
* @property font
2850
*/
2951
this.font = undefined;
3052
}
3153

3254
/**
33-
* Returns a tight bounding box for the given text string using this
34-
* font
55+
* Returns the bounding box for a string of text written using this
56+
* <a href="#/p5.Font">p5.Font</a>.
3557
*
36-
* @method textBounds
37-
* @param {String} line a line of text
38-
* @param {Number} x x-position
39-
* @param {Number} y y-position
40-
* @param {Number} [fontSize] font size to use (optional) Default is 12.
41-
* @param {Object} [options] opentype options (optional)
42-
* opentype fonts contains alignment and baseline options.
43-
* Default is 'LEFT' and 'alphabetic'
58+
* The first parameter, `str`, is a string of text. The second and third
59+
* parameters, `x` and `y`, are the text's position. By default, they set the
60+
* coordinates of the bounding box's bottom-left corner. See
61+
* <a href="#/p5/textAlign">textAlign()</a> for more ways to align text.
62+
*
63+
* The fourth parameter, `fontSize`, is optional. It sets the font size used to
64+
* determine the bounding box. By default, `font.textBounds()` will use the
65+
* current <a href="#/p5/textSize">textSize()</a>.
4466
*
45-
* @return {Object} a rectangle object with properties: x, y, w, h
67+
* @method textBounds
68+
* @param {String} str string of text.
69+
* @param {Number} x x-coordinate of the text.
70+
* @param {Number} y y-coordinate of the text.
71+
* @param {Number} [fontSize] font size. Defaults to the current
72+
* <a href="#/p5/textSize">textSize()</a>.
73+
* @return {Object} object describing the bounding box with
74+
* properties x, y, w, and h.
4675
*
4776
* @example
4877
* <div>
4978
* <code>
5079
* let font;
51-
* let textString = 'Lorem ipsum dolor sit amet.';
80+
*
81+
* function preload() {
82+
* font = loadFont('assets/inconsolata.otf');
83+
* }
84+
*
85+
* function setup() {
86+
* background(200);
87+
*
88+
* let bbox = font.textBounds('p5*js', 35, 53);
89+
* rect(bbox.x, bbox.y, bbox.w, bbox.h);
90+
*
91+
* textFont(font);
92+
* text('p5*js', 35, 53);
93+
*
94+
* describe('The text "p5*js" written in black inside a white rectangle.');
95+
* }
96+
* </code>
97+
* </div>
98+
*
99+
* <div>
100+
* <code>
101+
* let font;
102+
*
52103
* function preload() {
53-
* font = loadFont('./assets/Regular.otf');
104+
* font = loadFont('assets/inconsolata.otf');
54105
* }
106+
*
55107
* function setup() {
56-
* background(210);
108+
* background(200);
109+
*
110+
* textFont(font);
111+
* textSize(15);
112+
* textAlign(CENTER, CENTER);
57113
*
58-
* let bbox = font.textBounds(textString, 10, 30, 12);
59-
* fill(255);
60-
* stroke(0);
114+
* let bbox = font.textBounds('p5*js', 50, 50);
115+
* rect(bbox.x, bbox.y, bbox.w, bbox.h);
116+
*
117+
* text('p5*js', 50, 50);
118+
*
119+
* describe('The text "p5*js" written in black inside a white rectangle.');
120+
* }
121+
* </code>
122+
* </div>
123+
*
124+
* <div>
125+
* <code>
126+
* let font;
127+
*
128+
* function preload() {
129+
* font = loadFont('assets/inconsolata.otf');
130+
* }
131+
*
132+
* function setup() {
133+
* background(200);
134+
*
135+
* let bbox = font.textBounds('p5*js', 31, 53, 15);
61136
* rect(bbox.x, bbox.y, bbox.w, bbox.h);
62-
* fill(0);
63-
* noStroke();
64137
*
65138
* textFont(font);
66-
* textSize(12);
67-
* text(textString, 10, 30);
139+
* textSize(15);
140+
* text('p5*js', 31, 53);
68141
*
69-
* describe(`Words “Lorem ipsum dol” go off canvas and
70-
* contained by white bounding box`);
142+
* describe('The text "p5*js" written in black inside a white rectangle.');
71143
* }
72144
* </code>
73145
* </div>
@@ -174,59 +246,55 @@ p5.Font = class {
174246
}
175247

176248
/**
177-
* Computes an array of points following the path for specified text
249+
* Returns an array of points outlining a string of text written using this
250+
* <a href="#/p5.Font">p5.Font</a>.
178251
*
179-
* @method textToPoints
180-
* @param {String} txt a line of text
181-
* @param {Number} x x-position
182-
* @param {Number} y y-position
183-
* @param {Number} fontSize font size to use (optional)
184-
* @param {Object} [options] an (optional) object that can contain:
252+
* The first parameter, `str`, is a string of text. The second and third
253+
* parameters, `x` and `y`, are the text's position. By default, they set the
254+
* coordinates of the bounding box's bottom-left corner. See
255+
* <a href="#/p5/textAlign">textAlign()</a> for more ways to align text.
256+
*
257+
* The fourth parameter, `fontSize`, is optional. It sets the text's font
258+
* size. By default, `font.textToPoints()` will use the current
259+
* <a href="#/p5/textSize">textSize()</a>.
260+
*
261+
* The fifth parameter, `options`, is also optional. `font.textToPoints()`
262+
* expects an object with the following properties:
185263
*
186-
* <br>sampleFactor - the ratio of path-length to number of samples
187-
* (default=.1); higher values yield more points and are therefore
188-
* more precise
264+
* `sampleFactor` is the ratio of the text's path length to the number of
265+
* samples. It defaults to 0.1. Higher values produce more points along the
266+
* path and are more precise.
189267
*
190-
* <br>simplifyThreshold - if set to a non-zero value, collinear points will be
191-
* be removed from the polygon; the value represents the threshold angle to use
192-
* when determining whether two edges are collinear
268+
* `simplifyThreshold` removes collinear points if it's set to a number other
269+
* than 0. The value represents the threshold angle to use when determining
270+
* whether two edges are collinear.
193271
*
194-
* @return {Array} an array of points, each with x, y, alpha (the path angle)
272+
* @method textToPoints
273+
* @param {String} str string of text.
274+
* @param {Number} x x-coordinate of the text.
275+
* @param {Number} y y-coordinate of the text.
276+
* @param {Number} [fontSize] font size. Defaults to the current
277+
* <a href="#/p5/textSize">textSize()</a>.
278+
* @param {Object} [options] object with sampleFactor and simplifyThreshold
279+
* properties.
280+
* @return {Array} array of point objects, each with x, y, and alpha (path angle) properties.
195281
* @example
196282
* <div>
197283
* <code>
198284
* let font;
285+
*
199286
* function preload() {
200287
* font = loadFont('assets/inconsolata.otf');
201288
* }
202289
*
203-
* let points;
204-
* let bounds;
205290
* function setup() {
206-
* createCanvas(100, 100);
207-
* stroke(0);
208-
* fill(255, 104, 204);
209-
*
210-
* points = font.textToPoints('p5', 0, 0, 10, {
211-
* sampleFactor: 5,
212-
* simplifyThreshold: 0
291+
* background(200);
292+
* let points = font.textToPoints('p5*js', 6, 60, 35, { sampleFactor: 0.5 });
293+
* points.forEach(p => {
294+
* point(p.x, p.y);
213295
* });
214-
* bounds = font.textBounds(' p5 ', 0, 0, 10);
215-
* }
216296
*
217-
* function draw() {
218-
* background(255);
219-
* beginShape();
220-
* translate(-bounds.x * width / bounds.w, -bounds.y * height / bounds.h);
221-
* for (let i = 0; i < points.length; i++) {
222-
* let p = points[i];
223-
* vertex(
224-
* p.x * width / bounds.w +
225-
* sin(20 * p.y / bounds.h + millis() / 1000) * width / 30,
226-
* p.y * height / bounds.h
227-
* );
228-
* }
229-
* endShape(CLOSE);
297+
* describe('A set of black dots outlining the text "p5*js" on a gray background.');
230298
* }
231299
* </code>
232300
* </div>

0 commit comments

Comments
 (0)