Changeset 0c6b92a for imaps-frontend/src/scripts/main/MapDisplay.js
- Timestamp:
- 12/12/24 17:06:06 (5 weeks ago)
- Branches:
- main
- Parents:
- d565449
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
imaps-frontend/src/scripts/main/MapDisplay.js
rd565449 r0c6b92a 3 3 import HttpService from "../net/HttpService.js"; 4 4 import {zoomStage} from "../util/zoomStage.js"; 5 import {addEventHandling} from "../util/addEventHandling.js"; 6 import triggerNavigate from "../util/triggerNavigate.js"; 7 import config from "../net/netconfig.js"; 8 5 9 export class MapDisplay { 6 constructor(containerId) { 7 this.container = document.getElementById(containerId); 8 this.containerId = containerId; 9 this.stage = new Konva.Stage({ 10 container: containerId, 11 width: window.innerWidth, 12 height: window.innerHeight, 13 draggable: true, 14 }); 15 16 this.shapes = []; 17 this.loaded = false; 18 this.mainLayer = new Konva.Layer(); 19 this.routeLayer = new Konva.Layer(); 20 this.textLayer = new Konva.Layer(); 21 this.stage.add(this.mainLayer); 22 this.stage.add(this.routeLayer); 23 this.stage.add(this.textLayer); 24 this.route = new Konva.Line(); 25 26 this.stage.on("resize", () => { 27 this.stage.width = window.innerWidth; 28 this.stage.height = window.innerHeight; 29 }); 30 31 this.stage.on("wheel",(e) => { 32 zoomStage(e,this.stage); 33 }) 34 35 36 } 37 38 deserializeMap(data) { 39 data.forEach((child) => { 40 const shape = JSON.parse(child); 41 if (shape.className !== "InfoPin") { 42 const renderedShape = Factory.createRenderedShape( 43 shape.className, 44 shape.attrs 45 ); 46 this.shapes.push(renderedShape); 47 } 48 }); 49 } 50 51 displayRoomNames(){ 52 console.log("VLEZE") 53 this.shapes.forEach(shape => { 54 shape.displayName(this.textLayer); 55 }) 56 57 this.textLayer.children.forEach(child => console.log(child,"DECAAA")); 58 } 59 60 async loadMap() { 61 const httpService = new HttpService(); 62 const mapData = await httpService.get("/public/mapData"); 63 console.log("DESERIALIZED --->",mapData); 64 this.deserializeMap(mapData); 65 this.shapes.forEach((shape) => { 66 this.mainLayer.add(shape); 67 }); 68 this.displayRoomNames(); 69 } 70 71 drawRoute(path) { 72 this.routeLayer.removeChildren(); 73 console.log("====PATH===="); 74 path.forEach((point) => console.log(point.x, point.y)); 75 76 const pointsArray = path.flatMap((point) => [point.x, point.y]); 77 78 console.log(pointsArray, "POINTS"); 79 80 let buff = []; 81 let count = 0; 82 let index = 0; 83 84 const drawNextSegment = () => { 85 if (index >= pointsArray.length) return; 86 87 buff.push(pointsArray[index]); 88 count++; 89 90 if (count % 4 === 0) { 91 const line = new Konva.Arrow({ 92 points: buff, 93 stroke: "#e91332", 94 strokeWidth: 2.5, 95 dash: [5, 4], 96 lineCap: 'round', 97 lineJoin: 'round', 98 pointerLength: 7, 99 pointerWidth: 7, 100 fill:'red', 101 }); 102 103 this.routeLayer.add(line); 104 this.routeLayer.draw(); 105 106 console.log(buff, "BUFFER"); 107 buff = []; 108 index -= 2; 10 constructor(containerId, floorNum) { 11 this.container = document.getElementById(containerId); 12 this.containerId = containerId; 13 this.stage = new Konva.Stage({ 14 container: containerId, 15 width: window.innerWidth, 16 height: window.innerHeight, 17 draggable: true, 18 }); 19 20 this.shapes = []; 21 this.roomTypes = []; 22 this.loaded = false; 23 this.mainLayer = new Konva.Layer(); 24 this.routeLayer = new Konva.Layer(); 25 this.textLayer = new Konva.Layer(); 26 this.stage.add(this.mainLayer); 27 this.stage.add(this.routeLayer); 28 this.stage.add(this.textLayer); 29 30 this.floorNum = floorNum; 31 32 this.navArrow = new Konva.Arrow({ 33 stroke: "#bb0000", 34 strokeWidth: 3, 35 dash: [12, 7], 36 lineCap: "round", 37 tension: 10, 38 pointerLength: 2, 39 pointerWidth: 3, 40 }); 41 42 this.navArrow.cache(); 43 44 this.stage.on("resize", () => { 45 this.stage.width = window.innerWidth; 46 this.stage.height = window.innerHeight; 47 }); 48 49 this.stage.on("wheel", (e) => { 50 zoomStage(e, this.stage); 51 }); 52 } 53 54 clearMap() { 55 this.mainLayer.removeChildren(); 56 this.shapes.forEach(shape => shape.clearText()) 57 this.shapes = []; 58 } 59 60 deserializeMap(data) { 61 this.clearMap(); 62 63 let dsrData = JSON.parse(data); 64 dsrData.forEach((shape) => { 65 if (shape.className !== "InfoPin") { 66 const renderedShape = Factory.createRenderedShape(shape.className, shape.attrs); 67 addEventHandling(renderedShape, this, "click"); 68 this.shapes.push(renderedShape); 69 } 70 }); 71 } 72 73 displayRoomNames() { 74 this.shapes.forEach((shape) => { 75 shape.displayName(this.textLayer); 76 }); 77 78 } 79 80 81 82 loadMapN(floorData) { 83 if (floorData == null || floorData === "") return; 84 85 this.deserializeMap(floorData); 86 this.shapes.forEach((shape) => { 87 this.mainLayer.add(shape); 88 }); 89 this.displayRoomNames(); 90 this.initializeRoomTypes(); 91 92 } 93 94 clearRoute() { 95 this.routeLayer.removeChildren(); 96 } 97 98 99 drawRouteNEW(nodes, offset = 0) { 100 101 this.clearRoute(); 102 console.log("====PATH===="); 103 nodes.forEach((node) => console.log("NODE", node)); 104 105 let idx = offset; 106 let buff = [nodes[idx].coordinates.x, nodes[idx].coordinates.y]; 107 108 109 ++idx; 110 111 console.log("INIT BUFFER", buff); 112 console.log("INIT IDX", idx); 113 114 const drawNextSegment = () => { 115 116 if (idx >= nodes.length){ 117 return; 118 } 119 120 const currentNode = nodes[idx - 1]; 121 const nextNode = nodes[idx]; 122 123 if (nextNode.floorNumber !== currentNode.floorNumber) { 124 triggerNavigate(nodes, idx, nextNode.floorNumber, nextNode); 125 return; 126 } 127 128 const startX = currentNode.coordinates.x; 129 const startY = currentNode.coordinates.y; 130 const endX = nextNode.coordinates.x; 131 const endY = nextNode.coordinates.y; 132 133 const numSegments = 12; 134 135 const deltaX = (endX - startX) / numSegments; 136 const deltaY = (endY - startY) / numSegments; 137 138 const drawSegment = (i) => { 139 const segmentX = startX + deltaX * i; 140 const segmentY = startY + deltaY * i; 141 142 buff.push(segmentX, segmentY); 143 144 let line = this.navArrow.clone({ points: [...buff] }); 145 this.routeLayer.add(line); 146 this.routeLayer.draw(); 147 148 buff = [segmentX, segmentY]; 149 }; 150 151 let segmentIdx = 1; 152 const interval = setInterval(() => { 153 drawSegment(segmentIdx); 154 segmentIdx++; 155 156 if (segmentIdx > numSegments) { 157 clearInterval(interval); 158 idx++; 159 setTimeout(drawNextSegment, 150); 160 } 161 }, 50); 162 }; 163 164 drawNextSegment(); 165 } 166 167 168 169 initializeRoomTypes() { 170 this.roomTypes = this.shapes 171 .filter((shape) => shape.class === "Room" && shape.info.type !== "") 172 .map((shape) => shape.info.type); 173 } 174 175 getRoomTypes() { 176 return this.roomTypes; 177 } 178 179 180 getShapeByName(name){ 181 return this.shapes.find(shape => shape.info.name === name) 182 } 183 184 getShapeByType(type) { 185 return this.shapes.filter((shape) => shape.class === type) 186 } 187 188 toggleSearchRoom() { 189 this.toggleSearch = !this.toggleSearch; 190 } 191 192 //ova e loso ne trebit vaka 193 highlightShape(roomName) { 194 let foundShape = this.shapes.filter((shape) => shape.info.name === roomName)[0]; 195 foundShape.highlight(); 196 } 197 198 getMainEntrance() { 199 return this.shapes.filter(shape => shape.class === "Entrance").filter(el => el.info.isMainEntrance === true)[0]; 200 } 201 202 setFilter(filter) { 203 let rooms = this.getShapeByType("Room") 204 if (filter === "All") { 205 rooms.forEach((shape) => { 206 shape.unHighlight() 207 }) 208 } else { 209 rooms.filter((shape) => shape.info.type === filter).forEach((shape) => { 210 shape.highlight() 211 }) 212 rooms.filter((shape) => shape.info.type !== filter).forEach((shape) => { 213 shape.unHighlight() 214 }) 109 215 } 110 216 111 index++; 112 113 setTimeout(drawNextSegment, 25); 114 }; 115 116 drawNextSegment(); 217 } 117 218 } 118 119 120 search() {121 console.log("VLEZE VO SEARCH");122 }123 }
Note:
See TracChangeset
for help on using the changeset viewer.