function setCompass(degrees) {
var x, y, r, ctx, radians;
ctx = window.compass.getContext("2d");
radians = 0.0174533 * (degrees - 90);
x = ctx.canvas.width / 2;
y = ctx.canvas.height / 2;
r = x * 0.8;
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height );
ctx.beginPath();
ctx.strokeStyle = "rgba(255,0,0,0.5)";
ctx.fillStyle = "rgba(255,0,0,0.5)";
ctx.lineCap = 'round';
ctx.shadowOffsetX = 4;
ctx.shadowOffsetY = 4;
ctx.shadowBlur = 2;
ctx.shadowColor = "rgba(0, 0, 0, 0.5)";
ctx.lineWidth = 10;
ctx.moveTo(x, y);
ctx.lineTo(x + r * Math.cos(radians), y + r * Math.sin(radians));
ctx.stroke();
}
function getWeatherForecast() {
var url = 'http://api.wunderground.com/api/eb7a37c339cfd624/geolookup/conditions/forecast10day/lang:EN/q/Germany/Berlin.json';
$.getJSON(url, function(data) {
window.debug.innerHTML = JSON.stringify(data, false, ' ');
$('#status').html(
data.location.city + ', ' +
data.location.country_name + ': ' +
data.current_observation.temperature_string + ', ' +
'Wind ' +
data.current_observation.wind_string + ', ' +
data.current_observation.wind_degrees + '°'
);
setCompass(data.current_observation.wind_degrees);
});
}
$('#test').click(function() {
$(this).attr('disabled', true);
var d=0, id = setInterval(function() {
setCompass( d );
d += 10;
if (d > 360) {
clearInterval(id);
$('#test').attr('disabled',false);
getWeatherForecast();
}
}, 100);
});
$(document).ready(function() {
getWeatherForecast();
});
#compass {
background: url(http://i.imgur.com/44nyA.jpg);
background-size: cover;
}
body {
font-family: sans-serif;
}
#debug {
background-color: aliceblue;
border: 1px solid black;
padding: 0.5em;
width: 90%;
height: 50em;
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="test">Test</button> scroll down to view json data<br>
<canvas id="compass" height=200 width=200></canvas>
<div id="status">Berlin, Germany</div>
json data source: <a href="http://api.wunderground.com" target="_blank">Weather Underground</a><br>
<textarea id="debug" spellcheck=false></textarea>