index.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. (() => {
  2. // 初始地图
  3. const initMap = (map) => {
  4. return {
  5. map,
  6. async loadImage(args) {
  7. const { file, minWidth, minHeight } = args
  8. args.img = args.img ?
  9. args.img :
  10. await blobImageLoad(file, minWidth, minHeight)
  11. return loadImageLayer(map, args)
  12. },
  13. screenToLatlan({ x, y }) {
  14. const real = map.getCoordinateFromPixel([x, y])
  15. const latlan = ol.proj.transform(real, 'EPSG:3857', 'EPSG:4326')
  16. return latlan
  17. }
  18. }
  19. }
  20. const loadImageLayer = (map, args) => {
  21. const {
  22. lon,
  23. lat
  24. } = args
  25. const itude = ol.proj.fromLonLat([lon, lat])
  26. const { image: imageLayer, canvas } = loadImage(map, args, itude)
  27. map.addLayer(imageLayer);
  28. map.getView().setCenter(
  29. ol.proj.fromLonLat([lon, lat])
  30. );
  31. map.getView().setZoom(19)
  32. return canvas
  33. }
  34. // 经纬度转canvas坐标
  35. const itudeToCanvasPos = (map, extent, itude) => {
  36. //Canvas四至范围不同于当前地图四至范围,计算出南北方向与东西方向的偏移
  37. const mapExtent = map.getView()
  38. .calculateExtent(map.getSize())
  39. //当前底图视图范围的投影坐标
  40. const canvasOrigin = map.getPixelFromCoordinate(
  41. [extent[0], extent[3]]
  42. );
  43. //添加到地图上的canvas图像的左上角
  44. const mapOrigin = map.getPixelFromCoordinate(
  45. [mapExtent[0], mapExtent[3]]
  46. );
  47. const delta = [
  48. mapOrigin[0] - canvasOrigin[0],
  49. mapOrigin[1] - canvasOrigin[1]
  50. ];
  51. const leftTop = map.getPixelFromCoordinate(itude)
  52. return {
  53. x: leftTop[0] + delta[0],
  54. y: leftTop[1] + delta[1]
  55. }
  56. }
  57. // 平移,旋转,放大当前canvas
  58. const transformCanvasCall = (
  59. canvas,
  60. transform,
  61. oper,
  62. center = {
  63. x: 0,
  64. y: 0
  65. }
  66. ) => {
  67. const ctx = canvas.getContext('2d')
  68. const {
  69. translate,
  70. scale,
  71. rotate
  72. } = transform
  73. ctx.translate(center.x, center.y)
  74. translate && ctx.translate(translate.x, translate.y)
  75. rotate && ctx.rotate(rotate * (Math.PI / 180))
  76. scale && ctx.scale(scale[0], scale[1])
  77. oper && oper()
  78. // scale && ctx.scale(1 / scale, 1 / scale)
  79. // rotate && ctx.rotate(-rotate * (Math.PI / 180))
  80. // translate && ctx.translate(-translate.x, -translate.y)
  81. ctx.translate(-center.x, -center.y)
  82. }
  83. const genImgCanvasItudeToReal = (map, canvas, extent) =>
  84. (itude) => {
  85. return genImgCanvasPosToReal(map, canvas)(
  86. itudeToCanvasPos(map, extent, itude)
  87. )
  88. }
  89. const genImgCanvasPosToReal = (map, canvas) =>
  90. (pos) => {
  91. const $real = map.getViewport()
  92. const offsetWidth = (canvas.width - $real.offsetWidth) / 2
  93. const offsetHeight = (canvas.height - $real.offsetHeight) / 2
  94. return {
  95. x: pos.x - offsetWidth,
  96. y: pos.y - offsetHeight
  97. }
  98. }
  99. const genImgCanvasTransfrom = (canvas, arrayImgs, scale, initPos) =>
  100. (transform) => {
  101. const ctx = canvas.getContext('2d');
  102. const dscale = transform.scale || [1, 1]
  103. const resize = 1 / (scale * 10)
  104. const doScale = [
  105. resize * dscale[0],
  106. resize * dscale[1]
  107. ]
  108. const imgData = { width: 0, height: 0 }
  109. arrayImgs.forEach(imgs => {
  110. let height = 0
  111. imgs.forEach(([img]) => height += img.height)
  112. imgData.width += imgs[0][0].width
  113. if (imgData.height < height) {
  114. imgData.height = height
  115. }
  116. })
  117. initPos.x -= imgData.width / 2
  118. initPos.y -= imgData.height / 2
  119. // , translate: { x: -(imgData.width / 2) * doScale[0], y: -(imgData.height / 2) * doScale[1] }
  120. ctx.fillStyle = 'rgba(0,0,0,0.1)'
  121. ctx.fillRect(0, 0, canvas.width, canvas.height)
  122. transformCanvasCall(
  123. canvas, {...transform, scale: doScale },
  124. () => {
  125. transform.draw && transform.draw(ctx)
  126. let width = 0
  127. arrayImgs.forEach(imgs => {
  128. let height = 0
  129. imgs.forEach(([img]) => {
  130. ctx.drawImage(img, width, height)
  131. height += img.height
  132. })
  133. width += imgs[0][0].width
  134. })
  135. },
  136. transform.center
  137. )
  138. const move = {
  139. x: transform.translate.x - initPos.x,
  140. y: transform.translate.y - initPos.y,
  141. }
  142. const start = {
  143. x: initPos.x + move.x,
  144. y: initPos.y + move.y,
  145. }
  146. const end = {
  147. x: start.x + imgData.width * doScale[0],
  148. y: start.y + imgData.height * doScale[1],
  149. }
  150. canvas.position = [
  151. start,
  152. end,
  153. Math.abs(start.x - end.x) / resize,
  154. Math.abs(start.y - end.y) / resize
  155. ]
  156. canvas.resize = resize
  157. canvas.imgData = imgData
  158. canvas.imgBox = [
  159. canvas.posToReal(start),
  160. canvas.posToReal(end),
  161. Math.abs(start.x - end.x),
  162. Math.abs(start.y - end.y)
  163. ]
  164. }
  165. // 加载url
  166. const canvas = document.createElement('canvas')
  167. const loadImage = (map, args, itude) => {
  168. console.log('create canvas')
  169. const imageCanvas = new ol.source.ImageCanvas({
  170. canvasFunction(extent, scale, _2, size) {
  171. const pos = itudeToCanvasPos(map, extent, itude)
  172. const imgData = { width: 0, height: 0 }
  173. args.img.forEach(imgs => {
  174. let height = 0
  175. imgs.forEach(([img]) => height += img.height)
  176. imgData.width += imgs[0][0].width
  177. if (imgData.height < height) {
  178. imgData.height = height
  179. }
  180. })
  181. // pos.x -= imgData.width / 2 * scale
  182. // pos.y -= imgData.height / 2 * scale
  183. canvas.width = size[0];
  184. canvas.height = size[1]
  185. canvas.posToReal = genImgCanvasPosToReal(map, canvas);
  186. canvas.transform = genImgCanvasTransfrom(canvas, args.img, scale, pos, imageCanvas);
  187. canvas.itudeToReal = genImgCanvasItudeToReal(map, canvas, extent)
  188. canvas.transform({
  189. ...args,
  190. translate: {
  191. x: (args.translate ? args.translate.x : 0) + pos.x,
  192. y: (args.translate ? args.translate.y : 0) + pos.y
  193. }
  194. })
  195. return canvas;
  196. }
  197. })
  198. const image = new ol.layer.Image({ source: imageCanvas })
  199. canvas.imageLayer = imageCanvas
  200. return {
  201. image,
  202. canvas
  203. }
  204. }
  205. // 返回本地url
  206. const blobImageLoad = (arrayImages, minWidth, minHeight) => {
  207. const analysis = (blob) => new Promise((resolve, reject) => {
  208. const url = typeof blob !== 'string' ?
  209. window.URL.createObjectURL(blob) :
  210. blob
  211. const img = new Image()
  212. img.onload = () => {
  213. if (img.width < minWidth || img.height < minHeight) {
  214. reject('图片宽高需要大于512')
  215. } else {
  216. resolve([img, url, blob])
  217. }
  218. }
  219. img.src = url
  220. })
  221. let arrasPromises = []
  222. for (let images of arrayImages) {
  223. let analys = []
  224. for (let bolb of images) {
  225. analys.push(analysis(bolb))
  226. }
  227. arrasPromises.push(
  228. Promise.all(analys)
  229. )
  230. }
  231. return Promise.all(arrasPromises)
  232. }
  233. // 获取逆转矩阵
  234. const getCanvasInverImatrix = $canvas => {
  235. const ctx = $canvas.getContext('2d')
  236. const transform = ctx.getTransform()
  237. return transform.invertSelf();
  238. }
  239. // canvas坐标转屏幕坐标
  240. const getCanvasToScreenPos = ($canvas, { x, y }) => {
  241. const {
  242. a,
  243. b,
  244. c,
  245. d,
  246. e,
  247. f
  248. } = getCanvasInverImatrix($canvas)
  249. const screenX = (c * y - d * x + d * e - c * f) / (b * c - a * d)
  250. const screenY = (y - screenX * b - f) / d
  251. return {
  252. x: Math.round(screenX),
  253. y: Math.round(screenY),
  254. }
  255. }
  256. // 屏幕坐标转canvas坐标
  257. const getScreenToCanvasPos = ($canvas, { x, y }) => {
  258. const {
  259. a,
  260. b,
  261. c,
  262. d,
  263. e,
  264. f
  265. } = getCanvasInverImatrix($canvas)
  266. return {
  267. x: Math.round(x * a + y * c + e),
  268. y: Math.round(x * b + y * d + f)
  269. };
  270. }
  271. const sceneName = window.location.pathname.split('/')[2]
  272. const isDev = !sceneName || sceneName === 'addDataSet.html'
  273. const sceneCode = isDev ? 't-kJ2PEjZ' : window.location.pathname.split('/')[2]
  274. const root = isDev ? `https://testlaser.4dkankan.com` : ''
  275. // const root = 'http://192.168.0.135:9294'
  276. const request = {
  277. uploadFiles(files) {
  278. const fromData = new FormData()
  279. files.forEach(({ dir, file }) => {
  280. fromData.append(dir, file)
  281. })
  282. return axios({
  283. headers: { 'Content-Type': 'multipart/form-data' },
  284. method: 'POST',
  285. data: fromData,
  286. url: `${root}/indoor/${sceneCode}/api/mapSmall/upload`
  287. })
  288. },
  289. getDetail() {
  290. return axios.post(`${root}/indoor/${sceneCode}/api/mapSmall/detail`)
  291. },
  292. updateCoord(data) {
  293. return axios.post(
  294. `${root}/indoor/${sceneCode}/api/update/coord`, { param: data }
  295. )
  296. },
  297. getSceneInfo() {
  298. return axios.get(`${root}/indoor/${sceneCode}/api/datasets`)
  299. }
  300. }
  301. const analysisFiles = (files) => {
  302. const imagesArray = []
  303. const formatError = () => {
  304. alert('目录不规范 请上传 z/x/y.png 格式目录,且在最底级目录放置图片文件')
  305. }
  306. let imagesXYZ = {}
  307. for (let dir in files) {
  308. let file = files[dir]
  309. let locals = dir.split(/[\\|//]/)
  310. if (locals.length < 3) return formatError()
  311. let current = imagesXYZ
  312. for (let i = 0; i < locals.length; i++) {
  313. let dir = locals[i]
  314. if (i !== locals.length - 1) {
  315. if (!current[dir]) {
  316. current[dir] = i === locals.length - 2 ? [] : {}
  317. }
  318. current = current[dir]
  319. if (i === locals.length - 3) {
  320. current.key = 'z'
  321. }
  322. }
  323. if (i === locals.length - 1) {
  324. current.push(file)
  325. }
  326. }
  327. }
  328. (function analysis(updateXYZ) {
  329. if (updateXYZ.key === 'z') {
  330. imagesXYZ = updateXYZ
  331. return;
  332. }
  333. const names = Object.keys(updateXYZ).sort((a, b) => b - a)
  334. names.forEach(key => {
  335. if (key !== names[0]) {
  336. delete updateXYZ[key]
  337. }
  338. })
  339. analysis(updateXYZ[names[0]])
  340. })(imagesXYZ);
  341. if (!(imagesXYZ && imagesXYZ.key === 'z' && !Array.isArray(imagesXYZ))) {
  342. return formatError()
  343. }
  344. for (let key in imagesXYZ) {
  345. if (!Array.isArray(imagesXYZ[key]) && key !== 'key') {
  346. return formatError()
  347. }
  348. }
  349. delete imagesXYZ.key
  350. Object.keys(imagesXYZ).sort((a, b) => a - b).forEach(key => {
  351. imagesArray.push(
  352. imagesXYZ[key].sort((a, b) => parseInt(a.name) - parseInt(b))
  353. )
  354. })
  355. return imagesArray
  356. }
  357. // 目录:<input type="file" @change="imageChange" directory webkitdirectory multiple>
  358. Vue.component('imageTranform', {
  359. props: ['mapOl'],
  360. name: 'imageTranform',
  361. template: `
  362. <div class="transform-layer" @mousemove.stop.prevent="move" @mouseup="upMove">
  363. <div class="upload-layer">
  364. 单文件:<input type="file" @change="imageChange">
  365. </div>
  366. <div class="ctrls" :style="boxStyle" @mousedown.stop.prevent="startMove($event, 'move')"></div>
  367. <div class="cctrls" v-if="box.tl">
  368. <span class="tl" :style="{left: box.tl.x + 'px', top: box.tl.y + 'px'}" @mousedown.prevent.stop="startMove($event, 'scale', 'tl')"></span>
  369. <span class="tc" :style="{left: box.tc.x + 'px', top: box.tc.y + 'px'}" @mousedown.prevent.stop="startMove($event, 'scale', 'tc')"></span>
  370. <span class="tr" :style="{left: box.tr.x + 'px', top: box.tr.y + 'px'}" @mousedown.prevent.stop="startMove($event, 'scale', 'tr')"></span>
  371. <span class="rc" :style="{left: box.rc.x + 'px', top: box.rc.y + 'px'}" @mousedown.prevent.stop="startMove($event, 'scale', 'rc')"></span>
  372. <span class="lc" :style="{left: box.lc.x + 'px', top: box.lc.y + 'px'}" @mousedown.prevent.stop="startMove($event, 'scale', 'lc')"></span>
  373. <span class="br" :style="{left: box.br.x + 'px', top: box.br.y + 'px'}" @mousedown.prevent.stop="startMove($event, 'scale', 'br')"></span>
  374. <span class="bl" :style="{left: box.bl.x + 'px', top: box.bl.y + 'px'}" @mousedown.prevent.stop="startMove($event, 'scale', 'bl')"></span>
  375. <span class="bc" :style="{left: box.bc.x + 'px', top: box.bc.y + 'px'}" @mousedown.prevent.stop="startMove($event, 'scale', 'bc')"></span>
  376. <span class="cc" :style="{left: box.cc.x + 'px', top: box.cc.y + 'px'}" @mousedown.prevent.stop="startMove($event, 'rotate')"></span>
  377. </div>
  378. <div class="box-info" v-if="boxPos.tl">
  379. <div v-for="(item, key) in boxPos" :key="key">
  380. <span>{{key}}</span>
  381. <span>{{item}}</span>
  382. </div>
  383. </div>
  384. </div>
  385. `,
  386. data() {
  387. return {
  388. isHover: false,
  389. box: {},
  390. left: 0,
  391. top: 0
  392. }
  393. },
  394. methods: {
  395. imageChange(e) {
  396. const files = e.target.files;
  397. if (files && files[0]) {
  398. const file = files[0];
  399. // onload 里面不能用this
  400. let img = new Image();
  401. img.src = window.URL.createObjectURL(file);
  402. img.onload = async() => {
  403. if (img.width % 256 == 0 && img.height % 256 == 0) {
  404. let imagesArray = []
  405. if (e.target.files.length > 1) {
  406. const files = {}
  407. for (let file of e.target.files) {
  408. files[file.webkitRelativePath] = file
  409. }
  410. imagesArray = analysisFiles(files)
  411. } else {
  412. imagesArray = [
  413. [e.target.files[0]]
  414. ]
  415. }
  416. if (this.imgCanvas) {
  417. ctx = this.imgCanvas.getContext('2d')
  418. ctx.clearRect(-10000, -10000, 10000, 10000)
  419. this.imgCanvas.imageLayer.refresh()
  420. }
  421. await this.drawCanvas(imagesArray, [], {
  422. lat: this.lat,
  423. lon: this.lon
  424. })
  425. } else {
  426. alert('图片宽高需为256的倍数')
  427. }
  428. };
  429. }
  430. },
  431. async drawCanvas(imagesArray, transfroms, { lat, lon } = {}) {
  432. try {
  433. this.transfroms = transfroms || []
  434. this.args = {
  435. draw: (ctx) => {
  436. this.transfroms.forEach(transform => {
  437. transform.forEach(({ translate, scale, rotate, center }) => {
  438. // 设置绘制颜色
  439. center && ctx.translate(center.x, center.y)
  440. translate && ctx.translate(translate.x, translate.y)
  441. rotate && ctx.rotate(rotate * (Math.PI / 180))
  442. scale && ctx.scale(scale[0], scale[1])
  443. center && ctx.translate(-center.x, -center.y)
  444. // if (center) {
  445. // ctx.fillStyle = "geend";
  446. // // 绘制成矩形
  447. // ctx.fillRect(center.x, center.y, 100, 100);
  448. // }
  449. })
  450. })
  451. setTimeout(() => {
  452. this.updateBox(this.imgCanvas.imgBox)
  453. })
  454. },
  455. file: imagesArray,
  456. lon: lon || 113.59963069739054,
  457. lat: lat || 22.364821730960752,
  458. translate: { x: 0, y: 0 },
  459. scale: [1, 1],
  460. direction: 0
  461. }
  462. this.imgCanvas = await this.map.loadImage(this.args)
  463. } catch (e) {
  464. console.error(e)
  465. alert(e)
  466. }
  467. },
  468. updateBox() {
  469. const calcPos = pos => getCanvasToScreenPos(this.imgCanvas, pos)
  470. this.box = {
  471. tl: this.imgCanvas.posToReal(calcPos({ x: 0, y: 0 })),
  472. tc: this.imgCanvas.posToReal(calcPos({ x: this.imgCanvas.imgData.width / 2, y: 0 })),
  473. tr: this.imgCanvas.posToReal(calcPos({ x: this.imgCanvas.imgData.width, y: 0 })),
  474. rc: this.imgCanvas.posToReal(calcPos({ x: this.imgCanvas.imgData.width, y: this.imgCanvas.imgData.height / 2 })),
  475. lc: this.imgCanvas.posToReal(calcPos({ x: 0, y: this.imgCanvas.imgData.height / 2 })),
  476. br: this.imgCanvas.posToReal(calcPos({ x: this.imgCanvas.imgData.width, y: this.imgCanvas.imgData.height })),
  477. bl: this.imgCanvas.posToReal(calcPos({ x: 0, y: this.imgCanvas.imgData.height })),
  478. bc: this.imgCanvas.posToReal(calcPos({ x: this.imgCanvas.imgData.width / 2, y: this.imgCanvas.imgData.height })),
  479. cc: this.imgCanvas.posToReal(calcPos({ x: this.imgCanvas.imgData.width / 2, y: this.imgCanvas.imgData.height / 2 })),
  480. }
  481. let maxX = this.box.tl.x
  482. let minX = this.box.tl.x
  483. let maxY = this.box.tl.y
  484. let minY = this.box.tl.y
  485. Object.values(this.box).forEach(({ x, y }) => {
  486. x > maxX && (maxX = x)
  487. y > maxY && (maxY = y)
  488. x < minX && (minX = x)
  489. y < minY && (minY = y)
  490. })
  491. this.box.width = Math.abs(maxX - minX)
  492. this.box.height = Math.abs(maxY - minY)
  493. },
  494. mapStartHandle() {
  495. this.mapDown = true
  496. },
  497. moveHandle(e) {
  498. if (!this.imgCanvas || !this.imgCanvas.imgBox) {
  499. return;
  500. }
  501. if (this.moveing && this.oper) {
  502. this.move(e)
  503. } else {
  504. this.mapDown && this.imgCanvas.imageLayer.refresh()
  505. // const [start, end] = this.box
  506. // this.isHover = e.clientX > start.x && e.clientX < end.x &&
  507. // e.clientY > start.y && e.clientY < end.y
  508. }
  509. },
  510. startMove(ev, oper, dir) {
  511. this.startTransform = {
  512. ...this.args
  513. }
  514. this.transfroms.push([])
  515. this.moveing = true
  516. this.oper = oper
  517. this.dir = dir
  518. this.startMovePos = {
  519. x: ev.clientX,
  520. y: ev.clientY
  521. }
  522. },
  523. move(ev) {
  524. if (!this.moveing) return;
  525. const transfrom = this.transfroms[this.transfroms.length - 1]
  526. const start = getScreenToCanvasPos(
  527. this.imgCanvas,
  528. this.startMovePos
  529. )
  530. const end = getScreenToCanvasPos(
  531. this.imgCanvas, { x: ev.clientX, y: ev.clientY }
  532. )
  533. const move = {
  534. x: end.x - start.x,
  535. y: end.y - start.y
  536. }
  537. if (this.oper === 'move') {
  538. transfrom.push({ translate: move })
  539. } else if (this.oper === 'scale') {
  540. const width = this.imgCanvas.position[2]
  541. const height = this.imgCanvas.position[3]
  542. let xScale, yScale
  543. switch (this.dir) {
  544. case 'tl':
  545. xScale = (width - move.x) / width
  546. yScale = (height - move.y) / height
  547. if (xScale > 0.1 && yScale > 0.1) {
  548. transfrom.push({
  549. scale: [xScale, yScale],
  550. center: { x: this.imgCanvas.position[2], y: this.imgCanvas.position[3] }
  551. })
  552. }
  553. break;
  554. case 'tc':
  555. yScale = (height - move.y) / height
  556. if (yScale > 0.1) {
  557. transfrom.push({
  558. scale: [1, yScale],
  559. center: { x: 0, y: this.imgCanvas.position[3] }
  560. })
  561. }
  562. break;
  563. case 'tr':
  564. xScale = (width + move.x) / width
  565. yScale = (height - move.y) / height
  566. if (xScale > 0.1 && yScale > 0.1) {
  567. transfrom.push({
  568. scale: [xScale, yScale],
  569. center: { x: 0, y: this.imgCanvas.position[3] }
  570. })
  571. }
  572. break;
  573. case 'rc':
  574. xScale = (width + move.x) / width
  575. if (xScale > 0.1) {
  576. transfrom.push({
  577. scale: [xScale, 1],
  578. center: { x: 0, y: this.imgCanvas.position[3] }
  579. })
  580. }
  581. break;
  582. case 'lc':
  583. xScale = (width - move.x) / width
  584. if (xScale > 0.1) {
  585. transfrom.push({
  586. scale: [xScale, 1],
  587. center: { x: this.imgCanvas.position[2], y: this.imgCanvas.position[3] }
  588. })
  589. }
  590. break;
  591. case 'br':
  592. xScale = (width + move.x) / width
  593. yScale = (height + move.y) / height
  594. if (xScale > 0.1 && yScale > 0.1) {
  595. transfrom.push({
  596. scale: [xScale, yScale],
  597. center: { x: 0, y: 0 }
  598. })
  599. }
  600. break;
  601. case 'bl':
  602. xScale = (width - move.x) / width
  603. yScale = (height + move.y) / height
  604. if (xScale > 0.1 && yScale > 0.1) {
  605. transfrom.push({
  606. scale: [xScale, yScale],
  607. center: { x: this.imgCanvas.position[2], y: 0 }
  608. })
  609. }
  610. break;
  611. case 'bc':
  612. yScale = (height + move.y) / height
  613. if (yScale > 0.1) {
  614. transfrom.push({
  615. scale: [1, yScale],
  616. center: { x: 0, y: 0 }
  617. })
  618. }
  619. break;
  620. }
  621. } else if (this.oper === 'rotate') {
  622. let move = ev.clientX - this.startMovePos.x
  623. let height = this.imgCanvas.position[3]
  624. let width = this.imgCanvas.position[2]
  625. let center = { x: width / 2, y: height / 2 }
  626. // let zrotate = transfrom.
  627. transfrom.push({
  628. rotate: move / 3,
  629. center: center
  630. })
  631. }
  632. this.startMovePos = {
  633. x: ev.clientX,
  634. y: ev.clientY
  635. }
  636. this.imgCanvas.imageLayer.refresh()
  637. },
  638. upMove() {
  639. this.moveing = false
  640. this.mapDown = false
  641. this.oper = null
  642. this.dir = null
  643. this.startMovePos = null
  644. },
  645. uploadData() {
  646. if (!this.args) {
  647. return Promise.resolve(true)
  648. }
  649. const promises = []
  650. const files = []
  651. for (let i = 0; i < this.args.img.length; i++) {
  652. const images = this.args.img[i]
  653. for (let j = 0; j < images.length; j++) {
  654. const file = images[j][2]
  655. if (typeof file !== 'string') {
  656. const suffix = file.type.substr(file.type.indexOf('/') + 1)
  657. files.push({ dir: `${i}/${j}.${suffix}`, file })
  658. }
  659. }
  660. }
  661. if (files.length) {
  662. promises.push(
  663. request.uploadFiles(files)
  664. )
  665. }
  666. promises.push(
  667. request.updateCoord({
  668. ...this.boxPos,
  669. transfroms: this.transfroms
  670. })
  671. )
  672. return Promise.all(promises)
  673. },
  674. getInfo() {
  675. return {
  676. pos: this.boxPos,
  677. img: this.args.img
  678. }
  679. }
  680. },
  681. computed: {
  682. boxStyle() {
  683. if (this.box && Object.keys(this.box).length) {
  684. const box = this.box
  685. return {
  686. width: box.width + 20 + 'px',
  687. height: box.height + 20 + 'px',
  688. left: box.cc.x + 'px',
  689. top: box.cc.y + 'px'
  690. }
  691. } else {
  692. return {}
  693. }
  694. },
  695. boxPos() {
  696. if (this.box && Object.keys(this.box).length) {
  697. const ret = {}
  698. for (let key in this.box) {
  699. if (key !== 'width' && key !== 'height') {
  700. ret[key] = this.map.screenToLatlan(this.box[key])
  701. }
  702. }
  703. return ret
  704. } else {
  705. return {}
  706. }
  707. }
  708. },
  709. mounted() {
  710. Promise.all([
  711. request.getDetail(),
  712. request.getSceneInfo()
  713. ]).then(async([res1, res2]) => {
  714. const {
  715. path,
  716. position
  717. } = res1.data.data
  718. const { location } = res2.data[0]
  719. if (path && path.length > 0) {
  720. const files = {}
  721. path.forEach(path => (files[path] = root + path))
  722. await this.drawCanvas(
  723. analysisFiles(files),
  724. position ? position.transfroms : [], {
  725. lat: location[1],
  726. lon: location[0],
  727. }
  728. )
  729. }
  730. this.lat = location[1]
  731. this.lon = location[0]
  732. })
  733. document.documentElement.addEventListener('mousemove', ev => {
  734. ev.stopPropagation()
  735. ev.preventDefault()
  736. this.moveHandle.bind(this)(ev)
  737. this.move.bind(this)(ev)
  738. })
  739. document.documentElement.addEventListener('mousedown', ev => {
  740. this.mapStartHandle.bind(this)(ev)
  741. })
  742. document.documentElement.addEventListener('mouseup', ev => {
  743. ev.stopPropagation()
  744. ev.preventDefault()
  745. this.upMove.bind(this)()
  746. })
  747. this.map = initMap(this.mapOl)
  748. }
  749. })
  750. })();