0
votes

I want to use variant createPattern(Image image, string repetition), but I don't know how to pass the image parameter. I tried to pass the id of an Image but the function returns an empty object.

variant createPattern(color color, enumeration patternMode) works like a charm.

1
It looks that createPattern works only with string url, like var ptrn = ctx.createPattern("https://mdn.mozillademos.org/files/222/Canvas_createpattern.png", "repeat"); all other options (Image, CanvasImageData etc.) do nothing. Looks like a bug. - folibis
I've created issue in bug tracker - https://bugreports.qt.io/browse/QTBUG-57512 - folibis

1 Answers

0
votes

As @folibis said, the source of createPattern should be an url. For anyone looking for an example, here it is:

Canvas {
    id:canvas
    anchors.fill: parent
    onPaint:{
        var ctx = canvas.getContext('2d');
        var pattern = ctx.createPattern("file:///path_to_my_image.ext", 'repeat');
        ctx.fillStyle = pattern;
        ctx.fillRect(0, 0, canvas.width, canvas.height);
    }
  Component.onCompleted: canvas.requestPaint();
}