tpanorama.js 24 KB

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