Fonts in Processing.js

Since the documentation at http://processing.js is still tailored for processing code, I’ll post an example of how to port font usage to JS (assuming you have an “inArray” function like jQuery’s).  It shows how you can store all the fonts you need in an associative array for easy reference afterward:

var fonts = {};

processing.setup = function()
{
  var fontList = processing.PFont.list();

  if (jQuery.inArray('sans-serif', fontList) >= 0)
  {
    fonts['sans-serif'] = 
      {'12': pr.createFont('sans-serif', 12, true)};
  }
  else
  {
    // Error
  }
}

// Somewhere else in your code ...
processing.textFont(fonts['sans-serif']['12']);
processing.text("Hello world", 10, 10);
0 thoughts on “Fonts in Processing.js”

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: