xxxxxxxxxx
149
/* Translation Device, 2020, Wesley Chau
Thank you Olivier for your tutor help! */
let input, button;
let img;
let chickImg;
let chickens = null;
let dates = [];
var msg1 = ["The average Canadian industry chicken in the year"];
var msg2 = ["(Sorry, no data for this year)"];
var msg3 = ["weighed kg and was valued at $ CAD."];
let chickenQty = 10;
let chickenWeight = 10;
let chickenDollars = 10;
let maxQty = 771254;
let maxW = 1328368;
let maxH = 2828244;
let maxWeight = 0;
let maxDollars = 0;
function preload() {
chickens = loadJSON('chicken3.json');
img = loadImage('assets/chicken1a.png');
}
function setup() {
createCanvas(600, 600);
maxW = width * 0.26;
maxH = height * 0.26;
background('#cce6ff');
angleMode(DEGREES);
noStroke();
fill('#006600');
rect(0, height / 2, width, height);
input = createInput("1960");
input.position(330, 10.5);
input.size(40, 14.5);
button = createButton('submit');
button.mousePressed(submit);
button.position(380, 10.5);
button.size(55, 20);
fill(0);
textSize(14);
text(msg1, 10, 25);
text(msg3, 10, 45);
setMaxValues();
submit();
}
function setMaxValues() {
for (let c in chickens) {
let chicken = chickens[c];
// max quantity
if (chicken.qty > maxQty) {
maxQty = chicken.qty;
}
if (chicken.weight > maxWeight) {
maxWeight = chicken.weight;
}
if (chicken.dollars > maxDollars) {
maxDollars = chicken.dollars;
}
}
console.log('max qty is:', maxQty);
console.log('max weight is:', maxWeight);
console.log('max dollars is:', maxDollars)
}
function submit() {
console.log(input.value())
background('#cce6ff');
noStroke();
fill('#c6ffb3');
rect(3, height / 2, width-6, 297, 12);
fill(0);
textSize(14);
text(msg1, 10, 25);
text(msg3, 10, 45);
text('weight (kg) / chicken', 230, height-10);
push();
rotate(270);
textAlign(CENTER);
translate(-600, -280);
text('value ($) / chicken', width/2, height/2);
pop();
let isUpdated = false;
for (let c in chickens) {
let chicken = chickens[c];
if (chicken.date.toString() === input.value()) {
isUpdated = true;
console.log("yay, values updated")
chickenQty = chicken.qty;
chickenWeight = chicken.weight;
chickenDollars = chicken.dollars;
let x = round(chickenWeight/chickenQty, 2);
let y = round(chickenDollars/chickenQty, 2);
text(x, 65, 45);
text(y, 244, 45);
}
}
if (!isUpdated) {
console.log("Sorry, no values for this year")
textSize(10);
fill('red');
text(msg2, 440, 24);
}
push();
noFill();
stroke(0);
strokeWeight(1);
line(15, 60, 15, 235);
line(10, 60, 20, 60);
line(15, 365, 15, height-25);
line(10, height-25, 20, height-25);
line(25, height-15, 220, height-15);
line(25, height-20, 25, height-10);
line(365, height-15, width-25, height-15);
line(width-25, height-20, width-25, height-10);
pop();
}
function draw() {
let imgW = map(chickenWeight, 0, maxQty, 0, maxW); // weight
let imgH = map(chickenDollars, 0, maxQty, 0, maxH); // dollars
image(img, width / 2 - imgW / 2, height / 2 - imgH / 2, imgW, imgH);
}