index.js 24 KB

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