NanoCanvas
|
#include <Canvas.h>
Public Types | |
enum | Winding { Winding::CCW = 1, Winding::CW = 2 } |
enum | LineCap { LineCap::BUTT, LineCap::ROUND, LineCap::SQUARE } |
Line cap style. More... | |
enum | LineJoin { LineJoin::BEVEL, LineJoin::ROUND, LineJoin::MITER } |
Line join style. More... | |
Public Member Functions | |
Canvas (NVGcontext *ctx, float width, float height, float scaleRatio=1.0f) | |
Construct a canvas with NanoVG Context. More... | |
Canvas & | moveTo (float x, float y) |
Moves the path to the specified point in the canvas, without creating a line. More... | |
Canvas & | lineTo (float x, float y) |
Adds a new point and creates a line from that point to the last specified point in the canvas. More... | |
Canvas & | arcTo (float x1, float y1, float x2, float y2, float r) |
Creates an arc/curve between two tangents on the canvas. More... | |
Canvas & | quadraticCurveTo (float cpx, float cpy, float x, float y) |
Creates a quadratic Bézier curve. More... | |
Canvas & | bezierCurveTo (float cp1x, float cp1y, float cp2x, float cp2y, float x, float y) |
Creates a cubic Bézier curve. More... | |
Canvas & | arc (float x, float y, float r, float sAngle, float eAngle, bool counterclockwise=false) |
Creates an arc/curve (used to create circles, or parts of circles) The arc() method creates an arc/curve (used to create circles, or parts of circles). More... | |
Canvas & | closePath () |
Close current path with a line segment. More... | |
Canvas & | rect (float x, float y, float w, float h) |
Creates a rectangle. More... | |
Canvas & | roundedRect (float x, float y, float w, float h, float r) |
Creates a rounded rectangle. More... | |
Canvas & | circle (float cx, float cy, float r) |
Creates a circle. More... | |
Canvas & | ellipse (float cx, float cy, float rx, float ry) |
Creates an ellipse. More... | |
Canvas & | fill () |
Fills the current drawing (path) More... | |
Canvas & | stroke () |
Actually draws the path you have defined. More... | |
Canvas & | fillRect (float x, float y, float w, float h) |
Draws a "filled" rectangle. More... | |
Canvas & | strokeRect (float x, float y, float w, float h) |
Draws a rectangle (no fill) More... | |
Canvas & | clearColor (const Color &color) |
Clear the canvas with color. More... | |
Canvas & | fillText (const string &text, float x, float y, float rowWidth=NAN) |
Draws "filled" text on the canvas. More... | |
Canvas & | drawImage (Image &image, float x, float y, float width=NAN, float height=NAN, float sx=0, float sy=0, float swidth=NAN, float sheight=NAN) |
Draws an image onto the canvas. More... | |
Canvas & | lineCap (LineCap cap) |
Set the style of the end caps for a line. More... | |
Canvas & | lineJoin (LineJoin join) |
Set the type of corner created, when two lines meet. More... | |
Canvas & | lineWidth (float width) |
Set the current line width. More... | |
Canvas & | miterLimit (float limit) |
Sets the maximum miter length. More... | |
Canvas & | globalAlpha (float alpha) |
Sets the current alpha or transparency value of the drawing. More... | |
Canvas & | fillStyle (const Color &color) |
Sets the color to fill the drawing. More... | |
Canvas & | fillStyle (const Paint &paint) |
Set the gradient or pattern paint used to fill the drawing. More... | |
Canvas & | strokeStyle (const Color &color) |
Set the color used for strokes. More... | |
Canvas & | strokeStyle (const Paint &paint) |
Set the gradient or pattern paint used for strokes. More... | |
Canvas & | font (const Font &font) |
Set current font for text rendering. More... | |
Canvas & | font (float size) |
Set font size for current text style. More... | |
Canvas & | textAlign (HorizontalAlign hAlign, VerticalAlign vAlign) |
Sets the text alignment of current text style. More... | |
Canvas & | fillStyle (const TextStyle &textStyle) |
Set styles for text rendering. More... | |
float | measureText (const string &text, float rowWidth=NAN) |
Check the width of the text, before writing it on the canvas. More... | |
float | measureText (const string &text, float x, float y, float *bounds, float rowWidth=NAN) |
Check the boundary of the text, before writing it on the canvas. More... | |
Canvas & | scale (float scalewidth, float scaleheight) |
Scales the current drawing, bigger or smaller. More... | |
Canvas & | rotate (float angle) |
Rotates the current drawing. More... | |
Canvas & | translate (float x, float y) |
Remaps the (0,0) position on the canvas. More... | |
Canvas & | transform (float a, float b, float c, float d, float e, float f) |
Replaces the current transformation matrix for the drawing. More... | |
Canvas & | setTransform (float a, float b, float c, float d, float e, float f) |
Resets the current transform to the identity matrix. Then runs transform() More... | |
Canvas & | restTransform () |
Resets the current transform to the identity matrix. More... | |
Canvas & | begineFrame (int windowWidth, int windowHeight) |
Begin drawing a new frame. More... | |
Canvas & | cancelFrame () |
Cancels drawing the current frame. More... | |
void | endFrame () |
Ends drawing flushing remaining render state. More... | |
Canvas & | beginPath () |
Begins a path, or resets the current path. More... | |
Canvas & | pathWinding (Winding dir) |
Sets the current path winding. More... | |
Canvas & | clip (float x, float y, float w, float h) |
Clip of a rectangular region. More... | |
Canvas & | resetClip () |
Reset clip state ,remove all clip region. More... | |
Canvas & | save () |
Pushe and save the current render state into a state stack. More... | |
Canvas & | restore () |
Pop and restore current render state. More... | |
Canvas & | reset () |
Resets current render state to default values. Does not affect the render state stack. More... | |
bool | valid () const |
Check is the context avaliable. More... | |
Canvas & | setSize (float width, float height) |
Set canvas size. More... | |
Canvas & | setPosition (float x, float y) |
Set position of the canvas. More... | |
Canvas & | setScaleRatio (float ratio) |
Set scale ration of the canvas. More... | |
void | local2Global (float &x, float &y) |
Convert coordinates in canvas to coordinates in windows. More... | |
void | global2Local (float &x, float &y) |
Convert coordinates in windows to coordinates in canvas. More... | |
NVGcontext * | nvgContext () |
Get the NanoVG context for advanced contol. More... | |
Static Public Member Functions | |
static Paint | createLinearGradient (float x0, float y0, float x1, float y1, const Color &scolor, const Color &ecolor) |
Creates a linear gradient (to use on canvas content) More... | |
static Paint | createRadialGradient (float cx, float cy, float r1, float r2, const Color &icolor, const Color &ocolor) |
Creates a radial/circular gradient (to use on canvas content) More... | |
static Paint | createBoxGradient (float x, float y, float w, float h, float r, float f, Color icol, Color ocol) |
Creates and returns a box gradient. More... | |
static Paint | createPattern (const Image &image, float ox, float oy, float w, float h, float angle=0.0f, float alpha=1.0f) |
Creates and returns an image pattern paint. More... | |
Protected Attributes | |
NVGcontext * | m_nvgCtx |
The NanoVG context. More... | |
float | m_width |
The width of the canvas. More... | |
float | m_height |
The height of the canvas. More... | |
float | m_scaleRatio |
Buffer / window size ratio. More... | |
float | m_xPos |
The x-coordinate of the canvas in window. More... | |
float | m_yPos |
The y-coordinate of the canvas in window. More... | |
|
strong |
|
strong |
|
strong |
NanoCanvas::Canvas::Canvas | ( | NVGcontext * | ctx, |
float | width, | ||
float | height, | ||
float | scaleRatio = 1.0f |
||
) |
Construct a canvas with NanoVG Context.
ctx | The NanoVG Context used for this canvas |
width | The width of the canvas, in pixels |
height | The height of the canvas, in pixels |
scaleRatio | The device pixel ration |
Canvas& NanoCanvas::Canvas::arc | ( | float | x, |
float | y, | ||
float | r, | ||
float | sAngle, | ||
float | eAngle, | ||
bool | counterclockwise = false |
||
) |
Creates an arc/curve (used to create circles, or parts of circles) The arc() method creates an arc/curve (used to create circles, or parts of circles).
x | The x-coordinate of the center of the circle |
y | The y-coordinate of the center of the circle |
r | The radius of the circle |
sAngle | The starting angle, in radians (0 is at the 3 o'clock position of the arc's circle) |
eAngle | The ending angle, in radians |
counterclockwise | Optional. Specifies whether the drawing should be counterclockwise or clockwise. False is default, and indicates clockwise, while true indicates counter-clockwise. |
Canvas& NanoCanvas::Canvas::arcTo | ( | float | x1, |
float | y1, | ||
float | x2, | ||
float | y2, | ||
float | r | ||
) |
Creates an arc/curve between two tangents on the canvas.
x1 | The x-coordinate of the first tangent |
y1 | The y-coordinate of the first tangent |
x2 | The x-coordinate of the second tangent |
y2 | The y-coordinate of the second tangent |
r | The radius of the arc |
Canvas& NanoCanvas::Canvas::begineFrame | ( | int | windowWidth, |
int | windowHeight | ||
) |
Begin drawing a new frame.
Calls to Canvas drawing API should be wrapped in begineFrame() & endFrame() begineFrame() defines the size of the window to render to in relation currently set viewport (i.e. glViewport on GL backends).
windowWidth | Width of your window |
windowHeight | Height of your window |
Canvas& NanoCanvas::Canvas::beginPath | ( | ) |
Begins a path, or resets the current path.
Canvas& NanoCanvas::Canvas::bezierCurveTo | ( | float | cp1x, |
float | cp1y, | ||
float | cp2x, | ||
float | cp2y, | ||
float | x, | ||
float | y | ||
) |
Creates a cubic Bézier curve.
The bezierCurveTo() method adds a point to the current path by using the specified control points that represent a cubic Bézier curve.
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.
cp1x | The x-coordinate of the first Bézier control point |
cp1y | The y-coordinate of the first Bézier control point |
cp2x | The x-coordinate of the second Bézier control point |
cp2y | The y-coordinate of the second Bézier control point |
x | The x-coordinate of the ending point |
y | The y-coordinate of the ending point |
Canvas& NanoCanvas::Canvas::cancelFrame | ( | ) |
Cancels drawing the current frame.
Canvas& NanoCanvas::Canvas::circle | ( | float | cx, |
float | cy, | ||
float | r | ||
) |
Creates a circle.
cx | The x-coordinate of center point for the circle |
cy | The y-coordinate of center point for the circle |
r | The radius of the circle |
Clear the canvas with color.
color | The color to fill the hole canvas |
Canvas& NanoCanvas::Canvas::clip | ( | float | x, |
float | y, | ||
float | w, | ||
float | h | ||
) |
Clip of a rectangular region.
x | The x-coordinate of the upper-left corner of the clip region |
y | The y-coordinate of the upper-left corner of the clip region |
w | The width of the clip region, in pixels |
h | The width of the clip region, in pixels |
Canvas& NanoCanvas::Canvas::closePath | ( | ) |
Close current path with a line segment.
|
static |
Creates and returns a box gradient.
x | The x-coordinate of the upper-left corner of the rectangle |
y | The y-coordinate of the upper-left corner of the rectangle |
w | The width of the rectangle, in pixels |
h | The height of the rectangle, in pixels |
r | The radius of the circle formed by 4 corners of the rounded rectangle |
f | How blurry the border of the rectangle is |
icol | The inner color of the gradient |
ocol | The outer color of the gradient |
|
static |
Creates a linear gradient (to use on canvas content)
x0 | The x-coordinate of the start point of the gradient |
y0 | The y-coordinate of the start point of the gradient |
x1 | The x-coordinate of the end point of the gradient |
y1 | The y-coordinate of the end point of the gradient |
scolor | The start color |
outter | The end color |
|
static |
Creates and returns an image pattern paint.
image | Specifies the image of the pattern to use |
ox | The x-coordinate of the upper-left corner of the image would be draw |
oy | The y-coordinate of the upper-left corner of the image would be draw |
w | The width of the pattern |
h | The height of the pattern |
angle | The rotation around the top-left corner in radians |
alpha | The transparent of the image pattern |
|
static |
Creates a radial/circular gradient (to use on canvas content)
cx | The x-coordinate of the circle of the gradient |
cy | The y-coordinate of the circle of the gradient |
r1 | The radius of the inner circle |
r2 | The radius of the outter circle |
icolor | The color on inner circle |
ocolor | The color on outer circle |
Canvas& NanoCanvas::Canvas::drawImage | ( | Image & | image, |
float | x, | ||
float | y, | ||
float | width = NAN , |
||
float | height = NAN , |
||
float | sx = 0 , |
||
float | sy = 0 , |
||
float | swidth = NAN , |
||
float | sheight = NAN |
||
) |
Draws an image onto the canvas.
The drawImage() method can also draw parts of an image, and/or increase/reduce the image size.
image | Specifies the image to use |
x | The x coordinate where to place the image on the canvas |
y | The y coordinate where to place the image on the canvas |
width | The width of the image to use (stretch or reduce the image),NAN as the default be the same as wdith of the clipped area |
height | The height of the image to use (stretch or reduce the image),NAN as the default be the same as wdith of the clipped area |
sx | The x coordinate where to start clipping,0 as the default |
sy | The y coordinate where to start clipping,0 as the default |
swidth | The wdith of the clipped image,NAN as defualt to clip to right side of the image |
sheight | The height of the clipped image,NAN as defualt to clip to bottom side of the image |
Canvas& NanoCanvas::Canvas::ellipse | ( | float | cx, |
float | cy, | ||
float | rx, | ||
float | ry | ||
) |
Creates an ellipse.
cx | The x-coordinate of center point for the ellipse |
cy | The x-coordinate of center point for the ellipse |
rx | The radius of the ellipse in horizentoal |
ry | The radius of the ellipse in vertical |
void NanoCanvas::Canvas::endFrame | ( | ) |
Ends drawing flushing remaining render state.
Canvas& NanoCanvas::Canvas::fill | ( | ) |
Fills the current drawing (path)
The fill() method fills the current drawing (path). The default color is black.
Canvas& NanoCanvas::Canvas::fillRect | ( | float | x, |
float | y, | ||
float | w, | ||
float | h | ||
) |
Draws a "filled" rectangle.
The fillRect() method draws a "filled" rectangle. The default color of the fill is black.
x | The x-coordinate of the upper-left corner of the rectangle |
y | The y-coordinate of the upper-left corner of the rectangle |
w | The width of the rectangle, in pixels |
h | The height of the rectangle, in pixels |
Sets the color to fill the drawing.
color | The color to fill with |
Set the gradient or pattern paint used to fill the drawing.
paint | The paint used to fill the drawing |
Set styles for text rendering.
textStyle | The text style to use |
Canvas& NanoCanvas::Canvas::fillText | ( | const string & | text, |
float | x, | ||
float | y, | ||
float | rowWidth = NAN |
||
) |
Draws "filled" text on the canvas.
text | Specifies the text that will be written on the canvas |
x | The x coordinate where to start painting the text (relative to the canvas) |
y | The y coordinate where to start painting the text (relative to the canvas) |
rowWidth | The max row width of the text box,NAN is not limited |
Set current font for text rendering.
font | The font to use |
Canvas& NanoCanvas::Canvas::font | ( | float | size | ) |
Set font size for current text style.
size | The font size |
|
inline |
Convert coordinates in windows to coordinates in canvas.
x | [inout] The x-coordinate to convert |
y | [inout] The x-coordinate to convert |
Canvas& NanoCanvas::Canvas::globalAlpha | ( | float | alpha | ) |
Sets the current alpha or transparency value of the drawing.
alpha | new alpha vlaue of canvas |
Set the style of the end caps for a line.
cap | The line cap style |
Set the type of corner created, when two lines meet.
join | The line join style |
Canvas& NanoCanvas::Canvas::lineTo | ( | float | x, |
float | y | ||
) |
Adds a new point and creates a line from that point to the last specified point in the canvas.
The lineTo() method adds a new point and creates a line from that point to the last specified point in the canvas (this method does not draw the line).
x | The x-coordinate of where to create the line to |
y | The y-coordinate of where to create the line to |
Canvas& NanoCanvas::Canvas::lineWidth | ( | float | width | ) |
Set the current line width.
width | The current line width, in pixels |
|
inline |
Convert coordinates in canvas to coordinates in windows.
x | [inout] The x-coordinate to convert |
y | [inout] The x-coordinate to convert |
float NanoCanvas::Canvas::measureText | ( | const string & | text, |
float | rowWidth = NAN |
||
) |
Check the width of the text, before writing it on the canvas.
text | The text to be measured |
rowWidth | The max row width of the text box,NAN is not limited |
float NanoCanvas::Canvas::measureText | ( | const string & | text, |
float | x, | ||
float | y, | ||
float * | bounds, | ||
float | rowWidth = NAN |
||
) |
Check the boundary of the text, before writing it on the canvas.
text | The text to be measured |
x | The x-coordinate of the text |
y | The y-coordinate of the text |
bounds | [in] The float array to store boundary values should be a pointer to float[4] |
rowWidth | The max row width of the text box,NAN is not limited |
Canvas& NanoCanvas::Canvas::miterLimit | ( | float | limit | ) |
Sets the maximum miter length.
The miter length is the distance between the inner corner and the outer corner where two lines meet.
The miter length grows bigger as the angle of the corner gets smaller.
limit | A positive number that specifies the maximum miter length. If the current miter length exceeds the miterLimit, the corner will display as lineJoin "bevel" |
Canvas& NanoCanvas::Canvas::moveTo | ( | float | x, |
float | y | ||
) |
Moves the path to the specified point in the canvas, without creating a line.
The moveTo() method moves the path to the specified point in the canvas, without creating a line.
x | The x-coordinate of where to move the path to |
y | The y-coordinate of where to move the path to |
|
inline |
Get the NanoVG context for advanced contol.
Sets the current path winding.
dir | CVS_CW or CVS_CW |
Canvas& NanoCanvas::Canvas::quadraticCurveTo | ( | float | cpx, |
float | cpy, | ||
float | x, | ||
float | y | ||
) |
Creates a quadratic Bézier curve.
The quadraticCurveTo() method adds a point to the current path by using the specified control points that represent a quadratic Bézier curve.
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.
cpx | The x-coordinate of the Bézier control point |
cpy | The y-coordinate of the Bézier control point |
x | The x-coordinate of the ending point |
y | The y-coordinate of the ending point |
Canvas& NanoCanvas::Canvas::rect | ( | float | x, |
float | y, | ||
float | w, | ||
float | h | ||
) |
Creates a rectangle.
The rect() method creates a rectangle.
x | The x-coordinate of the upper-left corner of the rectangle |
y | The y-coordinate of the upper-left corner of the rectangle |
w | The width of the rectangle, in pixels |
h | The height of the rectangle, in pixels |
Canvas& NanoCanvas::Canvas::reset | ( | ) |
Resets current render state to default values. Does not affect the render state stack.
Canvas& NanoCanvas::Canvas::resetClip | ( | ) |
Reset clip state ,remove all clip region.
Canvas& NanoCanvas::Canvas::restore | ( | ) |
Pop and restore current render state.
Canvas& NanoCanvas::Canvas::restTransform | ( | ) |
Resets the current transform to the identity matrix.
Canvas& NanoCanvas::Canvas::rotate | ( | float | angle | ) |
Rotates the current drawing.
angle | The rotation angle, in radians. |
Canvas& NanoCanvas::Canvas::roundedRect | ( | float | x, |
float | y, | ||
float | w, | ||
float | h, | ||
float | r | ||
) |
Creates a rounded rectangle.
x | The x-coordinate of the upper-left corner of the rectangle |
y | The y-coordinate of the upper-left corner of the rectangle |
w | The width of the rectangle, in pixels |
h | The height of the rectangle, in pixels |
r | The radius of the circle formed by 4 corners of the rounded rectangle |
Canvas& NanoCanvas::Canvas::save | ( | ) |
Pushe and save the current render state into a state stack.
Canvas& NanoCanvas::Canvas::scale | ( | float | scalewidth, |
float | scaleheight | ||
) |
Scales the current drawing, bigger or smaller.
scalewidth | Scales the width of the current drawing (1=100%, 0.5=50%, 2=200%, etc.) |
scaleheight | Scales the height of the current drawing (1=100%, 0.5=50%, 2=200%, etc.) |
|
inline |
Set position of the canvas.
x | The x-coordinate of the upper-left corner of the rectangle |
y | The y-coordinate of the upper-left corner of the rectangle |
|
inline |
Set scale ration of the canvas.
Device pixel ration allows to control the rendering on Hi-DPI devices. For example, GLFW returns two dimension for an opened window: window size and frame buffer size. In that case you would set windowWidth/Height to the window size devicePixelRatio to: frameBufferWidth / windowWidth.
ratio | The device pixel ration |
|
inline |
Set canvas size.
width | The width of the canvas, in pixels |
height | The height of the canvas, in pixels |
Canvas& NanoCanvas::Canvas::setTransform | ( | float | a, |
float | b, | ||
float | c, | ||
float | d, | ||
float | e, | ||
float | f | ||
) |
Resets the current transform to the identity matrix. Then runs transform()
a | Scales the drawing horizontally |
b | Skew the the drawing horizontally |
c | Skew the the drawing vertically |
d | Scales the drawing vertically |
e | Moves the the drawing horizontally |
f | Moves the the drawing vertically |
Canvas& NanoCanvas::Canvas::stroke | ( | ) |
Canvas& NanoCanvas::Canvas::strokeRect | ( | float | x, |
float | y, | ||
float | w, | ||
float | h | ||
) |
Draws a rectangle (no fill)
The strokeRect() method draws a rectangle (no fill). The default color of the stroke is black.
x | The x-coordinate of the upper-left corner of the rectangle |
y | The y-coordinate of the upper-left corner of the rectangle |
w | The width of the rectangle, in pixels |
h | The height of the rectangle, in pixels |
Set the color used for strokes.
color | Stroke color |
Set the gradient or pattern paint used for strokes.
paint | The paint used to fill the drawing |
Canvas& NanoCanvas::Canvas::textAlign | ( | HorizontalAlign | hAlign, |
VerticalAlign | vAlign | ||
) |
Sets the text alignment of current text style.
hAlign | The horizontak alignment |
vAlign | The verical alignment |
Canvas& NanoCanvas::Canvas::transform | ( | float | a, |
float | b, | ||
float | c, | ||
float | d, | ||
float | e, | ||
float | f | ||
) |
Replaces the current transformation matrix for the drawing.
a | Scales the drawing horizontally |
b | Skew the the drawing horizontally |
c | Skew the the drawing vertically |
d | Scales the drawing vertically |
e | Moves the the drawing horizontally |
f | Moves the the drawing vertically |
Canvas& NanoCanvas::Canvas::translate | ( | float | x, |
float | y | ||
) |
Remaps the (0,0) position on the canvas.
x | The value to add to horizontal (x) coordinates |
y | The value to add to vertical (y) coordinates |
|
inline |
Check is the context avaliable.
|
protected |
The height of the canvas.
|
protected |
The NanoVG context.
|
protected |
Buffer / window size ratio.
|
protected |
The width of the canvas.
|
protected |
The x-coordinate of the canvas in window.
|
protected |
The y-coordinate of the canvas in window.