Class Canvas

Class representing a canvas A canvas is a simply a collection of pixels/colors. It may be more accurate to think of this canvas class as a container for a 2D array of colors, where each color represents a pixel that will display in a image.

Remarks

It is instantiated with width and height and an optional default color for every pixel. Leaving the initial pixel value unset, will fall back on a default of Color(0, 0, 0)/black for every pixel.

Example

We can instantiate a 2 x 2 canvas like so:

const canvas = new Canvas(2, 2)
canvas.toArray()

///output
[
[[0,0,0], [0,0,0]],
[[0,0,0], [0,0,0]]
]

When you look at it in this array form it actually looks more like a 3D array with the dimension 2 x 2 x 3. The third Dimension '3' is actually for the R, G & B channel values

Hierarchy

  • Canvas

Implements

Constructors

Properties

Methods

Constructors

  • Parameters

    • width: number
    • height: number
    • initalPixelValue: {
          b: number;
          g: number;
          r: number;
      } = ...
      • b: number
      • g: number
      • r: number

    Returns Canvas

Properties

height: number
state: Color[][]
width: number

Methods

  • Reads the color value at a given coordinate value

    Returns

    A Color object

    Parameters

    • x: number

      X coordinate value to read from

    • y: number

      Y coordinate value to read from

    Returns Color

  • Th toArray Method converts the Canvas to a 3D array with dimension width x height x 3

    Returns

    A 3-dimensional number array.

    Returns number[][][]

  • Converts the canvas to a string formatted according to the PPM Netpbm color image format formatted

    Remarks

    The flavour of PPM used here is plain P3 and not the P6 format.You can read more about the PPM format here

    Returns

    A string formatted in the PPM P3 flavour that can be directly output to a file.

    Returns string

  • Writes a color value to a pixel at the given x, y coordinate

    Parameters

    • x: number

      X coordinate value

    • y: number

      X coordinate value

    • color: Color

      Color object to write to the Canvas pixel

    Returns void

Generated using TypeDoc