UNPKG

1.43 kBMarkdownView Raw
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
8var PI = Math.PI;
9var HALF_PI = PI / 2.0;
10
11smartdown.setVariable('SEGMENTS', 30, 'number');
12smartdown.setVariable('SEG_WIDTH', 8, 'number');
13smartdown.setVariable('SEG_LENGTH', 50, 'number');
14
15var speed = 0.05;
16var ax = .01;
17var ay = ax;
18var az = ay;
19var dx, dy, dz;
20
21p5.windowResized = function() {
22 p5.resizeCanvas(p5.windowWidth - 30, p5.windowWidth - 30);
23};
24
25p5.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
36p5.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)