# nodesnim/nodes/canvas

Dark Mode

Search:
Group by:

Canvas is the root type of all 2D and Control nodes.

Canvas used for drawing primitive geometry.

# Imports

../thirdparty/opengl, ../core/vector2, ../core/color, ../core/anchor, ../core/enums, node

# Types

    DrawCommandType {...}{.size: 1.} = enum
      POINT, LINE, RECT, FILL, CIRCLE

    DrawCommand = object
      x1*, y1*: GLfloat
      color*: ColorRef
      case kind*: DrawCommandType
      of LINE, RECT:
          x2*, y2*: GLfloat

      of CIRCLE:
          points*: seq[GLfloat]

      else:
          nil

    CanvasObj = object of NodeObj
      commands*: seq[DrawCommand]

    CanvasRef = ref CanvasObj

# Procs

    proc Canvas(name: string = "Canvas"): CanvasRef {...}{.raises: [], tags: [].}

  Creates a new Canvas.

  Arguments:

  - `name` is a node name.

  Examples:

      var canvas1 = Canvas("Canvas")

# Methods

    method draw(canvas: CanvasRef; w, h: GLfloat) {...}{.raises: [GLerror], tags: [].}

  This uses in the `window.nim`.

    method duplicate(self: CanvasRef): CanvasRef {...}{.base, raises: [], tags: [].}

  Duplicates Canvas object and create a new Canvas.

    method circle(canvas: CanvasRef; x, y, radius: GLfloat; color: ColorRef;
                 quality: int = 100) {...}{.base, raises: [], tags: [].}

  Draws a circle in the canvas.

  Arguments:

  - `x` - circle center at X axis.
  - `y` - circle center at Y axis.
  - `radius` - circle radius.
  - `color` - Color object.
  - `quality` - circle quality.

    method point(canvas: CanvasRef; x, y: GLfloat; color: ColorRef) {...}{.base, raises: [],
        tags: [].}

  Draws a point in the canvas.

  Arguments:

  - `x` - point position at X axis.
  - `y` - point position at Y axis.
  - `color` - point color.

    method line(canvas: CanvasRef; x1, y1, x2, y2: GLfloat; color: ColorRef) {...}{.base,
        raises: [], tags: [].}

  Draws a line in the canvas.

  Arguments:

  - `x1` - first position at X axis.
  - `y1` - first position at Y axis.
  - `x2` - second position at X axis.
  - `y2` - second position at Y axis.
  - `color` - line color.

    method rect(canvas: CanvasRef; x1, y1, x2, y2: GLfloat; color: ColorRef) {...}{.base,
        raises: [], tags: [].}

  Draws a line in the canvas.

  Arguments:

  - `x1` - first position at X axis.
  - `y1` - first position at Y axis.
  - `x2` - second position at X axis.
  - `y2` - second position at Y axis.
  - `color` - rectangle color.

    method fill(canvas: CanvasRef; color: ColorRef) {...}{.base, raises: [], tags: [].}

  Fills canvas.

    method resize(canvas: CanvasRef; w, h: GLfloat) {...}{.base, raises: [], tags: [].}

  Resizes canvas.

  Arguments:

  - `w` is a new width.
  - `h` is a new height.