Skip to content

Commit

Permalink
Make angles respect angleMode
Browse files Browse the repository at this point in the history
  • Loading branch information
davepagurek committed Dec 20, 2024
1 parent 7bbbdec commit d7583b0
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/type/p5.Font.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function font(p5, fn) {
cmdContours[cmdContours.length - 1].push(cmd);
}

return cmdContours.map((commands) => pathToPoints(commands, options));
return cmdContours.map((commands) => pathToPoints(commands, options, this));
}

textToModel(str, x, y, width, height, options) {
Expand Down Expand Up @@ -678,7 +678,7 @@ function font(p5, fn) {
return path;
};

function pathToPoints(cmds, options) {
function pathToPoints(cmds, options, font) {

const parseOpts = (options, defaults) => {
if (typeof options !== 'object') {
Expand Down Expand Up @@ -720,12 +720,19 @@ function font(p5, fn) {
const totalPoints = Math.ceil(path.getTotalLength() * opts.sampleFactor);
let points = [];

const mode = font._pInst.angleMode();
const DEGREES = font._pInst.DEGREES;
for (let i = 0; i < totalPoints; i++) {
const length = path.getTotalLength() * (i / (totalPoints - 1));
points.push({
...path.getPointAtLength(length),
get angle() {
return path.getAngleAtLength(length) * 180 / Math.PI;
const angle = path.getAngleAtLength(length);
if (mode === DEGREES) {
return angle * 180 / Math.PI;
} else {
return angle;
}
},
// For backwards compatibility
get alpha() {
Expand Down
27 changes: 27 additions & 0 deletions test/unit/visual/cases/typography.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,33 @@ visualSuite("Typography", function () {
p5.endShape();
screenshot();
});

for (const mode of ['RADIANS', 'DEGREES']) {
visualTest(`Fonts point angles work in ${mode} mode`, async function(p5, screenshot) {
p5.createCanvas(100, 100);
const font = await p5.loadFont(
'/unit/assets/Inconsolata-Bold.ttf'
);
p5.background(255);
p5.strokeWeight(2);
p5.textSize(50);
p5.angleMode(p5[mode]);
const pts = font.textToPoints('p5*js', 0, 50, { sampleFactor: 0.25 });
p5.beginShape(p5.LINES);
for (const { x, y, angle } of pts) {
p5.vertex(
x - 5 * p5.cos(angle),
y - 5 * p5.sin(angle)
);
p5.vertex(
x + 5 * p5.cos(angle),
y + 5 * p5.sin(angle)
);
}
p5.endShape();
screenshot();
});
}
});

visualSuite('textToContours', function() {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"numScreenshots": 1
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"numScreenshots": 1
}

0 comments on commit d7583b0

Please sign in to comment.