Options
All
  • Public
  • Public/Protected
  • All
Menu

Canvas is a sophisticated palette of drawing and analysis capabilities to let you both construct 2D images using vector and bitmap graphics but also to use image processing to retrieve information. Canvas is an RGBA pixel buffer you can load images, draw shapes, perform per pixel and per kernel operations. This can be a buffer you create or one supplied as part of a video stream or from an external library.

To use a canvas component, start by adding one to a page. Then you need to set its properties in the Editor. the Canvas Component can be passed as a parameter to JavaScript actions. Code within an action can set properties and call methods on the Canvas Component. You can choose to either have an image as your background or generate a color for the background and this will set the size of your Canvas Component. The size of your canvas will either be the size of the image selected or the width/height properties depending on the options you choose. In either case the size of the canvas background can affect later functions applied to your Canvas Component. In the below example the size and position of the square can be affected by the size of the Canvas Component. If your image or generated background has a width of 50 pixels but your Canvas Component has a width of 200 pixels then the canvas space and background is up-scaled to match. A background of 50 pixels in a Canvas Component taking up 200 pixels on screen would mean the canvas space is being up-scaled by a factor of x4. This means that all the values of the square you draw will be magnified by a scale of x4 as well. This will make your square appear 4 times as large. The starting X position for drawing is also magnified. A value of 100 would end up as 400. This would appear well passed the end of the 200 width Canvas Component and may not be able to be seen. In the other direction if you choose a very large image or set the generated size very large then your square will appear very small as everything is down-scaled. For these reasons it is recommended when following the example code in this help document that you use a canvas size of about 500x500 pixels. The "stretch" resize property option can also affect the display of the square. If the canvas is stretched the square will strecth also and become a rectangle. For other components you often want to deal with the width and height of the component. However most of the functions here don't care about the component size, they only care about the canvas size which can be gotten from the properties pixelWidth and pixelHeight.

var uComps = require('u-components');
var uActions = require('u-actions');
var Action = uActions.Action;
new Action('Canvas Demo', canvasDemo)
.addParameter('Canvas', uActions.ParamType.CANVAS)
.register();
function canvasDemo(canvas) {
var ctx = uComps.cast.toCanvas(canvas);
ctx.beginPath();
ctx.fillStyle = '#9EFFF3'
ctx.brushMode = uComps.BrushMode.UNIFORM;
ctx.fillRect(100,100,200,200);
}

Hierarchy

Index

Constructors

Properties

Methods

Constructors

Properties

alpha: number

The alpha transparency of the Component. Range from 0 to 100.

angle: number
brushAngle: number

Sets the brush angle, in degrees. Useful for gradients

brushBlendMode: BlendMode

Sets the brush mode for operations like stroke and fill The brush blendmodes are uComps.BlendMode: 0 - NO_BLEND, 1 - SOURCE, 2 - DESTINATION, 3 - SRC_OVER, 4 - DST_OVER, 5 - SRC_IN, 6 - DST_IN, 7 - SRC_OUT, 8 - DST_OUT, 9 - SRC_ATOP, 10 - DST_ATOP, 11 - XOR, 12 - PLUS, 13 - MINUS, 14 - MULTIPLY, 15 - SCREEN, 16 - OVERLAY, 17 - DARKEN, 18 - LIGHTEN, 19 - DODGE, 20 - BURN, 21 - HARD_LIGHT, 22 - SOFT_LIGHT, 23 - DIFFERENCE, 24 - EXCLUSION, 25 - CONTRAST, 26 - INVERT, 27 - INVERT_RGB

ctx.brushBlendMode = uComps.BlendMode.MULTIPLY
brushMode: BrushMode

The brush modes are uComps.BrushMode: 0 - UNIFORM, 1 - IMAGE_CLIP, 2 - IMAGE_REPEAT, 3 - LINEAR_GRADIENT, 4 - RADIAL_GRADIENT, 5 - CONIC_GRADIENT, 6 - DIAMOND_GRADIENT

ctx.brushMode = uComps.BrushMode.CONIC_GRADIENT;
brushPointSample: number

Sets the brush to pointsample - this is used for sampling barcodes and other hard edged images. Default is 0. Set to 1 for barcodes and other hard edged images

brushSource: string

Sets the brush image source

clippingType: ClippingType

Clip the components contents using one of the options in ClippingType.

components: ComponentList

Access this Component's sub-components.

filename: string

File from images directory, to load into the canvas component. Canvas has been set up to primarily support SVG files, but other image files can also be used.

fillColor: string

Background color of the canvas

ctx.fillColor = '#FF0000' 
fillStyle: string
font: string

Combined size and font family,

ctx.font = '20px Vera' 
fontFilename: string
fontHeight: string
formEnabled: boolean

Whether or not is component is enabled for the parent form. Only relevant when inside a Form Component.

formFieldName: string

Label used to identify the field this Component belongs to in a form. Only relevant when inside a Form Component.

height: number

The height of the Component.

innerMiterLimit: number

Sets the brush inner miter limit for stroke operations

lineCap: LineCap

Style of the end of a line. Values are uComps.LineCap: 0 - BUTT, 1 - SQUARE, 2 - ROUND see also lineTo

ctx.lineCap = uComps.LineCap.ROUND;
lineInnerJoin: LineInnerJoin

Style for inner edge where lines meet. Values are uComps.LineInnerJoin: 0 - INNER_BEVEL, 1 - INNER_MITER, 2 - INNER_JAG, 3 - INNER_ROUND

ctx.lineInnerJoin = uComps.LineInnerJoin.INNER_JAG;
lineJoin: LineJoin

Style for outer edge where lines meet. Values are uComps.LineJoin: 0 - MITER, 1 - MITER_REVERT, 2 - ROUND, 3 - BEVEL, 4 - MITER_ROUND

ctx.lineJoin = uComps.LineJoin.BEVEL;
lineWidth: number

Set or return the current line width, in pixels

Shape component to be used as a mask. Can be either a Rectangle or Circle Component.

miterLimit: number

Sets the distance between the inner and outer corners when 2 lines meet. The property works only if lineJoin is 0 - MiterJoin.

name: string

Name of the component.

nativeHeight: number
nativeWidth: number
originX: number
originY: number
parent: Component

Access this Component's parent component.

passedFormValidation: boolean

Whether or not the Component has passed form validation. Only relevant when inside a Form Component.

pixelDepth: number

For accessing raw pixel data, the number of bytes per pixel. This correlates to the pixelFormat.

pixelFormat: PixelFormat

For accessing raw pixel data, the format of the data for each pixel

pixelHeight: number
pixelStride: number

For accessing raw pixel data, the number of bytes per row of pixels. May be greater than pixelWidth * pixelDepth if padding is in use.

pixelWidth: number
resizeMode: number

Sets the canvas display as stretch (0 - default) or fit (1)

strokeStyle: string

Brush colour

ctx.strokeStyle = '#FF0000' 
type: string

Type of the component.

uniqueId: string

Unique ID of the component.

visible: boolean

Whether or not the Component is visible.

width: number

The width of the Component.

x: number

The x position of the Component relative to its parent.

y: number

The y position of the Component relative to its parent.

