Your drawFrame() function is passed as a callback function for rendering, and you drawFrame() up in a loop. You either want to render on demand only, for example, after user input, to save resources at least, or you have some kind of logic that changes every frame or you just need continuous rendering.
If temporary rendering is what you want, just use Timer :
import QtQuick 2.4 Canvas { id: cvs width: 600; height: 600 contextType: "2d" property real i : 0 onPaint: { console.timeEnd("t") if (context) { context.clearRect (0, 0, width, height) context.fillRect(i, 50, 50, 50 + i) } console.time("t") } Timer { interval: 1 repeat: true running: true onTriggered: { cvs.i = (cvs.i + 0.1) % cvs.width cvs.requestPaint() } } }
Edit:
Just updated the code:
onPaint calls are synchronized with the display frame rate, even if the timer interval is set to 1 ms, as you can see from the log when you run the example above. In fact, the entire block assigned to the onTriggered signal is executed every millisecond, but requestPaint() allows you to synchronize the rendering calls for better performance, as requestAnimationFrame() does for the HTML canvas.
Apparently requestAnimationFrame() inside QML.Canvas does not work as expected, and there is not much documentation ...
Hope this helps!
source share