tpanorama.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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. sprite: 'label', // label,icon
  27. onClick: () => { }
  28. }
  29. function tpanorama(opt) {
  30. this.render(opt);
  31. }
  32. tpanorama.prototype = {
  33. constructor: this,
  34. def: {},
  35. render: function (opt) {
  36. this.def = extend(options, opt, true);
  37. document.getElementById(this.def.container).innerHTML = '';
  38. _lables = [];
  39. initContainer(this.def.container);
  40. initCamera();
  41. initRaycaster();
  42. makePanorama(this.def.pRadius, this.def.widthSegments, this.def.heightSegments, this.def.url);
  43. initRenderer();
  44. initLable(this.def.lables, this.def.sprite);
  45. _container.addEventListener('mousedown', onDocumentMouseDown, false);
  46. _container.addEventListener('mousemove', onDocumentMouseMove, false);
  47. _container.addEventListener('mouseup', onDocumentMouseUp, false);
  48. _container.addEventListener('mousewheel', (e) => {
  49. onDocumentMouseWheel(e, this.def.minFocalLength, this.def.maxFocalLength);
  50. }, false);
  51. _container.addEventListener('DOMMouseScroll', (e) => {
  52. onDocumentMouseWheel(e, this.def.minFocalLength, this.def.maxFocalLength);
  53. }, false);
  54. _container.addEventListener('click', onDocumentMouseClick.bind(this), false);
  55. global.addEventListener('resize', onWindowResize, false);
  56. animate();
  57. }
  58. }
  59. function extend(o, n, override) {
  60. for (var key in n) {
  61. if (n.hasOwnProperty(key) && (!o.hasOwnProperty(key) || override)) {
  62. o[key] = n[key];
  63. }
  64. }
  65. return o;
  66. }
  67. function isEmpty(str) {
  68. if (str == undefined || str == null || str == "" || typeof str == 'undefined') {
  69. return true;
  70. }
  71. }
  72. function initContainer(c) {
  73. _container = document.getElementById(c);
  74. }
  75. function initCamera() {
  76. _camera = new THREE.PerspectiveCamera(_fov, window.innerWidth / window.innerHeight, 1, 1100);
  77. _camera.target = new THREE.Vector3(0, 0, 0);
  78. _cameraOrtho = new THREE.OrthographicCamera(-window.innerWidth / 2, window.innerWidth / 2, window.innerHeight / 2, -window.innerHeight / 2, 1, 10);
  79. _cameraOrtho.position.z = 10;
  80. _scene = new THREE.Scene();
  81. _sceneOrtho = new THREE.Scene();
  82. }
  83. function initRaycaster() {
  84. _raycaster = new THREE.Raycaster();
  85. }
  86. function makePanorama(pRadius, widthSegments, heightSegments, u) {
  87. var mesh = new THREE.Mesh(new THREE.SphereGeometry(pRadius, widthSegments, heightSegments),
  88. new THREE.MeshBasicMaterial(
  89. { map: THREE.ImageUtils.loadTexture(u) }
  90. ));
  91. mesh.scale.x = -1;
  92. _scene.add(mesh);
  93. }
  94. function initRenderer() {
  95. _renderer = new THREE.WebGLRenderer();
  96. _renderer.setSize(window.innerWidth, window.innerHeight);
  97. _renderer.autoClear = false;
  98. _container.appendChild(_renderer.domElement);
  99. }
  100. function onDocumentMouseDown(event) {
  101. event.preventDefault();
  102. _isUserInteracting = true;
  103. _onPointerDownPointerX = event.clientX;
  104. _onPointerDownPointerY = event.clientY;
  105. _onPointerDownLon = _lon;
  106. _onPointerDownLat = _lat;
  107. }
  108. function onDocumentMouseMove(event) {
  109. if (_isUserInteracting) {
  110. _lon = (_onPointerDownPointerX - event.clientX) * 0.1 + _onPointerDownLon;
  111. _lat = (event.clientY - _onPointerDownPointerY) * 0.1 + _onPointerDownLat;
  112. }
  113. }
  114. function onDocumentMouseUp() {
  115. _isUserInteracting = false;
  116. }
  117. function onDocumentMouseClick(event) {
  118. _mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  119. _mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
  120. _raycaster.setFromCamera(_mouse, _cameraOrtho);
  121. var intersects = _raycaster.intersectObjects(_clickableObjects);
  122. intersects.forEach(this.def.onClick);
  123. }
  124. function onDocumentMouseWheel(ev, minFocalLength, maxFocalLength) {
  125. var ev = ev || window.event;
  126. var down = true;
  127. var m = _camera.getFocalLength();
  128. down = ev.wheelDelta ? ev.wheelDelta < 0 : ev.detail > 0;
  129. if (down) {
  130. if (m > minFocalLength) {
  131. m -= m * 0.05
  132. _camera.setFocalLength(m);
  133. }
  134. } else {
  135. if (m < maxFocalLength) {
  136. m += m * 0.05
  137. _camera.setFocalLength(m);
  138. }
  139. }
  140. if (ev.preventDefault) {
  141. ev.preventDefault();
  142. }
  143. return false;
  144. }
  145. function onWindowResize() {
  146. _camera.aspect = window.innerWidth / window.innerHeight;
  147. _camera.projectionMatrix.makePerspective(_fov, _camera.aspect, 1, 1100);
  148. _camera.updateProjectionMatrix();
  149. _cameraOrtho.left = -window.innerWidth / 2;
  150. _cameraOrtho.right = window.innerWidth / 2;
  151. _cameraOrtho.top = window.innerHeight / 2;
  152. _cameraOrtho.bottom = -window.innerHeight / 2;
  153. _cameraOrtho.updateProjectionMatrix();
  154. _renderer.setSize(window.innerWidth, window.innerHeight);
  155. }
  156. function initLable(lables, sprite) {
  157. if (sprite == 'label') {
  158. for (var i = 0; i < lables.length; i++) {
  159. _lables.push(createLableSprite(_sceneOrtho, lables[i].text, lables[i].position));
  160. }
  161. } else if (sprite == 'icon') {
  162. for (var i = 0; i < lables.length; i++) {
  163. _sprites.push(createSprite(lables[i].position, lables[i].logoUrl, lables[i].text));
  164. }
  165. }
  166. }
  167. function createLableSprite(scene, name, position) {
  168. var canvas1 = document.createElement('canvas');
  169. var context1 = canvas1.getContext('2d');
  170. var metrics = context1.measureText(name);
  171. var width = metrics.width * 1.5;
  172. context1.font = "10px 宋体";
  173. context1.fillStyle = "rgba(0,0,0,0.95)";
  174. context1.fillRect(0, 0, width + 8, 20 + 8);
  175. context1.fillStyle = "rgba(0,0,0,0.2)";
  176. context1.fillRect(2, 2, width + 4, 20 + 4);
  177. context1.fillStyle = "rgba(255,255,255,0.95)";
  178. context1.fillText(name, 4, 20);
  179. var texture1 = new THREE.Texture(canvas1);
  180. texture1.needsUpdate = true;
  181. var spriteMaterial = new THREE.SpriteMaterial({ map: texture1 });
  182. var sprite1 = new THREE.Sprite(spriteMaterial);
  183. sprite1.scale.set(1.0, 1.0, 1.0);
  184. sprite1.position.set(0, 0, 0);
  185. sprite1.name = name;
  186. var lable = {
  187. name: name,
  188. pos: position,
  189. canvas: canvas1,
  190. context: context1,
  191. texture: texture1,
  192. sprite: sprite1
  193. };
  194. _sceneOrtho.add(lable.sprite);
  195. _clickableObjects.push(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.onmousemove = () => { }
  404. document.getElementById(this.def.container).innerHTML = '';
  405. }
  406. }
  407. function initSetContainer(c, url) {
  408. _setContainer = document.getElementById(c);
  409. _himg = document.getElementById(_hideImgId);
  410. if (_himg != null) {
  411. document.body.removeChild(_himg);
  412. }
  413. _himg = document.createElement('img');
  414. _himg.style.visibility = 'hidden';
  415. _himg.id = _hideImgId;
  416. _himg.src = url;
  417. _cv = document.getElementById(_cvId);
  418. if (_cv != null) {
  419. _setContainer.removeChild(_cv);
  420. }
  421. _cv = document.createElement('canvas');
  422. _setContainer.appendChild(_cv);
  423. _cv.id = _cvId;
  424. _info = document.getElementById(_infoId);
  425. if (_info != null) {
  426. document.body.removeChild(_info);
  427. } else {
  428. _info = document.createElement('div');
  429. }
  430. _info.id = _infoId;
  431. _info.style.height = "40px";
  432. _info.style.width = "110px";
  433. _info.style.backgroundColor = "#3C8DBC";
  434. _info.style.display = "none";
  435. _info.style.position = "absolute";
  436. _info.style.filter = "alpha(Opacity=80)";
  437. _info.style.mozOpacity = 0.5;
  438. _info.style.opacity = 0.8;
  439. _info.style.fontFamily = "楷体";
  440. _info.style.fontWeight = "bold";
  441. _info.style.textShadow = "0 0 0.2em #fffd84";
  442. _info.style.textAlign = "center";
  443. document.body.appendChild(_info);
  444. }
  445. function adptpImg(width, url) {
  446. if (!isEmpty(width)) {
  447. _setContainer.style.width = width;
  448. }
  449. _setContainer.style.backgroundImage = '';
  450. var naturalHeight = _himg.naturalHeight;
  451. var naturalWidth = _himg.naturalWidth;
  452. var scale = naturalHeight / naturalWidth;
  453. var height = scale * _setContainer.style.width.split("px")[0];
  454. _setContainer.style.height = height + "px";
  455. setTimeout(function () {
  456. _setContainer.style.backgroundRepeat = 'no-repeat';
  457. _setContainer.style.backgroundPosition = '0% 0%';
  458. _setContainer.style.backgroundSize = 'cover';
  459. _setContainer.style.backgroundImage = "url(" + url + ")";
  460. }, 100);
  461. }
  462. function initGrid(color) {
  463. _cv.width = _setContainer.style.width.split("px")[0];
  464. _cv.height = _setContainer.style.height.split("px")[0];
  465. if (_cv.getContext) {
  466. var ctx = _cv.getContext("2d"),
  467. width = _cv.width,
  468. height = _cv.height;
  469. ctx.strokeStyle = color;
  470. for (var i = 1; i < 19; i++) {
  471. if (i == 9) {
  472. ctx.lineWidth = 3;
  473. } else {
  474. ctx.lineWidth = 0.8;
  475. }
  476. ctx.beginPath();
  477. ctx.moveTo(0, i * height / 18);
  478. ctx.lineTo(width, i * height / 18);
  479. ctx.stroke();
  480. }
  481. for (var j = 1; j < 37; j++) {
  482. if (j == 18) {
  483. ctx.lineWidth = 3;
  484. } else {
  485. ctx.lineWidth = 0.8;
  486. }
  487. ctx.beginPath();
  488. ctx.moveTo(j * width / 36, 0);
  489. ctx.lineTo(j * width / 36, height);
  490. ctx.stroke();
  491. }
  492. }
  493. }
  494. function clearCanvas() {
  495. var ctx = _cv.getContext("2d");
  496. var h = _setContainer.height;
  497. var w = _setContainer.width;
  498. ctx.clearRect(0, 0, w, h);
  499. }
  500. function initCursor() {
  501. var minX = _setContainer.offsetLeft;
  502. var maxX = minX + _setContainer.style.width.split("px")[0];
  503. var minY = _setContainer.offsetTop;
  504. var maxY = minY + _setContainer.style.height.split("px")[0];
  505. document.onmousemove = function (ev) {
  506. var oEvent = ev || event;
  507. var pos = getXY(oEvent);
  508. if (pos.x < maxX && pos.x > minX && pos.y < maxY && pos.y > minY) {
  509. _info.style.display = "block";
  510. _info.style.left = pos.x + "px";
  511. _info.style.top = pos.y + "px";
  512. updateInfoDiv(ev);
  513. } else {
  514. _info.style.display = "none";
  515. }
  516. };
  517. }
  518. function getXY(eve) {
  519. var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  520. var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
  521. return { x: scrollLeft + eve.clientX, y: scrollTop + eve.clientY };
  522. }
  523. function updateInfoDiv(e) {
  524. var position = calLonLat(e);
  525. var html = "经度:" + position.lon + "</br>" + "纬度:" + position.lat;
  526. _info.innerHTML = html;
  527. }
  528. function calLonLat(e) {
  529. var h = _setContainer.style.height.split("px")[0];
  530. var w = _setContainer.style.width.split("px")[0];
  531. var ix = _setContainer.offsetLeft;
  532. var iy = _setContainer.offsetTop;
  533. iy = iy + h;
  534. var x = e.clientX;
  535. var y = e.clientY;
  536. var lonS = (x - ix) / w;
  537. var lon = 0;
  538. if (lonS > 0.5) {
  539. lon = -(1 - lonS) * 360;
  540. } else {
  541. lon = 1 * 360 * lonS;
  542. }
  543. var latS = (iy - y) / h;
  544. var lat = 0;
  545. if (latS > 0.5) {
  546. lat = (latS - 0.5) * 180;
  547. } else {
  548. lat = (0.5 - latS) * 180 * -1
  549. }
  550. lon = lon.toFixed(2);
  551. lat = lat.toFixed(2);
  552. return { lon: lon, lat: lat };
  553. }
  554. function initLables(arr, color) {
  555. for (var i in arr) {
  556. var p = arr[i];
  557. var m = getXYByLonLat(p.lon, p.lat);
  558. drawCircle(m.x, m.y);
  559. drawText(m.x, m.y, p.text, color);
  560. }
  561. }
  562. function drawText(x, y, txt, lableColor) {
  563. var canvas = _cv;
  564. var ctx = canvas.getContext("2d");
  565. ctx.font = "bold 20px 楷体";
  566. ctx.fillStyle = lableColor;
  567. ctx.fillText(txt, x, y);
  568. }
  569. function drawCircle(x, y) {
  570. var canvas = _cv;
  571. var ctx = canvas.getContext("2d");
  572. ctx.fillStyle = "#0000ff";
  573. ctx.beginPath();
  574. ctx.arc(x, y, 5, 0, 2 * Math.PI, true);
  575. ctx.closePath();
  576. ctx.stroke();
  577. ctx.fill();
  578. ctx.fillStyle = "#ff0000";
  579. ctx.beginPath();
  580. ctx.arc(x, y, 2, 0, 2 * Math.PI, true);
  581. ctx.closePath();
  582. ctx.fill();
  583. }
  584. function getXYByLonLat(lon, lat) {
  585. var x = 0;
  586. var y = 0;
  587. var h = _setContainer.style.height.split("px")[0];
  588. var w = _setContainer.style.width.split("px")[0];
  589. if (lon > 0) {
  590. x = 1 * lon / 180 * 0.5 * w;
  591. } else {
  592. x = (1 + lon / 180) * 0.5 * w + 0.5 * w;
  593. }
  594. if (lat > 0) {
  595. y = (1 - lat / 90) * h * 0.5;
  596. } else {
  597. y = -1 * lat / 90 * 0.5 * h + 0.5 * h
  598. }
  599. return { x: x, y: y }
  600. }
  601. function addMark(e, color, text) {
  602. var pos = getXY(e);
  603. var iX = _setContainer.offsetLeft;
  604. var iY = _setContainer.offsetTop;
  605. var x = pos.x - iX;
  606. var y = pos.y - iY;
  607. drawCircle(x, y);
  608. drawText(x, y, text, color);
  609. var ll = calLonLat(e);
  610. var l = { lon: ll.lon, lat: ll.lat, text: text }
  611. _lable.push(l);
  612. return l;
  613. }
  614. function selectLable1(e) {
  615. var flag = false;
  616. var p;
  617. for (var i = 0; i < _lable.length; i++) {
  618. p = _lable[i];
  619. var m = getXYByLonLat(p.lon, p.lat);
  620. var iX = _setContainer.offsetLeft;
  621. var iY = _setContainer.offsetTop;
  622. var screenX = e.clientX;
  623. var screenY = e.clientY;
  624. var x = screenX - iX;
  625. var y = screenY - iY;
  626. var cx = x - m.x;
  627. var cy = y - m.y;
  628. var distence = Math.sqrt(cx * cx + cy * cy);
  629. if (distence <= 5) {
  630. flag = true;
  631. break;
  632. }
  633. }
  634. if (flag) {
  635. return p;
  636. } else {
  637. return {};
  638. }
  639. }
  640. function removeByValue(arr, val) {
  641. for (var i = 0; i < arr.length; i++) {
  642. if (arr[i].lon == val.lon && arr[i].lat == val.lat) {
  643. arr.splice(i, 1);
  644. break;
  645. }
  646. }
  647. }
  648. global.tpanorama = tpanorama;
  649. global.tpanoramaSetting = panoramaSetting;
  650. global.tpanoramaSetContainer = _setContainer;
  651. }(window));