UNPKG

991 BMarkdownView Raw
1## P5JS Vector Field
2
3
4Vector Field Example Based upon https://github.com/winkerVSbecks/material-vector-field
5
6```p5js/playable
7
8var locs = [];
9function calcVec(x, y) {
10 return new P5.Vector(y - x, - x - y);
11}
12
13p5.windowResized = function() {
14 p5.resizeCanvas(p5.windowWidth - 70, 500);
15};
16
17p5.setup = function() {
18 p5.createCanvas(100, 100);
19 p5.windowResized();
20
21 var res = 20;
22 var countX = p5.ceil(p5.width/res) + 1;
23 var countY = p5.ceil(p5.height/res) + 1;
24
25 for (var j = 0; j < countY; j++) {
26 for (var i = 0; i < countX; i++) {
27 locs.push( new P5.Vector(res*i, res*j) );
28 }
29 };
30
31 p5.noFill();
32 p5.stroke(249,78,128);
33
34};
35p5.draw = function() {
36 p5.background(30,67,137);
37 for (var i = locs.length - 1; i >= 0; i--) {
38 var h = calcVec( locs[i].x - p5.mouseX, locs[i].y - p5.mouseY);
39 p5.push();
40 p5.translate(locs[i].x, locs[i].y);
41 p5.rotate(h.heading());
42 p5.line(0, 0, 0, - 15);
43 p5.pop();
44 };
45};
46```
47
48
49---
50
51[Back to Home](:@Home)
52