Git Product home page Git Product logo

clip-path-polygon's Introduction

clip-path-polygon Build Status

This is a jQuery plugin that makes using clip-path property easy on whatever tag under different browsers.

Tested on latest Chrome, Safari, Firefox and Opera.

It should work on Chrome ≥31, Firefox ≥31, Safari ≥7 and Opera ≥30.

What it does?

Let's say that you want to achieve something like this:

Take a look at the example html file!

So you need to have a rectangle (e.g. 300x200) and you have to crop this image with such coordinates: (0; 0), (145; 0), (150; 20), (155; 0), (300; 0), (300; 200), (0; 200), (0; 0)

Absolute values

In Webkit all you have to do is to write a css style: -webkit-clip-path: polygon(0 0, 145px 0, 150px 20px, 155px 0, 300px 0, 300px 200px, 0 200px, 0 0)

In Firefox and in W3C standard what you should do is: clip-path: url(#my-definition) and somewhere in the file:

<svg>
    <defs>
        <clippath id="my-definition">
            <polygon points="0 0, 145 0, 150 20, 155 0, 300 0, 300 200, 0 200, 0 0"></polygon>
        </clippath>
    </defs>
</svg>

Relative values

If your design is resposive, you might want to use relative unit - percents. Then, the expected values would be:

  • Webkit: polygon(0 0, 49% 0, 50% 10%, 51% 0, 100% 0, 100% 100%, 0 100%, 0 0)
  • Firefox: <polygon points="0 0, 0.49 0, 0.5 0.1, 0.51 0, 1 0, 1 1, 0 1, 0 0"></polygon>

clip-path-polygon does this job for you!

Installation

Npm

Install with https://npmjs.org or add it to your package.json:

$ npm install clip-path-polygon

Then require it:

require('clip-path-polygon');

and use:

$myElement.clipPath();

Bower

$ bower install clip-path-polygon --save

and then add bower_components/clip-path-polygon/build/clip-path-polygon.min.js to your scripts.

<script src="bower_components/clip-path-polygon/build/clip-path-polygon.min.js"></script>

I'm assuming here that your bower installation folder is called bower_components.

Browser

Download clip-path-polygon.min.js (minified) or clip-path-polygon.js (dev) and add it to you HTML file:

<script src="clip-path-polygon.min.js"></script>

Compilation

If you want to compile the whole package with unit tests, run: npm install and then grunt (compilation) or grunt test (tests).

I use mocha, sinonjs and expect.js for testing.

Changelog

Changlelog is available here: CHANGELOG.md

API

Definition: clipPath(points [, options])

You can call it on a jQuery element:

$('#my-element').clipPath(points);

where points is an array of two-elements arrays: [[x0, y0], [x1, y1], [x2, y2]...] crops the element to this area defined by these points.

There are some options that you can use:

Option Default Description
isPercentage *false* specifies whether you want to use absolute numbers (pixels) or relative unit (percents)
isForWebkit *true* specifies if `-webkit-clip-path` property should be added to element
isForSvg *true* specifies if `-clip-path` property and `` element should be added
svgDefId *clipPathPolygonGenId* specifies *id* of SVG clippath definition

Examples

Basic

<html>
  <head>
    <script src="jquery.min.js"></script>
    <script src="clip-path-polygon.min.js"></script>
    <script>
      $(function() {
        var points = [[0, 0], [145, 0], [150, 20], [155, 0], [300, 0], [300, 200], [0, 200], [0, 0]];
        $('#test').clipPath(points);
      });
    </script>
  </head>
  <body>
    <div style="width: 300px; height: 200px">
      <div id="test" style="width: 100%; height: 100%; background-color: green"></div>
    </div>
  </body>
</html>

which gives you such an html code (remember that svg element has to have http://www.w3.org/2000/svg namespace!):

<div style="width: 300px; height: 200px">
  <div id="test" style="width: 100%; height: 100%; background-color: green;
    -webkit-clip-path: polygon(0 0, 145px 0, 150px 20px, 155px 0, 300px 0, 300px 200px, 0 200px, 0 0);
    clip-path: url(#clipPathPolygonGenId)"></div>
</div>

<svg>
  <defs>
    <clippath id="clipPathPolygonGenId">
      <polygon points="0 0, 145 0, 150 20, 155 0, 300 0, 300 200, 0 200, 0 0"></polygon>
    </clippath>
  </defs>
</svg>

Relative values

To use relative values, you have to pass isPercentage option. You might want to name the SVG clippath definition differently too:

<html>
  <head>
    <script src="jquery.min.js"></script>
    <script src="clip-path-polygon.min.js"></script>
    <script>
      $(function() {
        var points = [[0, 0], [49, 0], [50, 10], [51, 0], [100, 0], [100, 100], [0, 100], [0, 0]];
        $('#test').clipPath(points, {
          isPercentage: true,
          svgDefId: 'mySvg'
        });
      });
    </script>
  </head>
  <body>
    <div style="width: 300px; height: 200px">
      <div id="test" style="width: 100%; height: 100%; background-color: green"></div>
    </div>
  </body>
</html>

which gives you such an html code (remember that svg element has to have http://www.w3.org/2000/svg namespace!):

<div style="width: 300px; height: 200px">
  <div id="test" style="width: 100%; height: 100%; background-color: green;
    -webkit-clip-path: polygon(0 0, 49% 0, 50% 10%, 51% 0, 100% 0, 100% 100%, 0 100%, 0 0);
    clip-path: url(#mySvg)"></div>
</div>

<svg>
  <defs>
    <clippath id="mySvg">
      <polygon points="0 0, 0.49 0, 0.5 0.1, 51% 0, 1 0, 1 1, 0 1, 0 0"></polygon>
    </clippath>
  </defs>
</svg>

clip-path-polygon's People

Contributors

andrusieczko avatar danwild avatar dependabot[bot] avatar dipt avatar mannil avatar mortonfox avatar rjimenezda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

clip-path-polygon's Issues

2 polygon in the same page

Hi, thanks for your script it´s cool!

But i have a problem, when i use the script to generate 2 mask in the same page not work!
Any idea or solution for that?

Thanks!

401 - Unauthorized: Access is denied due to invalid credentials.

Hi,

The application is configured with waffle windows authentication framework and deployed into Web sphere 8.5 server, its working in an Local machine. but when the same has been moved to an environment where IIS is there interacting with websphere using Websphere plugin causing Authentication issues and displaying a windows authentication pop message, the entered username & password also not accepting.

can you please suggest if any settings need to be verified.?

Regards,
Jagadeesh

Value point with percentage and px

Hi, Mate

I like your plugin, but i have a question. I'm in case with value points, this is my value (0 60px,100% 0,100% 100%,0 100%); i'm use pixel because need accurate size on polygon but i'm use percentage too.
So how to put point in your plugin?

Thanks

Percentages in clip-path

Hi,
it´s great work, but - I have a responsive website, and I want use your jQuery for background. Is there some way to use percentages clip-path, please?
For example:
clip-path: polygon(50% 7%, 100% 0, 100% 93%, 50% 100%, 0 93%, 0 0);

Thanks! :)

Support different clip-paths on different elements

If clipPath() is called on multiple elements with different polygons:

$('#element1').clipPath([[0,1], [0,2], [1,1]]);
$('#element2').clipPath([[0,3], [0,4], [3,3],[3,4]]);

The plugin will only create one svg clipPath containing the second specified path.

The different clip paths appear to still work for Chrome, maybe because Chrome uses the -webkit-clip-path version in addition. although they are shown as overridden in the inspector. On Firefox the elements all use the last specified clip path.

It would be nice if it could create additional clipPath elements for each unique clip path, generating unique ids for them, and returning the ids to allow the user to remove specific paths later.

Upgrade chrome 55 breaks script

Chrome 55:
'CSS Clipping Path properties no longer require the webkit prefix.'

=> this means chrome gets the clip-path: url("#clippathid") iso clip-path: polygon(
and it doesn't work (no clip path applied)

For me changing the order in _createClipPath worked: first do the isForSvg and then isForWebkit. Still gives the -webkit- prefix which chrome strips.

Tested in the latest Chrome, FF, Opera, Safari on mac and IE11, Edge and Chrome on windows.

_createClipPath: function(points) {
  this._createSvgDefs();
  if (this.isForSvg) {
    this._createSvgBasedClipPath(points);
  }
  if (this.isForWebkit) {
    this._createWebkitClipPath(points);
  }
},

Not supporting safari 5.1 version

It is great plugin. Working fine in firefox and chrome but why it is not working in safari 5.1 version. Can you please help me how to work for safari 5.1
Thanks in advance

svg display: none breaking clip in FF OSX

In the latest commit,.css('display', 'none') on the svg elem (clip-path-polygon.js: 100) breaks the clip behaviour (by break, I mean I'm not getting any clip applied).

I'm using FF v46.0.1 on OSX.

If I use opacity: 0 instead - clip works as expected; however I also see that this change was made to eliminate empty space created by the empty elem - which will obviously won't be addressed by opacity..

Bower

Can you make the Bower's packgage for this? (Sorry i've added this into a pull request for mistake)

The size of mask-of

hi my friend, now i use your script to make an editor of pics to decoration, the user enter in a site, select her image and it´s put in a mask an her can see a preview of her pic...

The problem:
When user select a pic, and this is more big than mask, the elements in the page is move to border or image that is the mask, you can try in the next link:
http://curvart.creacionwebprofesional.com/personalizar/curvart-plano/204

  1. Push in right to: "Imagen principal > Subir"
  2. When you up yout image, the button without absolute position is move to border of image. You can see that with the checkbox: "Ajustar imagen"

Can you help me with that please?

clip-path-polygon transitions

Hello! Is there any possibility to make some transitions for clip-path-polygon?
I have tried something like that but it does not work:

jQuery(function($) {
var points = [[350, 0], [0, 350], [350, 350]];
$('#mask').clipPath(points);

$(".item-112").mouseover(function() {
points = [[0, 0], [0, 350], [350, 0]];
$('#mask').css('-moz-transition','all 2s ease 0s').clipPath(points);
})

$(".item-112").mouseout(function() {
var points = [[350, 0], [0, 350], [350, 350]];
$('#mask').css('-moz-transition','all 2s ease 0s').clipPath(points);
})
});

Regards

Webpack issue - "Critical dependency: require function is used in a way in which dependencies cannot be statically extracted"

I'm using webpack to convert my ES6 JS to "universal" JS code.

window.$ = window.jQuery = require('jquery');
//...
require('clip-path-polygon');

When first requiring jquery and then requiring clip-path-polygon, webpack throws the following error:

Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

It looks like the reason is line 11. When removing the require statements, the error vanishes.

var jQuery = jQuery || globalVariable.jQuery || (require && require('jquery'));

BTW: Changing the order of require statements does not change anything.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.