xxxxxxxxxx
44
// Click canvas to add points to a shape
// Click [Download shape] to download the current shape
// Press any key to clear all points
// Click [Browse] to upload shape
var points = [];
function setup() {
createCanvas(400, 400);
var downloadButton = createButton("Download shape");
downloadButton.mousePressed(function() {
saveJSON(points, 'points.json');
});
createFileInput(function(j) {
var encoded = j.data.substr(j.data.indexOf('base64') + 7);
points = eval(decodeBase64(encoded));
});
}
function draw() {
background(220);
beginShape();
for(var p of points) {
vertex(p.x+random(-2,2), p.y+random(-2,2));
}
endShape(CLOSE);
}
function mousePressed() {
points.push({ x:mouseX, y:mouseY });
}
function keyPressed() {
points = [];
}
decodeBase64 = function(s) {
var e={},i,b=0,c,x,l=0,a,r='',w=String.fromCharCode,L=s.length;
var A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for(i=0;i<64;i++){e[A.charAt(i)]=i;}
for(x=0;x<L;x++){
c=e[s.charAt(x)];b=(b<<6)+c;l+=6;
while(l>=8){((a=(b>>>(l-=8))&0xff)||(x<(L-2)))&&(r+=w(a));}
}
return r;
};