xxxxxxxxxx
107
// The Coding Train
// Coding Challenge 122.1
// Quick Draw
// Part 2: https://youtu.be/EcRK6oFddPQ
// https://thecodingtrain.com/CodingChallenges/122.2-quick-draw
// Part 1: https://youtu.be/yLuk0twx8Hc
// https://thecodingtrain.com/CodingChallenges/122.1-quick-draw
let char;
const url = 'https://quickdrawfiles.appspot.com/drawing/'+char+'?isAnimated=false&format=json&key='
let story='i ate a sandwich at the tree'
let strokeIndex = 0;
let index = 0;
let d;
let prevx, prevy;
let keyInput;
let start;
let text;
let count=0;
let key =[];
function preload() {
text = loadStrings('categories.txt');
}
function setup() {
createCanvas(windowWidth, windowHeight);
key =parseText(text);
newChar(key[count]);
}
function parseText(input){
console.log('inside parse');
let parse=story.split(" ");
for (let i=0;i<input.length;i++){
for (let j=0;j<parse.length;j++){
if(input[i]==parse[j]){
key.push(parse[j]);
}
}
}
if (key === undefined || key.length == 0) {
console.log('NO MATCHES, change story');
}
return key;
}
function newChar(x) {
let apiKey = 'AIzaSyCLxdiMV5-46xuFWFbdDhVoJi7DMwe-H9Q';
let url = 'https://quickdrawfiles.appspot.com/drawing/'+x+'?isAnimated=false&format=json&key='
loadJSON(url + apiKey, gotChar);
}
function gotChar(data) {
background(255);
d = data.drawing;
}
function draw() {
translate(width/3,height/3);
if (d) {
// console.log('drawing');
let x = d[strokeIndex][0][index];
let y = d[strokeIndex][1][index];
stroke(0);
strokeWeight(3);
if (prevx !== undefined) {
line(prevx, prevy, x, y);
}
index++;
if (index === d[strokeIndex][0].length) {
strokeIndex++;
prevx = undefined;
prevy = undefined;
index = 0;
if (strokeIndex === d.length) {
if (count<=key.length){
d = undefined;
strokeIndex = 0;
count++;
console.log(count)
console.log('done')
setTimeout(newChar(key[count]), 100);
}
else{
noLoop();
}
}
} else {
prevx = x;
prevy = y;
}
}
}
function mousePressed(){
}