Create a geometric shape with JavaScript by utilizing the CSS clip-path property

The code snippet above achieves the desired effect.

#overlay {
  background-color: rgba(0, 0, 0, 0.66);
  position: absolute;
  z-index: 1;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  clip-path: polygon( 0% 0%, /*exterior top left*/
  0% 100%, /*exterior bottom left*/
  220px 100%, /*overlapping point exterior 1*/
  220px 50%, /*overlapping point interior 1*/
  220px 310px, /*interior top left*/
  683px 310px, /*interior top right*/
  683px 450px, /*interior bottom right*/
  220px 450px, /*overlapping point interior 2*/
  220px 100%, /*overlapping point exterior 2*/
  100% 100%, /*exterior bottom right*/
  100% 0%);
  /*exterior top right*/
}
<body>
  <div>Some background</div>
  <div id="overlay">

  </div>
  <div id="closeButton">
    <p>
      Close
    </p>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script>
    function main() {
      $("#overlay").hide();
      $("#overlay").fadeIn(500);

      $("#closeButton").on("click", function() {
        $("#overlay").toggle();
      });
    }
    $(document).ready(main);
  </script>
</body>

Is there a way to create a reusable function that accepts an array of coordinates to achieve the same effect? This function would be triggered when the closeButton is clicked.

Answer â„–1

One way to create a polygon string in JavaScript is by using the following example function that can generate a polygon with 2 pixel coordinates:

#overlay {
  background-color: rgba(0, 0, 0, 0.66);
  position: absolute;
  z-index: 1;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
<body>
  <div>Some background</div>
  <div id="overlay">

  </div>
  <div id="closeButton">
    <p>
      Close
    </p>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script>

    // Use this function:
    function generatePoly(p1, p2) {
        var a = p1[0] + 'px';
        var b = p1[1] + 'px';
        var c = p2[0] + 'px';
        var d = p2[1] + 'px';
        return 'polygon(' + [
          '0% 0%',
          '0% 100%',
          a + ' 100%',
          a + ' 50%',
          a + ' ' + b,
          c + ' ' + b,
          c + ' ' + d,
          a + ' ' + d,
          a + ' 100%',
          '100% 100%',
          '100% 0%'
        ].join(', ') + ')';
    }

    function main() {

      // Set the polygon using this function:
      $("#overlay").css('clip-path', generatePoly([40, 80], [120, 200]));

      $("#overlay").hide();
      $("#overlay").fadeIn(500);

      $("#closeButton").on("click", function() {
        $("#overlay").toggle();
      });
    }
    $(document).ready(main);
  </script>
</body>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Load an image from cache using the ajax() method in jQuery asynchronously