Methods

  • adaptiveThreshold(strength: number, radius: number, blend: number): void
  • Thresholds with variable strength given a radius of local contrast. The blend 0-1 is how much of the image is preserved vs thresholded. This applies a 0-1 threshold with an adaptive radius in pixels. The blend 0-1 is how much of the image is preserved vs thresholded. Make sure you have set the image property of the canvas with something that has variation in color tones. This tries to make all the pixels either white or black. If your Canvas Component's selected image is all one flat color then it will either turn all white or all black. You need an image with variation for this function to work.

    new Action('Canvas adaptiveThreshold', canvasAdaptiveThreshold)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function canvasAdaptiveThreshold(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.adaptiveThreshold(0.2, 50, 0.5);
    }

    Parameters

    • strength: number

      -1 to 1 how strong the threshold is towards binary

    • radius: number

      1 to 1000 how wide the radius to check for local contrast

    • blend: number

      0 to 1 how much of the image is preserved vs thresholded (0.5 is a common value)

    Returns void

  • addDash(onLength: number, offLength: number): void
  • Adds a dash with on and off lengths, which can be chained together to create patterns for stroke operations like strokeText and stroke

    new Action('Canvas addDash', canvasAddDash)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function canvasAddDash(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath()
    ctx.lineWidth = 4;
    ctx.rect(100,200,200,300);
    ctx.strokeStyle = '#000000'
    ctx.addDash(5, 15);
    ctx.stroke()
    }

    Parameters

    • onLength: number

      length of dash in pixels

    • offLength: number

      length of the gap in pixels

    Returns void

  • arc(x: number, y: number, radius: number, startAngle: number, endAngle: number): void
  • Draws a part, or full ellipse.

    new Action('Canvas arc', canvasArc)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function canvasArc(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.arc(200, 200, 100, 0, 3.14);
    ctx.strokeStyle = '#000000'
    ctx.stroke();
    }

    Parameters

    • x: number

      center of the arc

    • y: number

      center of the arc

    • radius: number

      curvature of the arc

    • startAngle: number

      start angle in radians

    • endAngle: number

      end angle in radians

    Returns void

  • autoBrightnessContrast(): void
  • Automatically calculates and applies the maximum brightness and contrast.

    new Action('Canvas autoBrightnessContrast', autoBrightnessContrast)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function autoBrightnessContrast(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.autoBrightnessContrast();
    }

    Returns void

  • autoColor(): void
  • Automatically calculates and applies strong color saturation/balance

    new Action('Canvas autoColor', autoColor)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function autoColor(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.autoColor();
    }

    Returns void

  • beginPath(): void
  • Clears out the path buffer ready to add new commands. This does not have any immediate visual affect on its own. It is just used to prepare before other instructions. See stroke for example.

    Returns void

  • bezier4CurveTo(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, x4: number, y4: number): void
  • Adds a cubic Bezier curve (two control points) to the current path. Drawing will start from where ever you left off or (0,0) if clear so you may need to first move to your start point.

    new Action('Canvas bezier4CurveTo', bezier4CurveTo)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function bezier4CurveTo(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.moveTo(150, 150);
    ctx.bezier4CurveTo(150, 150, 175, 200, 180, 250, 140, 300);
    ctx.strokeStyle = '#000000'
    ctx.stroke();
    }

    Parameters

    • x1: number

      start point

    • y1: number

      start point

    • x2: number

      control point 1

    • y2: number

      control point 1

    • x3: number

      control point 2

    • y3: number

      control point 2

    • x4: number

      end point

    • y4: number

      end point

    Returns void

  • bezierCurveTo(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number): void
  • Adds a parabolic Bezier curve (one control point) to the current path

    new Action('Canvas bezierCurveTo', bezierCurveTo)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function bezierCurveTo(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.moveTo(150, 150);
    ctx.bezierCurveTo(150, 150, 175, 200, 150, 250);
    ctx.strokeStyle = '#000000'
    ctx.stroke();
    }

    Parameters

    • x1: number

      start point

    • y1: number

      start point

    • x2: number

      control point

    • y2: number

      control point

    • x3: number

      end point

    • y3: number

      end point

    Returns void

  • boxBlur(radius: number): void
  • Blurs the canvas

    new Action('Canvas boxBlur', boxBlur)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function boxBlur(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.boxBlur(30);
    }

    Parameters

    • radius: number

      radius of the blur in pixels

    Returns void

  • boxBlurRegion(x: number, y: number, width: number, height: number, radius: number): void
  • Blurs a region on the canvas

    new Action('Canvas boxBlurRegion', boxBlurRegion)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function boxBlurRegion(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.boxBlurRegion(100, 100, 100, 100, 50);
    }

    Parameters

    • x: number
    • y: number

      top left corner of the region

    • width: number

      width of the region

    • height: number

      height of the region

    • radius: number

      radius of the blur in pixels

    Returns void

  • brushGradientEnd(color: string): void
  • This pairs with brushGradientStart. See the example for brushGradientStart

    Parameters

    • color: string

    Returns void

  • brushGradientStart(color: string): void
  • Sets the brush start gradient color for operations like stroke and fill

    Parameters

    • color: string

    Returns void

  • brushPosition(x: number, y: number): void
  • Sets the brush position for image mode operations. Useful for gradients. See the example for brushGradientStart

    Parameters

    • x: number

      the position

    • y: number

      the position

    Returns void

  • brushScale(scaleX: number, scaleY: number): void
  • Sets the brush scale. This does not do anything on its own but is a setup to change other operations. Useful for gradients. See the example for brushGradientStart This can be used to make a gradient smaller or larger to better fit your shape.

    Parameters

    • scaleX: number

      the image scale x

    • scaleY: number

      the image scale y

    Returns void

  • brushSourceBottomRight(x: number, y: number): void
  • Sets the brush bottom right clip for image mode operations. See example in (@link setBrushSource)

    Parameters

    • x: number

      the image bottom right clip position

    • y: number

      the image bottom right clip position

    Returns void

  • brushSourceTopLeft(x: number, y: number): void
  • Sets the brush top left clip for image mode operations. See example in (@link setBrushSource) Setting brushMode to 2 allows it to actually use the image. This is needed.

    Parameters

    • x: number

      the image top left clip position

    • y: number

      the image top left clip position

    Returns void

  • chromaKey(hue: number, hueMask: number, luminanceMask: number, maskShadow: number, maskLight: number): void
  • Removes (known as a key) a specific color (or chroma). This function uses the HSV transform to calculate the hue (tint), saturation (amount of tint) and value (brightness) - this allows us to control how much of the highlights and shadows are preserved when a colored background is removed.

    new Action('Canvas chromaKey', chromaKey)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function chromaKey(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.chromaKey(90, 0.06, 1.5, 2.6, 0.9);
    }

    Parameters

    • hue: number

      the color converted to hue (hsv) from 0 to 360, 0 red, 90 green, 180 blue

    • hueMask: number

      the hue tollerance to generate the mask, 0 to 1, 0.1 or 0.06 are good values

    • luminanceMask: number

      the amount of luminance tollerance, 0 to 5, 1.5 is a good value

    • maskShadow: number

      the amount of shadow preserved, 0 to 5, 2.6 is a good value, smaller means less shadow is added

    • maskLight: number

      the amount of saturated color preserved, 0 to 5, 0.9 is a good value, more means more highlights may be picked up

    Returns void

  • clear(): void
  • clear refreshes the canvas with a new image buffer. This resets the canvas removing any previous drawing on the canvas. This also resets any settings applied to the draw path.

    new Action('Canvas clear', clear)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function clear(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.clear();
    }

    Returns void

  • clearDash(): void
  • Clears any dashes for future stroke operations. You use this if you added dashes for a stroke operation but you don't want dashes on the next stroke.

    new Action('Canvas clearDash', clearDash)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function clearDash(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.lineWidth = 4;
    ctx.rect(100,200,200,300);
    ctx.strokeStyle = '#000000'
    ctx.addDash(5, 15);
    ctx.stroke();

    ctx.beginPath();
    ctx.clearDash();
    ctx.rect(400,250,500,200);
    ctx.stroke();
    }

    Returns void

  • clearGradient(): void
  • clearGradient clears any point gradients for brush operations like fill and stroke that you may have used already so that those point gradients don't affect future operations. Note this will not stop the gradient mode and you need to set brushMode back to 0 for this. This just clears any previous gradient data to get ready for a new gradient.

    new Action('Canvas clearGradient', clearGradient)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function clearGradient(canvas) {
    //first make a gradient that goes between red and green
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.brushGradientStart('0xFF0000FF')
    ctx.brushGradientEnd('#0x00FF00FF')
    ctx.brushMode = uComps.BrushMode.LINEAR_GRADIENT
    ctx.fillEllipse(150, 300, 150, 100, 40);

    //Clear the gradient will mean the next one has no colors set so it will be greyscale.
    ctx.clearGradient();
    ctx.beginPath();
    ctx.fillStyle = '#FF22FF';
    ctx.fillEllipse(150, 500, 150, 100, 40);

    ctx.brushMode = 0; //This will exit gradient mode to go back to normal fills
    }

    Returns void

  • clip(x: number, y: number, width: number, height: number): void
  • Same functionality as crop will clip and resize the canvas.

    new Action('Canvas clip', clip)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function clip(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.clip(100,100,200,200);
    }

    This example will cut out a sqaure from your image starting at position (100,100) that is 200x200 pixel big. This square becomes the new image and will fill the canvas area.

    Parameters

    • x: number

      top left corner

    • y: number

      top left corner

    • width: number

      the width the area to be clipped

    • height: number

      the height of the area to be clipped

    Returns void

  • closePath(): void
  • This will draw the final line from your current position back to the start. This is a shortcut instead of doing a lineTo() and having to put in the correct numbers.

    new Action('Canvas closePath', closePath)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function closePath(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.strokeStyle = '#FF0000';
    ctx.lineWidth = 4;
    ctx.moveTo(50, 50);
    ctx.lineTo(100, 50);
    ctx.lineTo(100, 100);
    ctx.lineTo(50, 100);
    ctx.closePath();
    ctx.stroke();
    }

    Returns void

  • Parameters

    Returns void

  • createNew(filename: string, width: number, height: number, bgColor: string, backgroundMode: string, useImageSize: boolean): any
  • createNew refreshes the canvas with a new image buffer. This resets the canvas removing any previous drawing on the canvas.

    new Action('Canvas createNew', createNew)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function createNew(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    var size1 = ctx.createNew('', 50, 50, '0x00FF00FF','Generated background', false) //generated background
    var size2 = ctx.createNew('soccerbelgium.svg',0,0,'','Image background', true); //svg background image size
    var size3 = ctx.createNew('soccerbelgium.svg',50,50,'','Image background', false); //svg background set size
    var size4 = ctx.createNew('button_01_default_9.png',0,0,'','Image background', true); //image background
    var size5 = ctx.createNew('button_01_default_9.png',50,50,'','Image background', false); //image background
    print('size1 = ',size1);
    }

    Parameters

    • filename: string

      optional image, png, jpg, bmp or svg.

    • width: number

      pixel_width (used for svg or if no filename). Only used if useImageSize below is false.

    • height: number

      pixel_height (used for svg or if no filename). Only used if useImageSize below is false.

    • bgColor: string

      Optional background color if you don't want to use a filename. Can be empty string if using filename.

    • backgroundMode: string

      can be either 'Image background' or 'Generated background'. If using image background you need to supply the filename parameter. If using generated background then supply the bgColor parameter.

    • useImageSize: boolean

      true will use the native image size and false will use width and height params supplied. Must supply an image filename if true.

    Returns any

    width,height - the size of buffer allocated

  • crop(x: number, y: number, width: number, height: number): void
  • Same functionality as clip will cut out the image at the specified size and location. The image of the canvas background then becomes this cutout. The cropped image follows the same resize rules as the original (fit, fill, stretch). This example will cut out a sqaure from your image starting at position (100,100) that is 200x200 pixels big. This square becomes the new image background and will fill the canvas area.

    new Action('Canvas crop', crop)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function crop(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.crop(100,100,200,200);
    }

    Parameters

    • x: number

      top left corner

    • y: number

      top left corner

    • width: number

      the width and height of the area to be cropped

    • height: number

      the width and height of the area to be cropped

    Returns void

  • detectBlobs(color: string, rSimilarity: number, gSimilarity: number, bSimilarity: number, sensitivity: number, blobMinSize: number, visualise: number): any
  • Looks for pixel connected blobs which match a specific color

    new Action('Canvas detectBlobs', detectBlobs)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function detectBlobs(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    var blobs_JSON = ctx.detectBlobs('0xFFFFFF',10,10,10,3,10,0);
    var blobs = JSON.parse(blobs_JSON);
    for(var i = 0; i < blobs.length; i++) {
    var obj = blobs[i];
    var blobs_x = obj.x;
    var blobs_y = obj.y;
    var blobs_pixel_cnt = obj.total;
    }
    }

    Parameters

    • color: string

      color to match

    • rSimilarity: number

      from 0 to 255 the 8 bit range (0 is an exact match, 255 matches all) for red channel

    • gSimilarity: number

      from 0 to 255 the 8 bit range (0 is an exact match, 255 matches all) for green channel

    • bSimilarity: number

      from 0 to 255 the 8 bit range (0 is an exact match, 255 matches all) for blue channel

    • sensitivity: number

      the number of pixels to make or reject an RLE stride. Small number includes small blobs, large number includes large blobs

    • blobMinSize: number

      the minimum pixel count to be considered a blob (4 or higher)

    • visualise: number

      if true lets you see the matches on the source canvas

    Returns any

    JSON blobs - for each blob the x,y,total is recorded

  • detectSegments(destinationCanvas: Component, numElements: number, compactness: number, visualise: number): void
  • new Action('Canvas detectSegments', detectSegments)
    .addParameter('Source Canvas', uActions.ParamType.CANVAS)
    .addParameter('Destination Canvas', uActions.ParamType.CANVAS)
    .register();
    function detectSegments(canvas, destinationCanvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.detectSegments(destinationCanvas._ref,100,10,1);
    }

    Looks for pixel with a similar color and create (SNIC) superpixels into a result canvas with a unique color for each superpixel

    Parameters

    • destinationCanvas: Component

      the canvas to hold the result

    • numElements: number

      the number of superpixels (2 or higher)

    • compactness: number

      0 to 100, the super pixels tendancy to circularity (10 is a common value)

    • visualise: number

      if true lets you see the segments on the source canvas

    Returns void

  • detectSlidingTemplate(templateCanvas: Component, templateCountX: number, centerMatchX: number, centerMatchY: number, pixelSlideX: number, pixelSlideY: number, slideStep: number): any
  • Slides a template over the canvas returning the best matching value and position for each template

    var ctx = uComps.cast.toCanvas(canvas);
    var ctx_src = uComps.cast.toCanvas(canvas2);
    var templates = ctx.detectSlidingTemplate(ctx_src.ref,10,100,100,3,3,1)
    templates = JSON.parse(templates)
    for(var i = 0; i < templates.length; i++) {
    var obj = templates[i]
    var difference = obj.dif
    var match_x = obj.x
    var match_y = obj.y
    }

    Parameters

    • templateCanvas: Component

      the template canvas

    • templateCountX: number

      the canvas_template can have one or more templates left to right, they must all be the same width

    • centerMatchX: number

      the spot on the canvas to match the templates against

    • centerMatchY: number

      the spot on the canvas to match the templates against

    • pixelSlideX: number

      the number of pixels to slide in x to test the templates

    • pixelSlideY: number

      the number of pixels to slide in y to test the templates

    • slideStep: number

      by default 1 pixel, but can step in 2,3 or more pixel increments

    Returns any

    JSON template_matches - for each template the diff,x,y where the difference is the best match and x,y is the template position

  • drawPathFill(): void
  • This is just another name for fill() to match the old system naming.

    Returns void

  • emit(eventType: string, data: Object): void
  • Call each callback that is registered for the given eventType, in the order they were registered, passing the supplied data to each. Also works for 'on_down', 'on_up', and 'on_move'.

    new Action('Canvas emit', emit)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function emit(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.emit('on_press', 'hello world');
    }

    Parameters

    • eventType: string

      The type of the event to emit.

    • data: Object

      Data object to pass to each callback.

    Returns void

  • fill(): void
  • Draws the current path with the current brush in fill mode (fills in the middle)

    new Action('Canvas fill', fill)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function fill(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.strokeStyle = '#FF0000';
    ctx.lineWidth = 4;
    ctx.rect(10,250,200,200);
    ctx.stroke();
    ctx.fillStyle = '#00FF00';
    ctx.fill();
    }

    Returns void

  • fillEllipse(x: number, y: number, radiusX: number, radiusY: number, steps: number): void
  • Filled ellipse with center at the x/y position with radiuses radiusX, radiusY added to the path

    new Action('Canvas fillEllipse', fillEllipse)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function fillEllipse(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.fillStyle = '#FF22FF';
    ctx.fillEllipse(150, 100, 150, 100, 40);
    }

    Parameters

    • x: number

      position of center of ellipse

    • y: number

      position of center of ellipse

    • radiusX: number

      x radius

    • radiusY: number

      y radius (a circle if both radius values are the same)

    • steps: number

      a larger number produces a smoother ellipse

    Returns void

  • fillRect(x: number, y: number, width: number, height: number): void
  • A filled rectangle is added to the path

    new Action('Canvas fillRect', fillRect)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function fillRect(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.fillStyle = '#FF22FF';
    ctx.fillRect(200, 200, 100, 100);
    }

    Parameters

    • x: number
    • y: number
    • width: number

      width of the rectangle

    • height: number

      height of the rectangle

    Returns void

  • fillRoundedRect(x: number, y: number, width: number, height: number, radius: number): void
  • A filled rectangle with curved corners is added to the path

    new Action('Canvas fillRoundedRect', fillRoundedRect)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function fillRoundedRect(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.fillStyle = '#FF22FF';
    ctx.fillRoundedRect(200, 200, 100, 100, 20);
    }

    Parameters

    • x: number
    • y: number
    • width: number

      width of the rectangle

    • height: number

      height of the rectangle

    • radius: number

      curvature of the corners

    Returns void

  • fillText(text: string, x: number, y: number, maxWidth?: number): void
  • The specified text is drawn with the current brush in fill mode. When specifying a font you need to use the name of the font and not the filename. The name of a font can be found by going to properties of the font file and checking the details tab.

    var ctx = uComps.cast.toCanvas(canvas);
    ctx.font = "40px vera"
    ctx.fillStyle = "#000033"
    ctx.fillText("Solid",10,500,1000)

    Parameters

    • text: string

      the text to render

    • x: number

      the position of the top left corner

    • y: number

      the position of the top left corner

    • Optional maxWidth: number

    Returns void

  • flipHorizontal(): void
  • flipHorizontal flips the canvas horizontally.

    new Action('Canvas flipHorizontal', flipHorizontal)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function flipHorizontal(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.flipHorizontal();
    }

    Returns void

  • flipVertical(): void
  • flipVertical flips the canvas vertically.

    new Action('Canvas flipVertical', flipVertical)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function flipVertical(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.flipVertical();
    }

    Returns void

  • focus(): void
  • Set the keyboard focus to this component.

    new uActions.Action('set focus', setFocus)
    .addParameter('text entry',uActions.ParamType.TEXT_ENTRY)
    .register();
    function setFocus(textEntry) {
    textEntry.focus();
    }

    Returns void

  • get(propertyName: string): any
  • Get a property by name.

    var uActions = require('u-actions');
    new uActions.Action('get fontsize',getFontSize).register();
    function getFontSize() {
    var text1=uPages.current.components.getText('Text 1');
    console.log(text1.get('font_size'));
    }

    Parameters

    • propertyName: string

      The name of the property to get.

    Returns any

    Returned value can be of any type, or even null.

  • getHeight(): number
  • getHeight returns the canvas height. This is not the size of the component but the number of pixels the actual canvas was set to either from the background image or size specified.

    new Action('Canvas getHeight', getHeight)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function getHeight(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    var canasHeight = ctx.getHeight();
    print('canasHeight = ',canasHeight);
    }

    Returns number

  • getPixel(x: number, y: number): number
  • getPixel returns the RGBA color value of th pixel at the given coordinates on the canvas. Out of bounds requests return the color transparent black.

    new Action('Canvas getPixel', getPixel)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function getPixel(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    var pixelColor = ctx.getPixel(250, 250);
    print('pixelColor = ',pixelColor);
    }

    Parameters

    • x: number

      pixel position

    • y: number

      pixel position

    Returns number

    pixel RGBA color

  • getPixelData(): ArrayBuffer
  • Retrieves all the pixel data in the canvas in ARGB format. To understand this data, also look at properties pixelWidth, pixelHeight, pixelStride and pixelFormat.

    new Action('Canvas getPixelData', getPixelData)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function getPixelData(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    var ArrayBuffer = ctx.getPixelData();
    var dataReader = new DataView(ArrayBuffer);
    dataReader.setInt16(0, 256, true);
    // Int16Array uses the platform's endianness.
    var dataArray = new Int16Array(ArrayBuffer)
    var isLittleEndian = dataArray[0] === 256;
    print('isLittleEndian = ',isLittleEndian);
    for(var i = 0; i < 20; i++) {
    var test = dataReader.getUint8(i);
    print('test = ',test);
    }
    }

    Returns ArrayBuffer

  • getWidth(): number
  • getWidth returns the canvas width. This is not the size of the component but the number of pixels the actual canvas was set to either from the background image or size specified.

    new Action('Canvas getWidth', getWidth)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function getWidth(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    var canasWidth = ctx.getWidth();
    print('canasWidth = ',canasWidth);
    }

    Returns number

  • gradientAddPoint(color: string, position: number): void
  • Adds a color point at a position from 1-254. The multi point gradient starts with brushGradientStart, then any point gradients, then ends with brushGradientEnd. This gradient is used for brush operations like fill and stroke. Look at brushGradientEnd example for use of gradientAddPoint.

    Parameters

    • color: string

      the color point to add as a string such as '#DD33EE', or shorthand hex '#D3E', or include the alpha channel '0xDD33EEAA'

    • position: number

      from 1 to 254 (e.g. 128 will be a middle color between the start and end colors)

    Returns void

  • hsv(hue: number, saturation: number, value: number): void
  • Adjusts the canvas with an HSV transform. HSV provides control over the hue (tint), saturation (amount of tint) and value (brightness) giving control of the amount of color in the image

    Parameters

    • hue: number

      0 no change, (-180 to 180), each 90 plus and minus is a change in RGB hue

    • saturation: number

      0 no change, (-1 to 1) -1 = desaturated, 1 = saturated

    • value: number

      0 no change, (-1 to 1) -1 = dark, 1 = bright

      new Action('Canvas hsv', hsv)
      .addParameter('Canvas', uActions.ParamType.CANVAS)
      .register();
      function hsv(canvas) {
      var ctx = uComps.cast.toCanvas(canvas);
      var canasHeight = ctx.hsv(90, 0.5, 0.5);
      }

    Returns void

  • invert(): void
  • Inverts the colors in the canvas.

    new Action('Canvas invert', invert)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function invert(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    var canasHeight = ctx.invert();
    }

    Returns void

  • lighting(brightness: number, contrast: number, shadow: number, fill: number, highlight: number, temperature: number): void
  • Applies lighting to the canvas

    new Action('Canvas lighting', lighting)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function lighting(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    var canasHeight = ctx.lighting(0.5, 0.5, 0.5, 0.5, 0.5, 0.5);
    }

    Parameters

    • brightness: number

      (-1 to 1) -1 = black, 0 = unmodified, 1 = white

    • contrast: number

      (-1 to 1) -1 = no contrast / grey, 0 = unmodified, 1 = high contrast

    • shadow: number

      (-1 to 1) -1 = shadow reduced, 0 = unmodified, 1 = shadow increased

    • fill: number

      (-1 to 1) -1 = fill light reduced, 0 = unmodified, 1 = fill light increased

    • highlight: number

      (-1 to 1) -1 = highlights reduced / grey, 0 = unmodified, 1 = highlights increased

    • temperature: number

      (-1 to 1) -1 = warm / red, 0 = unmodified, 1 = cool / blue

    Returns void

  • lineTo(x: number, y: number): void
  • A line from the current position to the new position on the current path. Note that this does not render immediately, it simply adds to the current path and displays when you draw the path. This mirrors the original JS command canvasAddLine().

    new Action('Canvas lineTo', lineTo)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    new Action('Canvas lineTo', lineTo)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function lineTo(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.strokeStyle = '#FF0000';
    ctx.lineWidth = 4;
    ctx.moveTo(50, 50);
    ctx.lineTo(100, 100);
    ctx.moveTo(150, 100);
    ctx.lineTo(150, 150);
    ctx.moveTo(150, 200);
    ctx.lineTo(200, 200);
    ctx.stroke();
    }

    Parameters

    • x: number

      new position

    • y: number

      new position

    Returns void

  • moveTo(x: number, y: number): void
  • A move to a new position without drawing on a line. This does not have any immediate visual affect and simply sets up the position for where to start drawing the next command. This mirrors the original JS command canvasAddMove().

    new Action('Canvas lineTo', lineTo)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function lineTo(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.strokeStyle = '#FF0000';
    ctx.lineWidth = 4;
    ctx.moveTo(50, 50);
    ctx.lineTo(100, 100);
    ctx.moveTo(150, 100);
    ctx.lineTo(150, 150);
    ctx.moveTo(150, 200);
    ctx.lineTo(200, 200);
    ctx.stroke();
    }

    Parameters

    • x: number

      new position

    • y: number

      new position

    Returns void

  • on(eventType: string, callback: Function): void
  • Adds the callback function to the end of the listeners array for eventType.

    new Action('Canvas on', on)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function on(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.on('on_press', on_callback);
    }
    function on_callback() {
    print('on_callback!!!!');
    }

    Parameters

    • eventType: string

      The type of the event.

    • callback: Function

      The callback function

    Returns void

  • peakSignalToNoiseRatio(otherImage: Component): number
  • canvasCalcPsnr Returns the calculated peak signal to noise ratio comparing an input canvas to another canvas. The higher the returned value the more alike the images are. A typical value for comparisons for lossy images are between 30 and 50 dB, where higher is better. When the two images are identical, the resultant psnr will be infinity.

    See http://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio

    new Action('Canvas peakSignalToNoiseRatio', peakSignalToNoiseRatio)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .addParameter('Canvas Compare', uActions.ParamType.CANVAS)
    .register();
    function peakSignalToNoiseRatio(canvas, compareCanvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    var ctx2 = uComps.cast.toCanvas(compareCanvas);
    var result = ctx.peakSignalToNoiseRatio(ctx2._ref);
    print('peakSignalToNoiseRatio = ',result);
    }

    Parameters

    Returns number

    the calculated peak signal to noise ratio

  • prepareSvg(filename: string, svgdata: string): string
  • Preload an SVG ready to be stamped onto the canvas stampSvg. This has no immediate visual effect and is just a preparation but will display once you stamp it onto the canvas.

    new Action('Canvas prepareSvg', prepareSvg)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function prepareSvg(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.prepareSvg('soccerbelgium.svg', '');
    }

    Parameters

    • filename: string

      the svg file from the images folder

    • svgdata: string

      an alternative svg string to render

    Returns string

    dimensions -JSON data of the top left and bottom right coordinates of the svg prepared

  • This is the same as peakSignalToNoiseRatio and is just an abreviation.

    Parameters

    Returns number

    the calculated peak signal to noise ratio

  • readAprilTags(): any
  • Scans the canvas for april tags and tries to read in their data.

    new Action('Canvas readAprilTags', readAprilTags)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function readAprilTags(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.font = "30px vera";
    var tags = ctx.readAprilTags();
    tags = JSON.parse(tags);
    ctx.strokeStyle = '#FF0000';
    ctx.lineWidth = 4;
    for(var i = 0; i < tags.length; i++) {
    var obj = tags[i];
    ctx.beginPath();
    ctx.moveTo(obj.tl_x,obj.tl_y);
    ctx.lineTo(obj.tr_x,obj.tr_y);
    ctx.lineTo(obj.br_x,obj.br_y);
    ctx.lineTo(obj.bl_x,obj.bl_y);
    ctx.lineTo(obj.tl_x,obj.tl_y);
    ctx.stroke();
    ctx.fillText(obj.id,obj.x,obj.y-50,1000);
    }
    }

    Returns any

    JSON tagslist - id,margin,x,y,tl,tr,br,bl (The tag ID, the accuracy margin, the x/y center, the TopLeft, TopRight, BottomRight and BottomLeft corners of the tag)

  • readBarcode(): string
  • Scans the canvas for a barcode and attempts to read in the data if it finds one.

    new Action('Canvas readBarcode', readBarcode)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function readBarcode(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    var data = ctx.readBarcode();
    print('data = ',data,' len = ',data.length);
    }

    Returns string

    code - the data encoded

  • readQrcode(): string
  • Reads a potential QR code on the canvas. Note the QR code must have white space around it to work. If the QR code takes up the whole canvas and the black boxes are right on the edge it won't work.

    new Action('Canvas readQrcode', readQrcode)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function readQrcode(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    var data = ctx.readQrcode();
    print('data = ',data,' len = ',data.length);
    }

    Returns string

    code - the data encoded

  • rect(x: number, y: number, width: number, height: number): void
  • Adds to the path a rectangle shape in the position and size specificed by parameters. Will only display once stroke() is called to draw everything added to the path.

    new Action('Canvas rect', rect)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function rect(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.strokeStyle = '#FF0000';
    ctx.lineWidth = 4;
    ctx.rect(50, 50, 100, 100);
    ctx.stroke();
    }

    Parameters

    • x: number
    • y: number
    • width: number

      width of the rectangle

    • height: number

      height of the rectangle

    Returns void

  • refresh(): void
  • Refresh this component to force it to be re-rendered, or for its dynamic data to be updated.

    Returns void

  • reset(): void
  • Refreshes the canvas with a new image buffer and resets all properties back to defaults

    new Action('Canvas reset', canvasReset)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function canvasReset(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.reset();
    }

    Returns void

  • resetColor(color: string): void
  • Quickly updates the whole canvas to the specified color

    new Action('Canvas resetColor', resetColor)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function resetColor(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.resetColor('#FF0000');
    }

    Parameters

    • color: string

      the color to apply to the canvas

    Returns void

  • resetTransform(): void
  • Updates the transform back to its default value for the path. Useful for resetting operations such as rotateBrush, scale, setTransform, skew, translate, and transform. This removes these transforms from the current path so they won't apply on the next draw.

    new Action('Canvas resetTransform', resetTransform)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function resetTransform(canvas) {
    //first square will be skewed.
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.skew(0,1);
    ctx.strokeStyle = '#FF0000';
    ctx.lineWidth = 4;
    ctx.rect(50, 50, 100, 100);
    ctx.stroke();

    //second square will not be skewed as the skew is reset by resetTransform().
    ctx.resetTransform();
    ctx.beginPath();
    ctx.rect(250, 50, 100, 100);
    ctx.stroke();
    }

    Returns void

  • resize(width: number, height: number, horizontalFilter: number, verticalFilter: number): void
  • Resizes the canvas which will also recreate the canvas clearing away anything that had been drawn to the canvas. This resizes the actual canvas itself affecting the number of pixels available. This means if you resize to be a lot smaller the display will be the same size but the image will be blury as the canvas stretches to fit the component.

    new Action('Canvas resize', resize)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function resize(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.resize(100, 100, 0, 0);
    }

    Parameters

    • width: number

      the width and height to resize to

    • height: number

      the width and height to resize to

    • horizontalFilter: number

      0 nearest, 1 linear, 2 avg, 3 lanzcos

    • verticalFilter: number

      0 nearest, 1 linear, 2 avg, 3 lanzcos

    Returns void

  • rotate(angle: number): void
  • Rotates the canvas in 90 degree increments. This operation is additive. Calling it twice with mode 1 with result in a total of 180 degree rotation.

    new Action('Canvas rotate', rotate)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function rotate(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    var mode = 1; //1 = 90 degrees.
    ctx.rotate(mode);
    }

    Parameters

    • angle: number

      Rotates the canvas by a given angle (0 = no rotate, 1 = 90 degrees, 2 = 180, 3 = 270)

    Returns void

  • rotateBrush(angle: number): void
  • Transform the brush - rotate to the given angle (in radians)

    new Action('Canvas rotateBrush', rotateBrush)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function rotateBrush(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.rotateBrush(0.78);
    ctx.rect(100,100,300,300);
    ctx.lineWidth = 4;
    ctx.strokeStyle = '#FF0000'
    ctx.stroke();
    //This removes the rotateBrush change for the next operation.
    ctx.resetTransform();
    }

    Parameters

    • angle: number

      rotation angle in radians

    Returns void

  • roundedRect(x: number, y: number, width: number, height: number, radius: number): void
  • A rectangle with curved corners is added to the path

    new Action('Canvas roundedRect', roundedRect)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function roundedRect(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.fillStyle = '#FF0000'
    ctx.roundedRect(200, 200, 100, 100, 30);
    ctx.fill();
    }

    Parameters

    • x: number
    • y: number
    • width: number

      width of the rectangle

    • height: number

      height of the rectangle

    • radius: number

      curvature of the corners

    Returns void

  • saveAs(filename: string, quality: number): void
  • saveAs saves a render of the canvas as an image to storage.

    new Action('Canvas saveAs', saveAs)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function saveAs(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.saveAs('test.png', 1.0);
    }

    Parameters

    • filename: string

      the path and filename with extension .png or .jpg

    • quality: number

      the jpeg or png compression ratio from 0-1

    Returns void

  • scale(x: number, y: number): void
  • Updates the transform scale for the current path. Note this also changes the x and y position to scale which is not how Umajin works. this will be applied during fill or stroke

    new Action('Canvas scale', scale)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function scale(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.scale(0.5, 0.5);
    ctx.rect(100,100,300,300);
    ctx.lineWidth = 4;
    ctx.strokeStyle = '#FF0000'
    ctx.stroke();
    //This removes the scale change for the next operation.
    ctx.resetTransform();
    }

    Parameters

    • x: number
    • y: number

    Returns void

  • set(propertyName: string, value: any): void
  • set(data: Object): void
  • Set a properties value by name.

    var uActions = require('u-actions');
    new uActions.Action('set text color',setTextColor).register();
    function setTextColor() {
    uPages.get('follow').components.getText('Text 1').set('font_color','0x0000FFFF');
    }

    Parameters

    • propertyName: string

      The name of the property to set.

    • value: any

      The value to set. Can be of any type.

    Returns void

  • Set a number properties at once.

    Parameters

    • data: Object

      Data object containing property name and value pairs to set on this component.

    Returns void

  • setBrushSource(sourceCanvas: Component): void
  • Sets the image from a passed in canvas as the brush image to draw with.

    Parameters

    • sourceCanvas: Component

      the canvas component with the image set to it that you want to use for the brush.

      new Action('Canvas brushSourceBottomRight', brushSourceBottomRight)
      .addParameter('Canvas', uActions.ParamType.CANVAS)
      .addParameter('Canvas Source', uActions.ParamType.CANVAS)
      .register();
      function brushSourceBottomRight(canvas, canvas2) {
      var ctx = uComps.cast.toCanvas(canvas);
      var ctx2 = uComps.cast.toCanvas(canvas2);
      ctx.beginPath();
      ctx.setBrushSource(ctx2._ref);
      ctx.brushSourceTopLeft(100, 100);
      ctx.brushSourceBottomRight(300, 300);
      ctx.brushMode = 2;
      ctx.rect(10,250,200,200);
      ctx.fill();
      }

    Returns void

  • setPixel(x: number, y: number, color: string): void
  • setPixel sets the color of the pixel at the given coordinates on the canvas to the specified color.

    //This draws a thick red line by setting the pixels
    new Action('Canvas setPixel', setPixel)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function setPixel(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    for (i = 0; i < 75; i++) {
    ctx.setPixel(10+i, 10, '0xFF0000FF');
    ctx.setPixel(10+i, 11, '0xFF0000FF');
    ctx.setPixel(10+i, 12, '0xFF0000FF');
    ctx.setPixel(10+i, 13, '0xFF0000FF');
    }
    }

    Parameters

    • x: number

      pixel position

    • y: number

      pixel position

    • color: string

      pixel RGBA color

    Returns void

  • setPixelData(width: number, height: number, stride: number, format: PixelFormat, data: ArrayBuffer): boolean
  • Sets a new set of pixel data to the canvas in a specified format.

    new Action('Canvas setPixelData', setPixelData)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function setPixelData(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    var ArrayBuffer = ctx.getPixelData();
    var myDataView = new DataView(ArrayBuffer);
    myDataView.setInt16(0, 256, true);
    for(var i = 4; i < ctx.pixelStride*5; i+=4) { //5 rows of data written as blue (starts at bottom left of image)
    myDataView.setUint8(i, 0);
    myDataView.setUint8(i+1, 0);
    myDataView.setUint8(i+2, 255);
    myDataView.setUint8(i+3, 255);
    }
    var width = ctx.pixelWidth;
    var height = ctx.pixelHeight;
    var stride = ctx.pixelStride;
    var format = 4; //'RGBA'
    var resultData = myDataView.buffer;
    ctx.setPixelData(width, height, stride, format, resultData);
    }

    Parameters

    • width: number

      the width in pixels of the data

    • height: number

      the height in pixels of the data

    • stride: number

      the number of bytes per row of the data; often the same as width * bytes per pixel

    • format: PixelFormat

      uComps:PixelFormat - the format of the data for each pixel.

    • data: ArrayBuffer

      the pixel data

    Returns boolean

    True if the data was successfully loaded into the canvas

  • setTransform(horScaling: number, vertSkewing: number, horSkewing: number, vertScaling: number, horTranslation: number, vertTranslation: number): void
  • Clears any previous transform back to the canvas default state before applying the specified scaling, rotating, and skewing. This is different from just using transform() with all params as they don't replace exisintg values, it adds those values to any existing ones.

    new Action('Canvas setTransform', setTransform)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function setTransform(canvas) {
    //first square will be skewed.
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.skew(0,1);
    ctx.strokeStyle = '#FF0000';
    ctx.lineWidth = 4;
    ctx.rect(50, 50, 100, 100);
    ctx.stroke();

    //the skew will be removed by setTransform and the square will have default skew, and scale but be offset by (100,100).
    ctx.beginPath();
    ctx.setTransform(1, 0, 0, 1, 100, 100);
    ctx.rect(50, 50, 100, 100);
    ctx.stroke();
    }

    Parameters

    • horScaling: number

      horizontal scaling (1 = normal scale, 0.5 = half scale, 2.0 = double scale)

    • vertSkewing: number

      vertical skewing (0 = normal, negative values skew upwards )

    • horSkewing: number

      horizontal skewing (0 = normal, negative values skew left )

    • vertScaling: number

      vertical scaling (1 = normal scale, 0.5 = half scale, 2.0 = double scale)

    • horTranslation: number

      horizontal translation (0 = no change, translation in the x direction )

    • vertTranslation: number

      vertical translation (0 = no change, translation in the y direction )

    Returns void

  • skew(horSkewing: number, vertSkewing: number): void
  • Updates the transform skew for the current path - this will be applied during fill or stroke

    new Action('Canvas skew', skew)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function skew(canvas) {
    //first rectangle will be skewed.
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.skew(0,1);
    ctx.strokeStyle = '#FF0000';
    ctx.lineWidth = 4;
    ctx.rect(50, 50, 100, 100);
    ctx.stroke();
    //This removes the skew change for the next operation.
    ctx.resetTransform();
    }

    Parameters

    • horSkewing: number

      the skew in the x direction (can be negative or positive)

    • vertSkewing: number

      the skew in the y direction (can be negative or positive)

    Returns void

  • smoothPath(steps: number): void
  • Converts lines on a path into a bezier curve.

    new Action('Canvas smoothPath', smoothPath)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function smoothPath(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.resetColor('0x448844FF')
    ctx.beginPath()
    ctx.strokeStyle = '#FFFFFF'
    ctx.moveTo(20,Math.cos(0.0)*180+200)
    for (i = 0; i < 15; i++) {
    ctx.lineTo(i*50 + 20,Math.cos(i)*180 + 200)
    }
    //draw jagged lines
    ctx.stroke()
    //now smooth and draw smooth curve below
    ctx.smoothPath(2)
    ctx.translate(0,100)
    ctx.stroke()
    ctx.resetTransform()
    }

    Parameters

    • steps: number

      number of steps of smoothing (0 to 2)

    Returns void

  • stampAdaptiveThreshold(sourceCanvas: Component, srcX: number, srcY: number, srcWidth: number, srcHeight: number, dstX: number, dstY: number, strength: number, radius: number, blend: number): void
  • Thresholds with variable strength given a radius of local contrast. The blend 0-1 is how much of the image is preserved vs thresholded.

    Parameters

    • sourceCanvas: Component

      the source canvas

    • srcX: number

      the source top left

    • srcY: number

      the source top left

    • srcWidth: number

      the source width

    • srcHeight: number

      the source height

    • dstX: number

      the destination position top left

    • dstY: number

      the destination position top left

    • strength: number

      -1 to 1 how strong the threshold is towards binary

    • radius: number

      1 to 1000 how wide the radius to check for local contrast

    • blend: number

      0 to 1 how much of the image is preserved vs thresholded (0.5 is a common value)

    Returns void

  • stampAutoBrightnessContrast(sourceCanvas: Component, srcX: number, srcY: number, srcWidth: number, srcHeight: number, dstX: number, dstY: number): void
  • Stamps a source canvas region onto the target canvas while calculating and applying the maximum brightness and contrast

    Parameters

    • sourceCanvas: Component

      the source canvas

    • srcX: number

      the source top left

    • srcY: number

      the source top left

    • srcWidth: number

      the source width

    • srcHeight: number

      the source height

    • dstX: number

      the destination position top left

    • dstY: number

      the destination position top left

      new Action('Canvas stampAutoBrightnessContrast', stampAutoBrightnessContrast)
      .addParameter('Canvas', uActions.ParamType.CANVAS)
      .addParameter('Canvas2', uActions.ParamType.CANVAS)
      .register();
      function stampAutoBrightnessContrast(canvas, canvas2) {
      var ctx = uComps.cast.toCanvas(canvas);
      var ctx2 = uComps.cast.toCanvas(canvas2);
      ctx.stampAutoBrightnessContrast(ctx2._ref, 0, 0, ctx2.pixelWidth, ctx2.pixelHeight, 0, 0);
      }

    Returns void

  • stampAutoColor(sourceCanvas: Component, srcX: number, srcY: number, srcWidth: number, srcHeight: number, dstX: number, dstY: number): void
  • Stamps a source canvas region onto the target canvas while calculating and applying strong color saturation/balance

    new Action('Canvas stampAutoColor', stampAutoColor)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .addParameter('Canvas2', uActions.ParamType.CANVAS)
    .register();
    function stampAutoColor(canvas, canvas2) {
    var ctx = uComps.cast.toCanvas(canvas);
    var ctx2 = uComps.cast.toCanvas(canvas2);
    ctx.stampAutoColor(ctx2._ref, 0, 0, ctx2.pixelWidth, ctx2.pixelHeight, 0, 0);
    }

    Parameters

    • sourceCanvas: Component

      the source canvas

    • srcX: number

      the source top left

    • srcY: number

      the source top left

    • srcWidth: number

      the source width

    • srcHeight: number

      the source height

    • dstX: number

      the destination position top left

    • dstY: number

      the destination position top left

    Returns void

  • stampBarcode(code: string, x: number, y: number, angle: number, size: number, codeType: number, codeLevel: number): void
  • Stamps a 1D or 2D barcode onto the canvas.

    new Action('Canvas stampBarcode', stampBarcode)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function stampBarcode(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.stampBarcode("Text to user",180,180,0,17,uComps.StampCodeType.AZTEC,0);
    ctx.stampBarcode("123456",780,200,0,8,uComps.StampCodeType.PDF417,0);
    ctx.stampBarcode("345 and text",220,600,0,19,uComps.StampCodeType.QR_CODE,0);
    }

    Parameters

    • code: string

      the text/numbers to encode (note only some barcodes such as Aztec, QR, PDF417 support letters)

    • x: number

      the position

    • y: number

      the position

    • angle: number

      the angle in degrees

    • size: number

      size from 1-100 pixels per bar

    • codeType: number

      the barcode uComps.StampCodeType: (1 - AZTEC, 2 - CODE_ABAR, 4 - CODE_39, 8 - CODE_93, 16 - CODE_128, 32 - DATA_MATRIX, 64 - EAN8, 128 - EAN13, 256 - ITF, 512 - MAXICODE, 1024 - PDF417, 2048 - QR_CODE, 4096 - RSS14, 8192 - RSSE, 16384 - UPCA, 32768 - UPCE)

    • codeLevel: number

      Some codes support redundancy levels, 1=low, 4=medium, 7=high

    Returns void

  • stampBuffer(sourceCanvas: Component, srcX: number, srcY: number, srcWidth: number, srcHeight: number, dstX: number, dstY: number): void
  • stampBuffer stamps a source canvas region onto the target canvas

    new Action('Canvas stampBuffer', stampBuffer)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .addParameter('Canvas2', uActions.ParamType.CANVAS)
    .register();
    function stampBuffer(canvas, canvas2) {
    var ctx = uComps.cast.toCanvas(canvas);
    var ctx2 = uComps.cast.toCanvas(canvas2);
    ctx.stampBuffer(ctx2._ref, 0, 0, ctx2.pixelWidth, ctx2.pixelHeight, 0, 0);
    }

    Parameters

    • sourceCanvas: Component

      the source canvas

    • srcX: number

      the source top left

    • srcY: number

      the source top left

    • srcWidth: number

      the source width

    • srcHeight: number

      the source height

    • dstX: number

      the destination position top left

    • dstY: number

      the destination position top left

    Returns void

  • stampChromaKey(sourceCanvas: Component, srcX: number, srcY: number, srcWidth: number, srcHeight: number, dstX: number, dstY: number, hue: number, hueMask: number, luminanceMask: number, maskShadow: number, maskLight: number): void
  • Removes from the source_canvas (this process is known as a key) a specific color (or chroma). This function uses the HSV transform to calculate the hue (tint), saturation (amount of tint) and value (brightness) - this allows us to control how much of the highlights and shadows are preserved when a colored background is removed.

    Parameters

    • sourceCanvas: Component

      the source canvas

    • srcX: number

      the source top left

    • srcY: number

      the source top left

    • srcWidth: number

      the source width

    • srcHeight: number

      the source height

    • dstX: number

      the destination position top left

    • dstY: number

      the destination position top left

    • hue: number

      the color converted to hue (hsv) from 0 to 360, 0 red, 90 green, 180 blue

    • hueMask: number

      the hue tollerance to generate the mask, 0 to 1, 0.1 or 0.06 are good values

    • luminanceMask: number

      the amount of luminance tollerance, 0 to 5, 1.5 is a good value

    • maskShadow: number

      the amount of shadow preserved, 0 to 5, 2.6 is a good value, smaller means less shadow is added

    • maskLight: number

      the amount of saturated color preserved, 0 to 5, 0.9 is a good value, more means more highlights may be picked up

    Returns void

  • stampColorChannel(sourceCanvas: Component, srcX: number, srcY: number, srcWidth: number, srcHeight: number, dstX: number, dstY: number, srcChannel: number, dstChannel: number): void
  • Stamps a source canvas region onto the target canvas from one channel to another

    new Action('Canvas stampColorChannel', stampColorChannel)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .addParameter('Canvas2', uActions.ParamType.CANVAS)
    .register();
    function stampColorChannel(canvas, canvas2) {
    var ctx = uComps.cast.toCanvas(canvas);
    var ctx2 = uComps.cast.toCanvas(canvas2);
    ctx.stampColorChannel(ctx2._ref, 0, 0, ctx2.pixelWidth, ctx2.pixelHeight, 0, 0, 1, 2);
    }

    Parameters

    • sourceCanvas: Component

      the source canvas

    • srcX: number

      the source top left

    • srcY: number

      the source top left

    • srcWidth: number

      the source width

    • srcHeight: number

      the source height

    • dstX: number

      the destination position top left

    • dstY: number

      the destination position top left

    • srcChannel: number

      (0 to 3) 0 Red, 1 Green, 2 Blue, 3 Alpha

    • dstChannel: number

      (0 to 3) 0 Red, 1 Green, 2 Blue, 3 Alpha

    Returns void

  • stampCustom(canvas: Component, canvas_src: Component, canvas_lut: Component, src_x: number, src_y: number, src_width: number, src_height: number, dst_x: number, dst_y: number, registers: number, pixel_code: number): void
  • canvasStampCustom stamp a custom set of vectorised instructions onto the canvas

    This is an image processing register based virtual machine. This exploits a single CPU core and is vectorised for better performance than scripting. For much better performance a VulkanCompute shader would be recommended. There are 256 floating point registers which hold 4 channels each. For an 8 bit RGBA image this would be equivalent to 0-255 for each channel where each register is equivalent to a pixel. The instruction buffer is composed of 8 bit instruction op codes, 8 bit register indexes, 16 bit program addresses and 32 bit floating point values.

    The instruction set is as follows;

    noop = 0, // no operation samp = 1, // samp(pixel_index_from, reg) sample from 0 1 2 / 3 4 5 / 6 7 8 (source) // 9 10 11 / 12 13 14 / 15 16 17 (destination) into register sampgrey = 3, // sample grey(reg_to, channel_to) 9 pixels greyscale into register channel sampavg = 4, // sample avg(reg_to) register holds r, g and b averages for 9 source pixels sampsum = 5, // sample sum(reg_to) register holds r, g and b sum for 9 source pixels sampmax = 6, // sample max(reg_to) register holds r, g and b maximum across the 9 source pixels sampmin = 7, // sample min(reg_to) register holds r, g and b minimum across the 9 source pixels

    copy = 8, // direct copy() from source into the destination pixel ccopy = 9, // copy(channel_from, channel_to) from source into the destination pixel store = 10, // store(reg) from selected register to destination all channels (RGBA) cstore = 11, // channel store(reg, channel_from, channel_to) from register channel into the // destination pixel channel

    add = 16, // add(reg,reg_value) add reg_value into reg addc = 17, // add constant(reg,const) add constant into reg RGB cadd = 18, // channel add(reg,channel,reg_value,channel_value) add reg_value/channel_value // into reg/channel caddc = 19, // channel add constant (reg,channel,const) add constant into reg/channel

    sub = 20, // sub(reg,reg_value) sub reg_value into reg subc = 21, // sub constant(reg,const) sub constant into reg RGB csub = 22, // channel sub(reg,channel,reg_value,channel_value) sub reg_value/channel_value // into reg/channel csubc = 23, // channel sub constant (reg,channel,const) sub constant into reg/channel

    mul = 24, // mul(reg,reg_value) mul reg_value into reg mulc = 25, // mul constant(reg,const) mul constant into reg RGB cmul = 26, // channel mul(reg,channel,reg_value,channel_value) mul reg_value/channel_value // into reg/channel cmulc = 27, // channel mul constant (reg,channel,const) mul constant into reg/channel

    div = 28, // div(reg,reg_value) div reg_value into reg divc = 29, // div constant(reg,const) div constant into reg RGB cdiv = 30, // channel div(reg,channel,reg_value,channel_value) div reg_value/channel_value // into reg/channel cdivc = 31, // channel div constant (reg,channel,const) div constant into reg/channel

    sin = 32, // sin(reg, reg_value) sin(reg_value) into register csin = 33, // channel sin(reg, channel, reg_value, channel_value) sin(reg_value/channel_value) // into register/channel cos = 34, // cos(reg, reg_value) cos(reg_value) into register ccos = 35, // channel cos(reg, channel, reg_value, channel_value) cos(reg_value/channel_value) // into register/channel tan = 36, // tan(reg, reg_value) reg = tan(reg_value) ctan = 37, // channel tan(reg, channel, reg_value, channel_value) tan(reg_value/channel_value) // into register/channel atan = 38, // atan(reg, reg_value) atan(reg_value) into register catan = 39, // channel atan(reg, channel, reg_value, channel_value) // atan(reg_value/channel_value) into register/channel

    pow = 40, // pow(reg, reg_value, const) pow reg_value ^ const into reg (RGB) cpow = 41, // channel pow(reg, channel, reg_value, channel_value, const) pow // reg_value/channel_value ^ const into reg/channel (RGB) log = 42, // log(reg, reg_value) log reg_value into reg (RGB) clog = 43, // channel log(reg, channel, reg_value, channel_value) log reg_value/channel_value // into reg/channel (RGB) sqrt = 44, // sqrt(reg, reg_value) sqrt reg_value into reg (RGB) csqrt = 45, // channel sqrt(reg, channel, reg_value, channel_value) sqrt // reg_value/channel_value into reg/channel (RGB) abs = 46, // abs(reg, reg_value) abs reg_value into reg (RGB) cabs = 47, // channel abs(reg, channel, reg_value, channel_value) abs reg_value/channel_value // into reg/channel (RGB) clamp = 48, // clamp(reg, reg_value, min, max) clamp reg_value(min,max) into reg (RGB) cclamp = 49, // channel clamp(reg, channel, reg_value, channel_value, min, max) clamp // reg_value/channel_value(min,max) into reg/channel (RGB) floor = 51, // floor(reg, reg_value) floor reg_value into reg (RGB) cfloor = 52, // channel floor(reg, channel, reg_value, channel_value) floor // reg_value/channel_value into reg/channel (RGB) ceil = 53, // ceil(reg, reg_value) ceil reg_value into reg (RGB) cceil = 54, // channel ceil(reg, channel, reg_value, channel_value) ceil // reg_value/channel_value into reg/channel (RGB)

    compd = 55, // compute distance(reg,channel, x, y) compute distance to a point from current // pixel into reg/channel compa = 56, // compute angle(reg,channel, x, y) compute relative angle into reg/channel (radians) rcompd = 57, // register compute distance(reg,channel, reg_value) compute distance to a point // from current pixel to reg_value(RG) into reg/channel rcompa = 58, // register compute angle(reg,channel, reg_value) compute relative angle from // current pixel to reg_value(RG) into reg/channel (radians) uv = 59, // uv(reg) put current pixel uv (0-1) x and y ratio into register first two // channels(rg/xy) typeclamp = 60, // clamp(reg, reg_source) to the output type automatically (0-255 for current // 8 bit image types)

    hsv2rgb = 61, // hsv into rgb (reg, reg_value) compute rgb from reg_value into reg rgb2hsv = 62, // rgb into hsv (reg, reg_value) compute hsv from reg_value into reg xyz2rgb = 63, // normal map encode into color xyz into rgb (reg, reg_value) (0/127/255) to (-1/0/1) rgb2xyz = 64, // decode normal map into vectors rgb into xyz (reg, reg_value) (-1/0/1) to (0/127/255)

    load = 65, // load(reg,value) (RGB) cload = 66, // channel load(reg,channel,value)

    // alpha blended version of store cmixa8 = 67, // channel mix(channel_dst, reg_source, channel_source) from selected register, // using reg alpha (8 bit = 0-255), mixed with destination pixels (RGB) mixa8 = 68, // mix(reg) from selected register, using reg alpha (8 bit = 0-255), mixed with // destination pixels (RGB) cmixa = 69, // channel mix(channel_dst, reg_source, channel_source) from selected register, // using reg alpha (0-1), mixed with destination pixels (RGB) mixa = 70, // mix(reg) from selected register, using reg alpha (0-1), mixed with destination // pixels (RGB)

    rmixa = 71, // register mix(reg1,reg2) mix two rgb registers using alphas proportion into // destination pixels (RGB) cmixc = 72, // channel mix constant(channel_dst,reg_source,channel_source,ratio) mix dst and // register channels using a constant floating point ratio (0-1)

    jmp = 74, // jump to address (addr)

    cjeq = 75, // jump equal(reg, channel, reg2, channel, addr) cjneq = 76, // jump not equal(reg1, channel, reg2, channel, addr) cjlt = 77, // jump less than (reg1, channel, reg2, channel, addr) cjgt = 78, // jump greater than (reg1, channel, reg2, channel, addr)

    jeqz = 79, // jump equal zero(reg1, channel, addr) jneqz = 80, // jump not equal zero(reg1, channel, addr) jltz = 81, // jump less than zero(reg1, channel, addr) jgtz = 82, // jump greater than zero(reg1, channel, addr)

    jmplong = 83, // jump increment (addr_low,addr_high)

    lut = 85, // lut(reg_uv) point sample from LUT image using 0-1 UV in RG channels + alpha blend clut = 86, // channel lut(channel_dst, reg_uv, channel_src) point sample from LUT image using // 0-1 UV in RG from and to specified channels hlut8 = 87, // horizontal lut(reg_y,channel_y) point sample from LUT image using U = src // RGB(0-255) and V = reg_y (0-255) + alpha blend mixlut = 88, // blend destination with lut(reg_uv) point sample from LUT image using 0-1 UV in // RG channels + alpha blend mixhlut8 = 89, // blend destination with horizontal lut(reg_y,channel_y) point sample from LUT // image using U = src RGB(0-255) and V = reg_y (0-255) + alpha blend

    bezierspline = 91, // bezierspline(reg, channel, reg_packed(rgbargba), reg_value, channel_value)

    halt = 255

    Parameters

    • canvas: Component

      the target canvas

    • canvas_src: Component

      the source canvas

    • canvas_lut: Component

      the Look Up Table canvas (optional)

    • src_x: number

      the source position

    • src_y: number

      the source position

    • src_width: number
    • src_height: number
    • dst_x: number

      the destination position

    • dst_y: number

      the destination position

    • registers: number

      optional 256 real32 registers

    • pixel_code: number

      the 8 bit opcodes to execute

    Returns void

  • stampHsv(sourceCanvas: Component, srcX: number, srcY: number, srcWidth: number, srcHeight: number, dstX: number, dstY: number, hue: number, saturation: number, value: number): void
  • Stamps the source_canvas onto the canvas as an image which has been adjusted with the HSV transform. HSV provides control over the hue (tint), saturation (amount of tint) and value (brightness) giving control of the amount of color in the image

    new Action('Canvas stampHsv', stampHsv)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .addParameter('Canvas2', uActions.ParamType.CANVAS)
    .register();
    function stampHsv(canvas, canvas2) {
    var ctx = uComps.cast.toCanvas(canvas);
    var ctx2 = uComps.cast.toCanvas(canvas2);
    ctx.stampHsv(ctx2._ref, 0, 0, ctx2.pixelWidth, ctx2.pixelHeight, 0, 0, 90, 0.5, 0.5);
    }

    Parameters

    • sourceCanvas: Component

      the source canvas

    • srcX: number

      the source top left

    • srcY: number

      the source top left

    • srcWidth: number

      the source width

    • srcHeight: number

      the source height

    • dstX: number

      the destination position top left

    • dstY: number

      the destination position top left

    • hue: number

      0 no change, (-180 to 180), each 90 plus and minus is a change in RGB hue

    • saturation: number

      0 no change, (-1 to 1) -1 = desaturated, 1 = saturated

    • value: number

      0 no change, (-1 to 1) -1 = dark, 1 = bright

    Returns void

  • stampImage(sourceCanvas: Component, srcX: number, srcY: number, srcWidth: number, srcHeight: number, dstX: number, dstY: number, blendMode: number, sampleMode: boolean): void
  • Combines a target image with a source image with sub pixel accuracy and different blend and sample modes.

    new Action('Canvas stampImage', stampImage)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .addParameter('Canvas2', uActions.ParamType.CANVAS)
    .register();
    function stampImage(canvas, canvas2) {
    var ctx = uComps.cast.toCanvas(canvas);
    var ctx2 = uComps.cast.toCanvas(canvas2);
    ctx.stampImage(ctx2._ref, 0, 0, ctx2.pixelWidth, ctx2.pixelHeight, 0, 0, 1, true);
    }

    Parameters

    • sourceCanvas: Component

      the source canvas

    • srcX: number

      the source top left

    • srcY: number

      the source top left

    • srcWidth: number

      the source width

    • srcHeight: number

      the source height

    • dstX: number

      the destination position top left

    • dstY: number

      the destination position top left

    • blendMode: number

      0 noblend, 1 source - See here for the full set brushBlendMode

    • sampleMode: boolean

      true: default linear, false: nearest neighbor for hard edges images like barcodes

    Returns void

  • stampImageCenter(sourceCanvas: Component, srcX: number, srcY: number, srcWidth: number, srcHeight: number, dstX: number, dstY: number, scaleX: number, scaleY: number, angle: number, blendMode: number, sampleMode: boolean): void
  • Combines a target image with a centered, scaled and rotated source image with sub pixel accuracy and different blend and sample modes.

    new Action('Canvas stampImageCenter', stampImageCenter)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .addParameter('Canvas2', uActions.ParamType.CANVAS)
    .register();
    function stampImageCenter(canvas, canvas2) {
    var ctx = uComps.cast.toCanvas(canvas);
    var ctx2 = uComps.cast.toCanvas(canvas2);
    ctx.stampImageCenter(ctx2._ref, 0, 0, ctx2.pixelWidth, ctx2.pixelHeight, 200, 200, 0.5, 0.5, 90, 1, true);
    }

    Parameters

    • sourceCanvas: Component

      the source canvas

    • srcX: number

      the source top left

    • srcY: number

      the source top left

    • srcWidth: number

      the source width

    • srcHeight: number

      the source height

    • dstX: number

      the destination position top left

    • dstY: number

      the destination position top left

    • scaleX: number

      the scale

    • scaleY: number

      the scale

    • angle: number

      the angle in degrees

    • blendMode: number

      0 noblend, 1 source - See here for the full set brushBlendMode

    • sampleMode: boolean

      true: default linear, false: nearest neighbor for hard edges images like barcodes

    Returns void

  • stampImagePerspective(source_canvas: Component, srcP1x: number, srcP1y: number, srcP2x: number, srcP2y: number, srcP3x: number, srcP3y: number, srcP4x: number, srcP4y: number, dstP1x: number, dstP1y: number, dstP2x: number, dstP2y: number, dstP3x: number, dstP3y: number, dstP4x: number, dstP4y: number, blendmode: number): void
  • Combines a target image with a source image with sub pixel accuracy applying a perspective warp with a affine transformed source quad mapping to an affine transformed destination quad using different blend and sample modes.

    new Action('Canvas stampImagePerspective', stampImagePerspective)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .addParameter('Canvas2', uActions.ParamType.CANVAS)
    .register();
    function stampImagePerspective(canvas, canvas2) {
    var ctx = uComps.cast.toCanvas(canvas);
    var ctx2 = uComps.cast.toCanvas(canvas2);
    ctx.stampImagePerspective(ctx2._ref, 0, 0, 300, 0, 300, 300, 0, 300, 0, 0, 300, 0, 300, 300, 0, 300);
    }

    Parameters

    • source_canvas: Component

      the source canvas

    • srcP1x: number

      the source top left

    • srcP1y: number

      the source top left

    • srcP2x: number

      the source top right

    • srcP2y: number

      the source top right

    • srcP3x: number

      the source bottom right

    • srcP3y: number

      the source bottom right

    • srcP4x: number

      the source bottom left

    • srcP4y: number

      the source bottom left

    • dstP1x: number

      the destination top left

    • dstP1y: number

      the destination top left

    • dstP2x: number

      the destination top right

    • dstP2y: number

      the destination top right

    • dstP3x: number

      the destination bottom right

    • dstP3y: number

      the destination bottom right

    • dstP4x: number

      the destination bottom left

    • dstP4y: number

      the destination bottom left

    • blendmode: number

      0 noblend, 1 source - See here for the full set brushBlendMode

    Returns void

  • stampImageWarp(sourceCanvas: Component, srcX: number, srcY: number, srcWidth: number, srcHeight: number, dstP1x: number, dstP1y: number, dstP2x: number, dstP2y: number, dstP3x: number, dstP3y: number, dstP4x: number, dstP4y: number, blendmode: number): void
  • Combines a target image with a source image with sub pixel accuracy applying an affine warp with different blend and sample modes.

    new Action('Canvas stampImageWarp', stampImageWarp)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .addParameter('Canvas2', uActions.ParamType.CANVAS)
    .register();
    function stampImageWarp(canvas, canvas2) {
    var ctx = uComps.cast.toCanvas(canvas);
    var ctx2 = uComps.cast.toCanvas(canvas2);
    ctx.stampImageWarp(ctx2._ref, 0, 0, ctx2.pixelWidth, ctx2.pixelHeight, 0, 0, 200, 0, 150, 150, 40, 200);
    }

    Parameters

    • sourceCanvas: Component

      the source canvas

    • srcX: number

      the source top left

    • srcY: number

      the source top left

    • srcWidth: number

      the source width

    • srcHeight: number

      the source height

    • dstP1x: number

      the destination top left

    • dstP1y: number

      the destination top left

    • dstP2x: number

      the destination top right

    • dstP2y: number

      the destination top right

    • dstP3x: number

      the destination bottom right

    • dstP3y: number

      the destination bottom right

    • dstP4x: number

      the destination bottom left

    • dstP4y: number

      the destination bottom left

    • blendmode: number

      0 noblend, 1 source - See here for the full set brushBlendMode

    Returns void

  • stampKernel(sourceCanvas: Component, srcX: number, srcY: number, srcWidth: number, srcHeight: number, dstX: number, dstY: number, mode: RgbMode, divider: number, k1: number, k2: number, k3: number, k4: number, k5: number, k6: number, k7: number, k8: number, k9: number): void
  • stampKernel stamps the source_canvas onto the canvas using the 3x3 kernel. This is great for simple blur, edge detect and emboss effects

    None (0, 0, 0, 0, 1, 0, 0, 0, 0) / 1
    Average (1, 1, 1, 1, 1, 1, 1, 1, 1) / 9
    Edge Vertical (-1, 0, 1, -1, 0, 1, -1, 0, 1) / 1
    Edge Horizontal (-1, -1, -1, 0, 0, 0, 1, 1, 1) / 1
    Sobel Horizontal (-1, 0, 1, -2, 0, 2, -1, 0, 1) / 1
    lap edge ( 0, -1, 0, -1, 4, -1, 0, -1, 0 ) / 1
    lap sharpen (0, -1, 0, -1, 5, -1, 0, -1, 0) / 1
    emboss (-2, -1, 0, -1, 1, 1, 0, 1, 2) / 1
    gaussian (1, 2, 1, 2, 4, 2, 1, 2, 1) / 16
    new Action('Canvas stampKernel', stampKernel)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .addParameter('Canvas2', uActions.ParamType.CANVAS)
    .register();
    function stampKernel(canvas, canvas2) {
    var ctx = uComps.cast.toCanvas(canvas);
    var ctx2 = uComps.cast.toCanvas(canvas2);
    ctx.stampKernel(ctx2._ref, 0, 0, ctx2.pixelWidth, ctx2.pixelHeight, 0, 0, 1, 2, 3);
    }

    Parameters

    • sourceCanvas: Component

      the source canvas

    • srcX: number

      the source top left

    • srcY: number

      the source top left

    • srcWidth: number

      the source width

    • srcHeight: number

      the source height

    • dstX: number

      the destination position top left

    • dstY: number

      the destination position top left

    • mode: RgbMode

      uComps.RgbMode: 0 - RGB, 1 - RGB_COPY (copy A from source), 2 - RGB_COPY_NON_ZERO (copy A from source where not zero), 3 - RED_ONLY, 4 - GREEN_ONLY, 5 - BLUE_ONLY, 6 - ALPHA_ONLY

    • divider: number

      sum all of the 3x3 kernel then divide by this value

    • k1: number

      k1 to k9: the three by three kernel

    • k2: number
    • k3: number
    • k4: number
    • k5: number
    • k6: number
    • k7: number
    • k8: number
    • k9: number

    Returns void

  • stampLighting(sourceCanvas: Component, srcX: number, srcY: number, srcWidth: number, srcHeight: number, dstX: number, dstY: number, brightness: number, contrast: number, shadow: number, fill: number, highlight: number, temperature: number): void
  • Stamps a source canvas region onto the target canvas while applying lighting

    new Action('Canvas stampLighting', stampLighting)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .addParameter('Canvas2', uActions.ParamType.CANVAS)
    .register();
    function stampLighting(canvas, canvas2) {
    var ctx = uComps.cast.toCanvas(canvas);
    var ctx2 = uComps.cast.toCanvas(canvas2);
    ctx.stampLighting(ctx2._ref, 0, 0, ctx2.pixelWidth, ctx2.pixelHeight, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5);
    }

    Parameters

    • sourceCanvas: Component

      the source canvas

    • srcX: number

      the source top left

    • srcY: number

      the source top left

    • srcWidth: number

      the source width

    • srcHeight: number

      the source height

    • dstX: number

      the destination position top left

    • dstY: number

      the destination position top left

    • brightness: number

      (-1 to 1) -1 = black, 0 = unmodified, 1 = white

    • contrast: number

      (-1 to 1) -1 = no contrast / grey, 0 = unmodified, 1 = high contrast

    • shadow: number

      (-1 to 1) -1 = shadow reduced, 0 = unmodified, 1 = shadow increased

    • fill: number

      (-1 to 1) -1 = fill light reduced, 0 = unmodified, 1 = fill light increased

    • highlight: number

      (-1 to 1) -1 = highlights reduced / grey, 0 = unmodified, 1 = highlights increased

    • temperature: number

      (-1 to 1) -1 = warm / red, 0 = unmodified, 1 = cool / blue

    Returns void

  • stampMinMaxBlend(sourceCanvas: Component, srcX: number, srcY: number, srcWidth: number, srcHeight: number, dstX: number, dstY: number, minBlend: number, maxBlend: number): void
  • Combines a target image with a source image. This is a minimum and maximum clamp in pixel brighness where the least extreme of the two images is sampled from. It assumes both images are of the same scene but with different lighting. This is designed to take multiple photos of the same subject and remove extremes like shadow and bright reflections.

    Parameters

    • sourceCanvas: Component

      the source canvas

    • srcX: number

      the source top left

    • srcY: number

      the source top left

    • srcWidth: number

      the source width

    • srcHeight: number

      the source height

    • dstX: number

      the destination position top left

    • dstY: number

      the destination position top left

    • minBlend: number

      (0 to 255) if one of the pixels is < min_blend brightness then take the brightst (remove the extreme e.g. unlit shadow areas)

    • maxBlend: number

      (0 to 255) if one of the pixels is > max_blend brightness then take the darkest (remove extreme e.g. glare)

    Returns void

  • stampMorphological(sourceCanvas: Component, srcX: number, srcY: number, srcWidth: number, srcHeight: number, dstX: number, dstY: number, mode: RgbMode, operator: Operator, threshold: number): void
  • Stamps the source_canvas onto the canvas using a morphological operator. Normally these are used for binary images, but these operators are designed to work on RGB images. These operations are designed to be stacked, bounced from one buffer to another. For example the common open operation is dilate followed by erode. Meanwhile close is erode followed by dilate.

    Parameters

    • sourceCanvas: Component

      the source canvas

    • srcX: number

      the source top left

    • srcY: number

      the source top left

    • srcWidth: number

      the source width

    • srcHeight: number

      the source height

    • dstX: number

      the destination position top left

    • dstY: number

      the destination position top left

    • mode: RgbMode

      uComps.RgbMode: 0 - RGB, 1 - RGB_COPY (copy A from source), 2 - RGB_COPY_NON_ZERO (copy A from source where not zero), 3 - RED_ONLY, 4 - GREEN_ONLY, 5 - BLUE_ONLY, 6 - ALPHA_ONLY

    • operator: Operator

      uComps.Operator: 0 - THRESEHOLD, 1 ERODE, 2 - DILATE, 3 - GRADIENT, 4 - BOUNDARY, 5 - HIT_AND_MISS_CORNER, 6 - THINNING

    • threshold: number

      Normally 128. Below this is black, above this is white.

    Returns void

  • stampQr(code: string, x: number, y: number, angle: number, size: number, codeLevel: number): void
  • stampQr creates and stamps a QRcode onto the canvas.

    new Action('Canvas stampQr', stampQr)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function stampQr(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.stampQr("QRCODE",250,250,0,19,1);
    }

    Parameters

    • code: string

      the text/numbers to encode

    • x: number

      the position

    • y: number

      the position

    • angle: number

      the angle in degrees

    • size: number

      size from 1-100 pixels per bar

    • codeLevel: number

      Redundancy level, 1=low, 4=medium, 7=high

    Returns void

  • stampSvg(x: number, y: number, xratio: number, yratio: number, degrees: number): void
  • Stamps an SVG that had been preloaded with prepareSvg onto the canvas.

    new Action('Canvas stampSvg', stampSvg)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function stampSvg(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.stampSvg(0, 0, 0.5, 0.5, 90);
    }

    Parameters

    • x: number

      the x position for the top left of where to put the svg.

    • y: number

      the y position for the top left of where to put the svg.

    • xratio: number

      the scale width (1 = no scaling)

    • yratio: number

      the scale height (1 = no scaling)

    • degrees: number

    Returns void

  • stroke(): void
  • Draws the current path. Before you can use this you need to begin a path and specify the instructions and settings you want. The path will then be drawn using the instructions and the settings you have given it. Some aspects may be affected by settings that persited from a previously drawn path and might have to be reset. In the below example when you call stroke() it will draw the rectangle that was added to the path using the lineWidth setting specified. Note the lineWidth and strokeStyle can be set after the rect() call. This is because the order does not matter since it is all processed when stroke() is called.

    new Action('Canvas stroke', stroke)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function stroke(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.rect(100,100,300,300);
    ctx.lineWidth = 4;
    ctx.strokeStyle = '#FF0000'
    ctx.stroke();
    }

    Returns void

  • strokeEllipse(x: number, y: number, radiusX: number, radiusY: number, steps: number): void
  • Outline of an ellipse at the x/y position with radiuses radiusX, radiusY added to the path

    //draw a magenta elipse as an outline
    ctx.beginPath();
    ctx.strokeEllipse(200, 200, 80, 100, 50);
    ctx.strokeStyle = '#FF22FF';

    Parameters

    • x: number

      ellipse position

    • y: number

      ellipse position

    • radiusX: number

      x radius

    • radiusY: number

      y radius (a circle if both radius values are the same)

    • steps: number

      a larger number produces a smoother ellipse

    Returns void

  • strokeRect(x: number, y: number, width: number, height: number): void
  • A rectangle outline is added to the path

    new Action('Canvas strokeRect', strokeRect)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function strokeRect(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.strokeStyle = '#FF0000'
    ctx.strokeRect(200, 200, 100, 100);
    }

    Parameters

    • x: number
    • y: number
    • width: number

      width of the rectangle

    • height: number

      height of the rectangle

    Returns void

  • strokeRoundedRect(x: number, y: number, width: number, height: number, radius: number): void
  • An outlined rectangle with curved corners is added to the path

    new Action('Canvas strokeRoundedRect', strokeRoundedRect)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function strokeRoundedRect(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.strokeStyle = '#FF0000';
    ctx.strokeRoundedRect(200, 200, 100, 100, 30);
    }

    Parameters

    • x: number
    • y: number
    • width: number

      width of the rectangle

    • height: number

      height of the rectangle

    • radius: number

      curvature of the corners

    Returns void

  • strokeText(text: string, x: number, y: number, maxWidth?: number): void
  • The specified text is drawn as an outline with the current brush

    var ctx = uComps.cast.toCanvas(canvas);
    ctx.font = "40px vera"
    ctx.strokeStyle = "#000033"
    ctx.strokeText("Outline",10,500,1000)

    Parameters

    • text: string

      the text to render

    • x: number

      the position of the top left corner

    • y: number

      the position of the top left corner

    • Optional maxWidth: number

    Returns void

  • textWidth(text: string): number
  • The specified text width is calculated without rendering. Note: This should be used instead of measureText(text).width This is the same as the original JS system function CalculateTextWidth()

    var ctx = uComps.cast.toCanvas(canvas);
    ctx.font = "30px vera";
    var txt = "A small sentence"
    ctx.fillText("width:" + ctx.textWidth(txt), 10, 50)
    ctx.fillText(txt, 10, 100);

    Parameters

    • text: string

      the text to measure

    Returns number

    width - the width of the text if it was rendered

  • toTexture(outputTexture: any): void
  • Sends the canvas image to the texture target on the GPU for 3D rendering. You cannot access a texture object and so cannot use canvasToTexture without using a Renderkit. There is no simple example we can show here as it requires using a renderkit to get a texture to pass into this method.

    Parameters

    • outputTexture: any

      the target texture

    Returns void

  • transform(horScaling: number, vertSkewing: number, horSkewing: number, vertScaling: number, horTranslation: number, vertTranslation: number): void
  • Scales, rotates, and skews the current canvas context. Use resetTransform to reset the current transform.

    new Action('Canvas transform', transform)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function transform(canvas) {
    //first rectangle will be skewed.
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.transform(0.7, 1.3, 0.7, 1.3, 200, 200);
    ctx.strokeStyle = '#FF0000';
    ctx.lineWidth = 4;
    ctx.rect(50, 50, 100, 100);
    ctx.stroke();
    //This removes the transform change for the next operation.
    ctx.resetTransform();
    }

    Parameters

    • horScaling: number

      horizontal scaling (1 = normal scale, 0.5 = half scale, 2.0 = double scale)

    • vertSkewing: number

      vertical skewing (0 = normal, negative values skew upwards )

    • horSkewing: number

      horizontal skewing (0 = normal, negative values skew left )

    • vertScaling: number

      vertical scaling (1 = normal scale, 0.5 = half scale, 2.0 = double scale)

    • horTranslation: number

      horizontal translation (0 = no change, translation in the x direction )

    • vertTranslation: number

      vertical translation (0 = no change, translation in the y direction )

    Returns void

  • translate(x: number, y: number): void
  • Updates the transform translation for the current path - this will be applied during fill or stroke

    new Action('Canvas translate', translate)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function translate(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.translate(300, 300);
    ctx.rect(100,100,300,300);
    ctx.lineWidth = 4;
    ctx.strokeStyle = '#FF0000'
    ctx.stroke();
    //This removes the translate change for the next operation.
    ctx.resetTransform();
    }

    Parameters

    • x: number

      the translation in the x direction (can be negative or positive)

    • y: number

      the translation in the y direction (can be negative or positive)

    Returns void

  • tween(seconds: number, params: Object, type?: TweenType): void
  • Tween a component property over time.

    Parameters

    • seconds: number

      The number of seconds to tween for.

    • params: Object

      An objects that contains key/value pairs of the properties and values to tween.

    • Optional type: TweenType

      (Optional) Tween type can be any type from "Tween.type"

    Returns void

  • wedge(x: number, y: number, radiusX: number, radiusY: number, startAngle: number, endAngle: number, thickness: number): void
  • A part of an ellipse with an inner and outer radius, and starting and ending angle is added to the path

    new Action('Canvas wedge', wedge)
    .addParameter('Canvas', uActions.ParamType.CANVAS)
    .register();
    function wedge(canvas) {
    var ctx = uComps.cast.toCanvas(canvas);
    ctx.beginPath();
    ctx.wedge(350, 300, 200, 200, 0, 0.78, 5);
    ctx.lineWidth = 4;
    ctx.strokeStyle = '#FF0000'
    ctx.stroke();
    }

    Parameters

    • x: number

      center of the wedge

    • y: number

      center of the wedge

    • radiusX: number

      x radius

    • radiusY: number

      y radius (a circle if both radius values are the same)

    • startAngle: number

      start angle in radians

    • endAngle: number

      end angle in radians

    • thickness: number

      0 is no thickness (just follows the circumference), 1 is a wedge right to the center of the ellipse, 0.5 is halfway

    Returns void