topical media & game development

talk show tell print

#javascript-processing-example-basic-typography-letters.htm / htm



  <!DOCTYPE html>
  <html><head>
  <script src="javascript-processing-example-processing.js"></script>
  <script src="javascript-processing-example-init.js"></script>
  <link rel="stylesheet" href="javascript-processing-example-style.css">
  </head><body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1>
  <h2>Letters</h2>
  
  <p>Drawing letters to the screen in Processing
  uses a technology developed in the mid 1990s
  at the Visual Language Workshop at the MIT
  Media Laboratory. It is a closed system,
  but we have supplied a number of fonts
  located in the "font" directory in the main
  "processing" directory. We expect to change
  the Processing font technology in the future.</p>
  
  <p><a href="http://processing.org/learning/basics/letters.html"><b>Original Processing.org Example:</b> Letters</a><br>
  <script type="application/processing">
  size(200, 200);
  background(0);
  
  // Load the font. Fonts are located within the 
  // main Processing directory/folder and they
  // must be placed within the data directory
  // of your sketch for them to load
  PFont fontA = loadFont("Courier New");
  textFont(fontA, 36);
  textAlign(CENTER);
  
  // Set the gray value of the letters
  fill(255);
  
  // Set the left and top margin
  int margin = 6;
  int gap = 30;
  translate(margin*1.5, margin*2);
  
  // Create a matrix of letterforms
  int counter = 0;
  for(int i=0; i<margin; i++) {
    for(int j=0; j<margin; j++) {
      char letter;
      
      // Select the letter
      int count = 65+(i*margin)+j;
      if(count <= 90) {
        letter = char(65+counter);
        if(letter == 'A' || letter == 'E' || letter == 'I' || 
           letter == 'O' || letter == 'U') {
             fill(204, 204, 0);
        } else {
          fill(255);
        }
      } else {
        fill(153);
        letter = char(48+counter);
      }
   
      // Draw the letter to the screen
      text(letter, 15+j*gap, 20+i*gap);
   
      // Increment the counter
      counter++;
      if(counter >= 26) {
        counter = 0;
      }
    }
  }
  </script><canvas width="200" height="200"></canvas></p>
  <div style="overflow: hidden; height: 0px; width: 0px;"></div>
  
  <pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a>
  // unless otherwise stated.</b>
  size(200, 200);
  background(0);
  
  // Load the font. Fonts are located within the 
  // main Processing directory/folder and they
  // must be placed within the data directory
  // of your sketch for them to load
  PFont fontA = loadFont("Courier New");
  textFont(fontA, 36);
  textAlign(CENTER);
  
  // Set the gray value of the letters
  fill(255);
  
  // Set the left and top margin
  int margin = 6;
  int gap = 30;
  translate(margin*1.5, margin*2);
  
  // Create a matrix of letterforms
  int counter = 0;
  for(int i=0; i&lt;margin; i++) {
    for(int j=0; j&lt;margin; j++) {
      char letter;
      
      // Select the letter
      int count = 65+(i*margin)+j;
      if(count &lt;= 90) {
        letter = char(65+counter);
        if(letter == 'A' || letter == 'E' || letter == 'I' || 
           letter == 'O' || letter == 'U') {
             fill(204, 204, 0);
        } else {
          fill(255);
        }
      } else {
        fill(153);
        letter = char(48+counter);
      }
   
      // Draw the letter to the screen
      text(letter, 15+j*gap, 20+i*gap);
   
      // Increment the counter
      counter++;
      if(counter &gt;= 26) {
        counter = 0;
      }
    }
  }</pre>
  </body></html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.