Skip to main content

Canvas

touchgfx/widgets/canvas/Canvas.hpp

Class for easy rendering using CanvasWidgetRenderer. The Canvas class will make implementation of a new CanvasWidget very easy. The few simple primitives allows moving a "pen" and drawing the outline of a shape which can then be rendered.

The Canvas class has been optimized to eliminate drawing unnecessary lines outside the currently invalidated rectangle.

Public Functions

Canvas(const CanvasWidget * _widget, const Rect & invalidatedArea)
Canvas Constructor.
boolclose()
Closes the current shape so that the inside can be filled using a Painter.
voidcubicBezierTo(float x0, float y0, float x1, float y1, float x2, float y2, float x, float y)
Draw a Cubic Bezier curve via x1,y1 and x2,y2 to x3,y3.
Rasterizer::FillingRulegetFillingRule() const
Gets the filling rule being used when rendering the outline.
virtual voidlineTo(CWRUtil::Q5 x, CWRUtil::Q5 y)
Draw line from the current (x, y) to the new (x, y) as part of the shape being drawn.
template <typename T >
void
lineTo(T x, T y)
Draw line from the current (x, y) to the new (x, y) as part of the shape being drawn.
voidmoveTo(CWRUtil::Q5 x, CWRUtil::Q5 y)
Move the current pen position to (x, y).
template <typename T >
void
moveTo(T x, T y)
Move the current pen position to (x, y).
voidquadraticBezierTo(float x0, float y0, const float x1, const float y1, const float x, const float y)
Draw a Quadratic Bezier curve via x1,y1 to x2,y2.
boolrender(uint8_t customAlpha =255)
Render the graphical shape drawn using moveTo() and lineTo() with the given Painter.
voidsetFillingRule(Rasterizer::FillingRule rule)
Sets the filling rule to be used when rendering the outline.
FORCE_INLINE_FUNCTION boolwasOutlineTooComplex()
Determines if we the outline was too complex to draw completely.
virtual ~Canvas()
Finalizes an instance of the Canvas class.

Public Functions Documentation

Canvas

Canvas(const CanvasWidget *_widget ,
const Rect &invalidatedArea
)

Canvas Constructor.

Locks the framebuffer and prepares for drawing only in the allowed area which has been invalidated. The color depth of the LCD is taken into account.

Parameters:
_widgeta pointer to the CanvasWidget using this Canvas. Used for getting the canvas dimensions.
invalidatedAreathe are which should be updated.
Note

Locks the framebuffer.

close

bool close()

Closes the current shape so that the inside can be filled using a Painter.

Returns:

True if there is enough memory to calculate the shape outline, false if there is too little memory.

cubicBezierTo

void cubicBezierTo(floatx0 ,
floaty0 ,
floatx1 ,
floaty1 ,
floatx2 ,
floaty2 ,
floatx ,
floaty
)

Draw a Cubic Bezier curve via x1,y1 and x2,y2 to x3,y3.

Parameters:
x0The start x coordinate.
y0The start y coordinate.
x1The first 'via' x coordinate.
y1The first 'via' y coordinate.
x2The second 'via' x coordinate.
y2The second 'via' y coordinate.
xThe end x coordinate.
yThe end y coordinate.

getFillingRule

Rasterizer::FillingRule getFillingRule()const

Gets the filling rule being used when rendering the outline.

Returns:

The filling rule.

See also:

lineTo

virtual void lineTo(CWRUtil::Q5x ,
CWRUtil::Q5y
)

Draw line from the current (x, y) to the new (x, y) as part of the shape being drawn.

As for moveTo, lineTo commands completely outside the drawing are are discarded.

Parameters:
xThe x coordinate for the pen position in CWRUtil::Q5 format.
yThe y coordinate for the pen position in CWRUtil::Q5 format.
See also:

lineTo

void lineTo(Tx ,
Ty
)

Draw line from the current (x, y) to the new (x, y) as part of the shape being drawn.

As for moveTo, lineTo commands completely outside the drawing are are discarded.

Template Parameters:
Teither int or float.
Parameters:
xThe x coordinate for the pen position.
yThe y coordinate for the pen position.

moveTo

Move the current pen position to (x, y).

If the pen is outside the drawing area, nothing is done, but the coordinates are saved in case the next operation is lineTo a coordinate which is inside (or on the opposite side of) the drawing area.

Parameters:
xThe x coordinate for the pen position in CWRUtil::Q5 format.
yThe y coordinate for the pen position in CWRUtil::Q5 format.

moveTo

void moveTo(Tx ,
Ty
)

Move the current pen position to (x, y).

If the pen is outside (above or below) the drawing area, nothing is done, but the coordinates are saved in case the next operation is lineTo a coordinate which is inside (or on the opposite side of) the drawing area.

Template Parameters:
TEither int or float.
Parameters:
xThe x coordinate for the pen position.
yThe y coordinate for the pen position.

quadraticBezierTo

void quadraticBezierTo(floatx0 ,
floaty0 ,
const floatx1 ,
const floaty1 ,
const floatx ,
const floaty
)

Draw a Quadratic Bezier curve via x1,y1 to x2,y2.

Parameters:
x0The start x coordinate.
y0The start y coordinate.
x1The 'via' x coordinate.
y1The 'via' y coordinate.
xThe end x coordinate.
yThe end y coordinate.

render

bool render(uint8_tcustomAlpha =255)

Render the graphical shape drawn using moveTo() and lineTo() with the given Painter.

The shape is automatically closed, i.e. a lineTo() is automatically inserted connecting the current pen position with the initial pen position given in the first moveTo() command.

Parameters:
customAlpha(Optional) Alpha to apply to the entire canvas. Useful if the canvas is part of a more complex container setup that needs to be faded. Default is solid.
Returns:

true if the widget was rendered, false if insufficient memory was available to render the widget.

setFillingRule

void setFillingRule(Rasterizer::FillingRulerule)

Sets the filling rule to be used when rendering the outline.

Parameters:
ruleThe filling rule.
See also:

wasOutlineTooComplex

FORCE_INLINE_FUNCTION bool wasOutlineTooComplex()

Determines if we the outline was too complex to draw completely.

Returns:

True if it was too complex, false if not.

~Canvas

virtual ~Canvas()

Finalizes an instance of the Canvas class.

Note

Unlocks the framebuffer.