Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let Point = class {
- constructor(x, y, radius) {
- this.x = x;
- this.y = y;
- this.radius = radius;
- this.min_dist = 5;
- this.children = [];
- this.touching_mouse = false;
- this.color = color("red");
- this.visible = true;
- this.remove = false;
- this.uid = Math.random() * 1000;
- }
- }
- var target_points = 1000;
- var width, height;
- var points;
- var mouse_invoked = false;
- // Panning & Zooming
- 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;
- var zoom = 1.0, zoom_scale = 0.0009;
- function rand(min, max) {
- return Math.ceil(Math.random() * (max - min) + min);
- }
- function setup() {
- width = 1920 / 2, height = 1080 / 2;
- createCanvas(width, height);
- points = [];
- // TODO : add points from file
- for (var x = 0; x < target_points; x++) {
- points.push(new Point(rand(0, width), rand(0, height), 5));
- }
- }
- function draw() {
- // TICK
- translate(drag_x, drag_y);
- scale(zoom, zoom);
- //var t_points = points.filter(filter_onscreen);
- points = points.filter(filter_tagremove);
- // RENDER
- background(255);
- strokeWeight(0);
- var t_points = points.filter(filter_onscreen);
- for (var i = 0; i < t_points.length; i++) {
- fill(t_points[i].color);
- circle(t_points[i].x, t_points[i].y, t_points[i].radius);
- }
- }
- function filter_tagremove(point, index, arr) {
- return !point.tag_remove;
- }
- // TODO: fix for optimization
- function filter_onscreen(point, index, arr) {
- var x = (point.x + drag_x) * zoom;
- var y = (point.y + drag_y) * zoom;
- var radius = point.radius * zoom;
- return (x + radius) > 0 && (x - radius) < width && (y + radius) > 0 && (y - radius) < height || true;
- }
- function circle_touches(x, y, r, x2, y2, r2) {
- var dist = Math.sqrt(Math.pow(x2 - x, 2) + Math.pow(y2 - y, 2));
- return dist <= r + r2;
- }
- function mouseDragged(event) {
- if (last_drag_x == 9999 & last_drag_y == 9999) {
- last_drag_x = event.x;
- last_drag_y = event.y;
- } else {
- drag_x += event.x - last_drag_x - mouse_x;
- drag_y += event.y - last_drag_y - mouse_y;
- mouse_x = 0;
- mouse_y = 0;
- last_drag_x = event.x;
- last_drag_y = event.y;
- }
- }
- function mousePressed(event) {
- if (last_press_x != 9999 && last_press_y != 9999) {
- mouse_x = event.x - last_press_x;
- mouse_y = event.y - last_press_y;
- }
- }
- function mouseReleased(event) {
- last_press_x = event.x;
- last_press_y = event.y;
- }
- function mouseWheel(event) {
- zoom += event.delta * zoom_scale;
- drag_x -= (event.x - mouseX) / zoom;
- drag_y -= (event.y - mouseY) / zoom;
- mouse_invoked = true;
- }
- function keyPressed(event) {
- if (event.keyCode === LEFT_ARROW) {
- fullscreen();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement