|
19 | 19 | function runLiveSketch(s) {
|
20 | 20 | var letter = '';
|
21 | 21 | var words = 'Begin...';
|
| 22 | + var lineHeight = 36; |
| 23 | + var bottomPadding = 5; |
22 | 24 |
|
23 | 25 | s.setup = () => {
|
24 | 26 | s.createCanvas(640, 360);
|
25 |
| - // Create the font |
26 | 27 | s.textFont('Source Code Pro', 36);
|
| 28 | + s.textWrap(s.CHAR); |
27 | 29 | };
|
28 | 30 |
|
29 | 31 | s.draw = () => {
|
30 | 32 | s.background(0); // Set background to black
|
31 | 33 |
|
32 |
| - // Draw the letter to the center of the screen |
| 34 | + // Draw the letter and status text |
33 | 35 | s.textSize(14);
|
34 | 36 | s.fill(255);
|
35 | 37 | s.noStroke();
|
36 | 38 | s.text('Click on the program, then type to add to the String', 50, 50);
|
37 | 39 | s.text('Current key: ' + letter, 50, 70);
|
38 | 40 | s.text('The String is ' + words.length + ' characters long', 50, 90);
|
39 | 41 |
|
| 42 | + // Calculate the number of lines of text |
40 | 43 | s.textSize(36);
|
41 |
| - s.text(words, 50, 120, 540, 300); |
| 44 | + let textWidth = 540; |
| 45 | + let textHeight = s.textAscent() + s.textDescent(); |
| 46 | + |
| 47 | + // Calculate the number of lines needed for the text |
| 48 | + let numLines = s.ceil(s.textWidth(words) / textWidth); |
| 49 | + let contentHeight = numLines * textHeight + 120 + bottomPadding; |
| 50 | + |
| 51 | + // Resize canvas if the content height exceeds the current canvas height |
| 52 | + if (contentHeight > s.height) { |
| 53 | + s.resizeCanvas(640, contentHeight); |
| 54 | + } |
| 55 | + s.text(words, 50, 120, textWidth, s.height); |
42 | 56 | };
|
43 | 57 |
|
44 | 58 | s.keyPressed = () => {
|
45 |
| - // The variable "key" always contains the value |
46 |
| - // of the most recent key pressed. |
| 59 | + |
47 | 60 | if ((s.key >= 'A' && s.key <= 'z') || s.key == ' ') {
|
48 | 61 | letter = s.key;
|
49 | 62 | words = words + s.key;
|
50 | 63 | }
|
51 | 64 | };
|
52 | 65 | }
|
| 66 | + |
0 commit comments