Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

Canvas

The Canvas item provides a 2D canvas element which enables drawing via Javascript. More...

Inherits Item

This type was introduced in QtQuick 2.0.

Properties

Signals

Methods

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.

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
 }

Currently the Canvas item only supports the two-dimensional rendering context.

Threaded Rendering and Render Target

The 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.

The Canvas.FramebufferObject render target utilizes OpenGL hardware acceleration rather than rendering into system memory, which in many cases results in faster rendering.

The default render target is Canvas.Image and the default renderStrategy is Canvas.Threaded.

Tiled Canvas

The Canvas item supports tiled rendering by setting canvasSize, tileSize and canvasWindow properties.

Tiling allows efficient display of a very large virtual canvas via a smaller canvas window. The actual memory consumption is in relation to the canvas window size. The painting code can draw within the virtual canvas without handling coordinate system transformations.

The tiles overlapping with the canvas window may be cached eliminating the need to redraw, which can lead to significantly improved performance in some situations.

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. 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 {en.wikipedia.org/wiki/Screen_tearing}{screen tearing}) which will further impact pixel operations with Canvas.FrambufferObject render target.

Tips for Porting Existing HTML5 Canvas applications

Although the Canvas item is provides a 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 QtQuick2::Canvas::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.

See also QtQuick2::Context2D.

Property Documentation

read-onlyavailable : size

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 QtQuick2::Canvas::tileSize and QtQuick2::Canvas::canvasWindow.


canvasWindow : rect

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.

See also QtQuick2::Canvas::canvasSize and QtQuick2::Canvas::tileSize.


read-onlycontext : 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 QtQuick2::Canvas::getContext and QtQuick2::Canvas::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.Cooperative.

See also QtQuick2::Canvas::renderTarget.


renderTarget : enumeration

Holds the current canvas render target.

  • Canvas.Image - render to an in memory image buffer.
  • Canvas.FramebufferObject - render to an OpenGL frame 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.FramebufferObject.


tileSize : size

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.

See also QtQuick2::Canvas::canvaasSize and QtQuick2::Canvas::canvasWindow.


Signal Documentation

Canvas::onPaint(rect region)

This handler is called to render the region. If a context is active it can be referenced from the context property.

This signal can be triggered by QtQuick2::Canvas::markdirty, QtQuick2::Canvas::requestPaint or by changing the current canvas window.


Canvas::onPainted()

This handler is called after all context painting commands are executed and the Canvas has been rendered.


Method Documentation

bool Canvas::save(string filename)

Save the current canvas content into an image file filename. The saved image format is automatically decided by the filename's suffix.

Note: calling this method will force painting the whole canvas, not just the current canvas visible window.

See also canvasWindow, canvasSize, and toDataURL.


long Canvas::requestAnimationFrame(callback)

This function schedules callback to be invoked before composing the QtQuick scene.


object Canvas::getContext(string contextId, any)


string Canvas::toDataURL(string mimeType)

Returns a data URL for the image in the canvas.

The default mimeType is "image/png".

See also QtQuick2::Canvas::save.


void Canvas::cancelRequestAnimationFrmae(long handle)

This function will cancel the animation callback referenced by handle.


void Canvas::isImageError(url image)

Returns true if the image failed to load.

See also QtQuick2::Canvas::loadImage.


void Canvas::isImageLoaded(url image)

Returns true if the image is sucessfully loaded and ready to use.

See also QtQuick2::Canvas::loadImage.


void Canvas::isImageLoading(url image)

Returns true if the image is currently loading.

See also QtQuick2::Canvas::loadImage.


void Canvas::loadImage(url image)

Loads the given image asynchronously.

When the image is ready, onImageLoaded will be emitted. The loaded image can be unloaded by the QtQuick2::Canvas::unloadImage method.

Note: Only loaded images can be painted on the Canvas item.

See also QtQuick2::Canvas::unloadImage, QtQuick2::Canvas::imageLoaded, QtQuick2::Canvas::isImageLoaded, QtQuick2::Context2D::createImageData, and QtQuick2::Context2D::drawImage.


void Canvas::markDirty(rect area)

Mark the given area as dirty, so that when this area is visible the canvas renderer will redraw it. This will trigger the "onPaint" signal handler function.

See also QtQuick2::Canvas::paint and QtQuick2::Canvas::requestPaint.


void Canvas::requestPaint()

Request the entire visible region be re-drawn.

See also QtQuick::Canvas::markDirty.


void Canvas::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 QtQuick2::Canvas::loadImage, QtQuick2::Canvas::imageLoaded, QtQuick2::Canvas::isImageLoaded, QtQuick2::Context2D::createImageData, and QtQuick2::Context2D::drawImage.


Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web