Skip to content

Instantly share code, notes, and snippets.

@shiffman
shiffman / node_getimage.js
Created December 10, 2014 03:11
Download an image from flickr with Node
var fs = require('fs');
var request = require('request');
var url = 'https://farm8.staticflickr.com/7484/15685407577_d19030d469_m.jpg';
var saveFile = 'test.jpg';
request.head(url, getImage);
function getImage(err, res, body) {
var req = request(url);
@shiffman
shiffman / imageBase64.pde
Last active August 29, 2015 14:10
PImage encoded to Base64 String
// This is for Java 7
// With Java 8 you can now say:
import javax.xml.bind.DatatypeConverter;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
// Load an image
PImage img = loadImage("file.jpg");
@shiffman
shiffman / offscreen_pgraphics.pde
Created November 20, 2014 03:06
Render offscreen high resolution canvas and preview on screen
PGraphics canvas;
void setup() {
size(255,330);
canvas = createGraphics(2550,3300);
canvas.beginDraw();
canvas.background(0);
for (int i = 0; i < 1000; i++) {
@shiffman
shiffman / saturation.pde
Created November 19, 2014 03:23
Algorithm for altering the saturation of a single color. (not using HSB mode).
color col;
void setup() {
size(200, 100);
col = color(50, 100, 25);
}
void draw() {
background(0);
noStroke();
void setup() {
size (400, 300); // make a canvas that's 300 x 300
//SETTING UP MY UNDERLYING GRID STRUCTURE
int numImages = 12;
int colWidth = 10; //column width
int rowHeight = 10; //row height
int colNum = width/colWidth;
int rowNum = height/rowHeight;
@shiffman
shiffman / no_ds_store.pde
Created November 17, 2014 21:24
List without DS_Store
String[] listFileNames(String dir) {
File file = new File(dir);
if (file.isDirectory()) {
String names[] = file.list();
StringList filelist = new StringList();
for (String s : names) {
if (!s.contains("DS_Store")) {
filelist.append(s);
@shiffman
shiffman / dynamiccursor.pde
Created November 15, 2014 18:16
Dynamic Cursor
PGraphics canvas;
void setup() {
canvas = createGraphics(20, 20);
}
void draw() {
background(255);
canvas.beginDraw();
canvas.background(0);
// Daniel Shiffman
// Programming from A to Z, Fall 2014
// https://github.com/shiffman/Programming-from-A-to-Z-F14
// This examples builds a very simple DOM visualization of concordance
// It reads the text one word a a time and animates the words growing according to their counts
var concordance;
var wutang;
var canvas;
@shiffman
shiffman / ParseCommas.pde
Created October 28, 2014 20:48
Parsing numbers with commas, what a pain
import java.text.NumberFormat;
String s = "256,128";
int val = int(s.replaceAll(",",""));
println(val);
int val2 = 0;
NumberFormat nf = NumberFormat.getNumberInstance(java.util.Locale.US);
@shiffman
shiffman / 00_anonymous.js
Last active September 28, 2015 17:41
Discussion of issues around callbacks for multiple elements in a loop. Using setTimeout() here for demonstration purposes. Actual scenario relates to examples here: https://github.com/shiffman/Programming-from-A-to-Z-F14/tree/master/week6_apis
// While this seems like the right idea
// This does not work at all!
// The loop will finish and i will be at 10 by
// the time the anonymous function is called
for (var i = 0; i < 100; i++) {
setTimeout(function() {
console.log(i);
}, 100*i);