Canvas.js 40 KB

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