Skip to content

Use Container component

Common example:

html
<Container />

Example with x and y:

html
<script>
import { signal } from 'canvasengine'

const x = signal(10)
const y = signal(10)

const click = () => {
    x.update(x => x + 10)
    y.update(y => y + 10)
}
</script>

<Container x y click />

Flex layout

display="flex" or any container layout property such as flexDirection, justifyContent, alignItems, gap, or padding makes the container manage its direct children through Yoga. Children already mounted are enrolled if the container becomes flex reactively. If its last layout property returns to undefined, Yoga is detached again and ordinary PixiJS positioning resumes.

html
<Container
  width={480}
  height={240}
  display="flex"
  flexDirection="row"
  justifyContent="space-evenly"
  alignItems="center"
  padding={[16, 24]}
  gap={12}
>
  <Rect width={80} height={80} color="#38bdf8" />
  <Rect width={80} height={120} color="#8b5cf6" />
</Container>

Two-value spacing arrays use [vertical, horizontal]; four-value arrays use [top, right, bottom, left]. Zero is a valid reactive value.

Use display="none" to remove an object from Yoga and hide its rendered subtree. Switching it back to flex restores it at its declared child order.

Parent-relative layout

CanvasEngine automatically creates a lightweight Yoga containing box when a child uses values that need its direct parent, such as percentage dimensions, right/bottom insets, margins, or flex-item properties. The parent does not need display="flex" for an absolute inset panel to use its numeric dimensions:

html
<Container width={720} height={220}>
  <Container
    positionType="absolute"
    top={22}
    right={28}
    bottom={22}
    left={34}
  />
</Container>

Only children that depend on this containing box are enrolled. Ordinary PixiJS siblings keep their x/y positioning. The automatic box is removed when its last dependent child is removed or stops using parent-relative values.

Full-screen centered GUI

Percentage dimensions follow the canvas and are recalculated after a renderer resize. A column flex container can center a complete GUI group on both axes:

html
<Canvas backgroundColor="#08111f">
  <Container
    width="100%"
    height="100%"
    display="flex"
    flexDirection="column"
    justifyContent="center"
    alignItems="center"
    gap={16}
  >
    <Loading size={40} />
    <Text text="Loading area..." color="white" size={18} />
  </Container>
</Canvas>

Numeric dimensions supplied by signals can start at zero and update after mount; nested flex containers will use the new dimensions for their next layout calculation.

Native PixiJS children

Use pixiChildren when you need to mount PixiJS objects directly inside a CanvasEngine container without wrapping every object in a CanvasEngine component.

html
<script>
import { Container } from 'pixi.js'

const world = new Container()
</script>

<Container pixiChildren={[world]} />

CanvasEngine adds these objects to the PixiJS scene graph when the container is mounted. It does not manage their internal state or lifecycle, so update and destroy them manually when needed:

html
<script>
import { Container as PixiContainer, Graphics } from 'pixi.js'

const world = new PixiContainer()
const brushPreview = new Graphics()

world.addChild(brushPreview)

function updatePreview(point) {
    brushPreview.clear()
    if (!point) return
    brushPreview
        .circle(point.x, point.y, 32)
        .stroke({ width: 2, color: 0xf0dfb9, alpha: 0.85 })
}

function destroyWorld() {
    world.destroy({ children: true })
}
</script>

<Container
    width={960}
    height={640}
    pixiChildren={[world]}
    on-before-destroy={destroyWorld}
/>

Common Properties

PropertyTypeDescription
xnumberX-coordinate position of the display object.
ynumberY-coordinate position of the display object.
widthnumberWidth of the display object.
heightnumberHeight of the display object.
scaleobjectScale of the display object.
anchorobjectAnchor point of the display object.
skewobjectSkew of the display object.
tintnumberTint color of the display object.
rotationnumberRotation of the display object in radians.
anglenumberRotation of the display object in degrees.
zIndexnumberZ-index of the display object.
roundPixelsbooleanWhether to round pixel values.
cursorstringCursor style when hovering over the display object.
visiblebooleanVisibility of the display object.
alphanumberAlpha transparency of the display object.
pivotobjectPivot point of the display object.
filtersarrayFilters applied to the display object.
maskOfElementElement that this display object masks.
blendModestringBlend mode for rendering.
filterAreaobjectFilter area for rendering.
outlineobjectOutline effect following the object's alpha contour.
clipobjectRectangular mask used to keep or hide part of the object.
occlusionobjectLow-alpha redraw of the covered part when this object passes behind obstacles.