For my upcoming project, I am looking to dynamically display cached images with an async call using the ajax() function in jQuery. $.ajax({ type: "POST", url: "mypage.aspx/GetThumbnail", data: "{‘id’:'" + idThumbnail + "’}", con ...

What are some creative ways to represent data using different numerical intervals?

Looking to develop a visualization of a .CSV file containing 1.2 million lines, showcasing addresses in the format: source , destination 12.251.512 , 12.623.743 51.734.312 , 23.233.991 6334.6231.123 , 42.532.54453 (utiliz ...

When encountering an OR operator, Javascript will cease execution of the remaining conditions

This is a basic JavaScript form-validation I created. All the document.form.*.value references are present on my page, except for the document.form.dasdasdas.value ==''. In the code below, the purpose is to display an error if any of the forms a ...

Concealing the .html extension within hyperlinks

If I have a link on my website located at public_html/index.html like this: <a href="pictures/index.html">Link to picture</a> When clicking the "Link to picture" link which leads to a folder using index.html file, the URL displayed in the add ...

Resize background image to perfectly fit within div dimensions

One of my divs has a background image that is getting clipped: <div style='text-align:center;background-image: url(/media/img_1_bg.jpg);background-repeat:no-repeat;width:450px;height:900px;' id="mainpage" align="center"> Is there a soluti ...

Monitoring alterations in dynamically produced input fields using jQuery

My code currently generates multiple input fields dynamically, each related to a specific database record. I utilize PHP's dynamic array notation ('[]') to store the input values. Everything is working smoothly up to this point... Now, I am ...

Vuetify - Implementing a search feature in a table that automatically navigates to the matching row

Currently, I am working with 2 Vuetify data tables that do not have pagination enabled. Each row in the second table corresponds to exactly one parent in the first table. My goal is to be able to click on an entry in the second table and have it automati ...

The Appsheet algorithm determined the exact expiration date as 2 days from the specified date

In my Appsheet, I have two columns: Production Date and Expired Date. If the Production Date is 35 months before the Expired Date, how can I create a formula in Appsheet to calculate this? For example, if the Production Date is 01/10/2023, then the Expired ...

Iterate through HTML content and utilize JavaScript along with Regular Expressions to substitute specific strings

In my HTML located in Anki, I have the following structure: <p>[!Quote] Title of callout 1<br>Content of callout 1</p> <p>[!Quote] Title of callout 2<br>Content of callout 2</p> <p>[!Quote] Title of callout 3<br ...

Getting the value of a CSS variable under <script> in Vue.js

I am working on implementing a Doughnut chart using chartJs in my application. However, I want to set the color of the chart within the <script> tag and retrieve the color from theme/variables.css. In the current code snippet below, there is a hardc ...

Is there a way to insert a space in a single iteration of an SQL query in PHP without impacting the others?

Is there a way to add a space between two words for certain iterations of an SQL query without affecting all uses? I attempted using CSS margins, but it didn't have the desired effect. Here is a snippet of the code I am currently working on: <div ...

Utilize HTML5 to send a document to Google Drive API

I'm currently developing a Google Chrome extension that utilizes the Google Drive API to upload files. While handling text files isn't an issue, I encounter errors when attempting to upload binary files. When trying to upload a file using the Fi ...

Implementation of the Bootstrap navbar hamburger menu is malfunctioning within a Django application

I recently completed a django app, but I'm facing an unusual issue with the bootstrap navbar not displaying menu items on mobile devices. When I access my app from a mobile browser, the hamburger menu fails to expand and show the navigation items. It ...

How come I am receiving {"readyState":1} in the DOM instead of JSON data in AngularJS?

Currently, I am facing an issue where instead of the JSON data (which consists of only 49 items) showing up on the DOM, I am getting {"readyState":1}. I believe this is just a test to ensure that my code is functioning correctly. Although I have identifie ...

Modify the value of the <select> tag based on whether it is required or not

When utilizing a my-select.component within a form component, the following code snippet is used: <div *ngIf="items?.length"> <select [ngModel]="selectedItem" (ngModelChange)="valueChanged($event)"> <optio ...

Implement auto partial page refreshing in ASP.NET without using the UpdatePanel feature

Looking to implement auto partial page refresh in asp.net without sending excessive data through UpdatePanel. Considering using a webservice called by JavaScript instead, but unsure how to trigger it automatically rather than by button click event. Many e ...

What could be causing my handle button to slide off the timeline towards the right?

I'm facing an issue with my volume bar component where the slider button is rendering outside of the timeline instead of on top of the progress bar. I need assistance in adjusting its position. // Here is the code for my volume bar component: import ...

What is the best way to implement a seamless transition effect to a disappearing and appearing navbar using JavaScript?

I have a question regarding creating a navbar that appears and disappears using JavaScript when clicking on a button element. I've been able to achieve this functionality, but now I would like to add a smooth transition effect to make the navbar appea ...

Exploring the Power of ReactJS Source Mapping

After adding the .env file to my react project, I noticed that when the project is uploaded to the server, the console source is displaying all components. Although I added the .env file and included the following code: GENERATE_SOURCEMAP = false; The i ...

Make an axios request multiple times equal to the number of items in the previous response

In my project, I am using the axios library to convert addresses into their respective coordinates. First, I fetch a list of addresses from an API. Next, I take the RESPONSE object and use Google API to convert each address to coordinates. Finally, I wan ...