This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ES2015 | |
function zeroPad(num, places) { | |
var zero = places - num.toString().length + 1; | |
return Array(+(zero > 0 && zero)).join("0") + num; | |
} | |
zeroPad(5, 2); // "05" | |
zeroPad(5, 4); // "0005" | |
zeroPad(5, 6); // "000005" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new Date(seconds * 1000).toISOString().substr(11, 8) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// HTML index.html | |
// <div id="myCanvasContainer"></div> | |
// JS sketch.js | |
// Inside setup() function | |
let canvasDiv = document.getElementById('myCanvasContainer'); | |
let width = canvasDiv.offsetWidth; | |
let height = canvasDiv.offsetHeight; | |
let sketchCanvas = createCanvas(width,height); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let peaks = []; | |
let rectRadius = 4; | |
let barsW = 3; | |
let soundFile = loadSound('path/to/audio.mp3'); // loadSound() is a p5sound method | |
// loadSound inside the preload() function | |
// We calculate audio peaks to show bars and wave | |
// Do this inside setup() function | |
peaks = soundFile.getPeaks(width/(barsW*2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let canvas; | |
let pointerX = 0; | |
let pointerY = 0; | |
let isOnCanvas = false; | |
let inclinationX = 0; | |
let inclinationY = 0; | |
let pressure = 0; | |
let diameter = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Resizing the canvas is simple | |
resizeCanvas(new_width, new_height) | |
// Resizing graphics is tricky | |
function resizeGraphics(new_width, new_height, old_graphics){ | |
let new_graphics = createGraphics(new_width, new_height); | |
new_graphics.image(old_graphics, 0, 0, new_width, new_height); | |
return new_graphics; | |
} | |
//then call it directly to switch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function graphicsToBlob(){ | |
//for canvas | |
let image64 = canvas.elt.toDataURL('image/png'); | |
//for graphics | |
sourceGraphics.loadPixels(); | |
let image64 = sourceGraphics.canvas.toDataURL('image/png'); | |
fetch(img_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let img_result; | |
let img_to_download; | |
function blobToGraphics(buffer){ | |
// Create a blob from the buffer response | |
let myBlob = new Blob([buffer], {type: "image/png"}); | |
console.log(myBlob); | |
let uri = URL.createObjectURL(myBlob); |