Sets the current alignment for drawing text. The parameters LEFT, CENTER, and RIGHT set the display characteristics of the letters in relation to the values for the x and y parameters of the Ptext function.

An optional second parameter can be used to vertically align the text. BASELINE is the default, and the vertical alignment will be reset to BASELINE if the second parameter is not used. The TOP and CENTER parameters are straightforward. The BOTTOM parameter offsets the line based on the current PtextDescent. For multiple lines, the final line will be aligned to the bottom, with the previous lines appearing above it.

When using Ptext with width and height parameters, BASELINE is ignored, and treated as TOP. (Otherwise, text would by default draw outside the box, since BASELINE is the default setting. BASELINE is not a useful drawing mode for text drawn in a rectangle.)

The vertical alignment is based on the value of PtextAscent, which many fonts do not specify correctly. It may be necessary to use a hack and offset by a few pixels by hand so that the offset looks correct. To do this as less of a hack, use some percentage of PtextAscent or PtextDescent so that the hack works even if you change the size of the font.

Link to original Processing doc

PtextAlign is available since version 1.0.0.

See also



// @preview image 200 400 '2D3' PGraphics 255 Pbackground 0x4fff0000 Pstroke 0 Pfill 20 PtextSize 100 0 100 80 Pline 'Left' 100 25 Ptext //default: LEFT 'CENTER' PtextAlign 'Center' 100 50 Ptext 'RIGHT' PtextAlign 'Right' 100 75 Ptext 120 'y' STORE 0 $y 200 $y Pline 'CENTER' PtextAlign //default = baseline 'BASELINE bdjp' 100 $y Ptext 180 'y' STORE 0 $y 200 $y Pline 'CENTER' 'TOP' PtextAlign 'TOP bdjp' 100 $y Ptext 240 'y' STORE 0 $y 200 $y Pline 'CENTER' 'BOTTOM' PtextAlign 'BOTTOM bdjp' 100 $y Ptext 300 'y' STORE 0 $y 200 $y Pline 'CENTER' 'CENTER' PtextAlign 'CENTER bdjp' 100 $y Ptext Pencode