Layout Properties

Pour obtenir la documentation complète et détaillée sur toutes les propriétés de mise en page, consultez la documentation officielle de PixiJS Layout.

Sizing and Dimensions

PropertyTypeDescription
widthnumber/stringWidth of the display object. Accepts pixels or percentage (e.g. '50%').
heightnumber/stringHeight of the display object. Accepts pixels or percentage (e.g. '50%').
minWidthnumber/stringMinimum width the object can shrink to.
minHeightnumber/stringMinimum height the object can shrink to.
maxWidthnumber/stringMaximum width the object can expand to.
maxHeightnumber/stringMaximum height the object can expand to.
aspectRationumberRatio between width and height (e.g. 1.5 for 3:2 ratio).

Flex Layout

PropertyTypeDescription
flexDirectionstringDirection of flex items. Values: 'row', 'column', 'row-reverse', 'column-reverse'.
flexWrapstringWhether items wrap. Values: 'wrap', 'nowrap', 'wrap-reverse'.
justifyContentstringMain-axis alignment: 'flex-start', 'flex-end', 'center', 'space-between', 'space-around', 'space-evenly'.
alignItemsstringCross-axis alignment: 'flex-start', 'flex-end', 'center', 'stretch', 'baseline'.
alignContentstringMulti-line alignment, including 'stretch', 'space-between', 'space-around', and 'space-evenly'.
alignSelfstringItem override: 'auto', 'flex-start', 'flex-end', 'center', 'stretch', or 'baseline'.
flexGrownumberGrow factor of item relative to other items.
flexShrinknumberShrink factor of item relative to other items.
flexBasisnumber/stringInitial size of item before flex growing/shrinking.
gapnumber/stringGap between rows and columns, in pixels or percent.
rowGapnumberGap between rows.
columnGapnumberGap between columns.

Positioning

PropertyTypeDescription
positionTypestringType of positioning. Values: 'relative', 'absolute', 'static'.
topnumber/stringDistance from the top edge.
rightnumber/stringDistance from the right edge.
bottomnumber/stringDistance from the bottom edge.
leftnumber/stringDistance from the left edge.

Spacing, Margins and Borders

PropertyTypeDescription
marginnumber/arraySpace outside border box. Can be single value or array for different sides.
paddingnumber/arraySpace inside border box. Can be single value or array for different sides.
bordernumber/array/objectNumber/array: Yoga border width. Object on Graphics primitives: visual Pixi stroke.

Spacing arrays follow CSS shorthand ordering: [vertical, horizontal] or [top, right, bottom, left]. A visual Pixi border object is never included in Yoga sizing. display="none" removes the item from layout and hides its rendered subtree.

Object Fitting and Alignment

PropertyTypeDescription
objectFitstringHow object is resized to fit layout box. Values: 'contain', 'cover', 'fill', 'none', 'scale-down'.
objectPositionstringAnchor point of object inside layout box. E.g. 'center', 'top left'.
transformOriginstringPivot point for rotation and scaling of layout box.

Shadow

PropertyTypeDescription
blurnumberBlur strength.
colornumberColor of the shadow.
offsetobjectOffset of the shadow.
qualitynumberQuality of the shadow.

Hook before destroy

html
<script>
  import {
    signal,
    animatedSignal,
    effect,
    animatedSequence,
  } from "canvasengine";
  import MyViewport from "./viewport.ce";
  
  let bool = signal(true)
  const opacity = animatedSignal(1, { duration: 500 });

  const click = async () => {
    bool.set(!bool())
  }

  const beforeDestroy = async () => {
    await animatedSequence([
      () => opacity.set(0),
    ])
    console.log("before destroy")
  }
</script>


<Canvas antialias={true}>
     <Container onBeforeDestroy={beforeDestroy}>
        @if (bool) {
            <Rect width={300} height={300} color="red" alpha={opacity} click />
        }
    </Container>
</Canvas>