-
Notifications
You must be signed in to change notification settings - Fork 15
/
clip-path-polygon.js
135 lines (114 loc) · 3.93 KB
/
clip-path-polygon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*!
* jQuery clip-path-polygon Plugin v0.1.15 (2020-06-24)
* jQuery plugin that makes easy to use clip-path on whatever tag under different browsers
* https://github.com/andrusieczko/clip-path-polygon
*
* Copyright 2020 Karol Andrusieczko
* Released under MIT license
*/
var globalVariable = window || root;
var jQuery = jQuery || globalVariable.jQuery || require("jquery");
(function($) {
var id = 0;
var ClipPath = function(jQuery, $el, points, options) {
this.$ = jQuery;
this.$el = $el;
this.points = points;
this.svgDefId = 'clipPathPolygonGenId' + id++;
this.processOptions(options);
};
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = ClipPath;
}
exports.ClipPath = ClipPath;
} else {
globalVariable.ClipPath = ClipPath;
}
ClipPath.prototype = {
$: null,
$el: null,
points: null,
isForWebkit: true,
isForSvg: true,
svgDefId: null,
isPercentage: false,
create: function() {
this._createClipPath(this.points);
},
_createClipPath: function(points) {
this._createSvgDefs();
if (this.isForSvg) {
this._createSvgBasedClipPath(points);
}
if (this.isForWebkit) {
this._createWebkitClipPath(points);
}
},
_createWebkitClipPath: function(points) {
var clipPath = "polygon(" + this._translatePoints(points, true, this.isPercentage) + ")";
this.$el.css('-webkit-clip-path', clipPath);
},
_createSvgBasedClipPath: function(points) {
this.$('#' + this.svgDefId + '').find('polygon').attr('points', this._translatePoints(points, false, this.isPercentage));
this.$el.css('clip-path', 'url(#' + this.svgDefId + ')');
},
_translatePoints: function(points, withUnit, isPercentage) {
var result = [];
for (var i in points) {
var x = this._handlePxs(points[i][0], withUnit, isPercentage);
var y = this._handlePxs(points[i][1], withUnit, isPercentage);
result.push(x + ' ' + y);
}
return result.join(', ');
},
_handlePxs: function(number, withUnit, isPercentage) {
if (number === 0) {
return number;
}
if (!withUnit) {
if (isPercentage) {
return number / 100;
}
return number;
}
return number + (isPercentage ? "%" : "px");
},
_createSvgElement: function(elementName) {
return this.$(document.createElementNS('http://www.w3.org/2000/svg', elementName));
},
_createSvgDefs: function() {
if (this.$('#' + this.svgDefId + '').length === 0) {
var $svg = this._createSvgElement('svg').attr('width', 0).attr('height', 0).css({
'position': 'absolute',
'visibility': 'hidden',
'width': 0,
'height': 0
});
var $defs = this._createSvgElement('defs');
$svg.append($defs);
var $clippath = this._createSvgElement('clipPath').attr('id', this.svgDefId);
if (this.isPercentage) {
$clippath.get(0).setAttribute('clipPathUnits', 'objectBoundingBox');
}
$defs.append($clippath);
var $polygon = this._createSvgElement('polygon');
$clippath.append($polygon);
this.$('body').append($svg);
}
},
processOptions: function(options) {
this.isForWebkit = (options && typeof(options.isForWebkit) !== "undefined") ? options.isForWebkit : this.isForWebkit;
this.isForSvg = (options && typeof(options.isForSvg) !== "undefined") ? options.isForSvg : this.isForSvg;
this.isPercentage = (options && options.isPercentage || this.isPercentage);
this.svgDefId = (options && options.svgDefId) || this.svgDefId;
}
};
$.fn.clipPath = function(points, options) {
return this.each(function() {
var $el = $(this);
var clipPath = new ClipPath($, $el, points, options);
clipPath.create();
});
};
}).call(this, jQuery);