{"id":144,"date":"2016-07-29T18:14:52","date_gmt":"2016-07-29T18:14:52","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/?page_id=144"},"modified":"2018-10-17T11:14:04","modified_gmt":"2018-10-17T15:14:04","slug":"text-output","status":"publish","type":"page","link":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/text-output\/","title":{"rendered":"Text Output"},"content":{"rendered":"<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/REC-html40\/loose.dtd\">\n<html><body><p>Printing text is a time-honored debugging method. It&rsquo;s often frowned<br>\nupon as primitive, unfocused, or non-interactive, but printing has<br>\nadvantages:<\/p>\n<ul><li>The information is all there, so you can go forward or backward in (run) time, scanning for information.<\/li>\n<li>You do not have to manually step through or pause your program to look at values.<\/li>\n<li>Sometimes you can find timing-dependent or rare problems that you would not see using a debugger.<\/li>\n<\/ul><p>We have introduced <code>print<\/code>, but <code>print<\/code> is<br>\n awkward and requires you to open the console. Sometimes, it would be<br>\nnice to put the text output right on the page with your canvas.<\/p>\n<h2>hprint<\/h2>\n<p>I&rsquo;ve written a small library for displaying debugging output to the browser. It is called <b>hprint<\/b>, short for &ldquo;html print&rdquo; (maybe you can think of a better name). Here are the functions you can use:<\/p>\n<ul><li><code>hprint(a, b, c, ...)<\/code> &mdash; convert arguments to strings and print them with one space separation.<\/li>\n<li><code>hprintln(a, b, c, ...)<\/code> is identical to <code>hprint()<\/code> except that it prints a newline after printing the arguments.<\/li>\n<li><code>hlines(n)<\/code> &mdash; restrict the number of lines to appear; if <code>n<\/code> is negative (the default), there is no restriction.<\/li>\n<li><code>hprecision(d)<\/code> &mdash; Print floating point numbers with up to <code>d<\/code> places to the right of the decimal point. If <code>d<\/code> is negative (the default), numbers are printed using the default toString() method.<\/li>\n<li><code>createHlines(width, height)<\/code> &mdash; specifies output size in<br>\npixels. This is not required, but if called, output will appear in a<br>\nscrollable box. The default is to simply add text to the bottom of the<br>\npage, which can grow arbitrarily unless restricted by a call to <code>hlines().<\/code><\/li>\n<li><code>removeHlines()<\/code> &mdash; removes all output. Output may be resumed. To resume output to a scrollable box, call createHlines() again.<\/li>\n<\/ul><h3>Examples<\/h3>\n<ul><li><code>hprint(\"Hello World\")<\/code> &mdash; prints <code>\"Hello World\\n\"<\/code><\/li>\n<li><code>hprint(3, 2, 1, 'go')<\/code> &mdash; prints <code>\"3 2 1 go\"<\/code><\/li>\n<li><code>hprint(\"\" + 3 + 2 + 1 + 'go')<\/code> &mdash; prints <code>\"321go\"<\/code><\/li>\n<li><code>hprecision(3); hprint(12.0, 1.23456)<\/code> &mdash; prints <code>\"12 1.234\"<\/code><\/li>\n<\/ul><p>Here is an animated gif (you may have to click it to see it run). See below for how to use hlines.js in your programs.<\/p>\n<p><img loading=\"lazy\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2016\/07\/hprint.gif\" alt=\"hprint\" width=\"643\" height=\"533\" class=\"alignnone size-full wp-image-154\" srcset=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2016\/07\/hprint.gif 643w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2016\/07\/hprint-300x249.gif 300w\" sizes=\"(max-width: 643px) 85vw, 643px\"><\/p>\n<p>The code that created this is the following:<\/p>\n<pre lang=\"javascript\">\r\n \r\nfunction setup() {\r\n    createCanvas(250, 200);\r\n    background(220);\r\n    text(\"Example canvas with text output below.\", 10, 20);\r\n    text(\"Created with hprint.js library.\", 10, 40);\r\n    frameRate(4);\r\n    hlines(10);    \/\/ only keep last 10 lines of output\r\n    hprecision(3); \/\/ print numbers limited to 3 decimal places\r\n}\r\n \r\nvar count = 0;\r\n \r\nfunction draw() {\r\n    \/\/ hprint() does not print a newline:\r\n    if (count % 5 == 0) {\r\n        hprint(\"Hello\", millis());\r\n        hprintln(\" ms\");\r\n    } else { \/\/ hprintln() prints arguments followed by newline:\r\n        hprintln(\"Hello\", millis());\r\n    }\r\n    \/\/ Every 20 lines, we switch output style between scrolling box and \r\n    \/\/ output direct to the page.\r\n    if (count % 20 === 0) {\r\n        removeHlines(); \/\/ remove all hprint output\r\n        if (count \n<h3>Using hprint in your code<\/h3>\n<p>First, download the <a href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2016\/07\/hprint.js\">hprint.js<\/a> file and put it in the same directory as your sketch.<br>\nSecond, add a line to your .html file to load hprint.js, using the<br>\nfollowing as a guide. Notice line 9 with &ldquo;hprint.js&rdquo; &mdash; just add this<br>\nline to your .html file:<\/p>\n<pre lang=\"html\" mark=\"9\">\r\n\r\n\r\n  \r\n    <meta charset=\"UTF-8\"><title>hprint<\/title><script src=\"http:\/\/cdnjs.cloudflare.com\/ajax\/libs\/p5.js\/0.4.9\/p5.js\"><\/script><script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/p5.js\/0.4.9\/addons\/p5.dom.js\"><\/script><script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/p5.js\/0.4.9\/addons\/p5.sound.js\"><\/script><script src=\"hprint.js\" type=\"text\/javascript\"><\/script><script src=\"sketch.js\" type=\"text\/javascript\"><\/script><\/pre><\/pre><\/body><\/html>\n","protected":false},"excerpt":{"rendered":"<p>Printing text is a time-honored debugging method. It&rsquo;s often frowned upon as primitive, unfocused, or non-interactive, but printing has advantages: The information is all there, so you can go forward or backward in (run) time, scanning for information. You do not have to manually step through or pause your program to look at values. Sometimes &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/text-output\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Text Output&#8221;<\/span><\/a><\/p>\n","protected":false},"author":535,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/pages\/144"}],"collection":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/users\/535"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/comments?post=144"}],"version-history":[{"count":12,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/pages\/144\/revisions"}],"predecessor-version":[{"id":34233,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/pages\/144\/revisions\/34233"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/media?parent=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}