Convenience API

For operations on rectangles, a convenience API is provided which draws directly and does need a stroke or fill call.

  1. import QtQuick
  2. Canvas {
  3. id: root
  4. width: 120; height: 120
  5. onPaint: {
  6. var ctx = getContext("2d")
  7. ctx.fillStyle = 'green'
  8. ctx.strokeStyle = "blue"
  9. ctx.lineWidth = 4
  10. // draw a filles rectangle
  11. ctx.fillRect(20, 20, 80, 80)
  12. // cut our an inner rectangle
  13. ctx.clearRect(30,30, 60, 60)
  14. // stroke a border from top-left to
  15. // inner center of the larger rectangle
  16. ctx.strokeRect(20,20, 40, 40)
  17. }
  18. }

image

TIP

The stroke area extends half of the line width on both sides of the path. A 4 px lineWidth will draw 2 px outside the path and 2 px inside.