Derived from the cocosair-graphics-and-dom skill and its canonical docs/source pointers; reorganized on 2026-10-11. Source annotations below identify repository inputs, not files guaranteed to ship to npm consumers.
Contracts
- The UI host is a contract, not boilerplate.
Graphics,AirUIMeshandcreateAirTextureSpriteoutput are 2D renderers, so they draw only under aCanvaswhosecameraComponentis an ortho camera whosevisibilitycontains the node'sLayers.Enum.UI_2Dbit (src/air/ui-kit.ts:38,:44-51,:61,:65);createUICanvas(scene, name, options)builds that whole host andcocosair-ui-2downs it. A barenew Node()+addComponent(Camera)yields a perfectly uniform canvas with no error, andLayers.UI_2Disundefined- the bit isLayers.Enum.UI_2D. AirGraphicsCommand[]is plain drawing data with Y-up local coordinates; a command ismoveTo/lineTo/bezierCurveTo/quadraticCurveTo/arc/rect/roundRect/ellipse/circle/close/fill/stroke(src/air/graphics-commands.ts:5-15). Colors must match#rrggbband opacity stays in[0,1], else validation throwsAIR_E_GRAPHICS_COMMANDbefore any replay (:26-31,:32-70).drawAirGraphics(graphics, commands, clear = true)replays only solid nonzero fills on the nativeGraphics; it throws on any gradient, pattern orevenoddfill, pointing you atrasterizeAirGraphics(src/air/graphics-commands.ts:74-88,:76).arc's last boolean keeps native direction (true = counter-clockwise); stroke acceptsbutt|round|squarecaps andmiter|round|beveljoins (docs/manual/graphics-and-dom.md:54).airGraphicsCommandsToSvg(commands, bounds)serializes the same data into<svg>, flipping Y exactly once via thescale(1 -1)group and viewBox (src/air/graphics-commands.ts:156). It preserves original curves and does not reconstruct them from a subdividedGraphics.impl(docs/manual/graphics-and-dom.md:56); SVG supports solid/linear/radial fills plus local patterns with opacity andevenoddholes (:58).- Pattern paint is
{ kind: 'pattern', image, width, height, textureSpace, transform };transformis a direct affinepattern -> chosen space, never pre-inverted or premultiplied by texture size (src/air/graphics-commands.ts:22).textureSpace: 'global'makesairGraphicsCommandsToSvgthrow and requiresrasterizeAirGraphicsto receiveoptions.worldTransform(the local-to-world matrix[a,b,c,d,tx,ty]) (src/air/graphics-commands.ts:106,src/air/graphics-raster.ts:28-30). SVG pattern export needs local coordinates plus an explicitsourceURLthat is adata:imageorhttp(s)value (src/air/graphics-commands.ts:107). rasterizeAirGraphics(commands, { bounds, resolution = 1, worldTransform? })uses Canvas2D and a nativeTexture2D; it is a raster result, not a GPU vector filler.resolutionsets pixels per logical unit and each edge is capped at 8192, elseAIR_E_GRAPHICS_RESOLUTION(src/air/graphics-raster.ts:26-27). The-resolutiondevice transform plus top-firstgetImageDatarows match native Sprite UV, so there is no extra upload flip (:32,:77-80). Callupdate(commands?, options?)explicitly on any command/resolution/bounds/global-pattern-transform change; it is not an automatic per-frame loop (:23,docs/manual/graphics-and-dom.md:82).createAirTextureSprite(parent, texture, { name?, size?, anchor? })borrows theTextureBaseand owns the createdNode/SpriteFrame; default size is the texture size and default anchor[0.5, 0.5], set onUITransformnot the shared texture (src/air/texture-sprite.ts:14-25).refresh()rebuilds the framerect/originalSizeafter the texture resizes;dispose()destroys the owned node/frame but not the borrowed texture (:28-47).attachAirSpriteMask(sprite, maskTexture, options)samples a second texture in normalized sprite UVs and handles trimmed/rotated atlas frames via an inverse affine (src/air/effects/sprite-mask.ts:68-74).channelisalpha|red(default alpha),modeissoft|threshold(default soft;softattenuates output alpha,thresholddiscards fragments atthreshold, default0.1), plusinverted(:10-15,:21-23). It masks ONESprite, never an arbitrary subtree, and refuses aSpritethat already has acustomMaterial(docs/manual/graphics-and-dom.md:103,src/air/effects/sprite-mask.ts:24). Detach the binding before disposing the mask texture (:78-82).createAirUIBlendScope(root, mode = 'normal')is an opt-in controller: it rewrites the private_srcBlendFactor/_dstBlendFactorand pushesnormal|add|multiplythrough aMaterialInstanceoverridePipelineStates, covering nativeUIRendererincludingParticleSystem2D(src/air/ui-blend.ts:5-28). It adds no implicitNode.blendMode. Callupdate()after hierarchy/component/particle/material changes;set(node, null)restores inheritance and explicit child overrides win (:36-53).dispose()and moving a node out of the subtree restore only the factors the controller still owns (:29-35,:54).createAirDOMOverlay({ canvas, host?, className? })owns one independent fixed root and never mutatesgame.container;.attach(element, node, camera, { width?, height?, anchor? })borrows the element and.dispose()restores its original parent, order and inline style (src/air/dom-overlay.ts:24-44,:68-77).update()(from the app's own frame callback, not a second clock) projects the node's world matrix plus the canvas CSS-vs-backing size into one CSSmatrix(); this affine mapping is intended for planar orthographic UI, and perspective or tilted planes cannot be represented accurately (:50-67,docs/manual/graphics-and-dom.md:37).AirUIMeshis a nativeUIRenderercomponent (@requireComponent(UITransform)) that shares Sprite materials, stencil masks and batch buffers.setGeometry({ positions, uvs, indices })requires 3..1024 XY vertices, one[0,1]UV pair per vertex, and 1..1365 triangles (indices.lengthin3..4095, multiple of 3) or it throwsAIR_E_UI_MESH_GEOMETRY(src/air/ui-mesh.ts:21-31,:106).dispose()clears the renderer and releases its frame/texture leases without destroying the node or shared assets (:111-115).- HTML live textures are experimental APIs in the main
cocosair.jsentry (bundlebuild/cocosair.module.js):getAirHTMLSourceSupport(canvas)probes the API, andcreateAirHTMLSource({ canvas, element, texture, ... })throwsAIR_E_HTML_SOURCE_UNSUPPORTEDbefore any DOM/texture mutation when the browser lacks it. The element must be a direct child of the canvas and hold an exclusive lease, and the texture must be a single-mip RGBA8 nativeTexture2Dfor that WebGL2 device (src/air/html-source.ts:39-51,:69-72).