// Demonstrates bug in text placement. This sketch should draw some text and // a rectangle at the same location on screen. The draw() method moves the // drawing location left and right accross the screen. If text placement was // working correctly, the rectangle should surround the text. // // The bug appears to be due to the the text location being rounded before it // it scaled by the scaling factor. The larger the scaling, the coarser the // text placement. Note also, that even without scaling (set scaling=1), there // is some minor rounding of text position. Related to this is that the offset // implied by textAlign() is also rounded, so any values other than // textAlign(LEFT, BOTTOM) result in incorrect positioning. PFont font; float pos=0; float inc = 0.005; float w,h; float scaling = 50; // Change the value between 1 and 100 to see rounding effect. void setup() { size(400, 200); // Problem only occurs with default renderer. frameRate(10); font = createFont("GillSans",12); textFont(font, width/20); textAlign(LEFT,TOP); w = textWidth("Test"); h = textAscent(); } void draw() { background(200,180,180); textFont(font, width/(20*scaling)); strokeWeight(1/scaling); float x = lerp(0,(width-w)/scaling,pos); scale(scaling); // Draw rectangle and text at the same location. fill(255); rect(x,height/(2*scaling),w/scaling,h/scaling); fill(0); text("Test",x,height/(2*scaling)); // Move label a little to the left or right. pos += inc; if (pos >=1) inc = -0.005; if (pos <=0) inc = 0.005; }