Skip to content

Instantly share code, notes, and snippets.

@nebbishhacker
Last active January 1, 2021 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nebbishhacker/cde2bef286bffa1a7e3b523180402c89 to your computer and use it in GitHub Desktop.
Save nebbishhacker/cde2bef286bffa1a7e3b523180402c89 to your computer and use it in GitHub Desktop.
locale = require("locale");
const conditions = [
"thunderstorm with light rain",
"thunderstorm with rain",
"thunderstorm with heavy rain",
"light thunderstorm",
"thunderstorm",
"heavy thunderstorm",
"ragged thunderstorm",
"thunderstorm with light drizzle",
"thunderstorm with drizzle",
"thunderstorm with heavy drizzle",
"light intensity drizzle",
"drizzle",
"heavy intensity drizzle",
"light intensity drizzle rain",
"drizzle rain",
"heavy intensity drizzle rain",
"shower rain and drizzle",
"heavy shower rain and drizzle",
"shower drizzle",
"light rain",
"moderate rain",
"heavy intensity rain",
"very heavy rain",
"extreme rain",
"freezing rain",
"light intensity shower rain",
"shower rain",
"heavy intensity shower rain",
"ragged shower rain",
"light snow",
"Snow",
"Heavy snow",
"Sleet",
"Light shower sleet",
"Shower sleet",
"Light rain and snow",
"Rain and snow",
"Light shower snow",
"Shower snow",
"Heavy shower snow",
"mist",
"Smoke",
"Haze",
"sand/dust whirls",
"fog",
"sand",
"dust",
"volcanic ash",
"squalls",
"tornado",
"clear sky",
"few clouds: 11-25%",
"scattered clouds: 25-50%",
"broken clouds: 51-84%",
"overcast clouds: 85-100%"
];
const locations = [
"Tokyo",
"Jakarta",
"Chongqing",
"Manila",
"Delhi",
"Seoul",
"Mumbai",
"Shanghai",
"São Paulo",
"Beijing",
"New York City",
"Lagos",
"Mexico City",
"Guangzhou",
"Dhaka",
"Osaka",
"Cairo",
"Karachi",
"Moscow",
"Bangkok",
"Chengdu",
"Los Angeles",
"Kolkata",
"Buenos Aires",
"Istanbul",
"Tehran",
"London",
"Shenzhen",
"Tianjin",
"Kinshasa",
"Rio de Janeiro",
"Paris",
"Baoding",
"Lahore",
"Lima",
"Bangalore",
"Ho Chi Minh City",
"Harbin",
"Wuhan",
"Shijiazhuang",
"Bogotá",
"Suzhou",
"Linyi",
"Chennai",
"Nagoya",
"Nanyang",
"Zhengzhou",
"Hyderabad",
"Surabaya",
"Hangzhou",
"Johannesburg",
"Chicago",
"Qingdao",
"Xi'an",
"Quanzhou",
"Taipei",
"Dongguan",
"Bandung",
"Hanoi",
"Shenyang",
"Baghdad",
"Onitsha",
"Kuala Lumpur",
"Ahmedabad",
"Luanda",
"Washington, D.C.",
"Dallas",
"Hong Kong",
"Pune",
"Nanjing",
"Boston",
"Santiago",
"Houston",
"Riyadh",
"Düsseldorf",
"Madrid",
"Toronto",
"Surat",
"San Francisco"
];
function drawSun(x, y, r) {
g.setColor("#FF7700");
g.fillCircle(x, y, r);
}
function drawCloud(x, y, r, c) {
const u = r / 12;
if (c == null) c = "#EEEEEE";
g.setColor(c);
g.fillCircle(x - 8*u, y + 3*u, 4*u);
g.fillCircle(x - 4*u, y - 2*u, 5*u);
g.fillCircle(x + 4*u, y + 0*u, 4*u);
g.fillCircle(x + 9*u, y + 4*u, 3*u);
g.fillPoly([x - 8*u, y + 7*u,
x - 8*u, y + 3*u,
x - 4*u, y - 2*u,
x + 4*u, y + 0*u,
x + 9*u, y + 4*u,
x + 9*u, y + 7*u]);
}
function drawBrokenClouds(x, y, r) {
drawCloud(x + 1/8*r, y - 1/8*r, 7/8*r, "#777777");
drawCloud(x - 1/8*r, y + 1/8*r, 7/8*r);
}
function drawFewClouds(x, y, r) {
drawSun(x + 3/8*r, y - 1/8*r, 5/8*r);
drawCloud(x - 1/8*r, y + 1/8*r, 7/8*r);
}
function drawRainLines(x, y, r) {
g.setColor("#FFFFFF");
var y1 = y + 1/2*r;
var y2 = y + 1*r;
g.fillPoly([x - 6/12*r + 1, y1,
x - 8/12*r + 1, y2,
x - 7/12*r, y2,
x - 5/12*r, y1]);
g.fillPoly([x - 2/12*r + 1, y1,
x - 4/12*r + 1, y2,
x - 3/12*r, y2,
x - 1/12*r, y1]);
g.fillPoly([x + 2/12*r + 1, y1,
x + 0/12*r + 1, y2,
x + 1/12*r, y2,
x + 3/12*r, y1]);
}
function drawShowerRain(x, y, r) {
drawFewClouds(x, y - 1/3*r, r);
drawRainLines(x, y, r);
}
function drawRain(x, y, r) {
drawBrokenClouds(x, y - 1/3*r, r);
drawRainLines(x, y, r);
}
function drawThunderstorm(x, y, r) {
function drawLightning(x, y, r) {
g.setColor("#FF7700");
g.fillPoly([x - 2/6*r, y - r,
x - 4/6*r, y + 1/6*r,
x - 1/6*r, y + 1/6*r,
x - 3/6*r, y + 1*r,
x + 3/6*r, y - 1/6*r,
x + 0/6*r, y - 1/6*r,
x + 3/6*r, y - r]);
}
drawBrokenClouds(x, y - 1/3*r, r);
drawLightning(x - 1/12*r, y + 1/2*r, 1/2*r);
}
function drawSnow(x, y, r) {
function rotatePoints(points, pivotX, pivotY, angle) {
for (var i = 0; i < points.length; i += 2) {
const x = points[i];
const y = points[i + 1];
points[i] = Math.cos(angle) * (x - pivotX) - Math.sin(angle) * (y - pivotY) + pivotX;
points[i + 1] = Math.sin(angle) * (x - pivotX) + Math.cos(angle) * (y - pivotY) + pivotY;
}
}
g.setColor("#FFFFFF");
const w = 1/12 * r;
for (var i = 0; i <= 6; ++i) {
const points = [x + w, y,
x - w, y,
x - w, y + r,
x + w, y + r];
rotatePoints(points, x, y, i / 3 * Math.PI);
g.fillPoly(points);
for (var j = -1; j <= 1; j += 2) {
const points = [x + w, y + 7/12*r,
x - w,y + 7/12*r,
x - w, y + r,
x + w, y + r];
rotatePoints(points, x, y + 7/12*r, j / 3 * Math.PI);
rotatePoints(points, x, y, i / 3 * Math.PI);
g.fillPoly(points);
}
}
}
function drawMist(x, y, r) {
const layers = [[-0.4, 0.5],
[-0.8, 0.3],
[-0.2, 0.9],
[-0.9, 0.7],
[-0.2, 0.3]];
g.setColor("#FFFFFF");
for (var i = 0; i < 5; ++i) {
g.fillRect(x + layers[i][0] * r, y + (0.4 * i - 0.9) * r, x + layers[i][1] * r, y + (0.4 * i - 0.7) * r - 1);
g.fillCircle(x + layers[i][0] * r, y + (0.4 * i - 0.8) * r - 0.5, 0.1 * r - 0.5);
g.fillCircle(x + layers[i][1] * r, y + (0.4 * i - 0.8) * r - 0.5, 0.1 * r - 0.5);
}
}
function chooseIcon(condition) {
condition = condition.toLowerCase();
if (condition.includes("thunderstorm")) return drawThunderstorm;
if (condition.includes("freezing") || condition.includes("snow") || condition.includes("sleet")) return drawSnow;
if (condition.includes("drizzle") || condition.includes("shower")) return drawRain;
if (condition.includes("rain")) return drawShowerRain;
if (condition.includes("clear")) return drawSun;
if (condition.includes("few clouds")) return drawFewClouds;
if (condition.includes("scattered clouds")) return drawCloud;
if (condition.includes("clouds")) return drawBrokenClouds;
return drawMist;
}
function draw() {
w = {
"temp": Math.round(Math.random() * 60 - 30),
"hum": Math.round(Math.random() * 100),
"txt": conditions[Math.floor(Math.random() * conditions.length)],
"wind": Math.round(Math.random() * 100),
"loc": locations[Math.floor(Math.random() * locations.length)],
};
g.clear(true);
Bangle.drawWidgets();
chooseIcon(w.txt)(65, 90, 55);
temp = locale.temp(w.temp).match(/^(\D*\d*)(.*)$/);
g.setColor("#FFFFFF");
var width = g.setFont("Vector", 40).stringWidth(temp[1]);
width += g.setFont("Vector", 20).stringWidth(temp[2]);
g.setFont("Vector", 40).setFontAlign(-1, -1, 0);
g.drawString(temp[1], 180 - width / 2, 70);
g.setFont("Vector", 20).setFontAlign(1, -1, 0);
g.drawString(temp[2], 180 + width / 2, 70);
g.setFont("6x8", 1);
g.setFontAlign(-1, 0, 0);
g.drawString("Humidity", 135, 130);
g.drawString("Wind", 135, 142);
g.setFontAlign(1, 0, 0);
g.drawString(w.hum + "%", 225, 130);
g.drawString(locale.speed(w.wind), 225, 142);
g.setFont("6x8", 2).setFontAlign(0, 0, 0);
g.drawString(w.loc, 120, 170);
g.setFont("6x8", 1).setFontAlign(0, 0, 0);
g.drawString(w.txt.charAt(0).toUpperCase() + w.txt.slice(1), 120, 190);
g.flip();
}
draw();
setInterval(draw, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment