Skip to content

Language Settings

Follow 1

A line segment is pushed and pulled by the cursor. Based on code from Keith Peters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
let x = 100,
  y = 100,
  angle1 = 0.0,
  segLength = 50;
function setup() {
  createCanvas(710, 400);
  strokeWeight(20.0);
  stroke(255, 100);
}
function draw() {
  background(0);
  dx = mouseX - x;
  dy = mouseY - y;
  angle1 = atan2(dy, dx);
  x = mouseX - cos(angle1) * segLength;
  y = mouseY - sin(angle1) * segLength;
  segment(x, y, angle1);
  ellipse(x, y, 20, 20);
}
function segment(xya) {
  push();
  translate(x, y);
  rotate(a);
  line(0, 0, segLength, 0);
  pop();
}
X

creative commons license