Skip to main content
Version: 4.1

Canvas

Namespace: Wisej.Web

Assembly: Wisej.Framework (4.1.0.0)

Represents a drawing surface for immediate-mode 2D rendering, similar to the HTML5 Canvas API.

public class Canvas : Control, IWisejHandler

Important: Drawing calls made on the Canvas are executed immediately on the client, but they are not persisted. When the canvas is refreshed (for example, due to resizing or other UI updates), all previous drawing operations are lost. To maintain the visual state of the canvas, you must handle the Redraw event and re-issue all drawing commands that represent the desired final state of the canvas.

Example:


// Example usage of the Canvas control
var canvas = new Wisej.Web.Canvas();
canvas.Redraw += (s, e) =>
{
// Always draw the current state in the Redraw event.
canvas.FillStyle = Color.Red;
canvas.FillRect(10, 10, 100, 100);
canvas.StrokeStyle = Color.Black;
canvas.StrokeRect(10, 10, 100, 100);
};

Constructors

Instance member Canvas()

Initializes a new instance of the Canvas class.

Properties

Instance member BorderStyle

BorderStyle: Indicates the border style for the control. (Default: None)

Instance member FillStyle

Object: Sets or returns the color, gradient, or pattern used to fill the drawing. (Default: null)

Instance member GlobalAlpha

Single: Sets or returns the current alpha or transparency value of the drawing. (Default: 1)

Instance member GlobalCompositeOperation

CanvasCompositeOperation: Sets or returns how a new image are drawn onto an existing image. (Default: SourceOver)

Instance member LineCap

CanvasLineCap: Sets or returns the style of the end caps for a line. (Default: Butt)

Instance member LineDashOffset

Single: Sets or returns the type of corner created, when two lines meet. (Default: 0)

Instance member LineJoin

CanvasLineJoin: Sets or returns the type of corner created, when two lines meet. (Default: Miter)

Instance member LineWidth

Int32: Sets or returns the current line width, in pixels. (Default: 1)

Instance member LiveUpdate

Boolean: Turns live update mode on or off. When live update is on, all canvas calls are pushed to the client immediately, when is off (default) the control updates the client at the end of the request. (Default: False)

Instance member MiterLimit

Int32: Sets or returns the maximum miter length. (Default: 10)

Instance member ShadowBlur

Int32: Sets or returns the blur level for shadows. (Default: 0)

Instance member ShadowColor

Color: Sets or returns the color to use for shadows.

The default value is Color.Empty

Instance member ShadowOffsetX

Int32: Sets or returns the horizontal distance of the shadow from the shape. (Default: 0)

Instance member ShadowOffsetY

Int32: Sets or returns the vertical distance of the shadow from the shape. (Default: 0)

Instance member StrokeStyle

Object: Sets or returns the color, gradient, or pattern used for strokes. (Default: null)

Instance member TextAlign

CanvasTextAlign: Sets or returns the current alignment for text content, according to the anchor point. (Default: Start)

Instance member TextBaseline

CanvasTextBaseline: Sets or returns the current text baseline used when drawing text. (Default: Alphabetic)

Instance member TextFont

Font: Sets or returns the current font properties for text content on the canvas. (Default: null)

Methods

Instance member Arc(x, y, radius, startAngle, endAngle, counterClockwise)

Creates an arc/curve (used to create circles, or parts of circles).

ParameterTypeDescription
xInt32The x-coordinate of the center of the circle.
yInt32The y-coordinate of the center of the circle.
radiusInt32The radius of the circle.
startAngleSingleThe starting angle, in degrees.
endAngleSingleThe ending angle, in degrees.
counterClockwise optionalBooleanSpecifies whether the drawing should be counterclockwise or clockwise. Default is false.

Instance member Arc(location, radius, startAngle, endAngle, counterClockwise)

Creates an arc/curve (used to create circles, or parts of circles).

ParameterTypeDescription
locationPointThe x-coordinate and y-coordinate of the center of the circle.
radiusInt32The radius of the circle.
startAngleSingleThe starting angle, in degrees.
endAngleSingleThe ending angle, in degrees.
counterClockwise optionalBooleanSpecifies whether the drawing should be counterclockwise or clockwise. Default is false.

Instance member ArcTo(x1, y1, x2, y2, radius)

