Canvas.js 40 KB

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