canvas教程

Canvas QML Type

字号+ 作者:H5之家 来源:H5之家 2017-04-22 15:01 我要评论( )

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 List of all members, including inherited members Obsolete member

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

  • List of all members, including inherited members
  • Obsolete members
  • 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.

    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 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. 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 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 screen tearing) which will further impact pixel operations with Canvas.FrambufferObject render target.

    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:

    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.

     

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • HTML5 canvas随机画线和小方块基础反弹运动实例

      HTML5 canvas随机画线和小方块基础反弹运动实例

      2017-04-21 16:07

    • JavaScript Canvas绘制圆形时钟效果

      JavaScript Canvas绘制圆形时钟效果

      2017-04-21 09:04

    • 从底层谈WebGIS 原理设计与实现(六):WebGIS中地图瓦片在Canvas上的拼接显示原理

      从底层谈WebGIS 原理设计与实现(六):WebGIS中地图瓦片在Canvas上的

      2017-04-21 09:00

    • HTML5 canvas平铺的代码详解

      HTML5 canvas平铺的代码详解

      2017-04-21 08:02

    网友点评
    e