Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. let Point = class {
  2. constructor(x, y, radius) {
  3. this.x = x;
  4. this.y = y;
  5. this.radius = radius;
  6. this.min_dist = 5;
  7. this.children = [];
  8. this.touching_mouse = false;
  9. this.color = color("red");
  10. this.visible = true;
  11. this.remove = false;
  12. this.uid = Math.random() * 1000;
  13. }
  14. }
  15.  
  16. var target_points = 1000;
  17. var width, height;
  18. var points;
  19. var mouse_invoked = false;
  20.  
  21. // Panning & Zooming
  22. var drag_x = 0, drag_y = 0, last_drag_x = 9999, last_drag_y = 9999, last_press_x = 9999, last_press_y = 9999, mouse_x = 0, mouse_y = 0;
  23. var zoom = 1.0, zoom_scale = 0.0009;
  24.  
  25. function rand(min, max) {
  26. return Math.ceil(Math.random() * (max - min) + min);
  27. }
  28.  
  29. function setup() {
  30. width = 1920 / 2, height = 1080 / 2;
  31. createCanvas(width, height);
  32. points = [];
  33.  
  34. // TODO : add points from file
  35. for (var x = 0; x < target_points; x++) {
  36. points.push(new Point(rand(0, width), rand(0, height), 5));
  37. }
  38. }
  39.  
  40. function draw() {
  41. // TICK
  42. translate(drag_x, drag_y);
  43. scale(zoom, zoom);
  44.  
  45. //var t_points = points.filter(filter_onscreen);
  46. points = points.filter(filter_tagremove);
  47.  
  48.  
  49. // RENDER
  50. background(255);
  51. strokeWeight(0);
  52. var t_points = points.filter(filter_onscreen);
  53. for (var i = 0; i < t_points.length; i++) {
  54. fill(t_points[i].color);
  55. circle(t_points[i].x, t_points[i].y, t_points[i].radius);
  56. }
  57.  
  58. }
  59.  
  60. function filter_tagremove(point, index, arr) {
  61. return !point.tag_remove;
  62. }
  63.  
  64. // TODO: fix for optimization
  65. function filter_onscreen(point, index, arr) {
  66. var x = (point.x + drag_x) * zoom;
  67. var y = (point.y + drag_y) * zoom;
  68. var radius = point.radius * zoom;
  69. return (x + radius) > 0 && (x - radius) < width && (y + radius) > 0 && (y - radius) < height || true;
  70. }
  71.  
  72. function circle_touches(x, y, r, x2, y2, r2) {
  73. var dist = Math.sqrt(Math.pow(x2 - x, 2) + Math.pow(y2 - y, 2));
  74. return dist <= r + r2;
  75. }
  76.  
  77. function mouseDragged(event) {
  78. if (last_drag_x == 9999 & last_drag_y == 9999) {
  79. last_drag_x = event.x;
  80. last_drag_y = event.y;
  81. } else {
  82. drag_x += event.x - last_drag_x - mouse_x;
  83. drag_y += event.y - last_drag_y - mouse_y;
  84. mouse_x = 0;
  85. mouse_y = 0;
  86. last_drag_x = event.x;
  87. last_drag_y = event.y;
  88. }
  89. }
  90.  
  91. function mousePressed(event) {
  92. if (last_press_x != 9999 && last_press_y != 9999) {
  93. mouse_x = event.x - last_press_x;
  94. mouse_y = event.y - last_press_y;
  95. }
  96. }
  97.  
  98. function mouseReleased(event) {
  99. last_press_x = event.x;
  100. last_press_y = event.y;
  101. }
  102.  
  103. function mouseWheel(event) {
  104. zoom += event.delta * zoom_scale;
  105. drag_x -= (event.x - mouseX) / zoom;
  106. drag_y -= (event.y - mouseY) / zoom;
  107. mouse_invoked = true;
  108. }
  109.  
  110. function keyPressed(event) {
  111. if (event.keyCode === LEFT_ARROW) {
  112. fullscreen();
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement