Contents
Canvas QML Type
Provides a 2D canvas item enabling drawing via JavaScript
Import Statement: import QtQuick 2.7
Since: Qt 5.0
Inherits:
Item
Inherited By:
TraceCanvas
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 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 { : 200 onPaint: { var ctx = getContext("2d"); .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 TargetThe Canvas item supports two render targets: Canvas.Image and Canvas.FramebufferObject.
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 Canvas.FramebufferObject render target utilizes OpenGL hardware acceleration rather than rendering into system memory, which in many cases results in faster rendering. Canvas.FramebufferObject relies on the OpenGL extensions GL_EXT_framebuffer_multisample and GL_EXT_framebuffer_blit for antialiasing. It will also use more graphics memory when rendering strategy is anything other than Canvas.Cooperative. Framebuffer objects may not be available with Qt Quick backends other than OpenGL.
The default render target is Canvas.Image and the default is Canvas.Immediate.
Pixel OperationsAll HTML5 2D context pixel operations are supported. In order to ensure improved pixel reading/writing performance the Canvas.Image render target should be chosen. The Canvas.FramebufferObject render target requires the pixel data to be exchanged between the system memory and the graphic card, which is significantly more expensive. Rendering may also be synchronized with the V-sync signal (to avoid screen tearing) which will further impact pixel operations with Canvas.FrambufferObject render target.
Tips for Porting Existing HTML5 Canvas ApplicationsAlthough the Canvas item provides an HTML5-like API, HTML5 canvas applications need to be modified to run in the Canvas item:
Starting Qt 5.4, the Canvas is a texture provider and can be used directly in ShaderEffects and other classes that consume texture providers.
Note: 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 Context2D and QQuickPaintedItem.
Property Documentation
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 and .
context : object
Holds the active drawing context.
If the canvas is ready and there has been a successful call to or the 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 call, for the 2d canvas the value will be "2d".
See also and .
renderStrategy : enumeration
Holds the current canvas rendering strategy.