f7b194911e5e0ff7c94ac7b48fe3fe4c475f831f75815a1a7c2c07246086778b3705b9d3d82deb9349f3aceea6a21d8215ee914369670c1e15c6074129f6e1 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**
  2. * @description Draw a polyline path
  3. * @param {Object} ctx Canvas 2d context
  4. * @param {Array} points The points that makes up a polyline
  5. * @param {Boolean} beginPath Whether to execute beginPath
  6. * @param {Boolean} closePath Whether to execute closePath
  7. * @return {Undefined} Void
  8. */
  9. export function drawPolylinePath (ctx, points, beginPath = false, closePath = false) {
  10. if (!ctx || points.length < 2) return false
  11. if (beginPath) ctx.beginPath()
  12. points.forEach((point, i) =>
  13. point && (i === 0 ? ctx.moveTo(...point) : ctx.lineTo(...point)))
  14. if (closePath) ctx.closePath()
  15. }
  16. /**
  17. * @description Draw a bezier curve path
  18. * @param {Object} ctx Canvas 2d context
  19. * @param {Array} points The points that makes up a bezier curve
  20. * @param {Array} moveTo The point need to excute moveTo
  21. * @param {Boolean} beginPath Whether to execute beginPath
  22. * @param {Boolean} closePath Whether to execute closePath
  23. * @return {Undefined} Void
  24. */
  25. export function drawBezierCurvePath (ctx, points, moveTo = false, beginPath = false, closePath = false) {
  26. if (!ctx || !points) return false
  27. if (beginPath) ctx.beginPath()
  28. if (moveTo) ctx.moveTo(...moveTo)
  29. points.forEach(item => (item && ctx.bezierCurveTo(...item[0], ...item[1], ...item[2])))
  30. if (closePath) ctx.closePath()
  31. }
  32. export default {
  33. drawPolylinePath,
  34. drawBezierCurvePath
  35. }