Show where the sun is on the sky in real time - how?

夙愿已清 提交于 2019-12-04 17:12:26

I don't know the exact formula but here is an idea how you can create this using CSS then you simply have to adjust different values.

var sun = document.querySelector('.sun');

function update() {
  var x = Math.random() * 180;
  var y = Math.random() * 40;
  sun.style.transform="rotateX("+y+"deg) rotate("+x+"deg)"
}
.container {
  width:300px;
  height:150px;
  margin:auto;
  overflow:hidden;
  border:1px solid;
}
.sun {
  margin:20px;
  padding-top:calc(100% - 40px);
  position:relative;
  border-radius:50%;
  border:1px solid grey;
  transform:rotateX(20deg) rotate(20deg);
  background:
    linear-gradient(red,red) center/100% 1px;
  background-repeat:no-repeat;
  transition:1s;
}
.sun:before {
  content:"";
  position:absolute;
  top:calc(50% - 20px);
  left:-20px;
  width:40px;
  height:40px;
  background:yellow;
  border-radius:50%;
}
<div class="container">
<div class="sun">

</div>
</div>
<button onclick="update()">update</button>

Using the code below you simply need to convert your values to a degrees in order to rotate the sun element and place the sun in the correct place.

You can change css values by using JavaScript, I do not see any other way

    /* yes this JS code */
    const
      MvSun = document.getElementById('Mv-Sun'),
      Sun   = document.getElementById('sun')
    ;

    MvSun.onclick = function() {
      Sun.style.left = '200px';
    }
    #celestial-vault {
      position: relative;
      width: 400px;
      height: 300px;
      background-color: skyblue;
      display: block;
    }

    #sun {
      position: absolute;
      top: 10px;
      left: 20px;
      background-color:yellow;
      width:20px;
      height:20px;
      border-radius: 10px;
      display: block;
    }
  <div id="celestial-vault">
      <div id="sun"></div>
  </div>

  <button id="Mv-Sun">move sun</button>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!