frame-buffer.mdx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. ---
  2. title: FrameBuffer
  3. description: Low-level rendering surface for custom graphics
  4. order: 13
  5. ---
  6. # FrameBuffer
  7. A low-level rendering surface for custom graphics and complex visual effects. FrameBuffer provides a 2D array of cells with methods optimized for performance and memory.
  8. ## Basic usage
  9. ### Renderable API
  10. ```typescript
  11. import { FrameBufferRenderable, RGBA, createCliRenderer } from "@opentui/core"
  12. const renderer = await createCliRenderer()
  13. const canvas = new FrameBufferRenderable(renderer, {
  14. id: "canvas",
  15. width: 50,
  16. height: 20,
  17. })
  18. // Draw on the frame buffer
  19. canvas.frameBuffer.fillRect(5, 2, 20, 10, RGBA.fromHex("#FF0000"))
  20. canvas.frameBuffer.drawText("Hello!", 8, 6, RGBA.fromHex("#FFFFFF"))
  21. renderer.root.add(canvas)
  22. ```
  23. ### Construct API
  24. ```typescript
  25. import { FrameBuffer, createCliRenderer } from "@opentui/core"
  26. const renderer = await createCliRenderer()
  27. renderer.root.add(
  28. FrameBuffer({
  29. width: 50,
  30. height: 20,
  31. }),
  32. )
  33. ```
  34. ## Drawing methods
  35. ### setCell
  36. Set a single cell's content and colors:
  37. ```typescript
  38. canvas.frameBuffer.setCell(
  39. x, // X position
  40. y, // Y position
  41. char, // Character to display
  42. fg, // Foreground color (RGBA)
  43. bg, // Background color (RGBA)
  44. attributes, // Text attributes (optional, default: 0)
  45. )
  46. // Example
  47. canvas.frameBuffer.setCell(10, 5, "@", RGBA.fromHex("#FFFF00"), RGBA.fromHex("#000000"))
  48. ```
  49. ### setCellWithAlphaBlending
  50. Set a cell with alpha blending for transparency effects:
  51. ```typescript
  52. const semiTransparent = RGBA.fromValues(1.0, 0.0, 0.0, 0.5)
  53. const transparent = RGBA.fromValues(0, 0, 0, 0)
  54. canvas.frameBuffer.setCellWithAlphaBlending(10, 5, " ", transparent, semiTransparent)
  55. ```
  56. ### drawText
  57. Draw a string of text at a position:
  58. ```typescript
  59. canvas.frameBuffer.drawText(
  60. text, // String to draw
  61. x, // Starting X position
  62. y, // Y position
  63. fg, // Text color (RGBA)
  64. bg, // Background color (RGBA, optional)
  65. attributes, // Text attributes (optional, default: 0)
  66. )
  67. // Example
  68. canvas.frameBuffer.drawText("Score: 100", 2, 1, RGBA.fromHex("#00FF00"))
  69. ```
  70. ### fillRect
  71. Fill a rectangular area with a color:
  72. ```typescript
  73. canvas.frameBuffer.fillRect(
  74. x, // X position
  75. y, // Y position
  76. width, // Rectangle width
  77. height, // Rectangle height
  78. color, // Fill color (RGBA)
  79. )
  80. // Example: Draw a red rectangle
  81. canvas.frameBuffer.fillRect(10, 5, 20, 8, RGBA.fromHex("#FF0000"))
  82. ```
  83. ### drawFrameBuffer
  84. Copy another frame buffer onto this one:
  85. ```typescript
  86. canvas.frameBuffer.drawFrameBuffer(
  87. destX, // Destination X
  88. destY, // Destination Y
  89. sourceBuffer, // Source FrameBuffer (OptimizedBuffer)
  90. sourceX, // Source X offset (optional)
  91. sourceY, // Source Y offset (optional)
  92. sourceWidth, // Width to copy (optional)
  93. sourceHeight, // Height to copy (optional)
  94. )
  95. ```
  96. ### colorMatrix / colorMatrixUniform
  97. Apply native 4x4 RGBA matrix transforms for post-processing effects. Use `colorMatrixUniform` for full-buffer
  98. transforms, and `colorMatrix` when you want to target specific cells.
  99. ```typescript
  100. import { INVERT_MATRIX, TargetChannel } from "@opentui/core"
  101. // Full-buffer transform
  102. canvas.frameBuffer.colorMatrixUniform(INVERT_MATRIX, 1.0, TargetChannel.Both)
  103. // Per-cell transform with explicit mask
  104. const cellMask = new Float32Array([10, 5, 1.0, 11, 5, 0.5])
  105. canvas.frameBuffer.colorMatrix(INVERT_MATRIX, cellMask, 1.0, TargetChannel.FG)
  106. ```
  107. See [Color matrix reference](/docs/reference/color-matrix) for matrix layout, mask format, and behavior details.
  108. ## Properties
  109. | Property | Type | Default | Description |
  110. | -------------------------------- | --------- | ------------ | ------------------------------------- |
  111. | `width` | `number` | - | Buffer width in characters (required) |
  112. | `height` | `number` | - | Buffer height in rows (required) |
  113. | `respectAlpha` | `boolean` | `false` | Enable alpha blending when drawing |
  114. | `position` | `string` | `"relative"` | Positioning mode |
  115. | `left`, `top`, `right`, `bottom` | `number` | - | Position offsets |
  116. ## Example: Simple game canvas
  117. ```typescript
  118. import { FrameBufferRenderable, RGBA, createCliRenderer } from "@opentui/core"
  119. const renderer = await createCliRenderer()
  120. const gameCanvas = new FrameBufferRenderable(renderer, {
  121. id: "game",
  122. width: 40,
  123. height: 20,
  124. position: "absolute",
  125. left: 5,
  126. top: 2,
  127. })
  128. // Game state
  129. let playerX = 20
  130. let playerY = 10
  131. function render() {
  132. const fb = gameCanvas.frameBuffer
  133. const BG = RGBA.fromHex("#111111")
  134. // Clear the canvas
  135. fb.fillRect(0, 0, 40, 20, BG)
  136. // Draw border
  137. for (let x = 0; x < 40; x++) {
  138. fb.setCell(x, 0, "-", RGBA.fromHex("#444444"), BG)
  139. fb.setCell(x, 19, "-", RGBA.fromHex("#444444"), BG)
  140. }
  141. for (let y = 0; y < 20; y++) {
  142. fb.setCell(0, y, "|", RGBA.fromHex("#444444"), BG)
  143. fb.setCell(39, y, "|", RGBA.fromHex("#444444"), BG)
  144. }
  145. // Draw player
  146. fb.setCell(playerX, playerY, "@", RGBA.fromHex("#00FF00"), BG)
  147. // Draw score
  148. fb.drawText("Score: 0", 2, 0, RGBA.fromHex("#FFFF00"))
  149. }
  150. // Handle input
  151. renderer.keyInput.on("keypress", (key) => {
  152. switch (key.name) {
  153. case "up":
  154. playerY = Math.max(1, playerY - 1)
  155. break
  156. case "down":
  157. playerY = Math.min(18, playerY + 1)
  158. break
  159. case "left":
  160. playerX = Math.max(1, playerX - 1)
  161. break
  162. case "right":
  163. playerX = Math.min(38, playerX + 1)
  164. break
  165. }
  166. render()
  167. })
  168. render()
  169. renderer.root.add(gameCanvas)
  170. ```
  171. ## Example: Progress bar
  172. ```typescript
  173. const EMPTY_BG = RGBA.fromHex("#222222")
  174. function drawProgressBar(fb, x, y, width, progress, color) {
  175. const filled = Math.floor(width * progress)
  176. // Draw filled portion
  177. for (let i = 0; i < filled; i++) {
  178. fb.setCell(x + i, y, "█", color, EMPTY_BG)
  179. }
  180. // Draw empty portion
  181. for (let i = filled; i < width; i++) {
  182. fb.setCell(x + i, y, "░", RGBA.fromHex("#333333"), EMPTY_BG)
  183. }
  184. }
  185. // Usage
  186. drawProgressBar(canvas.frameBuffer, 5, 10, 30, 0.75, RGBA.fromHex("#00FF00"))
  187. ```
  188. ## Performance tips
  189. 1. **Batch updates**: Make multiple changes to the frame buffer before the next render cycle
  190. 2. **Minimize fillRect calls**: Use individual setCell calls for complex shapes
  191. 3. **Reuse RGBA objects**: Create color constants instead of calling `fromHex` repeatedly
  192. ```typescript
  193. // Good: Create once, reuse
  194. const RED = RGBA.fromHex("#FF0000")
  195. const GREEN = RGBA.fromHex("#00FF00")
  196. const BG = RGBA.fromHex("#000000")
  197. for (let i = 0; i < 100; i++) {
  198. fb.setCell(i, 5, "*", RED, BG)
  199. }
  200. // Avoid: Creating new RGBA objects in loops
  201. for (let i = 0; i < 100; i++) {
  202. fb.setCell(i, 5, "*", RGBA.fromHex("#FF0000"), RGBA.fromHex("#000000")) // Creates 200 objects
  203. }
  204. ```