Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let diam = 25;
- let bolas = [];
- let linhas = [];
- let espacamento = diam * 2.5,
- firstpointx, firstpointy, targetx, targety, dx, dy, xline, yline, drawing = false,
- desenhar = false;
- let cor = [];
- function setup() {
- createCanvas(500, 500);
- background(255);
- pixelDensity(displayDensity());
- for (let ya = 0; ya < width / espacamento + 1; ya++) {
- cor[ya] = [];
- bolas[ya] = [];
- for (let yo = 0; yo < height / espacamento + 1; yo++) {
- cor[ya][yo] = 0;
- bolas[ya][yo] = new Bola(ya * espacamento, yo * espacamento, diam, cor[ya][yo]);
- }
- }
- }
- function draw() {
- background(255);
- for (let ya = 0; ya < width / espacamento + 1; ya++) {
- bolas[ya] = [];
- for (let yo = 0; yo < height / espacamento + 1; yo++) {
- bolas[ya][yo].desenha();
- }
- }
- for (let i = 0; i < linhas.size(); i++) {
- linhas.get(i).desenha();
- }
- if (desenhar) {
- dx = targetx - xline;
- dy = targety - yline;
- xline += dx * 0.2;
- yline += dy * 0.2;
- linhas.add(new Linha(firstpointx, firstpointy, xline, yline, diam + 1));
- if (abs(dx) < 0.5 && abs(dy) < 5) {
- for (let ya = 0; ya < width / espacamento + 1; ya++) {
- bolas[ya] = [];
- for (let yo = 0; yo < height / espacamento + 1; yo++) {
- bolas[ya][yo].d = diam;
- }
- }
- desenhar = false;
- }
- }
- }
- function mousePressed() {
- for (let ya = 0; ya < width / espacamento + 1; ya++) {
- bolas[ya] = [];
- for (let yo = 0; yo < height / espacamento + 1; yo++) {
- if (mouseX > bolas[ya][yo].x - diam / 2 && mouseX < bolas[ya][yo].x + diam / 2 && mouseY > bolas[ya][yo].y - diam / 2 && mouseY < bolas[ya][yo].y + diam / 2) {
- if (!drawing && !desenhar) {
- firstpointx = bolas[ya][yo].x;
- firstpointy = bolas[ya][yo].y;
- xline = firstpointx;
- yline = firstpointy;
- bolas[ya][yo].d = diam + 5;
- drawing = true;
- } else if (!desenhar) {
- targetx = bolas[ya][yo].x;
- targety = bolas[ya][yo].y;
- bolas[ya][yo].d = diam + 5;
- desenhar = true;
- drawing = false;
- }
- }
- }
- }
- }
- class Bola {
- Bola(xx, yy, diam, cor) {
- this.x = xx;
- this.y = yy;
- this.d = diam;
- this.c = cor;
- }
- desenha() {
- fill(this.c);
- noStroke();
- circle(x, y, d);
- }
- }
- class Linha {
- Linha(xx1, yy1, xx2, yy2, weight) {
- this.x1 = xx1;
- this.x2 = xx2;
- this.y1 = yy1;
- this.y2 = yy2;
- this.w = weight;
- }
- desenha() {
- stroke(0);
- strokeWeight(w);
- line(x1, y1, x2, y2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement