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. Using Canvas for an arcTo
Forum Updated to NodeBB v4.3 + New Features

Using Canvas for an arcTo

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.1k 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.
  • S Offline
    S Offline
    SeDi
    wrote on 13 Dec 2018, 22:59 last edited by SeDi
    #1

    Hi,
    I am struggling to get an "arcTo" of a canvas' context2d to work.
    I have tried to follow the docs but there is no arc showing up.

    I have added a drawing below that shows what I'd like to achieve (the red arc) and what I use to construct it.

    I have managed to get lines working in a canvas' onPaint: {…} and even had some success with arc(…), but as for arcTo, I am lost. I have not managed to get anything up at all. I have no idea what is blocking me here. Probably something stupid... Here's the code I use:

    onPaint: {
                var ctx = getContext("2d")
                ctx.clearRect(0,0,width,height)
                ctx.lineWidth = 2
                ctx.strokeStyle = "red"
                ctx.beginPath()
    
                var r = Math.min(width, height)/2  //radius
                var alpha = 2*Math.PI * 15/360     // 15° in radians
                var ax = r * (1 - Math.sin(alpha))     // x of point A
                var ay = r * (1 + Math.cos(alpha))   // y of point A 
                var bx = r * (1 + Math.sin(alpha))    // x of point B
                var by = ay                                            // y of point B
    
                ctx.moveTo(ax, ay) // we don't want a "straight line" to the arc
                ctx.arcTo(ax, ay, bx, by, r)
    
                ctx.fillStyle = Qt.rgba(1, 1, 0,  0.5)
                ctx.fill()
                ctx.stroke();
            }
    

    Any ideas?
    0_1544742293367_arcTo.png

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SeDi
      wrote on 14 Dec 2018, 10:59 last edited by SeDi
      #2

      I haven't solved the arcTo problem (I actually tend to think that arcTo() might be broken or ill-documented), but i could solve my original problem (which is, how to create a speech balloon) with the arc() method:

      onPaint: {
                  var ctx = getContext("2d")
                  ctx.clearRect(0,0,width,height)
                  ctx.lineWidth = 2
                  ctx.strokeStyle = "red"
                  ctx.beginPath()
                  ctx.scale(1, 0.7)
                  var midX = width / 2
                  var midY = height / 2
      
                  ctx.moveTo(0,height)
                  ctx.arc(midX, midY, Math.min(midX, midY)*0.7, Math.PI*3/4, Math.PI*14/32, false)
                  ctx.closePath()
      
                  ctx.scale(1, 1/0.7)
                  ctx.fillStyle = Qt.rgba(1, 1, 0,  0.5)
                  ctx.fill()
                  ctx.stroke();
              }
      

      0_1544785274774_balloon.png

      1 Reply Last reply
      1
      • J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 14 Dec 2018, 11:02 last edited by J.Hilk
        #3

        great that you managed to reach your goal,

        however for future references, I would suggets this webside for canvas stuff
        https://www.w3schools.com/tags/canvas_arcto.asp
        It even has a web editor where you can see changes you make to the examples, very usefull.


        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.

        S 1 Reply Last reply 14 Dec 2018, 13:05
        1
        • J J.Hilk
          14 Dec 2018, 11:02

          great that you managed to reach your goal,

          however for future references, I would suggets this webside for canvas stuff
          https://www.w3schools.com/tags/canvas_arcto.asp
          It even has a web editor where you can see changes you make to the examples, very usefull.

          S Offline
          S Offline
          SeDi
          wrote on 14 Dec 2018, 13:05 last edited by
          #4

          @J.Hilk That site is really cool! Thank you for that link.

          1 Reply Last reply
          0

          1/4

          13 Dec 2018, 22:59

          • Login

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