生成图片

master
夜笙歌 6 months ago
parent ab1894cbcf
commit e5aa999008

@ -24,11 +24,11 @@
</script> </script>
<script> <script>
$(()=>{ $(() => {
let getColorByTemperature = {} let getColorByTemperature = {}
function getGradientColor(level,sum) { function getGradientColor(level, sum) {
// 确保级别在1到50之间 // 确保级别在1到50之间
level = Math.max(1, Math.min(sum, level)); level = Math.max(1, Math.min(sum, level));
@ -46,13 +46,25 @@
const q = v * (1 - f * s); const q = v * (1 - f * s);
const t = v * (1 - (1 - f) * s); const t = v * (1 - (1 - f) * s);
switch(i % 6){ switch (i % 6) {
case 0: r = v, g = t, b = p; break; case 0:
case 1: r = q, g = v, b = p; break; r = v, g = t, b = p;
case 2: r = p, g = v, b = t; break; break;
case 3: r = p, g = q, b = v; break; case 1:
case 4: r = t, g = p, b = v; break; r = q, g = v, b = p;
case 5: r = v, g = p, b = q; break; break;
case 2:
r = p, g = v, b = t;
break;
case 3:
r = p, g = q, b = v;
break;
case 4:
r = t, g = p, b = v;
break;
case 5:
r = v, g = p, b = q;
break;
} }
return { return {
@ -69,35 +81,35 @@
return `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`; return `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`;
} }
const getData = () =>{ const getData = () => {
$.post(prefix+'/newDataStreak',(e)=>{ $.post(prefix + '/newDataStreak', (e) => {
$('#temInfo').html(`<span style="color:#ff0000">最高温度:${e.rows[0].maxTemperature}℃</span> <span style="color:#0b05f7">最低温度:${e.rows[0].minTemperature}℃</span> <span style="color:#0066e4">平均温度:${e.rows[0].avgTemperature}℃</span>`) $('#temInfo').html(`<span style="color:#ff0000">最高温度:${e.rows[0].maxTemperature}℃</span> <span style="color:#0b05f7">最低温度:${e.rows[0].minTemperature}℃</span> <span style="color:#0066e4">平均温度:${e.rows[0].avgTemperature}℃</span>`)
$('#timeInfo').html(`<span style="color:#0b05f7">采集时间:${e.rows[0].collectTime}</span> `) $('#timeInfo').html(`<span style="color:#0b05f7">采集时间:${e.rows[0].collectTime}</span> `)
let colors = [] let colors = []
let tems = [] let tems = []
for(i in e.rows[0]){ for (i in e.rows[0]) {
if(i.includes('site')){ if (i.includes('site')) {
tems.push(e.rows[0][i]) tems.push(e.rows[0][i])
} }
} }
let sum = Math.max(...tems) - Math.min(...tems) let sum = Math.max(...tems) - Math.min(...tems)
getColorByTemperature={} getColorByTemperature = {}
for (var i = 0; i <= sum; i++) { for (var i = 0; i <= sum; i++) {
getColorByTemperature[(Math.min(...tems))+i] = getGradientColor(i,sum) getColorByTemperature[(Math.min(...tems)) + i] = getGradientColor(i, sum)
}
for(i in e.rows[0]){
if(i.includes('site')){
colors.push(getColorByTemperature[e.rows[0][i]])
} }
for (let i = 1; i <= 768; i++) {
colors.push(getColorByTemperature[e.rows[0]['site' + i]])
} }
console.log(colors)
const canvas = document.getElementById('canvas'); const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d'); const ctx = canvas.getContext('2d');
// 设置canvas的宽度和高度 // 设置canvas的宽度和高度
const width = $('#canvasDiv').width(); const width = $('#canvasDiv').width();
const height =($('#canvasDiv').width()/32) *24; const height = ($('#canvasDiv').width() / 32) * 24;
canvas.width = width; canvas.width = width;
canvas.height = height; canvas.height = height;
@ -105,10 +117,10 @@
const gridHeight = height / 24; const gridHeight = height / 24;
// 遍历颜色数组,绘制矩形 // 遍历颜色数组,绘制矩形
for (let i = 0; i < 768; i++) { for (let i = 1; i <= 768; i++) {
const color = colors[i]; // 获取当前颜色 const color = colors[i - 1]; // 获取当前颜色
const x = (i % 32) * gridWidth; // 计算x坐标 const x = ((i - 1) % 32) * gridWidth; // 计算x坐标
const y = Math.floor(i / 32) * gridHeight; // 计算y坐标 const y = Math.floor((i - 1) / 32) * gridHeight; // 计算y坐标
// 绘制格子 // 绘制格子
ctx.fillStyle = color; // 设置填充颜色为十六进制颜色值 ctx.fillStyle = color; // 设置填充颜色为十六进制颜色值
ctx.fillRect(x, y, gridWidth, gridHeight); // 绘制矩形格子 ctx.fillRect(x, y, gridWidth, gridHeight); // 绘制矩形格子
@ -117,18 +129,18 @@
}) })
} }
getData() getData()
setInterval(getData,1000*2) // setInterval(getData, 1000 * 2)
}) })
</script> </script>
</body> </body>
</html> </html>
<style> <style>
.centerDiv{ .centerDiv {
position: absolute; position: absolute;
top: 50%; top: 50%;
left: 50%; left: 50%;
transform: translate(-50%,-50%); transform: translate(-50%, -50%);
border: 1vw solid #fff; border: 1vw solid #fff;
border-radius: 0.25vw; border-radius: 0.25vw;
} }

Loading…
Cancel
Save