Components

Canvas

The <Canvas> component provides a drawing surface where you can paint graphics using a Canvas-like API. It supports paths, shapes, strokes, fills, and gradients.

Props

PropTypeDefaultDescription
styleStyleDimensions and position of the canvas.
paint*(painter: CanvasPainter) => voidCallback that receives a painter object for drawing.
fixedbooleanRepeat on every page.

Paint API

The painter object supports these methods:

PropTypeDefaultDescription
moveTo(x, y)voidMove the current point without drawing.
lineTo(x, y)voidDraw a line from the current point.
quadraticCurveTo(cpx, cpy, x, y)voidDraw a quadratic Bezier curve.
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)voidDraw a cubic Bezier curve.
arc(x, y, radius, startAngle, endAngle)voidDraw an arc.
rect(x, y, w, h)voidDraw a rectangle path.
circle(x, y, radius)voidDraw a circle path.
ellipse(x, y, rx, ry)voidDraw an ellipse path.
lineWidthnumberStroke width in points.
strokeColorstringStroke color (hex, rgb, name).
fillColorstringFill color (hex, rgb, name).
strokeOpacitynumberStroke opacity (0-1).
fillOpacitynumberFill opacity (0-1).
dash(length: number, options: { space: number }) => voidDash the stroke.
undash() => voidDisable dash mode.
clip() => voidClip the drawing area.
save() => voidSave the current graphics state.
restore() => voidRestore the previously saved state.
linearGradient(x1, y1, x2, y2)GradientCreate a linear gradient.
radialGradient(x1, y1, r1, x2, y2, r2)GradientCreate a radial gradient.
lineCapstringLine cap style ('butt', 'round', 'square').
lineJoinstringLine join style ('miter', 'round', 'bevel').
opacitynumberGlobal opacity for all operations.

Usage

vue
<template>
  <Document>
    <Page size="A4">
      <Canvas
        :style="{ width: 200, height: 200 }"
        :paint="(p) => {
          // Draw a red rectangle
          p.fillColor = '#dc2626'
          p.rect(20, 20, 160, 80)
          p.fill()

          // Draw a circle with stroke
          p.fillColor = '#2563eb'
          p.strokeColor = '#1e3a5f'
          p.lineWidth = 3
          p.circle(100, 140, 40)
          p.fill()
          p.stroke()

          // Draw a line
          p.strokeColor = '#059669'
          p.lineWidth = 2
          p.moveTo(0, 0)
          p.lineTo(200, 200)
          p.stroke()
        }"
      />
    </Page>
  </Document>
</template>

Canvas With Gradient

vue
<template>
  <Document>
    <Page size="A4">
      <Canvas
        :style="{ width: 200, height: 100 }"
        :paint="(p) => {
          const grad = p.linearGradient(0, 0, 200, 0)
          grad.stop(0, '#dc2626')
          grad.stop(0.5, '#f97316')
          grad.stop(1, '#eab308')
          p.fillColor = grad
          p.rect(0, 0, 200, 100)
          p.fill()
        }"
      />
    </Page>
  </Document>
</template>