xxxxxxxxxx
212
function setup() {
createCanvas(1280, 720);
}
function draw() {
background(220);
}
// When you press the 'F' key, this program will export a series of images into
// your default Downloads folder. These can then be made into an animated gif.
// This code is known to work with p5.js version 0.6.0
// Prof. Golan Levin, 28 January 2018
// INSTRUCTIONS FOR EXPORTING FRAMES (from which to make a GIF):
// 1. Run a local server, using instructions from here:
// https://github.com/processing/p5.js/wiki/Local-server
// 2. Set the bEnableExport variable to true.
// 3. Set the myNickname variable to your name.
// 4. Run the program from Chrome, press 'f'.
// Look in your 'Downloads' folder for the generated frames.
// 5. Note: Retina screens may export frames at twice the resolution.
//===================================================
// User-modifiable global variables.
var myNickname = "nickname";
var nFramesInLoop = 120;
var bEnableExport = false;
// Other global variables you don't need to touch.
var nElapsedFrames;
var bRecording;
var theCanvas;
//===================================================
function setup() {
theCanvas = createCanvas(1280, 720);
bRecording = false;
nElapsedFrames = 0;
}
//===================================================
function keyTyped() {
if (bEnableExport) {
if ((key === 'f') || (key === 'F')) {
bRecording = true;
frameRate(2); // while we're exporting
nElapsedFrames = 0;
}
}
}
//===================================================
function draw() {
// Compute a percentage (0...1) representing where we are in the loop.
var percentCompleteFraction = 0;
if (bRecording) {
percentCompleteFraction = float(nElapsedFrames) / float(nFramesInLoop);
} else {
percentCompleteFraction = float(frameCount % nFramesInLoop) / float(nFramesInLoop);
}
// Render the design, based on that percentage.
// This function renderMyDesign() is the one for you to change.
renderMyDesign(percentCompleteFraction);
// If we're recording the output, save the frame to a file.
// Note that the output images may be 2x large if you have a Retina mac.
// You can compile these frames into an animated GIF using a tool like:
if (bRecording && bEnableExport) {
var frameOutputFilename = myNickname + "_frame_" + nf(nElapsedFrames, 4) + ".png";
print("Saving output image: " + frameOutputFilename);
saveCanvas(theCanvas, frameOutputFilename, 'png');
nElapsedFrames++;
if (nElapsedFrames >= nFramesInLoop) {
bRecording = false;
frameRate(60);
}
}
}
//===================================================
function renderMyDesign(percent) {
//
// THIS IS WHERE YOUR ART GOES.
// This is an example of a function that renders a temporally looping design.
// It takes a "percent", between 0 and 1, indicating where we are in the loop.
// Use, modify, or delete whatever you prefer from this example.
// This example uses several different graphical techniques.
// Remember to SKETCH FIRST!
//----------------------
// here, I set the background and some other graphical properties
background(210);
smooth();
strokeWeight(2);
//----------------------
// Here, I assign some handy variables.
var cx = 100;
var cy = 100;
//----------------------
//shapes with sigmoid pattern
noStroke();
push();
fill(random(200,255),255,255);
rotate(45);
translate(-400,-900);
for (var x = 0; x < 30; x++){
for (var sy = 0; sy <= 2000; sy += 100) {
var t = map(sy, 0, height, 0.0, 0.25);
var sx = width/4 - 20 * -tan((t + percent) * PI);
rect(sx+100*x, sy-1, 5, 5);
}
}
pop();
// shifting shapes 1
noStroke();
push();
fill(155,155,255,100);
rotate(45);
translate(-300,-1000);
var angle = 100
rotate(millis() /3000)
for (var x = 0; x < 30; x++){
for (var sy = 0; sy <= 10000; sy += 200) {
var t = map(sy, 0, height, 0.0, 0.25);
var sx = width/4 + 50 * sin((t + percent) * TWO_PI);
rect(sx, sy, 10, 20);
rect(sx+100*x, sy-1, 30, 10);
angle = angle + 30
rotate(angle);
}
}
pop();
//shifting shapes 2
push();
noStroke();
rotate(45);
fill(255,0,205, 150)
translate(-300,-400);
var angle = 120
rotate(millis() /1000)
for (var x = 0; x < 10; x++){
for (var sy = 0; sy <= 1000; sy += 400) {
var t = map(sy, 0, height, 0.0, 0.25);
var sx = width/4 + 10 * cos((t + percent) * TWO_PI);
rect(sx, sy, 10, 20);
rect(sx+100*x, sy-1, 30, 30);
angle = angle + 3
rotate(angle);
}
}
pop();
//shifting shapes 3
push();
noStroke();
rotate(45);
fill(255,0,0, 170)
translate(500,-400);
var angle = 70
for (var x = 0; x < 5; x++){
for (var sy = 0; sy <= 2000; sy += 500) {
var t = map(sy, 10, height, 0.0, 0.5);
var sx = (width/4 + 100 * tan((t + percent) * TWO_PI))/10000;
rect(sx-50*x, sy-1*10, 70, 70);
angle = angle + 10
rotate(millis()/3000)
rotate(angle);
}
}
angle = angle + 10
rectMode(CENTER);
pop();
}
// Symmetric double-element sigmoid function ('_a' is the slope)
// See https://github.com/IDMNYU/p5.js-func/blob/master/lib/p5.func.js
// From: https://idmnyu.github.io/p5.js-func/
//===================================================
function doubleExponentialSigmoid(_x, _a) {
if (!_a) _a = 0.75; // default
var min_param_a = 0.0 + Number.EPSILON;
var max_param_a = 1.0 - Number.EPSILON;
_a = constrain(_a, min_param_a, max_param_a);
_a = 1 - _a;
var _y = 0;
if (_x <= 0.5) {
_y = (pow(2.0 * _x, 1.0 / _a)) / 2.0;
} else {
_y = 1.0 - (pow(2.0 * (1.0 - _x), 1.0 / _a)) / 2.0;
}
return (_y);
}