index.js 36 KB

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