Skip to content

Commit

Permalink
Improved JS Structure
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyhsGames committed Aug 29, 2024
1 parent bbf6467 commit 0e73ac4
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 81 deletions.
2 changes: 1 addition & 1 deletion src/LivelyInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"Contact": "",
"Type": 1,
"FileName": "wallpaper.html",
"Arguments": " --audio --system-information --system-nowplaying",
"Arguments": " --audio --system-nowplaying",
"IsAbsolutePath": false,
"Id": null,
"Tags": null,
Expand Down
11 changes: 10 additions & 1 deletion src/LivelyProperties.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"label1": {
"type": "label",
"value": "General"
"value": "Background"
},
"imgOverlay": {
"max": 100,
Expand All @@ -19,6 +19,15 @@
"folder": "images"
},
"label2": {
"type": "label",
"value": "Location"
},
"city": {
"type": "textbox",
"text": "City",
"value": "London"
},
"label3": {
"type": "label",
"value": "Audio Visualiser"
},
Expand Down
4 changes: 0 additions & 4 deletions src/data/weather.json

This file was deleted.

35 changes: 35 additions & 0 deletions src/js/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Define a global object if it doesn't exist
window.config = window.config || {};

function livelyPropertyListener(name, val) {
if (name === "color1") {
window.config.inputColor1 = val;
} else if (name === "color2") {
window.config.inputColor2 = val;
} else if (name === "glow") {
window.config.glow = val;
} else if (name === "glowStrength") {
window.config.glowStrength = val;
} else if (name === "opacity") {
window.config.opacity = val / 100;
} else if (name === "imgPath") {
window.config.imgName = val;
document.getElementById("backgroundImg").src = "/" + val;
} else if (name === "imgOverlay") {
window.config.imageOverlayDarkness = val / 100;
document.getElementById('backgroundOverlay').style.backgroundColor = `rgba(0, 0, 0, ${val / 100})`;
} else if (name === "albumSize") {
window.config.albumSize = val;
document.querySelector('.song-icon').style.width = `${val}px`;
} else if (name === "city") {
window.config.city = val;
}
}

function hexToRGB(hex) {
let r = parseInt(hex.slice(1, 3), 16);
let g = parseInt(hex.slice(3, 5), 16);
let b = parseInt(hex.slice(5, 7), 16);

return r + "," + g + "," + b;
}
79 changes: 17 additions & 62 deletions src/js/music.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,8 @@ const headerArtist = document.getElementById('song-artist');
const headerTitle = document.getElementById('song-name');

var numberOfFrequencies = 1;
var inputColor1 = "#5603fc";
var inputColor2 = "#fc0303";
var glow = true;
var glowStrength = 10;
var amplitude = 300;
var spectrumScale = 1;
var spectrumX = 0.5;
var sprcctrumY = 0.5;
var opacity = 1;
var imgName = "";
var imageOverlayDarkness = 0.3;
var albumSize = 200;

async function livelyCurrentTrack(data) {
let obj = JSON.parse(data);
Expand All @@ -32,82 +22,47 @@ async function livelyCurrentTrack(data) {
}
}

function livelyPropertyListener(name, val) {
// or switch-case...
if (name == "color1") {
inputColor1 = hexToRGB(val);
}
else if (name == "color2") {
inputColor2 = hexToRGB(val);
}
else if (name == "glow") {
glow = val;
}
else if (name == "glowStrength") {
glowStrength = val;
}
else if (name == "opacity") {
opacity = val / 100;
}
else if (name == "imgPath") {
imgName = val;
document.getElementById("backgroundImg").src = "/" + imgName;
}
else if (name == "imgOverlay") {
imageOverlayDarkness = val / 100;
document.getElementById('backgroundOverlay').style.backgroundColor = `rgba(0, 0, 0, ${imageOverlayDarkness})`;
}
else if (name == "albumSize") {
albumSize = val;
document.querySelector('.song-icon').style.width = `${albumSize}px`;
}
}

