Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Is It possible to create a secondary Canvas? Or use a Qml Image inside Canvas
Forum Update on Monday, May 27th 2025

Is It possible to create a secondary Canvas? Or use a Qml Image inside Canvas

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 803 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rubxcubedude
    wrote on last edited by
    #1

    in pure javascript, you can create a canvas to then use in conjunction with context.createPattern

    and I was wondering if there was a way to do this with qml.

    var ctx = canvas.getContext("2d");
    
    //creating a new canvas  
    var canvas = document.createElement('canvas');
    canvas.width = 500;
    canvas.height = 400;
    var img = canvas.getContext("2d");
    
    draw(img);
    var objPattern = ctx.createPattern(img, "repeat");
    ctx.fillStyle = objPattern;
    

    if that isn't possible then how do you create an Image for createPattern in qml?

    I've tried the below but the Image defaults to the top corner on my canvas instead of filling object I draw

    Canvas{
      id: canvas_id
    
      Image{
        id: image_id
       source: "./mysource.png"
      }
      
      onPaint: {
                var ctx = canvas_id.getContext('2d');
                
                //create a 2nd canvas for fill pattern
                var pattern = ctx.createPattern(image_id, "repeat");
                context.fillStyle = pattern
                ////drawRect with pattern
      }
    }
    
    J.HilkJ 1 Reply Last reply
    0
    • R rubxcubedude

      in pure javascript, you can create a canvas to then use in conjunction with context.createPattern

      and I was wondering if there was a way to do this with qml.

      var ctx = canvas.getContext("2d");
      
      //creating a new canvas  
      var canvas = document.createElement('canvas');
      canvas.width = 500;
      canvas.height = 400;
      var img = canvas.getContext("2d");
      
      draw(img);
      var objPattern = ctx.createPattern(img, "repeat");
      ctx.fillStyle = objPattern;
      

      if that isn't possible then how do you create an Image for createPattern in qml?

      I've tried the below but the Image defaults to the top corner on my canvas instead of filling object I draw

      Canvas{
        id: canvas_id
      
        Image{
          id: image_id
         source: "./mysource.png"
        }
        
        onPaint: {
                  var ctx = canvas_id.getContext('2d');
                  
                  //create a 2nd canvas for fill pattern
                  var pattern = ctx.createPattern(image_id, "repeat");
                  context.fillStyle = pattern
                  ////drawRect with pattern
        }
      }
      
      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @rubxcubedude have you looked at the qml/canvas docu yet?

      There is a drawImage function.

      http://doc.qt.io/qt-5/qml-qtquick-context2d.html#drawImage-method-2

      shoud work with img object, or just the url directly


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      R 1 Reply Last reply
      0
      • J.HilkJ J.Hilk

        @rubxcubedude have you looked at the qml/canvas docu yet?

        There is a drawImage function.

        http://doc.qt.io/qt-5/qml-qtquick-context2d.html#drawImage-method-2

        shoud work with img object, or just the url directly

        R Offline
        R Offline
        rubxcubedude
        wrote on last edited by
        #3

        @J.Hilk I have. both that method and the createPattern method say they accept an Image, a CanvasImageData, or a URL as acceptable parameters. my question is how do i create the Image and/or canvasImagedata to be used. when i try to define the image in my canvas as shown in my initial posts code, the image gets put at 0,0 instead of inside the rectangle i want it to fill.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved