Utilizing clip-path polygons for effective styling on Firefox and iOS

I have been working on a plugin to create animated modal boxes by utilizing the clip-path property. However, I have encountered an issue where this code only seems to work in Chrome. You can view the codepen demo here.

Unfortunately, it appears that Firefox does not support the use of polygon() in the clip-path property, and Safari & Mobile Safari also struggle with it.

Does anyone have any suggestions on how to make this plugin compatible with Firefox, Safari, and Mobile Safari as well? Any ideas on how to approach this problem would be greatly appreciated.

Here is the JavaScript code snippet for reference:


var $ov = $('.overlay');

$(document).on('click touchstart', '.inner', function(){
  // Code here
});

And here is the CSS code snippet:


body, html {
  background: silver;
}

.grid {
  // Grid styling
}

.inner {
  // Inner box styling
}

.overlay {
  // Overlay styling
}

You can include the jQuery library from this CDN link:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

To implement the grid structure and inner elements, you can use the following HTML template:


<div class="grid">
  <div class="inner"></div>
</div>
// Repeat the above structure for multiple grids and inner elements
<div class="overlay"></div>

Answer №1

If you're looking to add some animation to polygons, consider using svgs where you can animate the points of a polygon and morph it into different shapes.

Check out the DEMO here

Another DEMO with images and titles

This demonstration utilizes the snap.svg library to create and manipulate squares upon clicking.
While it's not yet finalized and there may be some bugs, it provides a good understanding.

I've completely revamped the code, with special thanks to rlemon for their help in improving it. The code has been tested on the latest Firefox and Chrome browsers, and a user confirmed it works on an iPhone using Safari.

The items are constructed using polygons, whereby a square polygon is added upon click and its points are animated individually for emphasis.

