kryxkan

netsimulator

Apr 17th, 2020
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. <div style="position: relative;">
  2. <canvas id="layer3" width="900" height="900" style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>
  3. <canvas id="layer2" width="900" height="900" style="position: absolute; left: 0; top: 0; z-index: 1;"></canvas>
  4. <canvas id="layer1" width="900" height="900" style="position: absolute; left: 0; top: 0; z-index: 2;"></canvas>
  5. </div>
  6. <script>
  7.  
  8. //layer 1- irrelevant for this bug
  9. var cal1 = document.getElementById('layer1');
  10. var col1 = cal1.getContext('2d');
  11. //layer 2 - devices are drawn here
  12. var cal2 = document.getElementById('layer2');
  13. var col2 = cal2.getContext('2d');
  14. //layer 3 - connections between devices
  15. var cal3 = document.getElementById('layer3');
  16. var col3 = cal3.getContext('2d');
  17. //images are stored inside the same folder as html file
  18. var sources = {
  19. router: 'router.jpg',
  20. switch: 'workgroup switch.jpg',
  21. pc: 'pc.jpg',
  22. packet: 'packet.png'
  23. };
  24.  
  25.  
  26. var images = [];
  27.  
  28. for(var src in sources) {
  29. images[src] = new Image();
  30. images[src].src = sources[src]; //most propably this line isn't doing it's job
  31. }
  32.  
  33.  
  34. class Device {
  35. constructor(image, x, y){
  36. this.image = image;
  37. this.x = x;
  38. this.y = y;
  39. }
  40. };
  41. //this only draws lines between devices, it's working
  42. function connect(startDevice, endDevice){
  43. col3.beginPath();
  44. col3.moveTo(startDevice.x + startDevice.image.width / 2, startDevice.y + startDevice.image.height / 2);
  45. col3.lineTo(endDevice.x + endDevice.image.width / 2, endDevice.y + endDevice.image.height / 2);
  46. col3.stroke();
  47. }
  48.  
  49. function drawDevice(device){
  50. col2.drawImage(device.image, device.x, device.y);
  51. }
  52.  
  53. var pc1 = new Device(images.pc, 50, 60);
  54. var router1 = new Device(images.router, 300, 300);
  55. var switch1 = new Device(images.switch, 700, 800);
  56. drawDevice(switch1);
  57. drawDevice(pc1);
  58. drawDevice(router1);
  59. connect(pc1, router1);
  60. connect(router1, switch1);
  61.  
  62. </script>
Advertisement
Add Comment
Please, Sign In to add comment