tpanorama.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. 'use strict';
  2. (function (global, undefined) {
  3. var _camera, _scene, _renderer;
  4. var _cameraOrtho, _sceneOrtho;
  5. var _fov = 75;
  6. var _pRadius = 1000;
  7. var _raycaster;
  8. var _container;
  9. var _isUserInteracting = false;
  10. var _lon = 0,
  11. _lat = 0;
  12. var _onPointerDownLon = 0,
  13. _onPointerDownLat = 0;
  14. var _onPointerDownPointerX = 0,
  15. _onPointerDownPointerY = 0;
  16. var _mouse = new THREE.Vector2();
  17. var _clickableObjects = [];
  18. var _sprites = [];
  19. var _lables = [];
  20. var _count1 = 1;
  21. var options = {
  22. container: 'panoramaConianer', //容器
  23. url: 'resources/img/panorama/pano-7.jpg', //全景图路径
  24. lables: [], //标记 {position:{lon:114,lat:38},logoUrl:'lableLogo.png',text:'我是一个标记'}
  25. widthSegments: 60, //水平切段数
  26. heightSegments: 40, //垂直切段数(值小粗糙速度快,值大精细速度慢)
  27. pRadius: 1000, //全景球的半径,推荐使用默认值
  28. minFocalLength: 1, //镜头最a小拉近距离
  29. maxFocalLength: 100, //镜头最大拉近距离
  30. sprite: 'label', // label,icon
  31. onClick: function onClick() {}
  32. };
  33. function tpanorama(opt) {
  34. this.render(opt);
  35. }
  36. tpanorama.prototype = {
  37. constructor: this,
  38. def: {},
  39. render: function render(opt) {
  40. var _this = this;
  41. this.def = extend(options, opt, true);
  42. document.getElementById(this.def.container).innerHTML = '';
  43. _lables = [];
  44. initContainer(this.def.container);
  45. initCamera();
  46. initRaycaster();
  47. makePanorama(this.def.pRadius, this.def.widthSegments, this.def.heightSegments, this.def.url);
  48. initRenderer();
  49. initLable(this.def.lables, this.def.sprite);
  50. _container.addEventListener('mousedown', onDocumentMouseDown, false);
  51. _container.addEventListener('mousemove', onDocumentMouseMove, false);
  52. _container.addEventListener('mouseup', onDocumentMouseUp, false);
  53. _container.addEventListener('mousewheel', function (e) {
  54. onDocumentMouseWheel(e, _this.def.minFocalLength, _this.def.maxFocalLength);
  55. }, false);
  56. _container.addEventListener('DOMMouseScroll', function (e) {
  57. onDocumentMouseWheel(e, _this.def.minFocalLength, _this.def.maxFocalLength);
  58. }, false);
  59. _container.addEventListener('click', onDocumentMouseClick.bind(this), false);
  60. global.addEventListener('resize', onWindowResize, false);
  61. animate();
  62. }
  63. };
  64. function extend(o, n, override) {
  65. for (var key in n) {
  66. if (n.hasOwnProperty(key) && (!o.hasOwnProperty(key) || override)) {
  67. o[key] = n[key];
  68. }
  69. }
  70. return o;
  71. }
  72. function isEmpty(str) {
  73. if (str == undefined || str == null || str == "" || typeof str == 'undefined') {
  74. return true;
  75. }
  76. }
  77. function initContainer(c) {
  78. _container = document.getElementById(c);
  79. }
  80. function initCamera() {
  81. _camera = new THREE.PerspectiveCamera(_fov, window.innerWidth / window.innerHeight, 1, 1100);
  82. _camera.target = new THREE.Vector3(0, 0, 0);
  83. _cameraOrtho = new THREE.OrthographicCamera(-window.innerWidth / 2, window.innerWidth / 2, window.innerHeight / 2, -window.innerHeight / 2, 1, 10);
  84. _cameraOrtho.position.z = 10;
  85. _scene = new THREE.Scene();
  86. _sceneOrtho = new THREE.Scene();
  87. }
  88. function initRaycaster() {
  89. _raycaster = new THREE.Raycaster();
  90. }
  91. function makePanorama(pRadius, widthSegments, heightSegments, u) {
  92. var mesh = new THREE.Mesh(new THREE.SphereGeometry(pRadius, widthSegments, heightSegments), new THREE.MeshBasicMaterial({ map: THREE.ImageUtils.loadTexture(u) }));
  93. mesh.scale.x = -1;
  94. _scene.add(mesh);
  95. }
  96. function initRenderer() {
  97. _renderer = new THREE.WebGLRenderer();
  98. _renderer.setSize(window.innerWidth, window.innerHeight);
  99. _renderer.autoClear = false;
  100. _container.appendChild(_renderer.domElement);
  101. }
  102. function onDocumentMouseDown(event) {
  103. event.preventDefault();
  104. _isUserInteracting = true;
  105. _onPointerDownPointerX = event.clientX;
  106. _onPointerDownPointerY = event.clientY;
  107. _onPointerDownLon = _lon;
  108. _onPointerDownLat = _lat;
  109. }
  110. function onDocumentMouseMove(event) {
  111. if (_isUserInteracting) {
  112. _lon = (_onPointerDownPointerX - event.clientX) * 0.1 + _onPointerDownLon;
  113. _lat = (event.clientY - _onPointerDownPointerY) * 0.1 + _onPointerDownLat;
  114. }
  115. }
  116. function onDocumentMouseUp() {
  117. _isUserInteracting = false;
  118. }
  119. function onDocumentMouseClick(event) {
  120. _mouse.x = event.clientX / window.innerWidth * 2 - 1;
  121. _mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
  122. _raycaster.setFromCamera(_mouse, _cameraOrtho);
  123. var intersects = _raycaster.intersectObjects(_clickableObjects);
  124. intersects.forEach(this.def.onClick);
  125. }
  126. function onDocumentMouseWheel(ev, minFocalLength, maxFocalLength) {
  127. var ev = ev || window.event;
  128. var down = true;
  129. var m = _camera.getFocalLength();
  130. down = ev.wheelDelta ? ev.wheelDelta < 0 : ev.detail > 0;
  131. if (down) {
  132. if (m > minFocalLength) {
  133. m -= m * 0.05;
  134. _camera.setFocalLength(m);
  135. }
  136. } else {
  137. if (m < maxFocalLength) {
  138. m += m * 0.05;
  139. _camera.setFocalLength(m);
  140. }
  141. }
  142. if (ev.preventDefault) {
  143. ev.preventDefault();
  144. }
  145. return false;
  146. }
  147. function onWindowResize() {
  148. _camera.aspect = window.innerWidth / window.innerHeight;
  149. _camera.projectionMatrix.makePerspective(_fov, _camera.aspect, 1, 1100);
  150. _camera.updateProjectionMatrix();
  151. _cameraOrtho.left = -window.innerWidth / 2;
  152. _cameraOrtho.right = window.innerWidth / 2;
  153. _cameraOrtho.top = window.innerHeight / 2;
  154. _cameraOrtho.bottom = -window.innerHeight / 2;
  155. _cameraOrtho.updateProjectionMatrix();
  156. _renderer.setSize(window.innerWidth, window.innerHeight);
  157. }
  158. function initLable(lables, sprite) {
  159. if (sprite == 'label') {
  160. for (var i = 0; i < lables.length; i++) {
  161. _lables.push(createLableSprite(_sceneOrtho, lables[i].text, lables[i].position));
  162. }
  163. } else if (sprite == 'icon') {
  164. for (var i = 0; i < lables.length; i++) {
  165. _sprites.push(createSprite(lables[i].position, lables[i].logoUrl, lables[i].text));
  166. }
  167. }
  168. }
  169. function createLableSprite(scene, name, position) {
  170. var canvas1 = document.createElement('canvas');
  171. var context1 = canvas1.getContext('2d');
  172. var metrics = context1.measureText(name);
  173. var width = metrics.width * 1.5;
  174. context1.font = "10px 宋体";
  175. context1.fillStyle = "rgba(0,0,0,0.95)";
  176. context1.fillRect(0, 0, width + 8, 20 + 8);
  177. context1.fillStyle = "rgba(0,0,0,0.2)";
  178. context1.fillRect(2, 2, width + 4, 20 + 4);
  179. context1.fillStyle = "rgba(255,255,255,0.95)";
  180. context1.fillText(name, 4, 20);
  181. var texture1 = new THREE.Texture(canvas1);
  182. texture1.needsUpdate = true;
  183. var spriteMaterial = new THREE.SpriteMaterial({ map: texture1 });
  184. var sprite1 = new THREE.Sprite(spriteMaterial);
  185. sprite1.scale.set(1.0, 1.0, 1.0);
  186. sprite1.position.set(0, 0, 0);
  187. sprite1.name = name;
  188. var lable = {
  189. name: name,
  190. pos: position,
  191. canvas: canvas1,
  192. context: context1,
  193. texture: texture1,
  194. sprite: sprite1
  195. };
  196. _sceneOrtho.add(lable.sprite);
  197. _clickableObjects.push(lable.sprite);
  198. return lable;
  199. }
  200. function createSprite(position, url, name) {
  201. var textureLoader = new THREE.TextureLoader();
  202. var ballMaterial = new THREE.SpriteMaterial({
  203. map: textureLoader.load(url)
  204. });
  205. var sp1 = {
  206. pos: position,
  207. name: name,
  208. sprite: new THREE.Sprite(ballMaterial)
  209. };
  210. sp1.sprite.scale.set(32, 32, 1.0);
  211. sp1.sprite.position.set(0, 0, 0);
  212. sp1.sprite.name = name;
  213. _sceneOrtho.add(sp1.sprite);
  214. _clickableObjects.push(sp1.sprite);
  215. return sp1;
  216. }
  217. function animate() {
  218. requestAnimationFrame(animate);
  219. render();
  220. }
  221. function render() {
  222. calPosition();
  223. addSprites();
  224. runRender();
  225. }
  226. function calPosition() {
  227. _lat = Math.max(-85, Math.min(85, _lat));
  228. var phi = THREE.Math.degToRad(90 - _lat);
  229. var theta = THREE.Math.degToRad(_lon);
  230. _camera.target.x = _pRadius * Math.sin(phi) * Math.cos(theta);
  231. _camera.target.y = _pRadius * Math.cos(phi);
  232. _camera.target.z = _pRadius * Math.sin(phi) * Math.sin(theta);
  233. _camera.lookAt(_camera.target);
  234. }
  235. function addSprites() {
  236. if (typeof _sprites != "undefined") {
  237. for (var i = 0; i < _sprites.length; i++) {
  238. var wp = geoPosition2World(_sprites[i].pos.lon, _sprites[i].pos.lat);
  239. var sp = worldPostion2Screen(wp, _camera);
  240. var test = wp.clone();
  241. test.project(_camera);
  242. if (test.x > -1 && test.x < 1 && test.y > -1 && test.y < 1 && test.z > -1 && test.z < 1) {
  243. _sprites[i].sprite.scale.set(32, 32, 32);
  244. _sprites[i].sprite.position.set(sp.x, sp.y, 1);
  245. } else {
  246. _sprites[i].sprite.scale.set(1.0, 1.0, 1.0);
  247. _sprites[i].sprite.position.set(0, 0, 0);
  248. }
  249. }
  250. }
  251. if (typeof _lables != "undefined") {
  252. for (var i = 0; i < _lables.length; i++) {
  253. var wp = geoPosition2World(_lables[i].pos.lon, _lables[i].pos.lat);
  254. var sp = worldPostion2Screen(wp, _camera);
  255. var test = wp.clone();
  256. test.project(_camera);
  257. if (test.x > -1 && test.x < 1 && test.y > -1 && test.y < 1 && test.z > -1 && test.z < 1) {
  258. var metrics = _lables[i].context.measureText(_lables[i].name);
  259. var width = metrics.width * 3.5;
  260. _lables[i].sprite.scale.set(400, 150, 1.0);
  261. _lables[i].sprite.position.set(sp.x + width, sp.y - 40, 1);
  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 config(opt) {
  325. this.def = extend(setOpt, opt, true);
  326. },
  327. init: function init() {
  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 getAllLables() {
  384. return _lable;
  385. },
  386. addLable: function addLable(e, text) {
  387. var position = addMark(e, this.def.lableColor, text);
  388. },
  389. getLable: function getLable(e) {
  390. return selectLable1(e);
  391. },
  392. listen: function listen(type, fun) {
  393. _info.addEventListener(type, function (e) {
  394. fun(e);
  395. });
  396. },
  397. delete: function _delete(p) {
  398. if (!isEmpty(p.lon)) {
  399. removeByValue(_lable, p);
  400. }
  401. },
  402. clean: function clean() {
  403. document.onmousemove = function () {};
  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);