Draw.ets 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * 专注画各种图形
  3. */
  4. @Component
  5. export struct Draw{
  6. private settings: RenderingContextSettings = new RenderingContextSettings(true)
  7. private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  8. /**
  9. * 画的范围
  10. */
  11. @Prop drawWidth:number=100
  12. @Prop drawHeight:number=100
  13. startWidth:number=0
  14. startHeight:number=0
  15. /**
  16. * 定点数组,
  17. * 比如想画一个三角形
  18. */
  19. @Prop drawArr:number[][]=[[50,0],[100,100],[0,100]]
  20. // @Prop drawArrX:number[]=[]
  21. // @Prop drawArrY:number[]=[]
  22. private drawCircleWithCustomRadii() {
  23. // this.context.clearRect(0,0,0,0)
  24. this.context.clearRect(0, 0, this.drawWidth, this.drawHeight); // 清空Canvas的内容
  25. this.drawArr.forEach((item:number[],index:number)=>{
  26. this.context.beginPath()
  27. this.context.moveTo(this.drawArr[index][0], this.drawArr[index][1])
  28. if(index!=this.drawArr.length-1) {
  29. this.context.lineTo(this.drawArr[index+1][0], this.drawArr[index+1][1])
  30. }else{
  31. this.context.moveTo(this.drawArr[0][0], this.drawArr[0][1])
  32. }
  33. this.context.strokeStyle = '#efd4f9' // 红色半径线
  34. this.context.lineWidth = 1.5
  35. this.context.stroke()
  36. })
  37. // const radius = 125 // 圆半径
  38. // 根据自定义角度数组绘制半径线
  39. // this.cells.forEach(angle => {
  40. // // 将角度转换为弧度(Canvas使用弧度制)
  41. // const radians = (angle.angleEnd-90) * Math.PI / 180
  42. //
  43. // // 计算半径线终点坐标
  44. // const endX = centerX + radius * Math.cos(radians)
  45. // const endY = centerY + radius * Math.sin(radians)
  46. //
  47. // // 绘制半径线
  48. // this.context.beginPath()
  49. // this.context.moveTo(centerX, centerY)
  50. // this.context.lineTo(endX, endY)
  51. // this.context.strokeStyle = '#efd4f9' // 红色半径线
  52. // this.context.lineWidth = 1.5
  53. // this.context.stroke()
  54. //
  55. // // // 标注角度值(修正后的文本绘制方式)
  56. // // this.context.font = '12px sans-serif'
  57. // // this.context.fillStyle = '#000000'
  58. // // this.context.textAlign = 'center'
  59. // // this.context.textBaseline = 'middle'
  60. //
  61. // // // 文本位置稍微向外偏移
  62. // // const textRadius = radius + 15
  63. // // const textX = centerX + textRadius * Math.cos(radians)
  64. // // const textY = centerY + textRadius * Math.sin(radians)
  65. //
  66. // // 使用fillText绘制文本
  67. // // this.context.fillText(`${angle}°`, textX, textY)
  68. // })
  69. }
  70. aboutToAppear(): void {
  71. // this.drawCircleWithCustomRadii()
  72. }
  73. build() {
  74. Column(){
  75. Canvas(this.context)
  76. .width(this.drawWidth)
  77. .height(this.drawHeight)
  78. .backgroundColor(Color.Transparent)
  79. .onReady(() => {
  80. this.drawCircleWithCustomRadii()
  81. })
  82. }.width(this.drawWidth)
  83. .height(this.drawHeight)
  84. }
  85. }