Creates an arc/curve between two tangents on the canvas.

ParameterTypeDescription
x1Int32The x-coordinate of the first tangent.
y1Int32The y-coordinate of the first tangent.
x2Int32The x-coordinate of the second tangent.
y2Int32The y-coordinate of the second tangent.
radiusInt32The radius of the arc

Instance member ArcTo(location1, location2, radius)

Creates an arc/curve between two tangents on the canvas.

ParameterTypeDescription
location1PointThe x-coordinate and y-coordinate of the first tangent.
location2PointThe x-coordinate and y-coordinate of the second tangent.
radiusInt32The radius of the arc

Instance member BeginPath()

begins a path, or resets the current path.

Instance member BezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)

Adds a point to the current path by using the specified control points that represent a cubic Bézier curve.

ParameterTypeDescription
cp1xInt32The x-coordinate of the first Bézier control point.
cp1yInt32The y-coordinate of the first Bézier control point.
cp2xInt32The x-coordinate of the second Bézier control point.
cp2yInt32The y-coordinate of the second Bézier control point.
xInt32The x-coordinate of the ending point.
yInt32The y-coordinate of the ending point.

A cubic bezier curve requires three points. The first two points are control points that are used in the cubic Bézier calculation and the last point is the ending point for the curve. The starting point for the curve is the last point in the current path. If a path does not exist, use the beginPath() and moveTo() methods to define a starting point.

Instance member ClearRect(rect)

Clears the specified pixels within a given rectangle.

ParameterTypeDescription
rectRectangleRectangle's coordinates and size.

Instance member ClearRect(x, y, width, height)

Clears the specified pixels within a given rectangle.

ParameterTypeDescription
xInt32The x-coordinate of the upper-left corner of the rectangle.
yInt32The y-coordinate of the upper-left corner of the rectangle.
widthInt32The width of the rectangle, in pixels.
heightInt32The height of the rectangle, in pixels.

Instance member Clip()

Clips a region of any shape and size from the original canvas.

Once a region is clipped, all future drawing will be limited to the clipped region (no access to other regions on the canvas). You can however save the current canvas region using the save() method before using the clip() method, and restore it (with the restore() method) any time in the future.

Instance member ClosePath()

Creates a path from the current point back to the starting point.

Instance member CreateLinearGradient(x0, y0, x1, y1, colorStops)

Creates a linear gradient object. The gradient can be used to fill rectangles, circles, lines, text, etc.

ParameterTypeDescription
x0Int32The x-coordinate of the start point of the gradient.
y0Int32The y-coordinate of the start point of the gradient.
x1Int32The x-coordinate of the end point of the gradient.
y1Int32The y-coordinate of the end point of the gradient.
colorStopsObject[]Array of color stops. Each element must define a stop value between 0.0 and 1.0 and a color value.

Returns: Object.

Instance member CreatePattern(image, repeat)

Repeats the specified element in the specified direction. The repeated element can be used to draw/fill rectangles, circles, lines etc.

ParameterTypeDescription
imageImageSpecifies the image of the pattern to use.
repeatCanvasRepeatPatternSpecifies the repeat pattern.

Returns: Object.

Instance member CreatePattern(imageSource, repeat)

Repeats the specified element in the specified direction. The repeated element can be used to draw/fill rectangles, circles, lines etc.

ParameterTypeDescription
imageSourceStringSpecifies the source of the image of the pattern to use.
repeatCanvasRepeatPatternSpecifies the repeat pattern.

Returns: Object.

Instance member CreateRadialGradient(x0, y0, r0, x1, y1, r1, colorStops)

Creates a linear gradient object. The gradient can be used to fill rectangles, circles, lines, text, etc.

ParameterTypeDescription
x0Int32The x-coordinate of the start point of the gradient.
y0Int32The y-coordinate of the start point of the gradient.
r0SingleThe radius of the starting circle.
x1Int32The x-coordinate of the end point of the gradient.
y1Int32The y-coordinate of the end point of the gradient.
r1SingleThe radius of the ending circle.
colorStopsObject[]Array of color stops. Each element must define a stop value between 0.0 and 1.0 and a color value.

Returns: Object.

Instance member DrawImage(image, clipRect, imageRect)

Draws an image onto the canvas.

ParameterTypeDescription
imageImageSpecifies the image to draw.
clipRectRectangleThe clip rectangle.
imageRectRectangleThe size and location of the image to place on the canvas.

Instance member DrawImage(imageSource, x, y)

Draws an image onto the canvas.

ParameterTypeDescription
imageSourceStringSpecifies the image to draw.
xInt32The x-coordinate where to place the image on the canvas.
yInt32The y-coordinate where to place the image on the canvas.

Instance member DrawImage(imageSource, location)

Draws an image onto the canvas.

ParameterTypeDescription
imageSourceStringSpecifies the image to draw.
locationPointThe x-coordinate and y-coordinate where to place the image on the canvas.

Instance member DrawImage(imageSource, x, y, width, height)

Draws an image onto the canvas.

ParameterTypeDescription
imageSourceStringSpecifies the image to draw.
xInt32The x coordinate where to place the image on the canvas.
yInt32The y coordinate where to place the image on the canvas.
widthInt32The width of the image to use (stretch or reduce the image).
heightInt32The height of the image to use (stretch or reduce the image).

Instance member DrawImage(imageSource, rect)

Draws an image onto the canvas.

ParameterTypeDescription
imageSourceStringSpecifies the image to draw.
rectRectangleThe coordinates and size of the image to place on the canvas.

Instance member DrawImage(imageSource, clipX, clipY, clipWidth, clipHeight, x, y, width, height)

Draws an image onto the canvas.

ParameterTypeDescription
imageSourceStringSpecifies the image to draw.
clipXInt32The x coordinate where to start clipping.
clipYInt32The y coordinate where to start clipping.
clipWidthInt32The width of the clipped image.
clipHeightInt32The height of the clipped image.
xInt32The x coordinate where to place the image on the canvas.
yInt32The y coordinate where to place the image on the canvas.
widthInt32The width of the image to use (stretch or reduce the image).
heightInt32The height of the image to use (stretch or reduce the image).

Instance member DrawImage(imageSource, clipRect, imageRect)

Draws an image onto the canvas.

ParameterTypeDescription
imageSourceStringSpecifies the image to draw.
clipRectRectangleThe clip rectangle.
imageRectRectangleThe size and location of the image to place on the canvas.

Instance member DrawImage(image, x, y)

Draws an image onto the canvas.

ParameterTypeDescription
imageImageSpecifies the image to draw.
xInt32The x-coordinate where to place the image on the canvas.
yInt32The y-coordinate where to place the image on the canvas.

Instance member DrawImage(image, location)

Draws an image onto the canvas.

ParameterTypeDescription
imageImageSpecifies the image to draw.
locationPointThe x-coordinate and y-coordinate where to place the image on the canvas.

Instance member DrawImage(image, x, y, width, height)

Draws an image onto the canvas.

ParameterTypeDescription
imageImageSpecifies the image to draw.
xInt32The x-coordinate where to place the image on the canvas.
yInt32The y-coordinate where to place the image on the canvas.
widthInt32The width of the image to use (stretch or reduce the image).
heightInt32The height of the image to use (stretch or reduce the image).

Instance member DrawImage(image, rect)

Draws an image onto the canvas.

ParameterTypeDescription
imageImageSpecifies the image to draw.
rectRectangleThe coordinates and size of the image to place on the canvas.

Instance member DrawImage(image, clipX, clipY, clipWidth, clipHeight, x, y, width, height)

Draws an image onto the canvas.

ParameterTypeDescription
imageImageSpecifies the image to draw.
clipXInt32The x-coordinate where to start clipping.
clipYInt32The y-coordinate where to start clipping.
clipWidthInt32The width of the clipped image.
clipHeightInt32The height of the clipped image.
xInt32The x coordinate where to place the image on the canvas.
yInt32The y coordinate where to place the image on the canvas.
widthInt32The width of the image to use (stretch or reduce the image).
heightInt32The height of the image to use (stretch or reduce the image).

Instance member Fill()

Fills the current drawing (path.)

Instance member FillRect(rect)

Draws a "filled" rectangle. The default color of the fill is black.

ParameterTypeDescription
rectRectangleRectangle's coordinates and size.

Instance member FillRect(x, y, width, height)

Draws a "filled" rectangle. The default color of the fill is black.

