index.js 34 KB

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