Draw text in screen space * Automatically splits new lines into rows * @param {string|number} text * @param {Vector2} pos * @param {number} size * @param {Color} [color=WHITE] * @param {number} [lineWidth] * @param {Color} [lineColor=BLACK] * @param {CanvasTextAlign} [textAli
(text, pos, size, color=WHITE, lineWidth=0, lineColor=BLACK, textAlign='center', font=fontDefault, fontStyle='', maxWidth, angle=0, context=drawContext)
| 914 | * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} [context=drawContext] |
| 915 | * @memberof Draw */ |
| 916 | function drawTextScreen(text, pos, size, color=WHITE, lineWidth=0, lineColor=BLACK, textAlign='center', font=fontDefault, fontStyle='', maxWidth, angle=0, context=drawContext) |
| 917 | { |
| 918 | ASSERT(isStringLike(text), 'text must be a string'); |
| 919 | ASSERT(isVector2(pos), 'pos must be a vec2'); |
| 920 | ASSERT(isNumber(size), 'size must be a number'); |
| 921 | ASSERT(isColor(color), 'color must be a color'); |
| 922 | ASSERT(isNumber(lineWidth), 'lineWidth must be a number'); |
| 923 | ASSERT(isColor(lineColor), 'lineColor must be a color'); |
| 924 | ASSERT(['left','center','right'].includes(textAlign), 'align must be left, center, or right'); |
| 925 | ASSERT(isStringLike(font), 'font must be a string'); |
| 926 | ASSERT(isStringLike(fontStyle), 'fontStyle must be a string'); |
| 927 | ASSERT(isNumber(angle), 'angle must be a number'); |
| 928 | |
| 929 | const lines = (text+'').split('\n'); |
| 930 | const posY = pos.y - (lines.length-1) * size/2; // center vertically |
| 931 | // save before style mutations so caller's context state is preserved |
| 932 | context.save(); |
| 933 | context.fillStyle = color.toString(); |
| 934 | context.strokeStyle = lineColor.toString(); |
| 935 | context.lineWidth = lineWidth; |
| 936 | context.textAlign = textAlign; |
| 937 | context.font = fontStyle + ' ' + size + 'px '+ font; |
| 938 | context.textBaseline = 'middle'; |
| 939 | context.translate(pos.x, posY); |
| 940 | context.rotate(-angle); |
| 941 | let yOffset = 0; |
| 942 | lines.forEach(line=> |
| 943 | { |
| 944 | lineWidth && context.strokeText(line, 0, yOffset, maxWidth); |
| 945 | context.fillText(line, 0, yOffset, maxWidth); |
| 946 | yOffset += size; |
| 947 | }); |
| 948 | context.restore(); |
| 949 | } |
| 950 | |
| 951 | /////////////////////////////////////////////////////////////////////////////// |
| 952 | // Drawing utilities |
no test coverage detected