/*! * 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);