viewportFitView.html 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>视口-自适应</title>
  8. </head>
  9. <body>
  10. 适应视口:
  11. <button class="fitView">tl</button>
  12. <button class="fitView">tc</button>
  13. <button class="fitView">tr</button>
  14. <button class="fitView">cc</button>
  15. <button class="fitView">lc</button>
  16. <button class="fitView">rc</button>
  17. <button class="fitView">bl</button>
  18. <button class="fitView">bc</button>
  19. <button class="fitView">br</button>
  20. <button class="fitView">autoZoom</button>
  21. <div id="mountNode"></div>
  22. <script src="../build/g6.js"></script>
  23. <script>
  24. const data = {
  25. nodes: [{
  26. id: 'node1',
  27. x: 100,
  28. y: 200
  29. }, {
  30. id: 'node2',
  31. x: 300,
  32. y: 200
  33. }],
  34. edges: [{
  35. id: 'edge1',
  36. target: 'node2',
  37. source: 'node1'
  38. }]
  39. };
  40. const graph = new G6.Graph({
  41. container: 'mountNode',
  42. fitView: 'cc',
  43. height: window.innerHeight,
  44. fitView: 'cc'
  45. });
  46. graph.read(data);
  47. const buttons = document.getElementsByClassName('fitView');
  48. for (let index = 0; index < buttons.length; index++) {
  49. const button = buttons[index];
  50. button.onclick = () => {
  51. graph.setFitView(button.innerHTML);
  52. };
  53. }
  54. </script>
  55. </body>
  56. </html>