CustomImage.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import VectorType from '../enum/VectorType.js'
  2. import Geometry from './Geometry'
  3. import { mathUtil } from '../MathUtil.js'
  4. import SelectState from '../enum/SelectState.js'
  5. import { coordinate } from '../Coordinate'
  6. export default class CustomImage extends Geometry {
  7. constructor(url,center,vectorId) {
  8. super()
  9. this.center = center //实际上是左下角
  10. this.url = url;
  11. this.image = null;
  12. this.width = 40;
  13. this.height = 30;
  14. this.angle = 0 //逆时针为负,顺时针为正。单位是:°
  15. this.scale = 1 //缩放比例
  16. this.ratio = 1;
  17. this.points = [];
  18. this.geoType = VectorType.CustomImage
  19. this.setId(vectorId)
  20. }
  21. isContain(position) {
  22. // if(Math.abs(position.x - this.center.x)* coordinate.res * coordinate.zoom/ 100 <this.ratio*this.width/2 && Math.abs(position.y - this.center.y)* coordinate.res * coordinate.zoom/ 100<this.ratio*this.height/2)
  23. // {
  24. // return SelectState.Select
  25. // } else {
  26. // return null
  27. // }
  28. // this.context.arc(pt.x-geometry.ratio * geometry.width/4*geometry.scale, pt.y-geometry.ratio * geometry.height/4*geometry.scale, 2 * coordinate.ratio, 0, Math.PI * 2, true)
  29. let p0 = {
  30. x:this.center.x - this.ratio*this.width/2/coordinate.res*this.scale,
  31. y:this.center.y + this.ratio*this.height/2/coordinate.res*this.scale
  32. }
  33. let p1 = {
  34. x:this.center.x,
  35. y:this.center.y + this.ratio*this.height/2/coordinate.res*this.scale
  36. }
  37. let p2 = {
  38. x:this.center.x,
  39. y:this.center.y
  40. }
  41. let p3 = {
  42. x:this.center.x- this.ratio*this.width/2/coordinate.res*this.scale,
  43. y:this.center.y
  44. }
  45. let center = {
  46. x:this.center.x - this.ratio*this.width/4/coordinate.res*this.scale,
  47. y:this.center.y + this.ratio*this.height/4/coordinate.res*this.scale
  48. }
  49. p0 = this.rotatePoint(p0, center, this.angle)
  50. p1 = this.rotatePoint(p1, center, this.angle)
  51. p2 = this.rotatePoint(p2, center, this.angle)
  52. p3 = this.rotatePoint(p3, center, this.angle)
  53. //let points = [];
  54. this.points = [];
  55. this.points.push(p0)
  56. this.points.push(p3)
  57. this.points.push(p2)
  58. this.points.push(p1)
  59. if(mathUtil.isPointInPoly(position, this.points)){
  60. return SelectState.Select
  61. }else {
  62. return null
  63. }
  64. }
  65. // ptSrc: 圆上某点(初始点);
  66. // ptRotationCenter: 圆心点;
  67. // angle: 旋转角度° -- [angle * M_PI / 180]:将角度换算为弧度
  68. // 【注意】angle 逆时针为正,顺时针为负
  69. rotatePoint(ptSrc, ptRotationCenter, angle) {
  70. angle = -1 * angle //设计是逆时针为负,顺时针为正
  71. var a = ptRotationCenter.x
  72. var b = ptRotationCenter.y
  73. var x0 = ptSrc.x
  74. var y0 = ptSrc.y
  75. var rx = a + (x0 - a) * Math.cos((angle * Math.PI) / 180) - (y0 - b) * Math.sin((angle * Math.PI) / 180)
  76. var ry = b + (x0 - a) * Math.sin((angle * Math.PI) / 180) + (y0 - b) * Math.cos((angle * Math.PI) / 180)
  77. var json = { x: rx, y: ry }
  78. return json
  79. }
  80. setImageData(imgData){
  81. this.image = imgData;
  82. }
  83. setUrl(url){
  84. this.url = url;
  85. }
  86. setAngle(angle){
  87. this.angle = angle;
  88. }
  89. setScale(scale){
  90. this.scale = scale;
  91. }
  92. setRatio(ratio){
  93. this.ratio = ratio;
  94. }
  95. }