|
| 1 | +var _setup = function(p, font) { |
| 2 | + var txt, tb, tw, x = 20, y = 50; |
| 3 | + |
| 4 | + p.createCanvas(240, 160); |
| 5 | + p.textFont(font); |
| 6 | + p.textSize(20); |
| 7 | + |
| 8 | + p.stroke("blue"); |
| 9 | + p.line(x, 0, x, p.height); |
| 10 | + |
| 11 | + txt = " leading space"; |
| 12 | + tb = font.textBounds(txt, x, y); |
| 13 | + tw = p.textWidth(txt); |
| 14 | + p.stroke("black"); |
| 15 | + p.rect(tb.x, tb.y, tb.w, tb.h); |
| 16 | + p.noStroke(); |
| 17 | + p.text(txt, x, y); |
| 18 | + p.stroke("red"); |
| 19 | + p.line(x, y + 6, x + tw, y + 6); |
| 20 | + |
| 21 | + y = 80; |
| 22 | + txt = "traction waste"; |
| 23 | + tb = font.textBounds(txt, x, y); |
| 24 | + tw = p.textWidth(txt); |
| 25 | + p.stroke("black"); |
| 26 | + p.rect(tb.x, tb.y, tb.w, tb.h); |
| 27 | + p.noStroke(); |
| 28 | + p.text(txt, x, y); |
| 29 | + p.stroke("red"); |
| 30 | + p.line(x, y + 6, x + tw, y + 6); |
| 31 | + |
| 32 | + y = 110; |
| 33 | + txt = "trailing space "; |
| 34 | + tb = font.textBounds(txt, x, y); |
| 35 | + tw = p.textWidth(txt); |
| 36 | + p.stroke("black"); |
| 37 | + p.rect(tb.x, tb.y, tb.w, tb.h); |
| 38 | + p.noStroke(); |
| 39 | + p.text(txt, x, y); |
| 40 | + p.stroke("red"); |
| 41 | + p.line(x, y + 6, x + tw, y + 6); |
| 42 | + |
| 43 | + y = 140; |
| 44 | + txt = " "; |
| 45 | + tb = font.textBounds(txt, x, y); |
| 46 | + tw = p.textWidth(txt); |
| 47 | + p.stroke("black"); |
| 48 | + p.rect(tb.x, tb.y, tb.w, p.max(tb.h, 3)); |
| 49 | + p.noStroke(); |
| 50 | + p.text(txt, x, y); |
| 51 | + p.stroke("red"); |
| 52 | + p.line(x, y + 6, x + tw, y + 6); |
| 53 | +}; |
1 | 54 |
|
2 | 55 | var textSketch = function(p) { |
| 56 | + p.setup = function() { |
| 57 | + p.loadFont("../acmesa.ttf", function(f) { |
| 58 | + _setup(p, f); |
| 59 | + }); |
| 60 | + }; |
| 61 | +}; |
3 | 62 |
|
4 | | - var font, txt, tb; |
| 63 | +var textSketchMono = function(p) { |
| 64 | + p.setup = function() { |
| 65 | + p.loadFont("../AndaleMono.ttf", function(f) { |
| 66 | + _setup(p, f); |
| 67 | + }); |
| 68 | + }; |
| 69 | +}; |
| 70 | + |
| 71 | +var textSketch1958 = function(p) { // issue #1958 |
| 72 | + var font, lineW, words = "swimming back to the rock"; |
5 | 73 |
|
6 | 74 | p.preload = function() { |
7 | | - font = p.loadFont("../acmesa.ttf"); |
| 75 | + font = p.loadFont("../OpenSans-Regular.ttf"); |
8 | 76 | }; |
9 | 77 |
|
10 | 78 | p.setup = function() { |
| 79 | + function textAsWords(words, x, y) { |
| 80 | + var tw, spaceW = p.textWidth(" "); |
| 81 | + console.log(spaceW); |
| 82 | + for (var i = 0; i < words.length; i++) { |
| 83 | + if (i != 0) { |
| 84 | + tw = p.textWidth(words[i - 1]); |
| 85 | + x += tw + spaceW; |
| 86 | + p.stroke(0); |
| 87 | + p.noFill(); |
| 88 | + p.rect(x - spaceW, y + 5, spaceW, -25); |
| 89 | + } |
| 90 | + p.fill(0); |
| 91 | + p.noStroke(); |
| 92 | + p.text(words[i], x, y); |
| 93 | + } |
| 94 | + } |
11 | 95 |
|
12 | | - p.createCanvas(240, 160); |
13 | | - p.textFont(font); |
14 | | - p.textSize(20); |
| 96 | + p.createCanvas(300, 200); |
| 97 | + p.background(255); |
15 | 98 |
|
16 | | - txt = ' space first'; |
17 | | - var tb = font.textBounds(txt,50,50,20); |
18 | | - p.rect(tb.x,tb.y,tb.w,tb.h); |
19 | | - p.text(txt, 50, 50); |
| 99 | + p.textSize(20); // Case 1: Default font |
| 100 | + p.noStroke(); |
| 101 | + p.text(words, 20, 50); |
| 102 | + textAsWords(words.split(" "), 20, 80); |
20 | 103 |
|
21 | | - txt = 'trailing space?' |
22 | | - tb = font.textBounds(txt,50,80,20); |
23 | | - p.rect(tb.x,tb.y,tb.w,tb.h); |
24 | | - p.text(txt, 50, 80); |
| 104 | + p.stroke(255, 0, 0); |
| 105 | + p.line(20, 0, 20, p.height); |
25 | 106 |
|
26 | | - txt = 'trailing space? ' |
27 | | - tb = font.textBounds(txt,50,110,20); |
28 | | - p.rect(tb.x,tb.y,tb.w,tb.h); |
29 | | - p.text(txt, 50, 110); |
| 107 | + lineW = p.textWidth(words); |
| 108 | + p.line(20 + lineW, 0, 20 + lineW, 90); |
30 | 109 |
|
31 | | - var tw = font._textWidth(txt); |
32 | | - tb = font.textBounds(' ',50,140,20); |
33 | | - p.rect(tb.x,tb.y,tb.w,tb.h); |
34 | | - }; |
| 110 | + p.textFont(font, 20); // Case 2: OpenSans |
| 111 | + p.noStroke(); |
| 112 | + p.text(words, 20, 120); |
| 113 | + textAsWords(words.split(" "), 20, 150); |
35 | 114 |
|
| 115 | + p.stroke(255, 0, 0); |
| 116 | + lineW = p.textWidth(words); |
| 117 | + p.line(20 + lineW, 100, 20 + lineW, p.height - 20); |
| 118 | + |
| 119 | + p.stroke(0); |
| 120 | + p.line(20, 160, 20 + p.textWidth(" "), 160); |
| 121 | + }; |
36 | 122 | }; |
37 | 123 |
|
| 124 | +/*var textSketch1957 = function(p) { // issue #1957 |
| 125 | + var font; |
| 126 | + p.preload = function() { |
| 127 | + font = p.loadFont("../AndaleMono.ttf"); |
| 128 | + }; |
| 129 | + p.setup = function() { |
| 130 | +
|
| 131 | + p.createCanvas(300, 400); |
| 132 | + p.textFont(font, 80); |
| 133 | +
|
| 134 | + p.text("a", 0, 100); |
| 135 | + p.text("b", 0, 200); |
| 136 | + p.text("c", 0, 300); |
| 137 | +
|
| 138 | + p.stroke(255,0,0); |
| 139 | + p.line(p.textWidth("a"), 0, p.textWidth("a"), p.height); |
| 140 | + p.line(p.textWidth("b"), 0, p.textWidth("b"), p.height); |
| 141 | + p.line(p.textWidth("c"), 0, p.textWidth("c"), p.height); |
| 142 | + p.noStroke(); |
| 143 | + p.textSize(10); |
| 144 | + p.text("a="+p.textWidth("a")+" b="+p.textWidth("b")+" c="+p.textWidth("c"), 10, 350); |
| 145 | + console.log(font); |
| 146 | + } |
| 147 | +} |
| 148 | +new p5(textSketch1957, "textSketch1957");*/ |
38 | 149 |
|
39 | | -new p5(textSketch, 'textSketch'); |
| 150 | +new p5(textSketch, "textSketch"); |
| 151 | +new p5(textSketchMono, "textSketchMono"); |
| 152 | +new p5(textSketch1958, "textSketch1958"); |
0 commit comments