function livelyAudioListener(audioArray) {
drawSpectrum(audioArray);
async function livelyAudioListener(audioArray) {
await drawSpectrum(audioArray);
}

function drawSpectrum(audioArray) {
let c = document.getElementById("canvas");
let ctx = c.getContext("2d");
async function drawSpectrum(audioArray) {
let canvas = document.getElementById("canvas");
let ctx = canvas.getContext("2d");
let Wwidth = Math.round(window.innerWidth * spectrumScale);
let Wheight = Math.round((window.innerHeight / 2 + amplitude) * spectrumScale);
c.setAttribute('width', Wwidth);
c.setAttribute('height', Wheight);

//c.style.left = Math.round(window.innerWidth * spectrumX - c.width / 2) + "px";
//c.style.top = Math.round(window.innerHeight * spectrumY - c.height) + "px";
canvas.setAttribute('width', Wwidth);
canvas.setAttribute('height', Wheight);

ctx.clearRect(0, 0, c.width, c.height);
if (glow) {
ctx.shadowBlur = glowStrength;
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (window.config.glow) {
ctx.shadowBlur = window.config.glowStrength;
}
else {
ctx.shadowBlur = 0;
}

ctx.globalAlpha = opacity;
ctx.globalAlpha = window.config.opacity;

let width = c.width;
let height = c.height;
let width = canvas.width;
let height = canvas.height;
let offset = Math.round(0 * width);
let numberOfSamples = 128;
let spacing = Math.round(3 * spectrumScale);
let barwidth = Math.floor((width - 2 * offset - (numberOfFrequencies - 1) * spacing) / numberOfFrequencies);

let a = 0;

for (let i = 0; i < numberOfSamples; i++) {
if (i == 0) {
ctx.fillStyle = interpolateColor(inputColor1, inputColor2, a / numberOfFrequencies);
ctx.shadowColor = interpolateColor(inputColor1, inputColor2, a / numberOfFrequencies);
ctx.fillStyle = interpolateColor(hexToRGB(window.config.inputColor1), hexToRGB(window.config.inputColor2), a / numberOfFrequencies);
ctx.shadowColor = interpolateColor(hexToRGB(window.config.inputColor1), hexToRGB(window.config.inputColor2), a / numberOfFrequencies);
ctx.fillRect(offset + a * (barwidth + spacing), height - 50 - Math.round((amplitude * spectrumScale) * Math.min(1, audioArray[i])), barwidth, Math.round((amplitude * spectrumScale) * Math.min(1, audioArray[i])));
a++
}
else {
if (audioArray[i] != audioArray[i - 1]) {
ctx.fillStyle = interpolateColor(inputColor1, inputColor2, a / numberOfFrequencies);
ctx.shadowColor = interpolateColor(inputColor1, inputColor2, a / numberOfFrequencies);
ctx.fillStyle = interpolateColor(hexToRGB(window.config.inputColor1), hexToRGB(window.config.inputColor2), a / numberOfFrequencies);
ctx.shadowColor = interpolateColor(hexToRGB(window.config.inputColor1), hexToRGB(window.config.inputColor2), a / numberOfFrequencies);
ctx.fillRect(offset + a * (barwidth + spacing), height - 50 - Math.round((amplitude * spectrumScale) * Math.min(1, audioArray[i])), barwidth, Math.round((amplitude * spectrumScale) * Math.min(1, audioArray[i])));
a++
}
Expand Down
13 changes: 4 additions & 9 deletions src/js/weather.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@ const icons = {
};

async function fetchCurrentWeather() {
await fetch('./data/weather.json')
.then(response => response.json())
.then(data => {
latitude = data.latitude;
longitude = data.longitude;
})
.catch(error => console.error('Error fetching weather:', error));
const cityReposnse = await fetch(`https://nominatim.openstreetmap.org/search?city=${window.config.city}&format=json`);
const cityData = await cityReposnse.json();

const response = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&current=temperature_2m,precipitation,weathercode&timezone=Pacific%2FAuckland`);
const response = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${cityData[0].lat}&longitude=${cityData[0].lon}&current=temperature_2m,precipitation,weathercode&timezone=Pacific%2FAuckland`);
const data = await response.json();

if (data.current) {
Expand All @@ -49,7 +44,7 @@ async function fetchCurrentWeather() {
}
};
fetchCurrentWeather();
setInterval(fetchCurrentWeather, 600000);
setInterval(fetchCurrentWeather, 1000);

function getWeatherDescriptionAndIcon(weatherCode) {
switch (weatherCode) {
Expand Down
10 changes: 6 additions & 4 deletions src/wallpaper.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./styles/wallpaper.css">
<script defer src="./js/wallpaper.js"></script>
<script defer src="./js/weather.js"></script>
<script defer src="./js/music.js"></script>
<title>Document</title>
</head>

Expand Down Expand Up @@ -36,7 +33,7 @@ <h3 id="song-name">Song Name</h3>
<div class="main-widget">
<div class="widget" id="current-weather-widget">
<div class="weather-icon" id="icon"></div>
<h3 id="temperature"></h3>
<h3 id="temperature">Loading...</h3>
</div>
<div class="widget" id="next-sub-widget">
<h2 id="next-subject">Loading...</h2>
Expand All @@ -45,6 +42,11 @@ <h2 id="next-subject">Loading...</h2>
</div>
<p style="bottom: 45px; position: absolute; color: #ffff; padding: 10px; text-shadow: 1px 1px 4px #000;">Made by
coreyhsGames</p>
<!-- SCRIPTS -->
<script defer src="./js/config.js"></script>
<script defer src="./js/wallpaper.js"></script>
<script defer src="./js/weather.js"></script>
<script defer src="./js/music.js"></script>
</body>

</html>

0 comments on commit 0e73ac4

Please sign in to comment.