Class Canvas2DMX

java.lang.Object
com.studiojordanshaw.canvas2dmx.Canvas2DMX
All Implemented Interfaces:
processing.core.PConstants

public class Canvas2DMX extends Object implements processing.core.PConstants
Canvas2DMX

Samples colors from locations on the sketch canvas and sends them to DMX fixtures using a configurable channel pattern (e.g., "rgb", "drgb", "rgbw"). Supports response curves, color temperature, mapping helpers, and a "no-hardware" path so examples run in the PDE.

Designed for ENTTEC USB Pro and compatible devices via DMXControl.

Original inspiration: Open Pixel Control (Micah Elizabeth Scott, 2013).

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
     
    static interface 
    Minimal DMX sender contract: deliver a (channel, value) pair.
    static class 
    Configuration for polygon LED fill orientation.
    static class 
    Configuration for row-based polygon LED fill using fixed LED counts per row.
  • Field Summary

    Fields inherited from interface processing.core.PConstants

    ADD, ALPHA, ALT, AMBIENT, ARC, ARGB, ARROW, BACKSPACE, BASELINE, BEVEL, BEZIER_VERTEX, BLEND, BLUR, BOTTOM, BOX, BREAK, BURN, CENTER, CHATTER, CHORD, CLAMP, CLOSE, CODED, COMPLAINT, CONTROL, CORNER, CORNERS, CROSS, CURVE_VERTEX, CUSTOM, DARKEST, DEG_TO_RAD, DELETE, DIAMETER, DIFFERENCE, DILATE, DIRECTIONAL, DISABLE_ASYNC_SAVEFRAME, DISABLE_BUFFER_READING, DISABLE_DEPTH_MASK, DISABLE_DEPTH_SORT, DISABLE_DEPTH_TEST, DISABLE_KEY_REPEAT, DISABLE_NATIVE_FONTS, DISABLE_OPENGL_ERRORS, DISABLE_OPTIMIZED_STROKE, DISABLE_STROKE_PERSPECTIVE, DISABLE_STROKE_PURE, DISABLE_TEXTURE_MIPMAPS, DODGE, DOWN, DXF, ELLIPSE, ENABLE_ASYNC_SAVEFRAME, ENABLE_BUFFER_READING, ENABLE_DEPTH_MASK, ENABLE_DEPTH_SORT, ENABLE_DEPTH_TEST, ENABLE_KEY_REPEAT, ENABLE_NATIVE_FONTS, ENABLE_OPENGL_ERRORS, ENABLE_OPTIMIZED_STROKE, ENABLE_STROKE_PERSPECTIVE, ENABLE_STROKE_PURE, ENABLE_TEXTURE_MIPMAPS, ENTER, EPSILON, ERODE, ESC, EXCLUSION, FX2D, GIF, GRAY, GROUP, HALF_PI, HAND, HARD_LIGHT, HINT_COUNT, HSB, IMAGE, INVERT, JAVA2D, JPEG, LANDSCAPE, LEFT, LIGHTEST, LINE, LINE_LOOP, LINE_STRIP, LINES, LINUX, MACOS, MACOSX, MAX_FLOAT, MAX_INT, MIN_FLOAT, MIN_INT, MITER, MODEL, MODELVIEW, MOVE, MULTIPLY, NORMAL, OPAQUE, OPEN, OPENGL, ORTHOGRAPHIC, OTHER, OVERLAY, P2D, P3D, PATH, PDF, PERSPECTIVE, PI, PIE, platformNames, POINT, POINTS, POLYGON, PORTRAIT, POSTERIZE, PROBLEM, PROJECT, PROJECTION, QUAD, QUAD_BEZIER_VERTEX, QUAD_STRIP, QUADRATIC_VERTEX, QUADS, QUARTER_PI, RAD_TO_DEG, RADIUS, RECT, REPEAT, REPLACE, RETURN, RGB, RIGHT, ROUND, SCREEN, SHAPE, SHIFT, SOFT_LIGHT, SPAN, SPHERE, SPOT, SQUARE, SUBTRACT, SVG, TAB, TARGA, TAU, TEXT, THIRD_PI, THRESHOLD, TIFF, TOP, TRIANGLE, TRIANGLE_FAN, TRIANGLE_STRIP, TRIANGLES, TWO_PI, UP, VERTEX, WAIT, WHITESPACE, WINDOWS, X, Y, Z
  • Constructor Summary

    Constructors
    Constructor
    Description
    Canvas2DMX(processing.core.PApplet parent)
    Construct the library.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    applyResponse(int argb)
    Apply response/temperature to an ARGB color.
    int[]
    buildDmxFrame(int frameLength)
    Build a complete DMX frame (1-based addressing) of the requested length.
    int[]
    buildDmxFrame(int[] pixelArray, int frameLength)
    Build a DMX frame (1-based addressing) from a specific pixel buffer.
    void
    Clear all LED mappings.
    void
    Processing lifecycle hook for cleanup.
     
    int[]
    Convenience: samples from the parent sketch's current frame.
    int[]
    getLedColors(int[] pixelArray)
    Sample mapped LEDs from a provided pixel buffer (ARGB).
    int
     
    processing.core.PVector
    getLedPosition(int index)
    Return the LED's mapped position in canvas coordinates.
    processing.core.PVector[]
    Return an array of LED positions in canvas coordinates.
    int
     
    int
     
    boolean
     
    void
    loadSettings(String filename)
    Load response/temperature/customCurve from a text file in the sketch folder.
    void
    mapLedGrid(int index, int stripLength, int numStrips, float x, float y, float ledSpacing, float stripSpacing, float angle, boolean zigzag, boolean flip)
    Map a grid of LED strips with optional zigzag and flip.
    int
    mapLedPolygon(int startIndex, float[][] vertices, Canvas2DMX.PolygonFillConfig config)
    Fill an arbitrary polygon with LEDs using scanline algorithm.
    int
    mapLedPolygon(int startIndex, Object[] vertices, Canvas2DMX.PolygonFillConfig config)
    Fill an arbitrary polygon with LEDs using scanline algorithm.
    int
    mapLedPolygon(int startIndex, processing.core.PVector[] vertices, Canvas2DMX.PolygonFillConfig config)
    Fill an arbitrary polygon with LEDs using scanline algorithm.
    void
    mapLedRing(int index, int count, float x, float y, float radius, float angle)
    Map a ring of LEDs (clockwise from angle).
    int
    mapLedRowLayout(int startIndex, float[][] vertices, Canvas2DMX.RowLayoutConfig config)
    Fill an arbitrary polygon with LEDs using fixed counts per row.
    int
    mapLedRowLayout(int startIndex, Object[] vertices, Canvas2DMX.RowLayoutConfig config)
    Fill an arbitrary polygon with LEDs using fixed counts per row.
    int
    mapLedRowLayout(int startIndex, processing.core.PVector[] vertices, Canvas2DMX.RowLayoutConfig config)
    Fill an arbitrary polygon with LEDs using fixed counts per row.
    void
    mapLedStrip(int index, int count, float x, float y, float spacing, float angle, boolean reversed)
    Map a linear strip of LEDs.
    void
    mapSquareCorners(int index, float x, float y, float size, float rotationDegrees)
    Map the 4 rotated corners of a square centered at (x,y).
    boolean
    pointInPolygon(float px, float py, float[] xVerts, float[] yVerts)
    Check if a point is inside a polygon (for validation/debugging).
    void
    saveSettings(String filename)
    Save response/temperature/customCurve to a text file in the sketch folder.
    void
    sendToDmx(int[] pixelArray, Canvas2DMX.DmxSender sender)
    Iterate over the current LED mapping and emit DMX channel/value pairs using colors sampled from the provided pixel array.
    void
    Iterate over the current LED mapping and emit DMX channel/value pairs using the current channelPattern and startAt.
    void
    setCanvasSize(int width, int height)
    Set custom canvas dimensions for LED mapping (for off-screen buffers).
    void
    Set the DMX channel pattern, e.g., "rgb", "drgb", "rgbw".
    void
    setCustomCurve(float[] curve)
    Provide a custom [0..1] → [0..1] response curve (disables simple exponent).
    void
    setDefaultValue(char channel, int value)
    Assign a default value (0–255) for a non-RGB placeholder char in the pattern.
    void
    setLed(int index, int x, int y)
    Map one LED to (x,y) in canvas coordinates.
    void
    setResponse(float response)
    Set simple response exponent (disables custom curve).
    int
    setRowLayout(int startIndex, float[][] vertices, Canvas2DMX.RowLayoutConfig config)
    Convenience alias for projects that conceptually refer to this as "setRowLayout".
    int
    setRowLayout(int startIndex, processing.core.PVector[] vertices, Canvas2DMX.RowLayoutConfig config)
    Convenience alias for projects that conceptually refer to this as "setRowLayout".
    void
    setShowLocations(boolean enabled)
    Enable or disable drawing LED markers.
    void
    setStartAt(int startAt)
    Set the starting DMX channel (1-based).
    void
    setTemperature(float temperature)
    Set color temperature in [-1, 1] (negative = warm, positive = cool).
    void
    Draw small markers and indices at mapped LED locations.
    void
    visualize(int[] processedColors)
    Quick swatch renderer for up to 20 LEDs at the bottom of the screen.
    void
    One-time helper: warn if pixelDensity != 1 (sampling can misalign).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Canvas2DMX

      public Canvas2DMX(processing.core.PApplet parent)
      Construct the library. Call this once in your sketch's setup():
       Canvas2DMX c2d = new Canvas2DMX(this);
       
      Parameters:
      parent - the parent PApplet
  • Method Details

    • dispose

      public void dispose()
      Processing lifecycle hook for cleanup.
    • setShowLocations

      public void setShowLocations(boolean enabled)
      Enable or disable drawing LED markers.
    • isShowLocationsEnabled

      public boolean isShowLocationsEnabled()
      Returns:
      whether LED markers are drawn.
    • setResponse

      public void setResponse(float response)
      Set simple response exponent (disables custom curve).
    • setCanvasSize

      public void setCanvasSize(int width, int height)
      Set custom canvas dimensions for LED mapping (for off-screen buffers).
    • setCustomCurve

      public void setCustomCurve(float[] curve)
      Provide a custom [0..1] → [0..1] response curve (disables simple exponent).
    • setTemperature

      public void setTemperature(float temperature)
      Set color temperature in [-1, 1] (negative = warm, positive = cool).
    • setChannelPattern

      public void setChannelPattern(String pattern)
      Set the DMX channel pattern, e.g., "rgb", "drgb", "rgbw".
    • setStartAt

      public void setStartAt(int startAt)
      Set the starting DMX channel (1-based).
    • setDefaultValue

      public void setDefaultValue(char channel, int value)
      Assign a default value (0–255) for a non-RGB placeholder char in the pattern.
    • clearLeds

      public void clearLeds()
      Clear all LED mappings. Call before remapping to avoid stale markers.
    • setLed

      public void setLed(int index, int x, int y)
      Map one LED to (x,y) in canvas coordinates.
    • mapLedStrip

      public void mapLedStrip(int index, int count, float x, float y, float spacing, float angle, boolean reversed)
      Map a linear strip of LEDs.
    • mapLedRing

      public void mapLedRing(int index, int count, float x, float y, float radius, float angle)
      Map a ring of LEDs (clockwise from angle).
    • mapLedGrid

      public void mapLedGrid(int index, int stripLength, int numStrips, float x, float y, float ledSpacing, float stripSpacing, float angle, boolean zigzag, boolean flip)
      Map a grid of LED strips with optional zigzag and flip.
    • mapSquareCorners

      public void mapSquareCorners(int index, float x, float y, float size, float rotationDegrees)
      Map the 4 rotated corners of a square centered at (x,y).
    • mapLedPolygon

      public int mapLedPolygon(int startIndex, float[][] vertices, Canvas2DMX.PolygonFillConfig config)
      Fill an arbitrary polygon with LEDs using scanline algorithm.
      Parameters:
      startIndex - First LED index to use
      vertices - Array of polygon vertices as float[][] where each element is {x, y}
      config - Fill configuration (orientation, spacing, etc.)
      Returns:
      Number of LEDs mapped (next available index = startIndex + return value)
    • mapLedPolygon

      public int mapLedPolygon(int startIndex, processing.core.PVector[] vertices, Canvas2DMX.PolygonFillConfig config)
      Fill an arbitrary polygon with LEDs using scanline algorithm. Overload accepting PVector array.
    • mapLedPolygon

      public int mapLedPolygon(int startIndex, Object[] vertices, Canvas2DMX.PolygonFillConfig config)
      Fill an arbitrary polygon with LEDs using scanline algorithm. Overload accepting PVector array (Processing-friendly).
      Parameters:
      startIndex - First LED index to use
      vertices - Array of PVector vertices
      config - Fill configuration (orientation, spacing, etc.)
      Returns:
      Number of LEDs mapped (next available index = startIndex + return value)
    • mapLedRowLayout

      public int mapLedRowLayout(int startIndex, float[][] vertices, Canvas2DMX.RowLayoutConfig config)
      Fill an arbitrary polygon with LEDs using fixed counts per row.
      Parameters:
      startIndex - First LED index to use
      vertices - Array of polygon vertices as float[][] where each element is {x, y}
      config - Row layout configuration (counts, direction, etc.)
      Returns:
      Number of LEDs mapped (next available index = startIndex + return value)
    • mapLedRowLayout

      public int mapLedRowLayout(int startIndex, processing.core.PVector[] vertices, Canvas2DMX.RowLayoutConfig config)
      Fill an arbitrary polygon with LEDs using fixed counts per row. Overload accepting PVector array.
    • mapLedRowLayout

      public int mapLedRowLayout(int startIndex, Object[] vertices, Canvas2DMX.RowLayoutConfig config)
      Fill an arbitrary polygon with LEDs using fixed counts per row. Overload accepting PVector array (Processing-friendly).
    • setRowLayout

      public int setRowLayout(int startIndex, float[][] vertices, Canvas2DMX.RowLayoutConfig config)
      Convenience alias for projects that conceptually refer to this as "setRowLayout".
    • setRowLayout

      public int setRowLayout(int startIndex, processing.core.PVector[] vertices, Canvas2DMX.RowLayoutConfig config)
      Convenience alias for projects that conceptually refer to this as "setRowLayout".
    • pointInPolygon

      public boolean pointInPolygon(float px, float py, float[] xVerts, float[] yVerts)
      Check if a point is inside a polygon (for validation/debugging).
    • applyResponse

      public int applyResponse(int argb)
      Apply response/temperature to an ARGB color.
      Parameters:
      argb - ARGB color from the canvas
      Returns:
      processed ARGB color
    • getLedColors

      public int[] getLedColors(int[] pixelArray)
      Sample mapped LEDs from a provided pixel buffer (ARGB).
      Parameters:
      pixelArray - parent.pixels or another ARGB buffer of size width*height
      Returns:
      processed colors, one per mapped LED (may be length 0)
    • getLedColors

      public int[] getLedColors()
      Convenience: samples from the parent sketch's current frame. Calls parent.loadPixels() internally.
      Returns:
      processed colors, one per mapped LED
    • showLedLocations

      public void showLedLocations()
      Draw small markers and indices at mapped LED locations. Call after drawing your scene.
    • visualize

      public void visualize(int[] processedColors)
      Quick swatch renderer for up to 20 LEDs at the bottom of the screen.
    • sendToDmx

      public void sendToDmx(Canvas2DMX.DmxSender sender)
      Iterate over the current LED mapping and emit DMX channel/value pairs using the current channelPattern and startAt. Agnostic to any backend.

      NOTE — frame tearing with threaded DMX libraries: some backends (e.g. dmxP512) send DMX on an independent timer thread. If that timer fires while this method is mid-loop, the first LEDs will go out in one DMX frame and the rest in the next, producing a visible rolling/cascade update on the strip.

      To avoid this, use buildDmxFrame(int) to pre-compute the complete frame array, then write it to your backend in one tight loop:

         int[] frame = c2d.buildDmxFrame(512);
         for (int i = 0; i < frame.length; i++) dmxPro.set(i + DMX_OFFSET, frame[i]);
       
      This minimises the window in which the timer thread can interrupt.
    • sendToDmx

      public void sendToDmx(int[] pixelArray, Canvas2DMX.DmxSender sender)
      Iterate over the current LED mapping and emit DMX channel/value pairs using colors sampled from the provided pixel array.
    • buildDmxFrame

      public int[] buildDmxFrame(int frameLength)
      Build a complete DMX frame (1-based addressing) of the requested length. frame[0] corresponds to DMX channel 1. Values outside the active LED range are 0.

      Prefer this over sendToDmx(DmxSender) when using a threaded DMX backend such as dmxP512. Computing the frame first and then writing it in one tight loop reduces the chance of the backend's send timer firing mid-update (which causes a visible rolling/cascade effect on LED strips).

      Parameters:
      frameLength - number of channels to produce (e.g., 512 for a full universe)
      Returns:
      int[] of length frameLength with values 0..255
    • buildDmxFrame

      public int[] buildDmxFrame(int[] pixelArray, int frameLength)
      Build a DMX frame (1-based addressing) from a specific pixel buffer.
    • saveSettings

      public void saveSettings(String filename)
      Save response/temperature/customCurve to a text file in the sketch folder.
    • loadSettings

      public void loadSettings(String filename)
      Load response/temperature/customCurve from a text file in the sketch folder.
    • getMappedLedCount

      public int getMappedLedCount()
    • getLedPixelLocation

      public int getLedPixelLocation(int i)
    • getLedPosition

      public processing.core.PVector getLedPosition(int index)
      Return the LED's mapped position in canvas coordinates. Returns null if the index is out of range or unmapped.
    • getLedPositions

      public processing.core.PVector[] getLedPositions()
      Return an array of LED positions in canvas coordinates. Unmapped LEDs are returned as null entries.
    • getStartAt

      public int getStartAt()
    • getChannelPattern

      public String getChannelPattern()
    • warnIfNonStandardPixelDensityOnce

      public void warnIfNonStandardPixelDensityOnce()
      One-time helper: warn if pixelDensity != 1 (sampling can misalign).