Skip to content

Commit 92099de

Browse files
committed
added option and example for textToPoints() to allow separated points as mentioned in #4086
1 parent 591d4ce commit 92099de

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/typography/p5.Font.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,11 @@ p5.Font.prototype.textBounds = function(str, x = 0, y = 0, fontSize, opts) {
169169
* be removed from the polygon; the value represents the threshold angle to use
170170
* when determining whether two edges are collinear
171171
*
172+
* <br>separatePaths - if set to true, returns a 2D Array [paths][points]
173+
* separating each letter and path, to avoid a connecting line
174+
*
172175
* @return {Array} an array of points, each with x, y, alpha (the path angle)
176+
*
173177
* @example
174178
* <div>
175179
* <code>
@@ -209,6 +213,40 @@ p5.Font.prototype.textBounds = function(str, x = 0, y = 0, fontSize, opts) {
209213
* </code>
210214
* </div>
211215
*
216+
* @example
217+
* <div>
218+
* <code>
219+
* let font, points;
220+
*
221+
* function preload() {
222+
* font = loadFont('assets/inconsolata.otf');
223+
* }
224+
*
225+
* function setup() {
226+
* createCanvas(100, 100);
227+
* stroke(0);
228+
*
229+
* points = font.textToPoints('p5', 0, 0, height * 0.7, {
230+
* sampleFactor: 5,
231+
* simplifyThreshold: 0,
232+
* separatePaths: true
233+
* });
234+
* }
235+
*
236+
* function draw() {
237+
* background(255);
238+
* translate(width * 0.15, height * 0.7);
239+
* for (let i = 0; i < points.length; i++) {
240+
* beginShape();
241+
* for (let j = 0; j < points[i].length; j++) {
242+
* let p = points[i][j];
243+
* vertex(p.x + sin(j * 0.02 + frameCount * 0.1) * 2, p.y);
244+
* }
245+
* endShape(CLOSE);
246+
* }
247+
* }
248+
* </code>
249+
* </div>
212250
*/
213251
p5.Font.prototype.textToPoints = function(txt, x, y, fontSize, options) {
214252
let xoff = 0;
@@ -233,11 +271,19 @@ p5.Font.prototype.textToPoints = function(txt, x, y, fontSize, options) {
233271
paths = splitPaths(gpath.commands);
234272

235273
for (let j = 0; j < paths.length; j++) {
274+
let pathPts = [];
236275
const pts = pathToPoints(paths[j], options);
237276

238277
for (let k = 0; k < pts.length; k++) {
239278
pts[k].x += xoff;
240-
result.push(pts[k]);
279+
if (options.separatePaths) {
280+
pathPts.push(pts[k]);
281+
} else {
282+
result.push(pts[k]);
283+
}
284+
}
285+
if (options.separatePaths) {
286+
result.push(pathPts);
241287
}
242288
}
243289
}

0 commit comments

Comments
 (0)