Skip to content

Instantly share code, notes, and snippets.

View fffiloni's full-sized avatar

Sylvain Filoni fffiloni

View GitHub Profile
@fffiloni
fffiloni / convert-blob-p5-image.js
Created July 21, 2022 16:38
Convert a blob to a p5 js image
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);
@fffiloni
fffiloni / convert-canvas-to-blob.js
Last active July 21, 2022 16:31
Convert p5js canvas to data then to blob
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)
@fffiloni
fffiloni / canvas-p5js-graphics-resize.js
Last active July 23, 2022 12:25
How to resive canvas and its graphics on p5js
// 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
@fffiloni
fffiloni / sketch.js
Last active July 21, 2022 16:13
PRESSURE & TILT CONTROL ON P5js CANVAS
let canvas;
let pointerX = 0;
let pointerY = 0;
let isOnCanvas = false;
let inclinationX = 0;
let inclinationY = 0;
let pressure = 0;
let diameter = 0;
@fffiloni
fffiloni / render-audiobars-p5.js
Last active January 28, 2021 14:07
Converts audio peaks to bars and renders them on p5js canvas
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));
@fffiloni
fffiloni / canvas-fit-to-container-p5.js
Created January 28, 2021 13:47
Create canvas with container's dimension in p5js
// 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);
@fffiloni
fffiloni / convert-seconds.js
Created January 28, 2021 13:42 — forked from martinbean/convert-seconds.js
Convert seconds to HH:MM:SS format in JavaScript.
new Date(seconds * 1000).toISOString().substr(11, 8)
@fffiloni
fffiloni / add-leading-zeros.js
Created January 28, 2021 13:37
Add leading zero
// 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"