ParameterTypeDescription
xInt32The x-coordinate of the upper-left corner of the rectangle.
yInt32The y-coordinate of the upper-left corner of the rectangle.
widthInt32The width of the rectangle, in pixels.
heightInt32The height of the rectangle, in pixels.

Instance member FillText(text, x, y)

Draws filled text on the canvas. The default color of the text is black.

ParameterTypeDescription
textStringSpecifies the text that will be written on the canvas.
xInt32The x-coordinate where to start painting the text (relative to the canvas).
yInt32The y-coordinate where to start painting the text (relative to the canvas)

Instance member FillText(text, location)

Draws filled text on the canvas. The default color of the text is black.

ParameterTypeDescription
textStringSpecifies the text that will be written on the canvas.
locationPointThe x-coordinate and y-coordinate where to start painting the text (relative to the canvas).

Instance member GetLineDash()

Returns the current line dash pattern.

Returns: Int32[]. A list of numbers that specifies distances to alternately draw a line and a gap (in coordinate space units).

Instance member LineTo(x, y)

Adds a new point and creates a line TO that point FROM the last specified point in the canvas (this method does not draw the line).

ParameterTypeDescription
xInt32The x-coordinate of where to create the line to.
yInt32The y-coordinate of where to create the line to.

Instance member LineTo(location)

Adds a new point and creates a line TO that point FROM the last specified point in the canvas (this method does not draw the line).

ParameterTypeDescription
locationPointThe x-coordinate and y-coordinate of where to create the line to.

Instance member MoveTo(x, y)

Moves the path to the specified point in the canvas, without creating a line.

ParameterTypeDescription
xInt32The x-coordinate of where to move the path to.
yInt32The y-coordinate of where to move the path to.

Use the stroke() method to actually draw the path on the canvas.

Instance member MoveTo(location)

Moves the path to the specified point in the canvas, without creating a line.

ParameterTypeDescription
locationPointThe x-coordinate and y-coordinate of where to move the path to.

Use the stroke() method to actually draw the path on the canvas.

Protected member OnCreateControl()

Creates and register this control and all its children.

Protected member OnRedraw(e)

Fires the Redraw event.

ParameterTypeDescription
eEventArgsAn EventArgs that contains the event data.

Protected member OnWebRender(config)

Renders the client component.

ParameterTypeDescription
configObjectDynamic configuration object.

Instance member QuadraticCurveTo(cpx, cpy, x, y)

Adds a point to the current path by using the specified control points that represent a quadratic Bézier curve.

ParameterTypeDescription
cpxInt32The x-coordinate of the Bézier control point.
cpyInt32The y-coordinate of the Bézier control point.
xInt32The x-coordinate of the ending point.
yInt32The y-coordinate of the ending point.

A quadratic Bézier curve requires two points. The first point is a control point that is used in the quadratic Bézier calculation and the second point is the ending point for the curve. The starting point for the curve is the last point in the current path. If a path does not exist, use the beginPath() and moveTo() methods to define a starting point.

Instance member Rect(rect)

Creates a rectangle.

ParameterTypeDescription
rectRectangleRectangle's coordinates and size.

Instance member Rect(x, y, width, height)

Creates a rectangle.

ParameterTypeDescription
xInt32The x-coordinate of the upper-left corner of the rectangle.
yInt32The y-coordinate of the upper-left corner of the rectangle.
widthInt32The width of the rectangle, in pixels.
heightInt32The height of the rectangle, in pixels.

Instance member Restore()

Returns previously saved path state and attributes.

Instance member Rotate(degrees)

Method rotates the current drawing.

ParameterTypeDescription
degreesSingleThe rotation angle, in degrees.

The rotation will only affect drawings made AFTER the rotation is done.

Instance member Save()

Saves the state of the current context.

Instance member Scale(scaleWidth, scaleHeight)

Scales the current drawing, bigger or smaller.

ParameterTypeDescription
scaleWidthSingleScales the width of the current drawing (1=100%, 0.5=50%, 2=200%, etc.)
scaleHeightSingleScales the height of the current drawing (1=100%, 0.5=50%, 2=200%, etc.)

Instance member Scale(scale)

Scales the current drawing, bigger or smaller.

