最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - p5.js loadFont function? - Stack Overflow

programmeradmin2浏览0评论

How can I change the font in p5.js? It does not recognize the Processing term "loadFont," does not carry over a font from CSS, nor does it let me put in a .vlw file or link to a GoogleFont. At least, not in any way I have tried.

The references page only contains "text" and "textFont" options (in the Typography section at the end of the p5.js references page), neither of which allow for actually specifying a font.

I have also tried the

text.style('font-family', 'Walter Turncoat');

option listed here (.js/wiki/Beyond-the-canvas) to no avail. It actually broke the whole page. In CSS:

@font-face {
    font-family: 'Walter Turncoat';
    src: url('+Turncoat');
}

Processing version did not work:

var type = loadFont("AmericanTypewriter-48.vlw");
var smallType = loadFont("AmericanTypewriter-14.vlw");

Also,

var type = "Helvetica"; 

which they have in the examples for text and textFont does not work.

There has to be a way to have another font. Please help!

How can I change the font in p5.js? It does not recognize the Processing term "loadFont," does not carry over a font from CSS, nor does it let me put in a .vlw file or link to a GoogleFont. At least, not in any way I have tried.

The references page only contains "text" and "textFont" options (in the Typography section at the end of the p5.js references page), neither of which allow for actually specifying a font.

I have also tried the

text.style('font-family', 'Walter Turncoat');

option listed here (https://github./lmccart/p5.js/wiki/Beyond-the-canvas) to no avail. It actually broke the whole page. In CSS:

@font-face {
    font-family: 'Walter Turncoat';
    src: url('http://fonts.googleapis./css?family=Walter+Turncoat');
}

Processing version did not work:

var type = loadFont("AmericanTypewriter-48.vlw");
var smallType = loadFont("AmericanTypewriter-14.vlw");

Also,

var type = "Helvetica"; 

which they have in the examples for text and textFont does not work.

There has to be a way to have another font. Please help!

Share Improve this question asked Sep 30, 2014 at 0:34 LucyLucy 691 gold badge1 silver badge2 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 6

The examples given in the reference work fine. Run code snippet below for results. What do you mean when you say it doesn't work for you?

function setup() {
  createCanvas(640, 480);
}

function draw() {
  fill(0);
  textSize(36);
  textFont("Georgia");
  text("Hello World! in Georgia.", 12, 40);
  textFont("Arial");
  text("Hello World! in Arial.", 12, 100);
  textFont("Walter Turncoat");
  text("Hello World! in Walter Turncoat.", 12, 160);
}
<link href="http://fonts.googleapis./css?family=Walter+Turncoat&.css" rel="stylesheet"/>
<script src="http://cdn.jsdelivr/p5.js/0.3.8/p5.min.js"></script>

To load a font in p5.js you need a .ttf or .otf file, p5 doesn't work with .vlw files. So to use a font in p5 you need to:

  1. Get a .ttf or .otf font file. This font file will be loaded on execution time to your app.
  2. Declare a global variable to keep the font.
  3. Load the font with loadFont in a preload function.
  4. After the font is loaded you must use textFont() to tell p5 that this is the font to be used.
  5. Print someting with text().

Here is an example:

var myFont, fontReady = false;

function fontRead(){
    fontReady = true;
}

function preload() {
    myFont = loadFont("./fonts/MyfontFile.ttf", fontRead);
}

function setup() {
    createCanvas(720, 400);
    doyourSetup();
}

function draw() {
    background(255);
    if (fontReady) {
        textFont(myFont);
        text("Hello World!", 10, 30);
    }
}

You need to load the font in preload:

var font;

function preload() {
    font = loadFont('thefont.ttf');
}

function setup() {
    createCanvas(600, 400);
    textFont(font);
}

function draw() {
    background(255);
    text('The Text', 280, 300);
}

According to the docs, if you have a font file that p5 recognizes (such as otf, ttf ect...), you can load that font file and than use it with the following 2 lines of code:

var myFont = loadFont('customfont.ttf');
textFont(myFont);

and then write with the font like this:

text('Stack overflow', 2,2);
var myfont;
function preload() {
font = loadFont('font.ttf)
}
function setup{
createCanvas(400, 400)
}
function draw{
textFont(myfont)
text("Hello", 200, 200)
}

There is no need for a ready function because newer versions of p5.js will not display the project until it is finshed loading if it is in the preload function.

发布评论

评论列表(0)

  1. 暂无评论