Quantcast
Channel: Active questions tagged qtquick2 - Stack Overflow
Viewing all articles
Browse latest Browse all 137

Multiple paths in QML Canvas Context2D

$
0
0

Is there any way to use multiple paths in the same canvas context like in HTML5 canvas? there's no Path2D object in qt/qml.

I need to draw a polygon from a point list and each vertex should have an ellipse, when trying to use the same path it gets messed up like this:

messed up polygon

I know i can do it with 2 for loops but the canvas is kinda slow already and with a different path each it would be even slower (point list can have hundreds of points), is there any other way of doing this without iterating twice through the point list?

Current code:

import QtQuickimport QtQuick.Layoutsimport QtQuick.Controlsimport QtQuick.WindowApplicationWindow {    id: main    title: qsTr("Hello World")    width: 800    height: 800    visible: true    Canvas {        id: poligono        width: parent.width        height: parent.height        onPaint: {            var points = backend.points();            var limits = backend.limits();            var ctx = getContext("2d");            // ctx.lineWidth = 1;            ctx.transform(1, 0, 0, -1, limits[0]+8, limits[1]+8);            ctx.beginPath();            for (let i = 0; i < points.length; i++) {                if (i == 0) {                    ctx.moveTo(points[i][0], points[i][1]);                } else {                    ctx.lineTo(points[i][0], points[i][1]);                }                ctx.ellipse(points[i][0], points[i][1], 5, 5);            }            ctx.closePath()            ctx.stroke()        }    }}

Viewing all articles
Browse latest Browse all 137

Trending Articles