1 | ## P5JS Mobius Example
|
2 |
|
3 | - [Number of Segments? [30]](:?SEGMENTS|number)
|
4 | - [Strip Thickness? [8]](:?SEG_WIDTH|number)
|
5 | - [Strip Width? [50]](:?SEG_LENGTH|number)
|
6 |
|
7 | ```p5js/playable/autoplay
|
8 | var PI = Math.PI;
|
9 | var HALF_PI = PI / 2.0;
|
10 |
|
11 | smartdown.setVariable('SEGMENTS', 30, 'number');
|
12 | smartdown.setVariable('SEG_WIDTH', 8, 'number');
|
13 | smartdown.setVariable('SEG_LENGTH', 50, 'number');
|
14 |
|
15 | var speed = 0.05;
|
16 | var ax = .01;
|
17 | var ay = ax;
|
18 | var az = ay;
|
19 | var dx, dy, dz;
|
20 |
|
21 | p5.windowResized = function() {
|
22 | p5.resizeCanvas(p5.windowWidth - 30, p5.windowWidth - 30);
|
23 | };
|
24 |
|
25 | p5.setup = function() {
|
26 | dx = p5.random(-speed, speed);
|
27 | dy = p5.random(-speed, speed);
|
28 | dz = p5.random(-speed, speed);
|
29 |
|
30 | p5.createCanvas(300, 300, 'webgl');
|
31 | p5.normalMaterial();
|
32 |
|
33 | p5.windowResized();
|
34 | };
|
35 |
|
36 | p5.draw = function() {
|
37 | var SEGMENTS = env.SEGMENTS;
|
38 | var SEG_WIDTH = env.SEG_WIDTH;
|
39 | var SEG_LENGTH = env.SEG_LENGTH;
|
40 | var DIAMETER = SEG_LENGTH * 2;
|
41 | p5.background('ivory');
|
42 | p5.camera(0, SEG_LENGTH, p5.windowHeight / 3, 0, 0, 0, 0, 1, 0);
|
43 | p5.rotateX(ax += dx);
|
44 | p5.rotateY(ay += dy);
|
45 | p5.rotateZ(az += dz);
|
46 |
|
47 | for (var i = 0; i < SEGMENTS; i++) {
|
48 | var frac = i * 2 / SEGMENTS;
|
49 | p5.push();
|
50 | p5.rotateX(frac * HALF_PI);
|
51 | p5.rotateY(HALF_PI);
|
52 | p5.translate(
|
53 | 0,
|
54 | DIAMETER * p5.cos(frac * HALF_PI),
|
55 | DIAMETER * p5.sin(frac * PI));
|
56 | p5.cylinder(SEG_WIDTH, SEG_LENGTH);
|
57 | p5.pop();
|
58 | }
|
59 | };
|
60 | ```
|
61 |
|
62 | ---
|
63 |
|
64 | [Back to Home](:@Home)
|