TraceUtils.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright (C) 2016 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
  3. .pragma library
  4. function renderSmoothedLine(ctx, trace, renderPos) {
  5. if (!trace)
  6. return renderPos
  7. if (renderPos >= trace.length)
  8. return renderPos
  9. // Fetch points and draw the initial "dot"
  10. var points, tp
  11. if (renderPos === 0) {
  12. points = trace.points()
  13. tp = points[renderPos++]
  14. ctx.beginPath()
  15. ctx.moveTo(tp.x, tp.y)
  16. ctx.lineTo(tp.x, tp.y + 0.000001)
  17. ctx.stroke()
  18. } else {
  19. points = trace.points(renderPos - 1)
  20. }
  21. // Draw smoothed line using quadratic curve
  22. var i = 1
  23. if (i + 1 < points.length) {
  24. var pt1, pt2
  25. if (renderPos === 1) {
  26. tp = points[i - 1]
  27. } else {
  28. pt1 = points[i - 1]
  29. pt2 = points[i]
  30. tp = Qt.point((pt1.x + pt2.x) / 2, (pt1.y + pt2.y) / 2)
  31. }
  32. ctx.beginPath()
  33. ctx.moveTo(tp.x, tp.y)
  34. while (i + 1 < points.length) {
  35. pt1 = points[i++]
  36. pt2 = points[i]
  37. tp = Qt.point((pt1.x + pt2.x) / 2, (pt1.y + pt2.y) / 2)
  38. ctx.quadraticCurveTo(pt1.x, pt1.y, tp.x, tp.y)
  39. ctx.moveTo(tp.x, tp.y)
  40. }
  41. ctx.stroke()
  42. }
  43. // Draw the remainder of the line
  44. if (trace.final) {
  45. if (i < points.length) {
  46. tp = points[i - 1]
  47. ctx.beginPath()
  48. ctx.moveTo(tp.x, tp.y)
  49. tp = points[i++]
  50. ctx.lineTo(tp.x, tp.y)
  51. ctx.stroke()
  52. }
  53. }
  54. return renderPos + i - 1
  55. }