var items = [
  [1, 1, 24, 1, 24, 24, 1, 24],
  ...
  ...
    
*{margin:0;padding:0;}
body{background:#E3DFD2;}
svg{display:block;}
<script src="http://thisisa.simple-url.com/js/snapsvg.js"></script>

Answer №2

When using clip-path, make sure to only use numbers for point values. Avoid using percentages or pixel scales as this may cause issues in Firefox.

To fix this issue, update the code with integer values:

coordsString = 'polygon(0 0,123 0,' + coordArray[2] + ',' + coordArray[3] + ')';
$ov.css({
  '-webkit-clip-path': coordsString,
    'clip-path': coordsString
});

Ensure that any pixel references in the coordArray are removed and calculate the actual integer values from the percentages instead.

Answer №3

Recently, I encountered an issue where the clipped element wasn't displaying correctly on iPhoneX.

Fortunately, I found a solution by adding the webkit property:

clip-path: polygon(0 0, 0% 100%, 100% 0);
-webkit-clip-path: polygon(0 0, 0% 100%, 100% 0);

Answer №4

After extensive testing, it seems that polygon is not supported in Firefox according to http://caniuse.com/#search=polygon. Despite trying various approaches, the FF development tools consistently flag it as an incorrect value.

This is the code I tested (taken from your Codepen):

var $ov = $('.overlay');

$(document).on('click touchstart', '.inner', function(){
  var coords, coordArray, coordsString;
  var windowWidth = window.innerWidth + "";
  coords = this.getBoundingClientRect();

  coordArray = [
    Math.floor(parseInt(coords.left)) + ' ' + Math.floor(parseInt(coords.top)) + '',
    Math.ceil((parseInt(coords.left) + parseInt(coords.width))) + ' ' + Math.ceil(parseInt(coords.top)) + '',
    Math.ceil((parseInt(coords.left) + parseInt(coords.width))) + ' ' + Math.ceil((parseInt(coords.top) + parseInt(coords.height) )) + '',
    Math.ceil(parseInt(coords.left)) + ' ' + Math.floor((parseInt(coords.top) + parseInt(coords.height) )) + ''
  ];


  coordsString = 'polygon(' + coordArray[0] + ',' + coordArray[1] + ',' + coordArray[2] + ',' + coordArray[3] + ')';

  $ov.css({
    '-webkit-clip-path': coordsString,

    'clip-path': coordsString
  });

  setTimeout(function(){
    $ov.addClass('show');
  },50);

  // Additional code snippets truncated for brevity

   setTimeout(function(){
    $ov.removeClass('show');
  },2000);

});

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

Controlling the Flow of Events in JavaScript Documents

Explore the integration of two interconnected Javascript files and delve into managing event propagation between them effectively. 1st js file var red = [0, 100, 63]; var orange = [40, 100, 60]; var green = [75, 100, 40]; var blue = [196, 77, 55]; var ...

Is it possible to create a CSS-only negative border radius?

I'm reaching out for help with a specific issue. I want to create a unique negative border radius for my navigation links, similar to the design shown in this image: http://prntscr.com/9vi0b5 Instead of using images like in the example, I'm look ...

Generating a single JSON record in React Native

Need help displaying a single JSON record from an API request on the screen. const [user, setUser] = useState(); const getUserData = async () => { // {headers: {Authorization: "Basic " + base64.encode(username + ":" + passwor ...

How can I successfully add an element to a nested array without making any mistakes in saving it?

Hello everyone, I'm new here. I previously posted about a similar issue, but now I have a different one. I am encountering this object: singleChat = [ { "chatid": 10000414, "connected": true, "index": 0, ...

Best practices for bulk inserting sequences in Node.js using MySQL

I have a dataset ready to be inserted into a MySQL database using nodejs. Here is the code I've written: con.connect(function (err) { myArray.forEach((el)=>{ con.query(1stQuery,1stValue,(error,result)=>{ //do something with ...

Issue with displaying a vTable component in VueJS / Vuetify

I am struggling with this basic HTML/Vue/Vuetify code snippet below, and I can't seem to get it functioning as intended. const { createApp, computed, ref, reactive } = Vue; const { createVuetify } = Vuetify; const ...

Cookies are strangely absent from the ajax call to the web api - a puzzling issue indeed for Web Api users

Hello, here is the code I'm working with using Ajax: function GetCurrentUserId() { return $.ajax({ type: "GET", url: rootUrl + '/api/Common/CurrentDateAndUser', dataType: 'json', ...

Ensuring the aspect ratio of images stays consistent within a flexbox using CSS

How can I create a grid of images without any spacing in between them? I want the width to be 100% of the parent element and each column to have the same size, for example, 20%. Using flex takes care of the columns, but I'm struggling with making the ...

AngularJS select box setting selected valueIt can be accomplished by manipulating the

I am facing an issue with a selectbox populated with options from the backend. <tr> <td class="col-lg-4">Superkund</td> <td><select class="form-control input-sm2" ng-model="selectedSupercustomer" ng-options="item as item ...

Utilize .map function to format a date string and generate a table

I am currently utilizing .map along with React in order to generate a table using a coupons object. My goal is to format a date string into the "mm/dd/yyyy" format, with coupon.starts_at typically being set as coupon.starts_at = "2013-08-03T02:00:00Z" I h ...

Utilize JavaScript conditions to dynamically apply styles within your web application

I am facing a challenge with managing two separate <style> tags that each contain a large number of styles and media queries. The issue is that one set of styles is intended for desktop users, while the other is meant for mobile users. When both se ...

Swap out original source files in HTML with minified versions

I have successfully utilized a maven plugin to create a minified and compressed version of my CSS and JavaScript files. Now, I am looking to update the section in my main HTML page that currently loads all those individual files. Here is what it looks lik ...

React powered interactive tables

I am in the process of creating a dynamic table using React, and here is the data structure I am working with: { numRows: 2, numCols: 3, cells: [ { id: 1, pos: { row: 1, col: 1 }, content: 'This is th ...

Verifying picture quality prior to transferring to a remote server

I am facing an issue with my image upload function to a remote server. The upload function works fine on its own, but when I integrate it into the size validation block, it stops working properly. <p> <input type="file" id="BtnBro ...

Tips for Implementing Color-coded Manchu/Mongolian Script on Your Website

My journey began with a seemingly straightforward task. I had a Manchu word written in the traditional script and I wanted to change the color of all the vowels in the word (ignoring ligatures). <p>ᠠᠮᠪᡠᠯᠠ ᠪᠠᠨᡳᡥᠠ</p> < ...

How come my web page is not displaying the guages from guage.js?

I am utilizing guage.js to create a graph based on this Fiddle. However, upon adding the same code to my website, the rendering does not appear as expected: Upon inspecting in Chrome developer tools, I noticed that the element still exists but lacks heigh ...

Unable to enclose a table within a div element for Chrome web browser

I attempted to keep a table within a div to ensure that the table's width does not go beyond the width of the containing DIV. This table consists of one row with two elements. The second element is fixed in length, while I want the first element to w ...

Creative ways to conceal JavaScript within your Wordpress website

After placing my javascripts in the header.php file, I realized that the code could easily be snatched by viewing the source on any post or homepage... Can you recommend a straightforward method to conceal my javascripts in WordPress and prevent them from ...

Using Javascript to remove spans that do not have an id assigned

Is there a way to identify and remove spans without ids from a string? I have a text with several spans, some with ids and some without. Input: <span>Hi there!</span><span id="blabla">This is a test</span> Output: Hi there!< ...

Display a confirmation modal before triggering $routeChangeStart in AngularJs, similar to the window.onbeforeunload event

When a user chooses to stay on the page as the route starts to change, the original route remains intact but the form directives are reloaded. This results in the loss of all checkbox and input values, resetting them to their defaults. If a user closes th ...