Canvas.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. //ClASS:渲染器类型
  2. function Canvas(layer) {
  3. this.layer = layer;
  4. this.canvas = document.getElementById("dynamiccanvas2d");
  5. this.context = this.canvas.getContext("2d");
  6. // 背景层canvas 栅栏格子
  7. this.backgroundcanvas = document.getElementById("backgroundcanvas2d");
  8. this.backgroundcontext = this.backgroundcanvas.getContext("2d");
  9. // 选择层canvas
  10. this.selectcanvas = document.getElementById("selectcanvas2d");
  11. this.selectcontext = this.selectcanvas.getContext("2d");
  12. this.reset = "reset";
  13. //只有当lock为false的时候才会执行绘制。
  14. this.lock = true;
  15. this.setSize(this.layer.size);
  16. this.geometrys = {};
  17. //grid
  18. this.gridGeometrys;
  19. //单位:米
  20. this.meter;
  21. this.interval = 10 * this.layer.res;
  22. this.layer.div.appendChild(this.canvas);
  23. };
  24. //设置canvas元素的大小。
  25. Canvas.prototype.setSize = function (size) {
  26. this.canvas.width = size.w;
  27. this.canvas.height = size.h;
  28. this.canvas.style.width = size.w + "px";
  29. this.canvas.style.height = size.h + "px";
  30. this.backgroundcanvas.width = size.w;
  31. this.backgroundcanvas.height = size.h;
  32. this.backgroundcanvas.style.width = size.w + "px";
  33. this.backgroundcanvas.style.height = size.h + "px";
  34. this.selectcanvas.width = size.w;
  35. this.selectcanvas.height = size.h;
  36. this.selectcanvas.style.width = size.w + "px";
  37. this.selectcanvas.style.height = size.h + "px";
  38. };
  39. // 绘制放大缩小层canvas
  40. Canvas.prototype.drawBackGround = function () {
  41. this.backgroundcontext.clearRect(0, 0, this.layer.size.w, this.layer.size.h);
  42. this.addGrid();
  43. this.addMeter();
  44. };
  45. // 绘画格子
  46. Canvas.prototype.addGrid = function () {
  47. var leftdown = {
  48. x: 0,
  49. y: height
  50. };
  51. var rightup = {
  52. x: width,
  53. y: 0
  54. };
  55. leftdown = this.layer.getPositionFromPx(leftdown);
  56. rightup = this.layer.getPositionFromPx(rightup);
  57. var Maxh = height;
  58. var Maxw = width;
  59. var Minw = 0;
  60. var Minh = 0;
  61. if (leftdown.y < -backgroundcanvasMax / 2) {
  62. leftdown.y = -backgroundcanvasMax / 2;
  63. var t = this.getLocalXY(leftdown);
  64. Maxh = t.y;
  65. }
  66. if (leftdown.x < -backgroundcanvasMax / 2) {
  67. leftdown.x = -backgroundcanvasMax / 2;
  68. var t = this.getLocalXY(leftdown);
  69. Minw = t.x;
  70. }
  71. if (rightup.x > backgroundcanvasMax / 2) {
  72. rightup.x = backgroundcanvasMax / 2;
  73. var t = this.getLocalXY(rightup);
  74. Maxw = t.x;
  75. }
  76. if (rightup.y > backgroundcanvasMax / 2) {
  77. rightup.y = backgroundcanvasMax / 2;
  78. }
  79. var downcount = Math.floor(leftdown.y / stepy);
  80. var leftcount = Math.floor(leftdown.x / stepx);
  81. var upcount = Math.floor(rightup.y / stepy);
  82. var rightcount = Math.floor(rightup.x / stepx);
  83. var context = this.backgroundcontext;
  84. context.save();
  85. var startPoint = {
  86. x: leftcount * stepx,
  87. y: downcount * stepy
  88. };
  89. var endPoint = {
  90. x: rightcount * stepx,
  91. y: upcount * stepy
  92. };
  93. context.shadowColor = undefined;
  94. context.beginPath();
  95. startPoint = this.getLocalXY(startPoint);
  96. endPoint = this.getLocalXY(endPoint);
  97. var count = 0;
  98. for (var i = leftcount; i <= rightcount; ++i) {
  99. context.moveTo(startPoint.x + this.getInt(count * stepx / this.layer.res), 0);
  100. context.lineTo(startPoint.x + this.getInt(count * stepx / this.layer.res), Maxh);
  101. ++count;
  102. }
  103. count = 0;
  104. for (var i = downcount; i <= upcount; ++i) {
  105. context.moveTo(Minw, this.getInt(startPoint.y - count * stepy / this.layer.res));
  106. context.lineTo(Maxw, this.getInt(startPoint.y - count * stepy / this.layer.res));
  107. ++count;
  108. }
  109. for (var key in gridStyle) {
  110. context[key] = gridStyle[key];
  111. }
  112. context.stroke();
  113. context.restore();
  114. };
  115. Canvas.prototype.addMeter = function () {
  116. var context = this.backgroundcontext;
  117. context.save();
  118. context.globalAlpha = 1;
  119. context.beginPath();
  120. var pt1 = {
  121. x: meterpositionX,
  122. y: height - meterpositionX
  123. };
  124. var pt2 = {
  125. x: meterpositionX,
  126. y: height - meterpositionX + 5
  127. };
  128. var pt3 = {
  129. x: meterpositionX + meter / this.layer.res,
  130. y: height - meterpositionX + 5
  131. };
  132. var pt4 = {
  133. x: meterpositionX + meter / this.layer.res,
  134. y: height - meterpositionX
  135. };
  136. context.moveTo(pt1.x, pt1.y);
  137. context.lineTo(pt2.x, pt2.y);
  138. context.lineTo(pt3.x, pt3.y);
  139. context.lineTo(pt4.x, pt4.y);
  140. context.stroke();
  141. context.font = textStyle.font;
  142. context.fillStyle = textStyle.fillStyle;
  143. context.globalAlpha = textStyle.globalAlpha;
  144. context.fillText("1m", pt1.x + meter / (2 * this.layer.res) - 6, pt1.y + 20);
  145. context.restore();
  146. };
  147. //这个方法用于收集所有需要绘制的矢量元素。
  148. Canvas.prototype.drawGeometry = function (geometry, style, contextindex) {
  149. console.log(arguments)
  150. this.geometrys[geometry.id] = [geometry, style];
  151. //如果渲染器没有被锁定则可以进行重绘。
  152. if (!this.lock) {
  153. this.redraw(contextindex);
  154. }
  155. };
  156. //只画当前的元素,别的不画
  157. Canvas.prototype.drawSingleGeometry = function (geometry, style) {
  158. this.geometrys[geometry.id] = [geometry, style];
  159. this.draw(geometry, style, geometry.contextIndex);
  160. };
  161. Canvas.prototype.clearCanvas = function (contextIndex) {
  162. if (contextIndex == this.layer.parameter.contextIndex) {
  163. this.context.clearRect(0, 0, width, height);
  164. } else if (contextIndex == this.layer.parameter.backgroundcontext) {
  165. this.backgroundcontext.clearRect(0, 0, width, height);
  166. } else if (contextIndex == this.layer.parameter.selectcontext) {
  167. this.selectcontext.clearRect(0, 0, width, height);
  168. }
  169. };
  170. /*
  171. this.refreshCanvas=false;
  172. this.refreshBackgroundCanvas=false;
  173. this.refreshSelectCanvas=false;
  174. */
  175. Canvas.prototype.autoRedraw = function () {
  176. ++TESTREFRESH;
  177. if (this.layer.control.refreshCanvas) {
  178. this.redraw(0);
  179. //console.log("刷新:"+TESTREFRESH);
  180. this.layer.control.refreshCanvas = false;
  181. }
  182. if (this.layer.control.refreshBackgroundCanvas) {
  183. this.redraw(1);
  184. //console.log("刷新背景:"+TESTREFRESH);
  185. this.layer.control.refreshBackgroundCanvas = false;
  186. }
  187. if (this.layer.control.refreshSelectCanvas) {
  188. this.redraw(2);
  189. //console.log("刷新选择:"+TESTREFRESH);
  190. this.layer.control.refreshSelectCanvas = false;
  191. }
  192. };
  193. //每次绘制都是全部清除,全部重绘。
  194. //todo加入快照后可以大大提高性能。
  195. Canvas.prototype.redraw = function (contextindex) {
  196. //this.context.clearRect(0, 0, this.layer.size.w, this.layer.size.h);
  197. var context = this.getContext(contextindex);
  198. context.clearRect(0, 0, width, height);
  199. this.setCanvasStyle("reset", contextindex);
  200. //0是正常,1是背景,2是选中
  201. if (contextindex == 1) {
  202. this.drawBackGround();
  203. } else {
  204. var geometry, style;
  205. if (!this.lock) {
  206. // 画墙
  207. for (var i = 0; i < this.layer.data2d.wallIds.length; ++i) {
  208. var geometryid = this.layer.vectors[this.layer.data2d.wallIds[i]].geometry.id;
  209. geometry = this.geometrys[geometryid][0];
  210. if (geometry.contextIndex == contextindex) {
  211. //
  212. if (geometry.floor == this.layer.selectFloor) {
  213. style = this.geometrys[geometryid][1];
  214. this.draw(geometry, style, geometry.contextIndex);
  215. }
  216. //else if((geometry.floor == this.layer.selectFloor-1)||(this.layer.selectFloor==1&&geometry.floor==2))
  217. else if ((geometry.floor == this.layer.selectFloor - 1)) {
  218. style = this.geometrys[geometryid][1];
  219. this.draw(geometry, style, geometry.contextIndex, this.layer.parameter.downfloorstyle);
  220. }
  221. }
  222. }
  223. if (this.layer.data2d.temp_wallId != null) {
  224. var geometryid = this.layer.vectors[this.layer.data2d.temp_wallId].geometry.id;
  225. geometry = this.geometrys[geometryid][0];
  226. if (geometry.contextIndex == contextindex) {
  227. style = this.geometrys[geometryid][1];
  228. this.draw(geometry, style, geometry.contextIndex);
  229. }
  230. }
  231. for (var id in this.geometrys) {
  232. if (this.geometrys[id][0].geoType == "Wall") {
  233. continue;
  234. }
  235. if (this.geometrys.hasOwnProperty(id)) {
  236. geometry = this.geometrys[id][0];
  237. if ((geometry.symbolType == "door" || geometry.symbolType == "window") && geometry.floor != this.layer.selectFloor) {
  238. continue;
  239. }
  240. /*
  241. if(geometry.geoType=="WinderStair")
  242. {
  243. style = this.geometrys[id][1];
  244. this.draw(geometry, style, geometry.contextIndex);
  245. }
  246. */
  247. else if (geometry.contextIndex == contextindex) {
  248. style = this.geometrys[id][1];
  249. this.draw(geometry, style, geometry.contextIndex);
  250. }
  251. }
  252. }
  253. }
  254. }
  255. };
  256. //每一个矢量元素的绘制,这里我们在以后会添加更多的矢量图形。
  257. Canvas.prototype.draw = function (geometry, style, contextIndex, floorstyle) {
  258. var strategy = {
  259. "Point": 'drawPoint',
  260. "Circle": 'drawCircle',
  261. "Line": 'drawLine',
  262. "Text": 'drawText',
  263. "Wall": 'drawWall',
  264. "OpenDoor": 'drawOpen',
  265. "OpenWindow": 'drawOpen',
  266. "SimpleDoor": 'drawSimpleDoor',
  267. "BiFoldDoor": 'drawBiFoldDoor',
  268. "SlidingDoor": 'drawSlidingDoor',
  269. "SingleCasement": 'drawSingleCasement',
  270. "DoubleCasement": 'drawDoubleCasement',
  271. "SlidingWindow": 'drawSlidingWindow',
  272. "WinderStair": 'drawWinderStair',
  273. "Spiral": 'drawSpiral',
  274. "Sector": 'drawSector',
  275. "Sector2": 'drawSector2',
  276. "LineMeasure": 'drawLineWithMeasure'
  277. }
  278. this[strategy[geometry.geoType]](geometry, style, contextIndex)
  279. //{todo} 我们在这里判断各种矢量要素的绘制。
  280. };
  281. Canvas.prototype.getContext = function (contextIndex) {
  282. if (contextIndex == 0) {
  283. return this.context;
  284. } else if (contextIndex == 1) {
  285. return this.backgroundcontext;
  286. } else if (contextIndex == 2) {
  287. return this.selectcontext;
  288. } else {
  289. alert("Canvas:237");
  290. return null;
  291. }
  292. };
  293. //针对点的绘制方法。
  294. Canvas.prototype.drawPoint = function (geometry, style, contextIndex) {
  295. this.setCanvasStyle(style, contextIndex);
  296. this.rendererPoint(geometry, contextIndex);
  297. this.setCanvasStyle(this.reset, contextIndex);
  298. };
  299. Canvas.prototype.rendererPoint = function (geometry, contextIndex) {
  300. var radius = geometry.radius;
  301. var twoPi = Math.PI * 2;
  302. var pt = this.getLocalXY(geometry);
  303. var context = this.getContext(contextIndex);
  304. context.beginPath();
  305. context.moveTo(pt.x, pt.y);
  306. context.arc(pt.x, pt.y, radius, 0, twoPi, true);
  307. context.closePath();
  308. context.fill();
  309. };
  310. //针对圆的绘制方法。
  311. Canvas.prototype.drawCircle = function (geometry, style, contextIndex) {
  312. this.setCanvasStyle(style, contextIndex);
  313. this.rendererCircle(geometry, style, contextIndex);
  314. this.setCanvasStyle(this.reset, contextIndex);
  315. };
  316. Canvas.prototype.rendererCircle = function (geometry, rendererType, contextIndex) {
  317. var radius = geometry.radius;
  318. var twoPi = Math.PI * 2;
  319. var pt = this.getLocalXY(geometry);
  320. var context = this.getContext(contextIndex);
  321. context.beginPath();
  322. context.arc(pt.x, pt.y, radius, 0, twoPi, true);
  323. if (rendererType.f_fill) {
  324. context.fill();
  325. }
  326. if (rendererType.f_stroke) {
  327. context.stroke();
  328. }
  329. };
  330. Canvas.prototype.drawSector2 = function (geometry, style, contextIndex) {
  331. //this.setCanvasStyle(style,contextIndex);
  332. this.rendererSector2(geometry, style, contextIndex);
  333. this.setCanvasStyle(this.reset, contextIndex);
  334. };
  335. Canvas.prototype.rendererSector2 = function (geometry, style, contextIndex) {
  336. var points = geometry.points;
  337. var len = 2;
  338. var pt = [];
  339. for (var i = 0; i < points.length; ++i) {
  340. var temp = this.getLocalXY(points[i]);
  341. pt.push(temp);
  342. }
  343. this.setCanvasStyle(openStyle, contextIndex);
  344. var context = this.getContext(contextIndex);
  345. context.lineWidth = openStyle.lineWidth / this.layer.res;
  346. context.beginPath();
  347. var start = this.getLocalXY(points[0]);
  348. var x = start.x;
  349. var y = start.y;
  350. if (!isNaN(x) && !isNaN(y)) {
  351. context.moveTo(x, y);
  352. var p = this.getLocalXY(points[3]);
  353. context.lineTo(p.x, p.y);
  354. if (openStyle.f_fill) {
  355. context.fill();
  356. }
  357. if (openStyle.f_stroke) {
  358. context.stroke();
  359. }
  360. }
  361. this.setCanvasStyle(style, contextIndex);
  362. context = this.getContext(contextIndex);
  363. context.lineWidth = style.lineWidth / this.layer.res;
  364. context.beginPath();
  365. context.moveTo(pt[0].x, pt[0].y);
  366. context.lineTo(pt[1].x, pt[1].y);
  367. context.arcTo(pt[2].x, pt[2].y, pt[3].x, pt[3].y, geometry.r / this.layer.res);
  368. context.stroke();
  369. /*
  370. context.fillStyle=style.fillStyle;
  371. context.strokeStyle=style.strokeStyle;
  372. context.beginPath();
  373. context.moveTo(pt[0].x, pt[0].y);
  374. context.lineTo(pt[3].x, pt[3].y);
  375. context.stroke();
  376. */
  377. };
  378. Canvas.prototype.drawSector = function (geometry, style, contextIndex) {
  379. this.setCanvasStyle(style, contextIndex);
  380. this.rendererSector(geometry, style, contextIndex);
  381. this.setCanvasStyle(this.reset, contextIndex);
  382. };
  383. Canvas.prototype.drawDoubleCasement = function (geometry, style, contextIndex) {
  384. this.setCanvasStyle(style, contextIndex);
  385. this.rendererDoubleCasement(geometry, style, contextIndex);
  386. this.setCanvasStyle(this.reset, contextIndex);
  387. };
  388. Canvas.prototype.rendererDoubleCasement = function (geometry, style, contextIndex) {
  389. var points = geometry.points;
  390. this.setCanvasStyle(openStyle, contextIndex);
  391. var context = this.getContext(contextIndex);
  392. //var width=this.layer.getThickness2(geometry);
  393. var width = geometry.thick;
  394. context.lineWidth = width / this.layer.res;
  395. context.beginPath();
  396. var p1 = this.getLocalXY(geometry.point1);
  397. var p2 = this.getLocalXY(geometry.point2);
  398. context.moveTo(p1.x, p1.y);
  399. context.lineTo(p2.x, p2.y);
  400. if (openStyle.f_fill) {
  401. context.fill();
  402. }
  403. if (openStyle.f_stroke) {
  404. context.stroke();
  405. }
  406. this.setCanvasStyle(style, contextIndex);
  407. context = this.getContext(contextIndex);
  408. context.lineWidth = style.lineWidth / this.layer.res;
  409. var p3 = this.getLocalXY(geometry.points[0]);
  410. var p4 = this.getLocalXY(geometry.points[1]);
  411. context.beginPath();
  412. context.moveTo(p1.x, p1.y);
  413. context.lineTo(p2.x, p2.y);
  414. context.lineTo(p3.x, p3.y);
  415. context.lineTo(p4.x, p4.y);
  416. context.closePath();
  417. context.stroke();
  418. var r = BABYLON.Vector2.Distance(p1, p2) / 2;
  419. context.beginPath();
  420. context.moveTo(p2.x, p2.y);
  421. context.arc(p2.x, p2.y, r, geometry.points[2], geometry.points[3]);
  422. context.closePath();
  423. context.stroke();
  424. context.beginPath();
  425. context.moveTo(p1.x, p1.y);
  426. context.arc(p1.x, p1.y, r, geometry.points[4], geometry.points[5]);
  427. context.closePath();
  428. context.stroke();
  429. }
  430. Canvas.prototype.drawSingleCasement = function (geometry, style, contextIndex) {
  431. this.setCanvasStyle(style, contextIndex);
  432. this.rendererSingleCasement(geometry, style, contextIndex);
  433. this.setCanvasStyle(this.reset, contextIndex);
  434. };
  435. Canvas.prototype.rendererSingleCasement = function (geometry, style, contextIndex) {
  436. var points = geometry.points;
  437. this.setCanvasStyle(openStyle, contextIndex);
  438. var context = this.getContext(contextIndex);
  439. //var width=this.layer.getThickness2(geometry);
  440. var width = geometry.thick;
  441. context.lineWidth = width / this.layer.res;
  442. context.beginPath();
  443. var p1 = this.getLocalXY(geometry.point1);
  444. var p2 = this.getLocalXY(geometry.point2);
  445. context.moveTo(p1.x, p1.y);
  446. context.lineTo(p2.x, p2.y);
  447. if (openStyle.f_fill) {
  448. context.fill();
  449. }
  450. if (openStyle.f_stroke) {
  451. context.stroke();
  452. }
  453. this.setCanvasStyle(style, contextIndex);
  454. context = this.getContext(contextIndex);
  455. context.lineWidth = style.lineWidth / this.layer.res;
  456. var p3 = this.getLocalXY(geometry.points[0]);
  457. var p4 = this.getLocalXY(geometry.points[1]);
  458. context.beginPath();
  459. context.moveTo(p1.x, p1.y);
  460. context.lineTo(p2.x, p2.y);
  461. context.lineTo(p3.x, p3.y);
  462. context.lineTo(p4.x, p4.y);
  463. context.closePath();
  464. context.stroke();
  465. context.beginPath();
  466. context.moveTo(p2.x, p2.y);
  467. //context.lineTo(p1.x,p1.y);
  468. var r = BABYLON.Vector2.Distance(p1, p2);
  469. context.arc(p2.x, p2.y, r, geometry.points[2], geometry.points[3]);
  470. //context.lineTo(p2.x,p2.y);
  471. context.closePath();
  472. context.stroke();
  473. };
  474. Canvas.prototype.drawSlidingDoor = function (geometry, style, contextIndex) {
  475. this.setCanvasStyle(style, contextIndex);
  476. this.rendererSliding(geometry, style, contextIndex);
  477. this.setCanvasStyle(this.reset, contextIndex);
  478. };
  479. Canvas.prototype.drawSlidingWindow = function (geometry, style, contextIndex) {
  480. this.setCanvasStyle(style, contextIndex);
  481. this.rendererSliding(geometry, style, contextIndex);
  482. this.setCanvasStyle(this.reset, contextIndex);
  483. }
  484. Canvas.prototype.drawBiFoldDoor = function (geometry, style, contextIndex) {
  485. this.setCanvasStyle(style, contextIndex);
  486. this.rendererBiFoldDoor(geometry, style, contextIndex);
  487. this.setCanvasStyle(this.reset, contextIndex);
  488. };
  489. Canvas.prototype.drawSpiral = function (geometry, style, contextIndex) {
  490. this.setCanvasStyle(style, contextIndex);
  491. this.rendererSpiral(geometry, style, contextIndex);
  492. this.setCanvasStyle(this.reset, contextIndex);
  493. };
  494. Canvas.prototype.drawWinderStair = function (geometry, style, contextIndex) {
  495. this.setCanvasStyle(style, contextIndex);
  496. this.rendererWinderStair(geometry, style, contextIndex);
  497. this.setCanvasStyle(this.reset, contextIndex);
  498. var measurepoints = {};
  499. measurepoints.arrows1 = geometry.arrowpoints.arrows1;
  500. measurepoints.arrows2 = geometry.arrowpoints.arrows2;
  501. measurepoints.border1 = geometry.borderpoints.border1;
  502. measurepoints.border2 = geometry.borderpoints.border2;
  503. var lines = null;
  504. if (geometry.temp_points.length > 0) {
  505. lines = this.layer.calculateElement.getWallMappingPoints(measurepoints, geometry.temp_points, 1);
  506. } else {
  507. lines = this.layer.calculateElement.getWallMappingPoints(measurepoints, geometry.points, 1);
  508. }
  509. this.setCanvasStyle(measureStyle, contextIndex);
  510. this.rendererMeasure(lines, 1, contextIndex);
  511. this.setCanvasStyle(this.reset, contextIndex);
  512. };
  513. Canvas.prototype.rendererSliding = function (geometry, style, contextIndex) {
  514. this.context.save();
  515. this.setCanvasStyle(openStyle, contextIndex);
  516. var context = this.getContext(contextIndex);
  517. context.beginPath();
  518. var pt = [];
  519. var points = geometry.points;
  520. for (var i = 0; i < points.length; ++i) {
  521. var temp = this.getLocalXY(points[i]);
  522. pt.push(temp);
  523. }
  524. //var width=this.layer.getThickness2(geometry);
  525. var width = geometry.thick;
  526. context.lineWidth = width / this.layer.res;
  527. context.beginPath();
  528. context.moveTo(pt[0].x, pt[0].y);
  529. context.lineTo(pt[1].x, pt[1].y);
  530. if (openStyle.f_fill) {
  531. context.fill();
  532. }
  533. if (openStyle.f_stroke) {
  534. context.stroke();
  535. }
  536. context.strokeStyle = "rgba(255, 255, 255, .8)";
  537. context.fillStyle = "rgba(255, 255, 255, .4)";
  538. context.lineWidth = 1;
  539. context.moveTo(pt[1].x, pt[1].y);
  540. context.lineTo(pt[0].x, pt[0].y);
  541. context.lineTo(pt[2].x, pt[2].y);
  542. context.lineTo(pt[3].x, pt[3].y);
  543. context.lineTo(pt[4].x, pt[4].y);
  544. context.lineTo(pt[5].x, pt[5].y);
  545. context.closePath();
  546. context.fill();
  547. context.stroke();
  548. this.context.restore();
  549. };
  550. Canvas.prototype.rendererSpiral = function (geometry, style, contextIndex) {
  551. var point = geometry.point;
  552. var points = geometry.points;
  553. var radius1 = this.layer.parameter.spiralR1 / this.layer.res;
  554. var radius2 = this.layer.parameter.spiralR2 / this.layer.res;
  555. var twoPi = Math.PI * 2;
  556. var pt = this.getLocalXY(point);
  557. var context = this.getContext(contextIndex);
  558. context.beginPath();
  559. context.arc(pt.x, pt.y, radius1, 0, twoPi, true);
  560. context.stroke();
  561. context.closePath();
  562. context.beginPath();
  563. for (var i = 0; i < points.length; ++i) {
  564. var p = this.getLocalXY(points[i]);
  565. context.moveTo(pt.x, pt.y);
  566. context.lineTo(p.x, p.y);
  567. context.stroke();
  568. }
  569. context.closePath();
  570. context.beginPath();
  571. context.strokeStyle = style.strokeStyle2;
  572. context.globalAlpha = style.globalAlpha2;
  573. context.globalAlpha = 1;
  574. var p1 = this.getLocalXY(geometry.point1);
  575. context.moveTo(p1.x, p1.y);
  576. context.lineTo(pt.x, pt.y);
  577. context.stroke();
  578. var p2 = this.getLocalXY(geometry.point2);
  579. context.moveTo(p2.x, p2.y);
  580. context.lineTo(pt.x, pt.y);
  581. context.stroke();
  582. context.closePath();
  583. context.beginPath();
  584. context.arc(pt.x, pt.y, radius2, 0, twoPi, true);
  585. context.stroke();
  586. context.fill();
  587. context.closePath();
  588. context.strokeStyle = style.strokeStyle2;
  589. context.globalAlpha = style.globalAlpha2;
  590. context.beginPath();
  591. context.arc(pt.x, pt.y, radius1, geometry.sAngle, geometry.eAngle);
  592. context.stroke();
  593. context.closePath();
  594. context.globalAlpha = 0.5;
  595. };
  596. Canvas.prototype.rendererWinderStair = function (geometry, style, contextIndex) {
  597. var points = geometry.points;
  598. var context = this.getContext(contextIndex);
  599. this.setCanvasStyle(style, contextIndex);
  600. if (points.length == 0) {
  601. return;
  602. } else if (points.length == 1 && geometry.linkedpoints.length == 0) {
  603. var radius = this.layer.parameter.selectCircle_R;
  604. var twoPi = Math.PI * 2;
  605. var pt = this.getLocalXY(points[0]);
  606. context.beginPath();
  607. context.moveTo(pt.x, pt.y);
  608. context.arc(pt.x, pt.y, radius, 0, twoPi, true);
  609. context.closePath();
  610. context.fill();
  611. } else if (geometry.linkedpoints.length > 0) {
  612. context.beginPath();
  613. for (var i = 0; i < geometry.linkedpoints.length; ++i) {
  614. var p1 = this.getLocalXY(geometry.linkedpoints[i][1]);
  615. var p2 = this.getLocalXY(geometry.linkedpoints[i][2]);
  616. context.moveTo(p1.x, p1.y);
  617. context.lineTo(p2.x, p2.y);
  618. context.stroke();
  619. }
  620. for (var i = 0; i < geometry.borderpoints.border1.length - 1; ++i) {
  621. var p1 = this.getLocalXY(geometry.borderpoints.border1[i]);
  622. var p2 = this.getLocalXY(geometry.borderpoints.border1[i + 1]);
  623. context.moveTo(p1.x, p1.y);
  624. context.lineTo(p2.x, p2.y);
  625. context.stroke();
  626. }
  627. for (var i = 0; i < geometry.borderpoints.border2.length - 1; ++i) {
  628. var p1 = this.getLocalXY(geometry.borderpoints.border2[i]);
  629. var p2 = this.getLocalXY(geometry.borderpoints.border2[i + 1]);
  630. context.moveTo(p1.x, p1.y);
  631. context.lineTo(p2.x, p2.y);
  632. context.stroke();
  633. }
  634. }
  635. };
  636. Canvas.prototype.rendererBiFoldDoor = function (geometry, style, contextIndex) {
  637. var points = geometry.points;
  638. this.setCanvasStyle(openStyle, contextIndex);
  639. var context = this.getContext(contextIndex);
  640. //var width=this.layer.getThickness2(geometry);
  641. var width = geometry.thick;
  642. context.lineWidth = width / this.layer.res;
  643. context.beginPath();
  644. var p1 = this.getLocalXY(geometry.point1);
  645. var p2 = this.getLocalXY(geometry.point2);
  646. context.moveTo(p1.x, p1.y);
  647. context.lineTo(p2.x, p2.y);
  648. if (openStyle.f_fill) {
  649. context.fill();
  650. }
  651. if (openStyle.f_stroke) {
  652. context.stroke();
  653. }
  654. this.setCanvasStyle(style, contextIndex);
  655. context = this.getContext(contextIndex);
  656. context.lineWidth = style.lineWidth / this.layer.res;
  657. var r = BABYLON.Vector2.Distance(geometry.point1, geometry.point2) / 2;
  658. var p3 = this.getLocalXY(geometry.points[0]);
  659. var p4 = this.getLocalXY(geometry.points[1]);
  660. var p5 = this.getLocalXY(geometry.points[2]);
  661. context.beginPath();
  662. context.moveTo(p3.x, p3.y);
  663. context.arcTo(p4.x, p4.y, p5.x, p5.y, r / this.layer.res);
  664. context.moveTo(p5.x, p5.y);
  665. context.stroke();
  666. var p6 = this.getLocalXY(geometry.points[3]);
  667. var p7 = this.getLocalXY(geometry.points[4]);
  668. var p8 = this.getLocalXY(geometry.points[5]);
  669. context.beginPath();
  670. context.moveTo(p6.x, p6.y);
  671. context.arcTo(p7.x, p7.y, p8.x, p8.y, r / this.layer.res);
  672. context.moveTo(p8.x, p8.y);
  673. context.stroke();
  674. var p9 = this.getLocalXY(geometry.points[6]);
  675. var p10 = this.getLocalXY(geometry.points[7]);
  676. var p11 = this.getLocalXY(geometry.points[8]);
  677. var p12 = this.getLocalXY(geometry.points[9]);
  678. var p13 = this.getLocalXY(geometry.points[10]);
  679. var p14 = this.getLocalXY(geometry.points[11]);
  680. context.shadowColor = "rgba(0, 0, 0, .7)";
  681. context.shadowBlur = 1;
  682. context.lineWidth = 3 / this.layer.res;
  683. context.beginPath();
  684. context.moveTo(p5.x, p5.y);
  685. context.lineTo(p9.x, p9.y);
  686. context.lineTo(p10.x, p10.y);
  687. context.lineTo(p11.x, p11.y);
  688. context.closePath();
  689. context.fill();
  690. context.stroke();
  691. context.beginPath();
  692. context.moveTo(p8.x, p8.y);
  693. context.lineTo(p12.x, p12.y);
  694. context.lineTo(p13.x, p13.y);
  695. context.lineTo(p14.x, p14.y);
  696. context.closePath();
  697. context.fill();
  698. context.stroke();
  699. };
  700. Canvas.prototype.drawSimpleDoor = function (geometry, style, contextIndex) {
  701. this.setCanvasStyle(style, contextIndex);
  702. this.rendererSimpleDoor(geometry, style, contextIndex);
  703. this.setCanvasStyle(this.reset, contextIndex);
  704. };
  705. Canvas.prototype.rendererSimpleDoor = function (geometry, style, contextIndex) {
  706. var points = geometry.points;
  707. this.setCanvasStyle(openStyle, contextIndex);
  708. var context = this.getContext(contextIndex);
  709. //var width=this.layer.getThickness2(geometry);
  710. var width = geometry.thick;
  711. context.lineWidth = width / this.layer.res;
  712. context.beginPath();
  713. var p1 = this.getLocalXY(geometry.point1);
  714. var p2 = this.getLocalXY(geometry.point2);
  715. context.moveTo(p1.x, p1.y);
  716. context.lineTo(p2.x, p2.y);
  717. if (openStyle.f_fill) {
  718. context.fill();
  719. }
  720. if (openStyle.f_stroke) {
  721. context.stroke();
  722. }
  723. this.setCanvasStyle(style, contextIndex);
  724. context = this.getContext(contextIndex);
  725. context.lineWidth = style.lineWidth / this.layer.res;
  726. context.beginPath();
  727. var p3 = this.getLocalXY(geometry.points[0]);
  728. var p4 = this.getLocalXY(geometry.points[1]);
  729. var p5 = this.getLocalXY(geometry.points[2]);
  730. var p6 = this.getLocalXY(geometry.points[3]);
  731. var p7 = this.getLocalXY(geometry.points[4]);
  732. var p8 = this.getLocalXY(geometry.points[5]);
  733. context.moveTo(p3.x, p3.y);
  734. var r = BABYLON.Vector2.Distance(geometry.point1, geometry.point2);
  735. context.arcTo(p4.x, p4.y, p5.x, p5.y, r / this.layer.res);
  736. context.moveTo(p5.x, p5.y);
  737. context.stroke();
  738. context.beginPath();
  739. context.moveTo(p5.x, p5.y);
  740. context.lineTo(p6.x, p6.y);
  741. context.lineTo(p7.x, p7.y);
  742. context.lineTo(p8.x, p8.y);
  743. context.closePath();
  744. context.shadowColor = "rgba(0, 0, 0, .7)";
  745. context.shadowBlur = 1;
  746. context.lineWidth = 3 / this.layer.res;
  747. context.fill();
  748. context.stroke();
  749. };
  750. Canvas.prototype.rendererSector = function (geometry, style, contextIndex) {
  751. var start = geometry.startAngle;
  752. var end = geometry.endAngle;
  753. var pt = this.getLocalXY(geometry);
  754. var r = 70;
  755. var context = this.getContext(contextIndex);
  756. context.beginPath();
  757. context.moveTo(pt.x, pt.y);
  758. var flag = false;
  759. if (Math.abs(end - start) > Math.PI) {
  760. flag = true;
  761. }
  762. context.arc(pt.x, pt.y, r, start, end, flag);
  763. context.closePath();
  764. context.stroke();
  765. context.fill();
  766. var angle = 180 * Math.abs(end - start) / Math.PI;
  767. if (angle > 180) {
  768. angle = 360 - angle;
  769. }
  770. var text = Math.round(angle) + " °";
  771. var textpoint;
  772. if (flag) {
  773. textpoint = {
  774. x: pt.x - r * Math.cos((end + start) / 2),
  775. y: pt.y - r * Math.sin((end + start) / 2)
  776. };
  777. } else {
  778. textpoint = {
  779. x: pt.x + r * Math.cos((end + start) / 2),
  780. y: pt.y + r * Math.sin((end + start) / 2)
  781. };
  782. }
  783. context.fillStyle = "#FAFAFA";
  784. context.beginPath();
  785. context.arc(textpoint.x, textpoint.y, context.measureText(text).width / 2 + 5, 0, 2 * Math.PI),
  786. context.fill();
  787. context.stroke();
  788. context.fillStyle = "#888";
  789. context.font = "8px sans-serif";
  790. context.textAlign = "center";
  791. context.textBaseline = "middle";
  792. context.fillText(text, textpoint.x, textpoint.y);
  793. };
  794. Canvas.prototype.drawText = function (geometry, style, contextIndex) {
  795. this.setCanvasStyle(style, contextIndex);
  796. this.rendererText(geometry, contextIndex);
  797. this.setCanvasStyle(this.reset, contextIndex);
  798. };
  799. Canvas.prototype.rendererText = function (geometry, contextIndex) {
  800. var points = geometry.points;
  801. var pt = this.getLocalXY(new Point(points[0].x, points[0].y));
  802. var context = this.getContext(contextIndex);
  803. context.fillText(geometry.name, pt.x, pt.y);
  804. };
  805. Canvas.prototype.drawLine = function (geometry, style, contextIndex) {
  806. this.setCanvasStyle(style, contextIndex);
  807. this.rendererPath(geometry, style, contextIndex);
  808. this.setCanvasStyle(this.reset, contextIndex);
  809. };
  810. Canvas.prototype.drawOpen = function (geometry, style, contextIndex) {
  811. this.setCanvasStyle(style, contextIndex);
  812. this.rendererOpen(geometry, style, contextIndex);
  813. this.setCanvasStyle(this.reset, contextIndex);
  814. };
  815. Canvas.prototype.drawSliding = function (geometry, style, contextIndex) {
  816. this.setCanvasStyle(style, contextIndex);
  817. this.rendererSliding(geometry, style, contextIndex);
  818. this.setCanvasStyle(this.reset, contextIndex);
  819. };
  820. Canvas.prototype.drawLineWithMeasure = function (geometry, rendererType, contextIndex) {
  821. //this.setCanvasStyle(style,contextIndex);
  822. //this.rendererPath(geometry, rendererType, contextIndex);
  823. //this.setCanvasStyle(this.reset,contextIndex);
  824. this.setCanvasStyle(measureStyle, contextIndex);
  825. this.drawSymbolMeasure(geometry.points[0], geometry.points[1], geometry.wallType, contextIndex);
  826. this.setCanvasStyle(this.reset, contextIndex);
  827. };
  828. Canvas.prototype.drawSymbolMeasure = function (point1, point2, type, contextIndex) {
  829. var context = this.getContext(contextIndex);
  830. context.beginPath();
  831. var calculateLine = this.layer.calculateLine;
  832. var distance = BABYLON.Vector2.Distance(point1, point2) / meter;
  833. //var arrows=calculateLine.gettwoline(point1,point2,type);
  834. var distance2;
  835. if (type == 1) {
  836. distance2 = 30;
  837. } else {
  838. distance2 = 15;
  839. }
  840. var arrows = calculateLine.getParallelLineForDistance(calculateLine.createLine(point1, point2), distance2);
  841. var mid = {
  842. x: (point1.x + point2.x) / 2,
  843. y: (point1.y + point2.y) / 2
  844. };
  845. var inpt1 = calculateLine.getJoinLinePoint(point1, arrows.line1);
  846. var inpt2 = calculateLine.getJoinLinePoint(point2, arrows.line1);
  847. var inmeter = calculateLine.getJoinLinePoint(mid, arrows.meter1);
  848. inpt1 = this.getLocalXY(inpt1);
  849. inpt2 = this.getLocalXY(inpt2);
  850. inmeter = this.getLocalXY(inmeter);
  851. context.moveTo(inpt1.x, inpt1.y);
  852. context.lineTo(inpt2.x, inpt2.y);
  853. context.stroke();
  854. context.fillText(distance.toFixed(2) + "m", inmeter.x, inmeter.y);
  855. this.drawArrow(inpt1, inpt2, contextIndex);
  856. this.drawArrow(inpt2, inpt1, contextIndex);
  857. var outpt1 = calculateLine.getJoinLinePoint(point1, arrows.line2);
  858. var outpt2 = calculateLine.getJoinLinePoint(point2, arrows.line2);
  859. var outmeter = calculateLine.getJoinLinePoint(mid, arrows.meter2);
  860. outpt1 = this.getLocalXY(outpt1);
  861. outpt2 = this.getLocalXY(outpt2);
  862. outmeter = this.getLocalXY(outmeter);
  863. context.moveTo(outpt1.x, outpt1.y);
  864. context.lineTo(outpt2.x, outpt2.y);
  865. context.stroke();
  866. context.fillText(distance.toFixed(2) + "m", outmeter.x, outmeter.y);
  867. this.drawArrow(outpt1, outpt2, contextIndex);
  868. this.drawArrow(outpt2, outpt1, contextIndex);
  869. };
  870. Canvas.prototype.drawWall = function (geometry, rendererType, contextIndex, floorstyle) {
  871. this.setCanvasStyle(rendererType, contextIndex);
  872. this.rendererWallPath(geometry, rendererType, contextIndex, floorstyle);
  873. this.setCanvasStyle(this.reset, contextIndex);
  874. if (!floorstyle) {
  875. var wallpoint = this.layer.calculateElement.computerMeasure(geometry);
  876. if (geometry.state == 1) {
  877. wallpoint = this.layer.calculateElement.changeEndPointMeasure(wallpoint, geometry);
  878. }
  879. var lines = this.layer.calculateElement.getWallMappingPoints(wallpoint, geometry.points, geometry.state);
  880. //geometry.border1=wallpoint.border1;
  881. //geometry.border2=wallpoint.border2;
  882. for (var i = 0; i < wallpoint.border1.length; ++i) {
  883. geometry.wallInfo[i].border1 = wallpoint.border1[i];
  884. geometry.wallInfo[i].border2 = wallpoint.border2[i];
  885. }
  886. this.setCanvasStyle(measureStyle, contextIndex);
  887. this.rendererMeasure(lines, geometry.state, contextIndex);
  888. this.setCanvasStyle(this.reset, contextIndex);
  889. }
  890. };
  891. Canvas.prototype.rendererMeasure2 = function (lines, geometry, contextIndex) {
  892. var context = this.getContext(contextIndex);
  893. context.beginPath();
  894. var mixlin = [];
  895. var maxlout = [];
  896. var inpt_0 = null;
  897. var inpt2_x = null;
  898. var outpt_0 = null;
  899. var outpt2_x = null;
  900. if (geometry.state == 1) {
  901. if (geometry.wallInfo[0].newarrow1 != null && typeof (geometry.wallInfo[0].newarrow1) != "undefined") {
  902. inpt_0 = this.getLocalXY(geometry.wallInfo[0].newarrow1);
  903. delete geometry.wallInfo[0].newarrow1;
  904. }
  905. if (geometry.wallInfo[lines.length].newarrow1 != null && typeof (geometry.wallInfo[lines.length].newarrow1) != "undefined") {
  906. inpt2_x = this.getLocalXY(geometry.wallInfo[lines.length].newarrow1);
  907. delete geometry.wallInfo[lines.length].newarrow1;
  908. }
  909. if (geometry.wallInfo[0].newarrow2 != null && typeof (geometry.wallInfo[0].newarrow2) != "undefined") {
  910. outpt_0 = this.getLocalXY(geometry.wallInfo[0].newarrow2);
  911. delete geometry.wallInfo[0].newarrow2;
  912. }
  913. if (geometry.wallInfo[lines.length].newarrow2 != null && typeof (geometry.wallInfo[lines.length].newarrow2) != "undefined") {
  914. outpt2_x = this.getLocalXY(geometry.wallInfo[lines.length].newarrow2);
  915. delete geometry.wallInfo[lines.length].newarrow2;
  916. }
  917. }
  918. for (var i = 0; i < lines.length; ++i) {
  919. mixlin.push(lines[i].border1Line1);
  920. maxlout.push(lines[i].border2Line1);
  921. var inpt = this.getLocalXY(lines[i].arrow1Line1);
  922. var inpt2 = this.getLocalXY(lines[i].arrow1Line2);
  923. var outpt = this.getLocalXY(lines[i].arrow2Line1);
  924. var outpt2 = this.getLocalXY(lines[i].arrow2Line2);
  925. if (i == 0) {
  926. if (inpt_0 != null) {
  927. inpt = inpt_0;
  928. }
  929. if (outpt_0 != null) {
  930. outpt = outpt_0;
  931. }
  932. }
  933. if (i == lines.length - 1) {
  934. if (inpt2_x != null) {
  935. inpt2 = inpt2_x;
  936. }
  937. if (outpt2_x != null) {
  938. outpt2 = outpt2_x;
  939. }
  940. }
  941. var indistance = lines[i].distance1;
  942. var outdistance = lines[i].distance2;
  943. var inlinecenter = this.getLocalXY(lines[i].text1pt);
  944. var outlinecenter = this.getLocalXY(lines[i].text2pt);
  945. context.moveTo(inpt.x, inpt.y);
  946. context.lineTo(inpt2.x, inpt2.y);
  947. context.stroke();
  948. context.fillText(indistance.toFixed(2) + "m", inlinecenter.x, inlinecenter.y);
  949. this.drawArrow(inpt, inpt2, contextIndex);
  950. this.drawArrow(inpt2, inpt, contextIndex);
  951. context.moveTo(outpt.x, outpt.y);
  952. context.lineTo(outpt2.x, outpt2.y);
  953. context.stroke();
  954. context.fillText(outdistance.toFixed(2) + "m", outlinecenter.x, outlinecenter.y);
  955. this.drawArrow(outpt, outpt2, contextIndex);
  956. this.drawArrow(outpt2, outpt, contextIndex);
  957. }
  958. if (geometry.state == 0) {
  959. this.drawArea(mixlin, maxlout);
  960. }
  961. };
  962. // 绘画标注线上面的文字
  963. Canvas.prototype.rendererLineTagging = function (ps, inlinecenter, indistance, contextIndex) {
  964. var context = this.getContext(contextIndex);
  965. var spaceWidth = 12
  966. var textWidth = 6
  967. var text = indistance.toFixed(2) + "m"
  968. var rectW = text.length * textWidth + 2 * spaceWidth
  969. var rectH = 2 * textWidth
  970. var start = ps[0],
  971. end = ps[1];
  972. if (Math.abs(ps[0].x - ps[1].x) > Math.abs(ps[0].y - ps[1].y)) {
  973. if (ps[0].x > ps[1].x) {
  974. start = ps[1]
  975. end = ps[0]
  976. } else {
  977. start = ps[0]
  978. end = ps[1]
  979. }
  980. } else {
  981. if (ps[0].y > ps[1].y) {
  982. start = ps[0]
  983. end = ps[1]
  984. } else {
  985. start = ps[1]
  986. end = ps[0]
  987. }
  988. }
  989. context.save();
  990. // this.context.save();
  991. var deg = Math.atan2((end.y - start.y), (end.x - start.x))
  992. context.translate(inlinecenter.x, inlinecenter.y)
  993. context.rotate(deg);
  994. context.translate(-rectW / 2, -rectH / 2)
  995. context.strokeStyle = null
  996. context.fillStyle = "#ccc";
  997. context.rect(0, 0, rectW, rectH);
  998. context.fill();
  999. context.translate(spaceWidth, textWidth + textWidth / 2)
  1000. context.fillStyle = "#000000";
  1001. context.fillText(text, 0, 0);
  1002. context.restore();
  1003. }
  1004. // 会面墙边长度
  1005. Canvas.prototype.rendererMeasure = function (lines, state, contextIndex) {
  1006. //var context = this.context;
  1007. var context = this.getContext(contextIndex);
  1008. context.beginPath();
  1009. var mixlin = [];
  1010. var maxlout = [];
  1011. for (var i = 0; i < lines.length; ++i) {
  1012. mixlin.push(lines[i].border1Line1);
  1013. maxlout.push(lines[i].border2Line1);
  1014. var inpt = this.getLocalXY(lines[i].arrow1Line1);
  1015. var inpt2 = this.getLocalXY(lines[i].arrow1Line2);
  1016. var indistance = lines[i].distance1;
  1017. var inlinecenter = this.getLocalXY(lines[i].text1pt);
  1018. // 添加箭头
  1019. this.drawArrow(inpt, inpt2, contextIndex);
  1020. this.drawArrow(inpt2, inpt, contextIndex);
  1021. context.moveTo(inpt.x, inpt.y);
  1022. context.lineTo(inpt2.x, inpt2.y);
  1023. context.stroke();
  1024. // 添加标注
  1025. this.rendererLineTagging([inpt, inpt2], inlinecenter, indistance, contextIndex)
  1026. }
  1027. if (state == 0) {
  1028. this.drawArea(mixlin, maxlout);
  1029. }
  1030. };
  1031. // 绘画箭头
  1032. Canvas.prototype.drawArrow = function (p0, p1, index) {
  1033. var _ANGLE = 20;
  1034. var _RADIUS = 10.0;
  1035. var _ANGLE_CROSS = 90.0;
  1036. var context;
  1037. if (typeof (index) == "undefined") {
  1038. context = this.context;
  1039. } else {
  1040. context = this.getContext(index);
  1041. }
  1042. var nP = {};
  1043. var angle = Math.atan2(p0.x - p1.x, p0.y - p1.y);
  1044. this.context.save();
  1045. //rotate by _ANGLE
  1046. var tAngle = angle - _ANGLE * Math.PI / 180.0;
  1047. nP.x = parseFloat(p1.x) + _RADIUS * Math.sin(tAngle);
  1048. nP.y = parseFloat(p1.y) + _RADIUS * Math.cos(tAngle);
  1049. context.beginPath();
  1050. context.moveTo(p1.x, p1.y);
  1051. context.lineTo(nP.x, nP.y);
  1052. context.stroke();
  1053. //lower part
  1054. tAngle = angle + _ANGLE * Math.PI / 180.0;
  1055. nP.x = parseFloat(p1.x) + _RADIUS * Math.sin(tAngle);
  1056. nP.y = parseFloat(p1.y) + _RADIUS * Math.cos(tAngle);
  1057. context.lineJoin = "miter"
  1058. context.beginPath();
  1059. context.moveTo(p1.x, p1.y);
  1060. context.lineTo(nP.x, nP.y);
  1061. context.stroke();
  1062. this.context.restore();
  1063. };
  1064. Canvas.prototype.drawArea = function (mixlin, maxlout) {
  1065. var calculateLine = this.layer.calculateLine;
  1066. var context = this.context;
  1067. this.context.save();
  1068. if (mixlin.length != 0) {
  1069. var mixlinposition = [];
  1070. for (var i = 0; i < mixlin.length; ++i) {
  1071. var pt = this.getLocalXY(mixlin[i]);
  1072. mixlinposition.push(pt);
  1073. }
  1074. var centerpoint = calculateLine.getCenterPoint(mixlinposition);
  1075. var area = calculateLine.polygonArea(mixlin);
  1076. var area2 = calculateLine.polygonArea(maxlout);
  1077. context.textBaseline = "middle";
  1078. context.textAlign = "center";
  1079. context.fillText("使用面积:" + area + "m²/建筑面积:" + area2 + "m²", centerpoint.x, centerpoint.y);
  1080. document.getElementById("subsubMenuContainer").innerHTML = area2 + " m2";
  1081. }
  1082. this.context.restore();
  1083. };
  1084. //
  1085. Canvas.prototype.rendererWallPath = function (geometry, rendererType, contextIndex, floorstyle) {
  1086. var points = geometry.points;
  1087. var len = points.length;
  1088. var context = this.getContext(contextIndex);
  1089. context.beginPath();
  1090. if (geometry.wallType == 1) {
  1091. context.lineWidth = wallThickness / this.layer.res;
  1092. } else {
  1093. context.lineWidth = partitionThickness / this.layer.res;
  1094. }
  1095. var img = geometry.image;
  1096. context.strokeStyle = '#383838';
  1097. // context.createPattern(img, "repeat");
  1098. context.lineCap = 'butt';
  1099. if (floorstyle) {
  1100. context.globalAlpha = floorstyle;
  1101. }
  1102. var start = this.getLocalXY(points[0]);
  1103. var x = start.x;
  1104. var y = start.y;
  1105. if (!isNaN(x) && !isNaN(y)) {
  1106. context.moveTo(x, y);
  1107. if (len == 1) {
  1108. return;
  1109. } else if (len == 2) {
  1110. var pt = this.getLocalXY(points[1]);
  1111. context.lineTo(pt.x, pt.y);
  1112. if (rendererType.f_stroke) {
  1113. context.stroke();
  1114. }
  1115. return;
  1116. }
  1117. for (var i = 1; i < len; ++i) {
  1118. var pt = this.getLocalXY(points[i]);
  1119. context.lineTo(pt.x, pt.y);
  1120. }
  1121. if (geometry.state == 0) {
  1122. context.closePath();
  1123. if (rendererType.f_fill) {
  1124. context.fillStyle = 'white'; //填充白色
  1125. context.fill();
  1126. }
  1127. }
  1128. if (rendererType.f_stroke) {
  1129. context.stroke();
  1130. }
  1131. }
  1132. };
  1133. Canvas.prototype.rendererPath = function (geometry, rendererType, contextIndex) {
  1134. var points = geometry.points;
  1135. var len = points.length;
  1136. var context = this.getContext(contextIndex);
  1137. if (rendererType.name == "open") {
  1138. rendererType.lineWidth = geometry.width / this.layer.res;
  1139. }
  1140. context.lineWidth = rendererType.lineWidth;
  1141. context.beginPath();
  1142. var start = this.getLocalXY(points[0]);
  1143. var x = start.x;
  1144. var y = start.y;
  1145. if (!isNaN(x) && !isNaN(y)) {
  1146. context.moveTo(x, y);
  1147. for (var i = 1; i < len; ++i) {
  1148. var pt = this.getLocalXY(points[i]);
  1149. context.lineTo(pt.x, pt.y);
  1150. if (rendererType.f_fill) {
  1151. context.fill();
  1152. }
  1153. if (rendererType.f_stroke) {
  1154. context.stroke();
  1155. }
  1156. }
  1157. }
  1158. };
  1159. Canvas.prototype.rendererOpen = function (geometry, rendererType, contextIndex) {
  1160. var points = geometry.points;
  1161. var len = points.length;
  1162. var context = this.getContext(contextIndex);
  1163. //var width=this.layer.getThickness2(geometry);
  1164. var width = geometry.thick
  1165. context.lineWidth = width / this.layer.res;
  1166. context.beginPath();
  1167. var start = this.getLocalXY(points[0]);
  1168. var x = start.x;
  1169. var y = start.y;
  1170. if (!isNaN(x) && !isNaN(y)) {
  1171. context.moveTo(x, y);
  1172. for (var i = 1; i < len; ++i) {
  1173. var pt = this.getLocalXY(points[i]);
  1174. context.lineTo(pt.x, pt.y);
  1175. if (rendererType.f_fill) {
  1176. context.fill();
  1177. }
  1178. if (rendererType.f_stroke) {
  1179. context.stroke();
  1180. }
  1181. }
  1182. }
  1183. };
  1184. //设置canvas的样式。
  1185. Canvas.prototype.setCanvasStyle = function (style, contextIndex) {
  1186. if (contextIndex == null || typeof (contextIndex) == "undefined") {
  1187. return;
  1188. }
  1189. var context = this.getContext(contextIndex);
  1190. if (style == "reset") {
  1191. //for(var key in defaultStyle)
  1192. //{
  1193. // context[key]=defaultStyle[key];
  1194. //}
  1195. context.restore();
  1196. return;
  1197. } else {
  1198. context.save();
  1199. for (var key in style) {
  1200. if (key != "fill" && key != "stroke") {
  1201. context[key] = style[key];
  1202. }
  1203. }
  1204. return;
  1205. }
  1206. };
  1207. //获得一个点的屏幕显示位置。
  1208. Canvas.prototype.getLocalXY = function (point) {
  1209. var resolution = this.layer.getRes();
  1210. var extent = this.layer.bounds;
  1211. var x = (point.x / resolution + (-extent.left / resolution));
  1212. var y = ((extent.top / resolution) - point.y / resolution);
  1213. x = (0.5 + x) << 0;
  1214. y = (0.5 + y) << 0;
  1215. return {
  1216. x: Math.floor(x),
  1217. y: Math.floor(y)
  1218. };
  1219. };
  1220. Canvas.prototype.getInt = function (value) {
  1221. value = (0.5 + value) << 0;
  1222. return value;
  1223. };