Skip to content

Instantly share code, notes, and snippets.

@nataliefreed
nataliefreed / tinkerablebeziers.jscad
Created October 25, 2016 15:42
bezier tinkering program OpenJSCAD
// title : OpenJSCAD Example
// author : Natalie Freed
function getParameterDefinitions() {
var parameters = [{ name: 'numCurves', type: 'int', initial: 2, caption: "num curves:" }];
parameters = parameters.concat(makeSliders(1));
return parameters;
}
function main(params) {
@nataliefreed
nataliefreed / butterfly.jscad
Last active October 19, 2016 17:20
Butterfly example with beziers and 2D/3D Export, OpenJSCAD
// title : OpenJSCAD Bezier, 2D/3D Export Example
// author : Natalie Freed
// sources : 2D/3D export technique from Lamp Shade Example
function getParameterDefinitions() {
return [{ name: 'height', type: 'float', initial: 6, caption: "height:" },
{
name: 'type',
type: 'choice',
values: ["2D", "3D"],
// title : OpenJSCAD Example
// author : Natalie Freed
function getParameterDefinitions() {
return [{ name: 'heightOfBox1', type: 'float', initial: 6, caption: "Height of box 1:" }];
}
function main(params) {
var box1 = cube({size: [1,2,params.heightOfBox1]});
var box2 = cube({size: [4,3,2]});
@nataliefreed
nataliefreed / pumpkin.jscad
Created October 9, 2016 22:44
OpenJSCAD parametric pumpkin
// title : OpenJSCAD Pumpkin
// author : Natalie Freed
function getParameterDefinitions() {
return [
{ name: 'numSegments', type: 'int', initial: 10, caption: "Number of segments:" },
{ name: 'proportions', type: 'choice', caption: 'Proportions:', values: ["tall", "wide", "medium"], captions: ["tall", "wide", "medium"], initial: "medium" }
];
}
@nataliefreed
nataliefreed / radial-helper.js
Last active September 30, 2017 02:36
Wrapper function to make a radial version of a shape in P5
/*
Radial Shape Wrapper Function
Natalie Freed September 2016
*/
function setup() {
createCanvas(600, 600);
//draw a shape
arrow(width/2+20, height/2, 20, color(0));
@nataliefreed
nataliefreed / rainbowArray.js
Created September 28, 2016 22:41
Array example in P5, make an array of colors and draw a rainbow. Includes how to append and splice in elements
var rainbow; //global variable
function setup() {
createCanvas(600, 600);
//make the list (array) of rainbow colors
rainbow = [color('#c0392b'), color('#e67e22'), color('#f1c40f'), color('#2ecc71'), color('#3498db'), color('#8e44ad')];
//shift to the center and down a bit
translate(width / 2, height / 3);
@nataliefreed
nataliefreed / randomColorFromArray.js
Created September 28, 2016 20:46
Pick a random color from an array in P5
function setup() {
createCanvas(600, 600);
var listOfColors = [color('#aabf12'), color('#33ab12'), color('#165512'), color('#fe3fa2'), color('#a345cd')];
var stripeWidth = 20;
strokeWeight(stripeWidth);
for(var i=0;i<=width/stripeWidth;i++) {
stroke(listOfColors[int(random(0, listOfColors.length))]);
@nataliefreed
nataliefreed / randomFunction.js
Created September 28, 2016 02:06
Example of choosing a random function in P5
/*
Example of choosing a random function
Notice this program re-randomizes the row of shapes every time it's run!
*/
function setup() {
createCanvas(800, 400);
background(50);
//draw some shapes to test. These aren't randomized
@nataliefreed
nataliefreed / shape_scaling_helper.js
Created September 26, 2016 23:14
Wrapper function to help scale shapes in P5
function setup() {
createCanvas(600, 600);
noFill();
strokeWeight(3);
//example: draw something, unscaled
yourFunctionThatDrawsAThing(100, 200);
//example: make a half scale version
var scaledByHalf = autoScale(yourFunctionThatDrawsAThing, 0.5);
//Scale your shapes up without them moving all over the place!
var scalingFactor = 1;
var x = 100;
var y = 200;
var w = 40;
var h = 30;
function setup()
{