index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. Page({
  2. data:{
  3. sf:0
  4. },
  5. onLoad() {
  6. const query = wx.createSelectorQuery()
  7. query.select('#myCanvas')
  8. .fields({
  9. node: true,
  10. size: true
  11. })
  12. .exec((res) => {
  13. const canvas = res[0].node
  14. canvas.width = res[0].width
  15. canvas.height = res[0].height
  16. const ctx = canvas.getContext('2d');
  17. // 缩放率=》实际隧道宽度1264,后台有传?
  18. // 为居中减少宽度10
  19. const cw = res[0].width-10
  20. const sw = cw / 1264
  21. this.setData({
  22. sf:sw
  23. })
  24. /** 炮孔示意图 start */
  25. // 画弧=>需要后台传每个圆弧的半径+起始弧度
  26. ctx.beginPath()
  27. ctx.arc(...this.drawArc(632,cw,Math.PI,2*Math.PI))
  28. ctx.stroke()
  29. ctx.beginPath()
  30. ctx.arc(...this.drawArc(400,cw,Math.PI,2*Math.PI))
  31. ctx.stroke()
  32. // 画炮孔=》后台传坐标
  33. ctx.beginPath()
  34. ctx.arc(...this.drawPi(0,632))
  35. ctx.stroke()
  36. ctx.beginPath()
  37. ctx.arc(...this.drawPi(632,632))
  38. ctx.stroke()
  39. ctx.beginPath()
  40. ctx.arc(...this.drawPi(1264,632))
  41. ctx.stroke()
  42. ctx.beginPath()
  43. ctx.arc(...this.drawPi(500,632))
  44. ctx.stroke()
  45. ctx.beginPath()
  46. ctx.arc(...this.drawPi(500,500))
  47. ctx.stroke()
  48. // 直线=》后台传坐标
  49. ctx.moveTo(...this.drawLine(500,632))
  50. ctx.lineTo(...this.drawLine(500,500))
  51. ctx.stroke()
  52. /** 炮孔示意图 end */
  53. /** 炮孔俯视图 start*/
  54. ctx.moveTo(...this.drawLine(50,800))
  55. ctx.lineTo(...this.drawLine(65,1100))
  56. ctx.stroke()
  57. ctx.moveTo(...this.drawLine(200,800))
  58. ctx.lineTo(...this.drawLine(200,1100))
  59. ctx.stroke()
  60. ctx.moveTo(...this.drawLine(300,800))
  61. ctx.lineTo(...this.drawLine(300,1100))
  62. ctx.stroke()
  63. ctx.moveTo(...this.drawLine(400,800))
  64. ctx.lineTo(...this.drawLine(400,1100))
  65. ctx.stroke()
  66. ctx.moveTo(...this.drawLine(700,800))
  67. ctx.lineTo(...this.drawLine(500,1100))
  68. ctx.stroke()
  69. ctx.moveTo(...this.drawLine(50,1100))
  70. ctx.lineTo(...this.drawLine(1200,1100))
  71. ctx.stroke()
  72. })
  73. },
  74. /**
  75. * 画圆弧s
  76. * @param {*} arcR 圆弧实际半径
  77. * @param {*} cw canvas 宽度
  78. * @param {*} sPi 起始弧度
  79. * @param {*} ePi 终止弧度
  80. */
  81. drawArc(arcR,cw,sPi,ePi){
  82. // 缩放倍数
  83. const sw =this.data.sf * arcR
  84. const arc = [cw/2+5,cw/2+5,sw,sPi,ePi]
  85. return arc
  86. },
  87. /**
  88. * 炮孔
  89. * @param {*} x 实际坐标
  90. * @param {*} y 实际坐标
  91. * @param {*} r 炮孔半径默认2
  92. */
  93. drawPi(x,y,r=2){
  94. // 缩放倍数
  95. const sw =this.data.sf
  96. const arc = [x*sw+5, y*sw+5, r, 0, 2 * Math.PI]
  97. return arc
  98. },
  99. /**
  100. * 直线
  101. * @param {*} x 实际坐标
  102. * @param {*} y 实际坐标
  103. */
  104. drawLine(x,y){
  105. // 缩放倍数
  106. const sw =this.data.sf
  107. const arc = [x*sw+5, y*sw+5]
  108. return arc
  109. }
  110. })