tpanorama.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. (function (global, undefined) {
  2. var _camera, _scene, _renderer;
  3. var _cameraOrtho, _sceneOrtho;
  4. var _fov = 75;
  5. var _pRadius = 1000;
  6. var _raycaster;
  7. var _container;
  8. var _isUserInteracting = false;
  9. var _lon = 0, _lat = 0;
  10. var _onPointerDownLon = 0, _onPointerDownLat = 0;
  11. var _onPointerDownPointerX = 0, _onPointerDownPointerY = 0;
  12. var _mouse = new THREE.Vector2();
  13. var _clickableObjects = [];
  14. var _sprites = [];
  15. var _lables = [];
  16. var _count1 = 1;
  17. var options = {
  18. container: 'panoramaConianer',//容器
  19. url: 'resources/img/panorama/pano-7.jpg',//全景图路径
  20. lables: [],//标记 {position:{lon:114,lat:38},logoUrl:'lableLogo.png',text:'我是一个标记'}
  21. widthSegments: 60,//水平切段数
  22. heightSegments: 40,//垂直切段数(值小粗糙速度快,值大精细速度慢)
  23. pRadius: 1000,//全景球的半径,推荐使用默认值
  24. minFocalLength: 1,//镜头最a小拉近距离
  25. maxFocalLength: 100,//镜头最大拉近距离
  26. showlable: 'show' // show,click
  27. }
  28. function tpanorama(opt) {
  29. this.render(opt);
  30. }
  31. tpanorama.prototype = {
  32. constructor: this,
  33. def: {},
  34. render: function (opt) {
  35. this.def = extend(options, opt, true);
  36. document.getElementById(this.def.container).innerHTML = '';
  37. _lables = [];
  38. initContainer(this.def.container);
  39. initCamera();
  40. initRaycaster();
  41. makePanorama(this.def.pRadius, this.def.widthSegments, this.def.heightSegments, this.def.url);
  42. initRenderer();
  43. initLable(this.def.lables, this.def.showlable);
  44. _container.addEventListener('mousedown', onDocumentMouseDown, false);
  45. _container.addEventListener('mousemove', onDocumentMouseMove, false);
  46. _container.addEventListener('mouseup', onDocumentMouseUp, false);
  47. _container.addEventListener('mousewheel', (e) => {
  48. onDocumentMouseWheel(e, this.def.minFocalLength, this.def.maxFocalLength);
  49. }, false);
  50. _container.addEventListener('DOMMouseScroll', (e) => {
  51. onDocumentMouseWheel(e, this.def.minFocalLength, this.def.maxFocalLength);
  52. }, false);
  53. _container.addEventListener('click', onDocumentMouseClick, false);
  54. global.addEventListener('resize', onWindowResize, false);
  55. animate();
  56. }
  57. }
  58. function extend(o, n, override) {
  59. for (var key in n) {
  60. if (n.hasOwnProperty(key) && (!o.hasOwnProperty(key) || override)) {
  61. o[key] = n[key];
  62. }
  63. }
  64. return o;
  65. }
  66. function isEmpty(str) {
  67. if (str == undefined || str == null || str == "" || typeof str == 'undefined') {
  68. return true;
  69. }
  70. }
  71. function initContainer(c) {
  72. _container = document.getElementById(c);
  73. }
  74. function initCamera() {
  75. _camera = new THREE.PerspectiveCamera(_fov, window.innerWidth / window.innerHeight, 1, 1100);
  76. _camera.target = new THREE.Vector3(0, 0, 0);
  77. _cameraOrtho = new THREE.OrthographicCamera(-window.innerWidth / 2, window.innerWidth / 2, window.innerHeight / 2, -window.innerHeight / 2, 1, 10);
  78. _cameraOrtho.position.z = 10;
  79. _scene = new THREE.Scene();
  80. _sceneOrtho = new THREE.Scene();
  81. }
  82. function initRaycaster() {
  83. _raycaster = new THREE.Raycaster();
  84. }
  85. function makePanorama(pRadius, widthSegments, heightSegments, u) {
  86. var mesh = new THREE.Mesh(new THREE.SphereGeometry(pRadius, widthSegments, heightSegments),
  87. new THREE.MeshBasicMaterial(
  88. { map: THREE.ImageUtils.loadTexture(u) }
  89. ));
  90. mesh.scale.x = -1;
  91. _scene.add(mesh);
  92. }
  93. function initRenderer() {
  94. _renderer = new THREE.WebGLRenderer();
  95. _renderer.setSize(window.innerWidth, window.innerHeight);
  96. _renderer.autoClear = false;
  97. _container.appendChild(_renderer.domElement);
  98. }
  99. function onDocumentMouseDown(event) {
  100. event.preventDefault();
  101. _isUserInteracting = true;
  102. _onPointerDownPointerX = event.clientX;
  103. _onPointerDownPointerY = event.clientY;
  104. _onPointerDownLon = _lon;
  105. _onPointerDownLat = _lat;
  106. }
  107. function onDocumentMouseMove(event) {
  108. if (_isUserInteracting) {
  109. _lon = (_onPointerDownPointerX - event.clientX) * 0.1 + _onPointerDownLon;
  110. _lat = (event.clientY - _onPointerDownPointerY) * 0.1 + _onPointerDownLat;
  111. }
  112. }
  113. function onDocumentMouseUp() {
  114. _isUserInteracting = false;
  115. }
  116. function onDocumentMouseClick(event) {
  117. _mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  118. _mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
  119. _raycaster.setFromCamera(_mouse, _cameraOrtho);
  120. var intersects = _raycaster.intersectObjects(_clickableObjects);
  121. intersects.forEach(function (element) {
  122. alert("Intersection: " + element.object.name);
  123. });
  124. }
  125. function onDocumentMouseWheel(ev, minFocalLength, maxFocalLength) {
  126. var ev = ev || window.event;
  127. var down = true;
  128. var m = _camera.getFocalLength();
  129. down = ev.wheelDelta ? ev.wheelDelta < 0 : ev.detail > 0;
  130. if (down) {
  131. if (m > minFocalLength) {
  132. m -= m * 0.05
  133. _camera.setFocalLength(m);
  134. }
  135. } else {
  136. if (m < maxFocalLength) {
  137. m += m * 0.05
  138. _camera.setFocalLength(m);
  139. }
  140. }
  141. if (ev.preventDefault) {
  142. ev.preventDefault();
  143. }
  144. return false;
  145. }
  146. function onWindowResize() {
  147. _camera.aspect = window.innerWidth / window.innerHeight;
  148. _camera.projectionMatrix.makePerspective(_fov, _camera.aspect, 1, 1100);
  149. _camera.updateProjectionMatrix();
  150. _cameraOrtho.left = -window.innerWidth / 2;
  151. _cameraOrtho.right = window.innerWidth / 2;
  152. _cameraOrtho.top = window.innerHeight / 2;
  153. _cameraOrtho.bottom = -window.innerHeight / 2;
  154. _cameraOrtho.updateProjectionMatrix();
  155. _renderer.setSize(window.innerWidth, window.innerHeight);
  156. }
  157. function initLable(lables, showlable) {
  158. if (showlable == 'show') {
  159. for (var i = 0; i < lables.length; i++) {
  160. _lables.push(createLableSprite(_sceneOrtho, lables[i].text, lables[i].position));
  161. }
  162. } else if (showlable == 'click') {
  163. for (var i = 0; i < lables.length; i++) {
  164. _sprites.push(createSprite(lables[i].position, lables[i].logoUrl, lables[i].text));
  165. }
  166. }
  167. }
  168. function createLableSprite(scene, name, position) {
  169. var canvas1 = document.createElement('canvas');
  170. var context1 = canvas1.getContext('2d');
  171. var metrics = context1.measureText(name);
  172. var width = metrics.width * 1.5;
  173. context1.font = "10px 宋体";
  174. context1.fillStyle = "rgba(0,0,0,0.95)"; // white border
  175. context1.fillRect(0, 0, width + 8, 20 + 8);
  176. context1.fillStyle = "rgba(0,0,0,0.2)"; // black filler
  177. context1.fillRect(2, 2, width + 4, 20 + 4);
  178. context1.fillStyle = "rgba(255,255,255,0.95)"; // text color
  179. context1.fillText(name, 4, 20);
  180. var texture1 = new THREE.Texture(canvas1);
  181. texture1.needsUpdate = true;
  182. var spriteMaterial = new THREE.SpriteMaterial({ map: texture1 });
  183. var sprite1 = new THREE.Sprite(spriteMaterial);
  184. sprite1.scale.set(1.0, 1.0, 1.0);
  185. sprite1.position.set(0, 0, 0);
  186. sprite1.name = name;
  187. var lable = {
  188. name: name,
  189. pos: position,
  190. canvas: canvas1,
  191. context: context1,
  192. texture: texture1,
  193. sprite: sprite1
  194. };
  195. _sceneOrtho.add(lable.sprite);
  196. return lable;
  197. }
  198. function createSprite(position, url, name) {
  199. var textureLoader = new THREE.TextureLoader();
  200. var ballMaterial = new THREE.SpriteMaterial({
  201. map: textureLoader.load(url)
  202. });
  203. var sp1 = {
  204. pos: position,
  205. name: name,
  206. sprite: new THREE.Sprite(ballMaterial)
  207. };
  208. sp1.sprite.scale.set(32, 32, 1.0);
  209. sp1.sprite.position.set(0, 0, 0);
  210. sp1.sprite.name = name;
  211. _sceneOrtho.add(sp1.sprite);
  212. _clickableObjects.push(sp1.sprite);
  213. return sp1;
  214. }
  215. function animate() {
  216. requestAnimationFrame(animate);
  217. render();
  218. }
  219. function render() {
  220. calPosition();
  221. addSprites();
  222. runRender();
  223. }
  224. function calPosition() {
  225. _lat = Math.max(-85, Math.min(85, _lat));
  226. var phi = THREE.Math.degToRad(90 - _lat);
  227. var theta = THREE.Math.degToRad(_lon);
  228. _camera.target.x = _pRadius * Math.sin(phi) * Math.cos(theta);
  229. _camera.target.y = _pRadius * Math.cos(phi);
  230. _camera.target.z = _pRadius * Math.sin(phi) * Math.sin(theta);
  231. _camera.lookAt(_camera.target);
  232. }
  233. function addSprites() {
  234. if (typeof (_sprites) != "undefined") {
  235. for (var i = 0; i < _sprites.length; i++) {
  236. var wp = geoPosition2World(_sprites[i].pos.lon, _sprites[i].pos.lat);
  237. var sp = worldPostion2Screen(wp, _camera);
  238. var test = wp.clone();
  239. test.project(_camera);
  240. if (test.x > -1 && test.x < 1 && test.y > -1 && test.y < 1 && test.z > -1 && test.z < 1) {
  241. _sprites[i].sprite.scale.set(32, 32, 32);
  242. _sprites[i].sprite.position.set(sp.x, sp.y, 1);
  243. }
  244. else {
  245. _sprites[i].sprite.scale.set(1.0, 1.0, 1.0);
  246. _sprites[i].sprite.position.set(0, 0, 0);
  247. }
  248. }
  249. }
  250. if (typeof (_lables) != "undefined") {
  251. for (var i = 0; i < _lables.length; i++) {
  252. var wp = geoPosition2World(_lables[i].pos.lon, _lables[i].pos.lat);
  253. var sp = worldPostion2Screen(wp, _camera);
  254. var test = wp.clone();
  255. test.project(_camera);
  256. if (test.x > -1 && test.x < 1 && test.y > -1 && test.y < 1 && test.z > -1 && test.z < 1) {
  257. var metrics = _lables[i].context.measureText(_lables[i].name);
  258. var width = metrics.width * 3.5;
  259. _lables[i].sprite.scale.set(400, 150, 1.0);
  260. _lables[i].sprite.position.set(sp.x + width, sp.y - 40, 1);
  261. }
  262. else {
  263. _lables[i].sprite.scale.set(1.0, 1.0, 1.0);
  264. _lables[i].sprite.position.set(0, 0, 0);
  265. }
  266. }
  267. }
  268. }
  269. function geoPosition2World(lon, lat) {
  270. lat = Math.max(-85, Math.min(85, lat));
  271. var phi = THREE.Math.degToRad(90 - lat);
  272. var theta = THREE.Math.degToRad(lon);
  273. var result = {
  274. x: _pRadius * Math.sin(phi) * Math.cos(theta),
  275. y: _pRadius * Math.cos(phi),
  276. z: _pRadius * Math.sin(phi) * Math.sin(theta)
  277. }
  278. return new THREE.Vector3(result.x, result.y, result.z);
  279. }
  280. function worldPostion2Screen(world_vector, camera) {
  281. var vector = world_vector.clone();
  282. vector.project(camera);
  283. var result = {
  284. x: Math.round((vector.x + 1) * window.innerWidth / 2 - window.innerWidth / 2),
  285. y: Math.round(window.innerHeight / 2 - (-vector.y + 1) * window.innerHeight / 2),
  286. z: 0
  287. };
  288. return new THREE.Vector3(result.x, result.y, result.z);
  289. }
  290. function runRender() {
  291. _renderer.clear();
  292. _renderer.render(_scene, _camera);
  293. _renderer.clearDepth();
  294. _renderer.render(_sceneOrtho, _cameraOrtho);
  295. }
  296. var _setContainer;
  297. var _hideImgId = "hideimgid825";
  298. var _himg;
  299. var _cvId = 'cv825';
  300. var _cv;
  301. var _infoId = 'info825';
  302. var _info;
  303. var _lable = [];
  304. var count = 1;
  305. var setOpt = {
  306. container: 'myDiv',//setting容器
  307. imgUrl: 'resources/img/panorama/3.jpg',
  308. width: '',//指定宽度,高度自适应
  309. showGrid: true,//是否显示格网
  310. showPosition: true,//是否显示经纬度提示
  311. lableColor: '#9400D3',//标记颜色
  312. gridColor: '#48D1CC',//格网颜色
  313. lables: [],//标记 {lon:114,lat:38,text:'标记一'}
  314. addLable: true,//开启后双击添加标记 (必须开启经纬度提示)
  315. getLable: true,//开启后右键查询标记 (必须开启经纬度提示)
  316. deleteLbale: true,//开启默认中键删除 (必须开启经纬度提示)
  317. }
  318. function panoramaSetting(opt) {
  319. this.config(opt);
  320. }
  321. panoramaSetting.prototype = {
  322. constructor: this,
  323. def: {},
  324. config: function (opt) {
  325. this.def = extend(setOpt, opt, true);
  326. },
  327. init: function () {
  328. var that = this;
  329. _lable = this.def.lables;
  330. initSetContainer(this.def.container, this.def.imgUrl);
  331. setTimeout(function () {
  332. adptpImg(that.def.width, that.def.imgUrl);
  333. clearCanvas();
  334. if (that.def.showGrid) {
  335. initGrid(that.def.gridColor);
  336. }
  337. if (that.def.showPosition) {
  338. initCursor();
  339. }
  340. initLables(that.def.lables, that.def.lableColor);
  341. var then = that;
  342. if (count == 2) {
  343. if (that.def.addLable) {
  344. _info.addEventListener("dblclick", function (e) {
  345. var text = prompt("标记名称");
  346. if (!isEmpty(text)) {
  347. addMark(e, then.def.lableColor, text);
  348. }
  349. });
  350. }
  351. if (that.def.getLable) {
  352. document.oncontextmenu = function (e) {
  353. e.preventDefault();
  354. };
  355. _info.addEventListener("mousedown", function (e) {
  356. if (e.button == 2) {
  357. var p = selectLable1(e);
  358. if (!isEmpty(p.lon)) {
  359. alert("经度" + p.lon + ",纬度" + p.lat + ",名称" + p.text);
  360. }
  361. }
  362. });
  363. }
  364. if (that.def.deleteLbale) {
  365. _info.addEventListener("mousedown", function (e) {
  366. if (e.button == 1) {
  367. var p = selectLable1(e);
  368. if (!isEmpty(p.lon)) {
  369. var c = confirm("您确认要删除该标记吗?");
  370. if (c) {
  371. removeByValue(_lable, p);
  372. that.clean();
  373. that.init();
  374. }
  375. }
  376. }
  377. });
  378. }
  379. }
  380. }, 100);
  381. count++;
  382. },
  383. getAllLables: function () {
  384. return _lable;
  385. },
  386. addLable: function (e, text) {
  387. var position = addMark(e, this.def.lableColor, text);
  388. },
  389. getLable: function (e) {
  390. return selectLable1(e);
  391. },
  392. listen: function (type, fun) {
  393. _info.addEventListener(type, function (e) {
  394. fun(e);
  395. })
  396. },
  397. delete: function (p) {
  398. if (!isEmpty(p.lon)) {
  399. removeByValue(_lable, p);
  400. }
  401. },
  402. clean: function () {
  403. document.getElementById(this.def.container).innerHTML = '';
  404. }
  405. }
  406. function initSetContainer(c, url) {
  407. _setContainer = document.getElementById(c);
  408. _himg = document.getElementById(_hideImgId);
  409. if (_himg != null) {
  410. document.body.removeChild(_himg);
  411. }
  412. _himg = document.createElement('img');
  413. _himg.style.visibility = 'hidden';
  414. _himg.id = _hideImgId;
  415. _himg.src = url;
  416. _cv = document.getElementById(_cvId);
  417. if (_cv != null) {
  418. _setContainer.removeChild(_cv);
  419. }
  420. _cv = document.createElement('canvas');
  421. _setContainer.appendChild(_cv);
  422. _cv.id = _cvId;
  423. _info = document.getElementById(_infoId);
  424. if (_info != null) {
  425. document.body.removeChild(_info);
  426. } else {
  427. _info = document.createElement('div');
  428. }
  429. _info.id = _infoId;
  430. _info.style.height = "40px";
  431. _info.style.width = "110px";
  432. _info.style.backgroundColor = "#3C8DBC";
  433. _info.style.display = "none";
  434. _info.style.position = "absolute";
  435. _info.style.filter = "alpha(Opacity=80)";
  436. _info.style.mozOpacity = 0.5;
  437. _info.style.opacity = 0.8;
  438. _info.style.fontFamily = "楷体";
  439. _info.style.fontWeight = "bold";
  440. _info.style.textShadow = "0 0 0.2em #fffd84";
  441. _info.style.textAlign = "center";
  442. document.body.appendChild(_info);
  443. }
  444. function adptpImg(width, url) {
  445. if (!isEmpty(width)) {
  446. _setContainer.style.width = width;
  447. }
  448. _setContainer.style.backgroundImage = '';
  449. var naturalHeight = _himg.naturalHeight;
  450. var naturalWidth = _himg.naturalWidth;
  451. var scale = naturalHeight / naturalWidth;
  452. var height = scale * _setContainer.style.width.split("px")[0];
  453. _setContainer.style.height = height + "px";
  454. setTimeout(function () {
  455. _setContainer.style.backgroundRepeat = 'no-repeat';
  456. _setContainer.style.backgroundPosition = '0% 0%';
  457. _setContainer.style.backgroundSize = 'cover';
  458. _setContainer.style.backgroundImage = "url(" + url + ")";
  459. }, 100);
  460. }
  461. function initGrid(color) {
  462. _cv.width = _setContainer.style.width.split("px")[0];
  463. _cv.height = _setContainer.style.height.split("px")[0];
  464. if (_cv.getContext) {
  465. var ctx = _cv.getContext("2d"),
  466. width = _cv.width,
  467. height = _cv.height;
  468. ctx.strokeStyle = color;
  469. for (var i = 1; i < 19; i++) {
  470. if (i == 9) {
  471. ctx.lineWidth = 3;
  472. } else {
  473. ctx.lineWidth = 0.8;
  474. }
  475. ctx.beginPath();
  476. ctx.moveTo(0, i * height / 18);
  477. ctx.lineTo(width, i * height / 18);
  478. ctx.stroke();
  479. }
  480. for (var j = 1; j < 37; j++) {
  481. if (j == 18) {
  482. ctx.lineWidth = 3;
  483. } else {
  484. ctx.lineWidth = 0.8;
  485. }
  486. ctx.beginPath();
  487. ctx.moveTo(j * width / 36, 0);
  488. ctx.lineTo(j * width / 36, height);
  489. ctx.stroke();
  490. }
  491. }
  492. }
  493. function clearCanvas() {
  494. var ctx = _cv.getContext("2d");
  495. var h = _setContainer.height;
  496. var w = _setContainer.width;
  497. ctx.clearRect(0, 0, w, h);
  498. }
  499. function initCursor() {
  500. var minX = _setContainer.offsetLeft;
  501. var maxX = minX + _setContainer.style.width.split("px")[0];
  502. var minY = _setContainer.offsetTop;
  503. var maxY = minY + _setContainer.style.height.split("px")[0];
  504. document.onmousemove = function (ev) {
  505. var oEvent = ev || event;
  506. var pos = getXY(oEvent);
  507. if (pos.x < maxX && pos.x > minX && pos.y < maxY && pos.y > minY) {
  508. _info.style.display = "block";
  509. _info.style.left = pos.x + "px";
  510. _info.style.top = pos.y + "px";
  511. updateInfoDiv(ev);
  512. } else {
  513. _info.style.display = "none";
  514. }
  515. };
  516. }
  517. function getXY(eve) {
  518. var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  519. var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
  520. return { x: scrollLeft + eve.clientX, y: scrollTop + eve.clientY };
  521. }
  522. function updateInfoDiv(e) {
  523. var position = calLonLat(e);
  524. var html = "经度:" + position.lon + "</br>" + "纬度:" + position.lat;
  525. _info.innerHTML = html;
  526. }
  527. function calLonLat(e) {
  528. var h = _setContainer.style.height.split("px")[0];
  529. var w = _setContainer.style.width.split("px")[0];
  530. var ix = _setContainer.offsetLeft;
  531. var iy = _setContainer.offsetTop;
  532. iy = iy + h;
  533. var x = e.clientX;
  534. var y = e.clientY;
  535. var lonS = (x - ix) / w;
  536. var lon = 0;
  537. if (lonS > 0.5) {
  538. lon = -(1 - lonS) * 360;
  539. } else {
  540. lon = 1 * 360 * lonS;
  541. }
  542. var latS = (iy - y) / h;
  543. var lat = 0;
  544. if (latS > 0.5) {
  545. lat = (latS - 0.5) * 180;
  546. } else {
  547. lat = (0.5 - latS) * 180 * -1
  548. }
  549. lon = lon.toFixed(2);
  550. lat = lat.toFixed(2);
  551. return { lon: lon, lat: lat };
  552. }
  553. function initLables(arr, color) {
  554. for (var i in arr) {
  555. var p = arr[i];
  556. var m = getXYByLonLat(p.lon, p.lat);
  557. drawCircle(m.x, m.y);
  558. drawText(m.x, m.y, p.text, color);
  559. }
  560. }
  561. function drawText(x, y, txt, lableColor) {
  562. var canvas = _cv;
  563. var ctx = canvas.getContext("2d");
  564. ctx.font = "bold 20px 楷体";
  565. ctx.fillStyle = lableColor;
  566. ctx.fillText(txt, x, y);
  567. }
  568. function drawCircle(x, y) {
  569. var canvas = _cv;
  570. var ctx = canvas.getContext("2d");
  571. ctx.fillStyle = "#0000ff";
  572. ctx.beginPath();
  573. ctx.arc(x, y, 5, 0, 2 * Math.PI, true);
  574. ctx.closePath();
  575. ctx.stroke();
  576. ctx.fill();
  577. ctx.fillStyle = "#ff0000";
  578. ctx.beginPath();
  579. ctx.arc(x, y, 2, 0, 2 * Math.PI, true);
  580. ctx.closePath();
  581. ctx.fill();
  582. }
  583. function getXYByLonLat(lon, lat) {
  584. var x = 0;
  585. var y = 0;
  586. var h = _setContainer.style.height.split("px")[0];
  587. var w = _setContainer.style.width.split("px")[0];
  588. if (lon > 0) {
  589. x = 1 * lon / 180 * 0.5 * w;
  590. } else {
  591. x = (1 + lon / 180) * 0.5 * w + 0.5 * w;
  592. }
  593. if (lat > 0) {
  594. y = (1 - lat / 90) * h * 0.5;
  595. } else {
  596. y = -1 * lat / 90 * 0.5 * h + 0.5 * h
  597. }
  598. return { x: x, y: y }
  599. }
  600. function addMark(e, color, text) {
  601. var pos = getXY(e);
  602. var iX = _setContainer.offsetLeft;
  603. var iY = _setContainer.offsetTop;
  604. var x = pos.x - iX;
  605. var y = pos.y - iY;
  606. drawCircle(x, y);
  607. drawText(x, y, text, color);
  608. var ll = calLonLat(e);
  609. var l = { lon: ll.lon, lat: ll.lat, text: text }
  610. _lable.push(l);
  611. return l;
  612. }
  613. function selectLable1(e) {
  614. var flag = false;
  615. var p;
  616. for (var i = 0; i < _lable.length; i++) {
  617. p = _lable[i];
  618. var m = getXYByLonLat(p.lon, p.lat);
  619. var iX = _setContainer.offsetLeft;
  620. var iY = _setContainer.offsetTop;
  621. var screenX = e.clientX;
  622. var screenY = e.clientY;
  623. var x = screenX - iX;
  624. var y = screenY - iY;
  625. var cx = x - m.x;
  626. var cy = y - m.y;
  627. var distence = Math.sqrt(cx * cx + cy * cy);
  628. if (distence <= 5) {
  629. flag = true;
  630. break;
  631. }
  632. }
  633. if (flag) {
  634. return p;
  635. } else {
  636. return {};
  637. }
  638. }
  639. function removeByValue(arr, val) {
  640. for (var i = 0; i < arr.length; i++) {
  641. if (arr[i].lon == val.lon && arr[i].lat == val.lat) {
  642. arr.splice(i, 1);
  643. break;
  644. }
  645. }
  646. }
  647. global.tpanorama = tpanorama;
  648. global.tpanoramaSetting = panoramaSetting;
  649. global.tpanoramaSetContainer = _setContainer;
  650. }(window));