index.js 33 KB

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