Canvas QML Type▲
-
Import Statement: import QtQuick
-
Since:: Qt 5.0
-
Inherited By:: TraceCanvas
-
Inherits:: Item
-
Group: Canvas is part of qtquick-canvas, qtquick-visual
Detailed Description▲
The Canvas item allows drawing of straight and curved lines, simple and complex shapes, graphs, and referenced graphic images. It can also add text, colors, shadows, gradients, and patterns, and do low level pixel operations. The Canvas output may be saved as an image file or serialized to a URL.
Rendering to the Canvas is done using a Context2D object, usually as a result of the paint signal.
To define a drawing area in the Canvas item set the width and height properties. For example, the following code creates a Canvas item which has a drawing area with a height of 100 pixels and width of 200 pixels:
import
QtQuick 2.0
Canvas
{
id
:
mycanvas
width
:
100
height
:
200
onPaint
: {
var ctx =
getContext
(
"2d"
);
ctx.
fillStyle =
Qt.rgba
(
1
,
0
,
0
,
1
);
ctx.fillRect
(
0
,
0
,
width,
height);
}
}
Currently the Canvas item only supports the two-dimensional rendering context.
Threaded Rendering and Render Target▲
In Qt 6.0 the Canvas item supports one render target: Canvas.Image.
The Canvas.Image render target is a QImage object. This render target supports background thread rendering, allowing complex or long running painting to be executed without blocking the UI. This is the only render target that is supported by all Qt Quick backends.
The default render target is Canvas.Image and the default renderStrategy is Canvas.Immediate.
Pixel Operations▲
All HTML5 2D context pixel operations are supported. In order to ensure improved pixel reading/writing performance the Canvas.Image render target should be chosen.
Tips for Porting Existing HTML5 Canvas Applications▲
Although the Canvas item provides an HTML5-like API, HTML5 canvas applications need to be modified to run in the Canvas item:
-
Replace all DOM API calls with QML property bindings or Canvas item methods.
-
Replace all HTML event handlers with the MouseArea item.
-
Change setInterval/setTimeout function calls with the Timer item or the use of requestAnimationFrame().
-
Place painting code into the onPaint handler and trigger painting by calling the markDirty() or requestPaint() methods.
-
To draw images, load them by calling the Canvas's loadImage() method and then request to paint them in the onImageLoaded handler.
Starting Qt 5.4, the Canvas is a texture provider and can be used directly in ShaderEffects and other classes that consume texture providers.
In general large canvases, frequent updates, and animation should be avoided with the Canvas.Image render target. This is because with accelerated graphics APIs each update will lead to a texture upload. Also, if possible, prefer QQuickPaintedItem and implement drawing in C++ via QPainter instead of the more expensive and likely less performing JavaScript and Context2D approach.
See Also▲
See also Context2D, QQuickPaintedItem
Property Documentation▲
[read-only] available : bool▲
Indicates when Canvas is able to provide a drawing context to operate on.
canvasSize : size▲
Holds the logical canvas size that the context paints on.
By default, the canvas size is the same size as the current canvas item size.
By setting the canvasSize, tileSize and canvasWindow, the Canvas item can act as a large virtual canvas with many separately rendered tile rectangles. Only those tiles within the current canvas window are painted by the Canvas render engine.
See Also▲
See also tileSize, canvasWindow
[read-only] context : object▲
Holds the active drawing context.
If the canvas is ready and there has been a successful call to getContext() or the contextType property has been set with a supported context type, this property will contain the current drawing context, otherwise null.
contextType : string▲
The type of drawing context to use.
This property is set to the name of the active context type.
If set explicitly the canvas will attempt to create a context of the named type after becoming available.
The type name is the same as used in the getContext() call, for the 2d canvas the value will be "2d".
See Also▲
See also getContext(), available
renderStrategy : enumeration▲
Holds the current canvas rendering strategy.
-
Canvas.Immediate - context will perform graphics commands immediately in the main UI thread.
-
Canvas.Threaded - context will defer graphics commands to a private rendering thread.
-
Canvas.Cooperative - context will defer graphics commands to the applications global render thread.
This hint is supplied along with renderTarget to the graphics context to determine the method of rendering. A renderStrategy, renderTarget or a combination may not be supported by a graphics context, in which case the context will choose appropriate options and Canvas will signal the change to the properties.
Configuration or runtime tests may cause the QML Scene Graph to render in the GUI thread. Selecting Canvas.Cooperative, does not guarantee rendering will occur on a thread separate from the GUI thread.
The default value is Canvas.Immediate.
See Also▲
See also renderTarget
renderTarget : enumeration▲
Holds the current canvas render target.
-
Canvas.Image - render to an in memory image buffer.
This hint is supplied along with renderStrategy to the graphics context to determine the method of rendering. A renderStrategy, renderTarget or a combination may not be supported by a graphics context, in which case the context will choose appropriate options and Canvas will signal the change to the properties.
The default render target is Canvas.Image.
Signal Documentation▲
imageLoaded()▲
This signal is emitted when an image has been loaded.
The corresponding handler is onImageLoaded.
See Also▲
See also loadImage()
paint(rect region)▲
This signal is emitted when the region needs to be rendered. If a context is active it can be referenced from the context property.
This signal can be triggered by markdirty(), requestPaint() or by changing the current canvas window.
The corresponding handler is onPaint.
painted()▲
This signal is emitted after all context painting commands are executed and the Canvas has been rendered.
The corresponding handler is onPainted.
Method Documentation▲
cancelRequestAnimationFrame(int handle)▲
This function will cancel the animation callback referenced by handle.
object getContext(string contextId, ... args)▲
Returns a drawing context, or null if no context is available.
The contextId parameter names the required context. The Canvas item will return a context that implements the required drawing mode. After the first call to getContext, any subsequent call to getContext with the same contextId will return the same context object. Any additional arguments (args) are currently ignored.
If the context type is not supported or the canvas has previously been requested to provide a different and incompatible context type, null will be returned.
Canvas only supports a 2d context.
isImageError(url image)▲
isImageLoaded(url image)▲
isImageLoading(url image)▲
loadImage(url image)▲
Loads the given image asynchronously.
Once the image is ready, imageLoaded() signal will be emitted. The loaded image can be unloaded with the unloadImage() method.
Only loaded images can be painted on the Canvas item.
See Also▲
See also unloadImage(), imageLoaded(), isImageLoaded(), Context2D::createImageData(), Context2D::drawImage()
markDirty(rect area)▲
Marks the given area as dirty, so that when this area is visible the canvas renderer will redraw it. This will trigger the paint signal.
See Also▲
See also paint, requestPaint()
int requestAnimationFrame(callback)▲
This function schedules callback to be invoked before composing the Qt Quick scene.
requestPaint()▲
bool save(string filename, size imageSize = undefined)▲
Saves the current canvas content into an image file filename. The saved image format is automatically decided by the filename's suffix. Returns true on success. If imageSize is specified, the resulting image will have this size, and will have a devicePixelRatio of 1.0. Otherwise, the devicePixelRatio() of the window in which the canvas is displayed is applied to the saved image.
Calling this method will force painting the whole canvas, not just the current canvas visible window.
See Also▲
See also canvasWindow, canvasSize, toDataURL()
string toDataURL(string mimeType)▲
Returns a data URL for the image in the canvas.
The default mimeType is "image/png".
See Also▲
See also save()
unloadImage(url image)▲
Unloads the image.
Once an image is unloaded, it cannot be painted by the canvas context unless it is loaded again.
See Also▲
See also loadImage(), imageLoaded(), isImageLoaded(), Context2D::createImageData(), Context2D::drawImage
Obsolete Members for Canvas▲
The following members of QML type Canvas are deprecated. We strongly advise against using them in new code.
Obsolete Property Documentation▲
canvasWindow : rect▲
This property is deprecated. We strongly advise against using it in new code.
Holds the current canvas visible window.
By default the canvasWindow size is the same as the Canvas item size with the top-left point as (0, 0).
If the canvasSize is different to the Canvas item size, the Canvas item can display different visible areas by changing the canvas windowSize and/or position.
This feature is incomplete. For details, see QTBUG-33129.
See Also▲
See also canvasSize, tileSize
tileSize : size▲
This property is deprecated. We strongly advise against using it in new code.
Holds the canvas rendering tile size.
The Canvas item enters tiled mode by setting canvasSize, tileSize and the canvasWindow. This can improve rendering performance by rendering and caching tiles instead of rendering the whole canvas every time.
Memory will be consumed only by those tiles within the current visible region.
By default the tileSize is the same as the canvasSize.
This feature is incomplete. For details, see QTBUG-33129.
See Also▲
See also canvasSize, canvasWindow