4

I searched throughout stackoverflow for this but the thing is, there exist several answers which explain how to do so however it turns out that none of those answers really achieve the actual goal. The output does look like a semi circle but it isn't a perfect semicircle. Allow me to explain, I ran the equation of circle and moved something across the semi circle however near the top of the shape, my object deviates but does in fact overlap the shape on the left and right ends. See the three images below

Left endCenter (top)right end

My css is below:

slider {
    width: 75%;
    border-right: solid 0.2vw;
    border-left: solid 0.2vw;
    border-top: solid 0.2vw;
    border-bottom-color: transparent;
    border-bottom: hidden;
    border-bottom: none;
    border-left-color: white;
    border-right-color: white;
    border-top-color: white;
    position: relative;
    border-top-left-radius: 37.7vw; /*keeping in mind the 0.2vw width*/
    border-top-right-radius: 37.7vw; /*100% 200%*/
    display: inline-block;
    padding-top: 37.5%;
    padding-bottom: 0px;
    padding-left: 0px;
    padding-right: 0px;
    margin: 0px;
}

Seemingly, this should be enough information to come up with a solution but if you want to read further, feel free to.

I need my shape to be responsive which can be inferred through the css I have written. I am preserving the shape by fixing its aspect ratio through the padding hack. My html is below:

<holder>
        <slider style="top: 25%; left: 50%; transform: translate(-50%,-25%);" id="sliderx" class="slider"></slider>
        <img style="width:13%; position: absolute; top: 45%; left: 50%; transform: translate(-50%,-45%); opacity:0;" src="~/Assets/Images/SomeImageThatIHaveNotShownInMyScreenshots.png" />
</holder>

Although I believe I have provided sufficient information and it is unnecessary to show holder's css or my function which moves the blue ball in a circle but I am still posting them below: (Not very relevant [Feel free to skip])

holder {
    grid-row: span 20;
    display: block;
    position: relative;
    background-image: linear-gradient(rgba(52, 64, 122, 0.3), rgba(53, 63, 121, 0.3));
    padding: 0%;
}

    holder:hover {
        background-image: linear-gradient(rgba(52, 64, 122, 0.5), rgba(53, 63, 121, 0.5));
    }

The holder exists within another element whose css is: (Not very relevant [Feel free to skip])

divm {
    position: relative;
    height: 100%;
    z-index: 2;
    grid-column: span 30;
    overflow-y: auto;
    display: grid;
    grid-template-rows: repeat(41, 1fr);
    padding: 0px;
}

My function is: (Not very relevant [Feel free to skip])

function adjuster(coordinatex) {
    var x, y, xc, yc, r;
    var leftgap = document.getElementById("sliderx").getBoundingClientRect().left - document.getElementById("holder").getBoundingClientRect().left;
    var topgap = document.getElementById("sliderx").getBoundingClientRect().top - document.getElementById("holder").getBoundingClientRect().top;
    var widthen = document.getElementById("sliderx").getBoundingClientRect().width;
    r = (widthen / 2) - ($("#sliderx").css("border-left-width").replace(/[^-\d\.]/g, '') / 2);
    xc = leftgap + (widthen / 2);
    yc = topgap + (widthen / 2);
    x = coordinatex;
    y = (-1 * Math.pow((-1 * Math.pow((x - xc), 2)) + (Math.pow(r, 2)), 0.5)) + yc;
    document.getElementById("knob").style.left = x - (document.getElementById("knob").getBoundingClientRect().width / 2);
    document.getElementById("knob").style.top = y - (document.getElementById("knob").getBoundingClientRect().height / 2);
}

All I want is to make a perfect scalable and responsive semi circle. I cannot use an image and maintain the aspect ratio because as the ball moves on the semi circle, i want to change the color of the semicircle to blue to show the covered region. I can easily do it by color covered parts of the box border. I am looking forward to a fix in my current work (the box-border method) as most of my code revolves around it right now. I know other methods like svg or using a picture and painting it can work but the box border method seems simple.

Afnan Makhdoom
  • 654
  • 1
  • 8
  • 20
  • 1
    related : https://stackoverflow.com/q/54836891/8620333 can probably give you some ideas – Temani Afif Jul 07 '19 at 20:09
  • Yes, I have seen this – Afnan Makhdoom Jul 07 '19 at 20:11
  • My function and circle equation is working perfectly, I need to create a perfect semi circular arc – Afnan Makhdoom Jul 07 '19 at 20:12
  • a perfect semi-circle is half a perfect circle and a perfect circle can lie inside a perfect square which is a 1:1 ratio element so create a circle and hide its bottom half – Temani Afif Jul 07 '19 at 20:13
  • I have made a 1:1 box and with my css shown above, I do try to achieve just that but it just isn't a perfect semicircle. – Afnan Makhdoom Jul 07 '19 at 20:15
  • what about this: https://jsfiddle.net/cxe2t8wd/, is it perfect? (add/remove box-sizing:border-box if needed) – Temani Afif Jul 07 '19 at 21:14
  • to create half a circle in css, you may use a full circle hidding half of it or use a radial-gradient . for the ball spinning, rotation is the way and you can use a single value to update it https://codepen.io/gc-nomade/pen/VJEMqM inspired from https://stackoverflow.com/questions/44958320/how-to-set-an-animation-to-have-elements-moving-around-a-circle/44971071#44971071 / https://stackoverflow.com/questions/43215883/rotate-objects-in-a-specific-axis-and-stop/43218244#43218244 – G-Cyrillus Jul 07 '19 at 23:13

0 Answers0