ParameterTypeDescription
scaleSizeFScales the width of the current drawing (1=100%, 0.5=50%, 2=200%, etc.)

Instance member SetLineDash(segments)

Sets the line dash pattern.

ParameterTypeDescription
segmentsInt32[]A list of numbers that specifies distances to alternately draw a line and a gap (in coordinate space units). If the number of elements in the array is odd, the elements of the array get copied and concatenated. For example, [5, 15, 25] will become [5, 15, 25, 5, 15, 25].

Instance member SetTransform(scaleWidth, skewHoriz, skewVert, scaleHeight, x, y)

Resets the current transform to the identity matrix, and then runs transform() with the same arguments.

ParameterTypeDescription
scaleWidthSingleScales the drawing horizontally.
skewHorizSingleSkew the drawing horizontally.
skewVertSingleSkew the drawing vertically.
scaleHeightSingleScales the drawing vertically.
xInt32Moves the drawing horizontally
yInt32Moves the drawing vertically.

The transformation will only affect drawings made after the transform() method is called. The transform() method behaves relatively to other transformations made by rotate(), scale(), translate(), or transform(). Example: If you already have set your drawing to scale by two, and the transform() method scales your drawings by two, your drawings will now scale by four.

Instance member Stroke()

Draws the path you have defined.

Instance member StrokeRect(rect)

Draws a rectangle (no fill). The default color of the stroke is black.

ParameterTypeDescription
rectRectangleRectangle's coordinates and size.

Instance member StrokeRect(x, y, width, height)

Draws a rectangle (no fill). The default color of the stroke is black.

ParameterTypeDescription
xInt32The x-coordinate of the upper-left corner of the rectangle.
yInt32The y-coordinate of the upper-left corner of the rectangle.
widthInt32The width of the rectangle, in pixels.
heightInt32The height of the rectangle, in pixels.

Instance member StrokeText(text, x, y)

Draws text (with no fill) on the canvas. The default color of the text is black.

ParameterTypeDescription
textStringSpecifies the text that will be written on the canvas.
xInt32The x-coordinate where to start painting the text (relative to the canvas).
yInt32The y-coordinate where to start painting the text (relative to the canvas)

Instance member StrokeText(text, location)

Draws text (with no fill) on the canvas. The default color of the text is black.

ParameterTypeDescription
textStringSpecifies the text that will be written on the canvas.
locationPointThe x-coordinate and y-coordinate where to start painting the text (relative to the canvas).

Instance member Transform(scaleWidth, skewHoriz, skewVert, scaleHeight, x, y)

Replaces the current transformation matrix.

ParameterTypeDescription
scaleWidthSingleScales the drawing horizontally.
skewHorizSingleSkew the drawing horizontally.
skewVertSingleSkew the drawing vertically.
scaleHeightSingleScales the drawing vertically.
xInt32Moves the drawing horizontally
yInt32Moves the drawing vertically.

The transformation will only affect drawings made after the transform() method is called. The transform() method behaves relatively to other transformations made by rotate(), scale(), translate(), or transform(). Example: If you already have set your drawing to scale by two, and the transform() method scales your drawings by two, your drawings will now scale by four.

Instance member Translate(x, y)

Remaps the (0,0) position on the canvas.

ParameterTypeDescription
xInt32The value to add to horizontal (x) coordinates.
yInt32The value to add to vertical (y) coordinates.

When you call a method such as fillRect() after translate(), the value is added to the x- and y-coordinate values.

Instance member Translate(location)

Remaps the (0,0) position on the canvas.

ParameterTypeDescription
locationPointThe value to add to horizontal (x) and vertical (y) coordinates.

When you call a method such as fillRect() after translate(), the value is added to the x- and y-coordinate values.

Events

Instance member Redraw

EventHandler Fired when the canvas requires a full redraw.

Implements

NameDescription
IUserDataProvides access to the UserData and Tag properties associated to the component implementing this interface.
IBindableComponentBindable components implement this interface.
IDropTargetControls that support drag & drop operations implement this interface.
IWisejComponentAll wisej components implement this interface.
IWisejControlAll wisej controls derived from the Control class must implement this interface.
IWisejHandlerRepresents a Wisej component that is capable of handling postback requests from the client.
IWisejSerializableAllows an object to serialize itself.