I'm working on some code that takes a weather model image and tests a pixel's color. Here my entire code:
<html>
<head>
<meta title="PRWW PreCast (v0.1)">
<meta charset="UTF-8">
</head>
<body>
<canvas id="canvas" width="1024px" height="764px">
<img id="map" src=""/>
</canvas>
<script>
//Time Variables
var currentYear = new Date().getFullYear();
var currentMonth = new Date().getMonth() + 1;
var currentDay = new Date().getDate();
var currentHour = new Date().getHours();
var currentRun;
//Formatting Time Variables for URLs
if (currentMonth < 10) {
currentMonth = "0" + currentMonth;
}
if (currentDay < 10) {
currentDay = "0" + currentDay;
}
//Finding Latest Model Run
if (currentHour >= 0 && currentHour < 6) {
currentRun = "00";
}
if (currentHour >= 6 && currentHour < 12) {
currentRun = "06";
}
if (currentHour >= 12 && currentHour < 18) {
currentRun = "12";
}
if (currentHour >= 18 && currentHour < 24) {
currentRun = "18";
}
var currentRun = currentRun;
//Creating URL
var mapAddress = "http://www.tropicaltidbits.com/analysis/models/gfs/" + currentYear + currentMonth + currentDay + currentRun + "/gfs_mslp_pcpn_neus_" + 1 + ".png";
//Insert Image to Document
document.getElementById("map").setAttribute("src", mapAddress);
setTimeout(2000);
//Load Image and Get Image Data
document.getElementById("canvas").getContext("2d").drawImage(document.getElementById("map"), 0, 0);
document.getElementById("map").crossOrigin = "Anonymous";
var pixelColor = document.getElementById("canvas").getContext("2d").getImageData(469,451,1,1).data;
//Test Pixel Data for Color
if (pixelColor[0] == 255 && pixelColor[1] == 255 && pixelColor[2] == 255 && pixelColor[3] == 255) {
console.log("The code ran the 'if' part of the PixelColor if statement.");
} else {
console.log("The code ran the 'else' part of the pixelColor if statement.");
}
</script>
For some reason, the image that is supposed to be displayed in the canvas isn't showing up. Can anyone tell me why, and how I can fix it? I want solutions that only involve HTML or Javascript since I am a beginner.
setTimeout(2000);does nothing. The signature issetTimeout(functionToCall, milliseconds). It sets up an asynchronous call to the function you give it, X milliseconds (or so) later. Whether this relates to the image thing I have no idea, it just jumped out at me.currentRun"calculation" correct? Using your algorithm (currentRun == 18) I get an404error. If I use the value prior to that (currentRun == 12) there is an image.currentRunandmapAddressvariables work perfectly when I run the code. If I printmapAddressto the console, the link to image I want comes up.