xxxxxxxxxx
178
// Daniel Shiffman
// http://codingtra.in
// https://youtu.be/CKeyIbT3vXI
let fireworks = [];
let gravity;
let easycam;
let input; let button; let qn; let name; let namelength;
let button2;
let lat; let lng;
let _text;
let latpos; let lngpos; let erase = false;
let col;
// function preload() {
// yatra = loadFont('YatraOne.ttf');
// }
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
colorMode(HSB);
gravity = createVector(0, 0.2, 0);
stroke(255);
strokeWeight(4);
background(0);
easycam = createEasyCam();
easycam.zoom(-250);
document.oncontextmenu = function() { return false; }
input = createInput();
input.position(20, 45);
button = createButton('submit');
button.position(input.x + input.width+5, input.y);
button.mousePressed(render);
qn = createElement('h2', '✨ what is your name? ✨');
qn.position(input.x, input.y - 40);
qn.style ('color','white');
qn.style('font-family', 'sans-serif')
qn.style('font-size', '16px')
//textAlign(CENTER);
//textSize(10);
namelength = 0;
if (!navigator.geolocation) {
alert("navigator.geolocation is not available");
}
navigator.geolocation.getCurrentPosition(setPos);
_text = createGraphics(height, height);
// _text.textFont(yatra);
_text.textAlign(CENTER);
_text.noStroke();
}
function setPos (position) {
latpos = position.coords.latitude;
lngpos = position.coords.longitude;
lat = int(map(latpos, 5, 40, 0, 100));
lng = int(map(lngpos, 65, 100, 0, 100));
console.log(lat, lng);
}
function windowResized(){
resizeCanvas(windowWidth, windowHeight);
background(0);
}
function draw() {
colorMode(RGB);
background(0);
if (name!=null) {
//fades out old text when 'try another one' is clicked
if (erase === true) {
_text.fill(0);
} else { _text.fill(255, 30); }
//3D text:
_text.textSize(50);
_text.text(name, height*0.5, height*0.5);
_text.textSize(15);
_text.text("LAT " + int(latpos) + " LNG " + int(lngpos) + " // HOUR " + int(hour()), height*0.5, height*0.5 + 30);
texture(_text);
push();
translate(0,0,-160)
plane(height);
pop();
}
// if (random(1) < 0.1) { // density of fireworks
// fireworks.push(new Firework());
// }
rotateY(millis()/10000);
rotateX(millis()/10000);
translate (-width/2, -height/2);
for (let i = fireworks.length - 1; i >= 0; i--) {
fireworks[i].update();
fireworks[i].show();
if (fireworks[i].done()) {
// fireworks.splice(i, 1);
break;
}
}
}
function render () {
fireworks = [];
name = input.value();
namelength = map(name.length, 0, 20, 0, 100);
print(int([name]));
qn.html('Hey ' + name + '! This a unique firework based on your name, location and the current time. Use the mouse to rotate the scene. When you like the view, hit save and share! 😊');
qn. style("font-size", "14px")
qn. size(width-50, AUTO)
input.hide();
button.hide();
console.log(namelength);
erase = false;
// one firework at a time:
fireworks.push(new Firework());
button2 = createButton('try another one');
button2.size(110,AUTO)
button2.position(width-button2.width -20, height-button2.height - 20, input.y);
button2.mousePressed(again);
button3 = createButton('save image');
button3.size(90,button2.height)
button3.position(width-button2.width*2 -10, height-button2.height - 20, input.y);
button3.mousePressed(save);
}
function again () {
button2.hide();
button3.hide();
_text.remove();
qn.html('✨ what is your name? ✨');
qn. style("font-size", "16px")
input.show();
input.value("");
button.show();
erase = true;
}
function save() {
save('DiwaliWishes_2020.png');
return false;
}