This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
jquery.invertImages.js
117 lines (93 loc) · 4.27 KB
/
jquery.invertImages.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
/*!
* jQuery Invert Images
* Licensed under the MIT license
* Author: @shekhardesigner
* Version: 1.0.3
*
* The MIT License (MIT)
*
* Copyright (c) 2015 Shekhar Sharma <itsmes.sh@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
;(function ($, window, document, undefined) {
var defaults = {
svgWidth: 20,
svgHeight: 20,
idHash: "",
svgSource: "",
svgContent: "",
cssFilters: false,
destroy: false
};
function InvertImages(elm, options) {
this.$elm = $(elm);
this.options = $.extend({}, defaults, options);
this.options.idHash += Math.floor((Math.random() * 100 )) + new Date().getSeconds() + new Date().getMilliseconds();
if(this.options.destroy){
this.destroy();
return false;
};
this.init();
}
InvertImages.prototype = {
//Build SVG and Append
init: function () {
this.options.cssFilters = this.cssSupport("cssfilters");
if (this.options.cssFilters) return false;
this.options.svgWidth = this.$elm.width();
this.options.svgHeight = this.$elm.height();
this.options.svgSource = this.$elm.attr("src");
this.options.svgContent = '<svg xmlns="http://www.w3.org/2000/svg" id="svgroot_' + this.options.idHash + '" class="tile-image '+this.$elm[0].className+' " viewBox="0 0 ' + this.options.svgWidth + ' ' + this.options.svgHeight + '" width="' + this.options.svgWidth + '" height="' + this.options.svgHeight + '">';
this.options.svgContent += '<defs>';
this.options.svgContent += '<filter id="filtersPicture_' + this.options.idHash + '" >';
this.options.svgContent += '<feComposite result="inputTo_' + this.options.idHash + '" in="SourceGraphic" in2="SourceGraphic" operator="arithmetic" k1="0" k2="1" k3="0" k4="0" />';
this.options.svgContent += '<feColorMatrix in="SourceGraphic" type="matrix" values="-1 0 0 0 1 0 -1 0 0 1 0 0 -1 0 1 0 0 0 1 0"/>';
this.options.svgContent += '</feComponentTransfer>';
this.options.svgContent += '</filter>';
this.options.svgContent += '</defs>';
this.options.svgContent += '<image filter="url(\'#filtersPicture_' + this.options.idHash + '\')" x="0" y="0" width="' + this.options.svgWidth + '" height="' + this.options.svgHeight + '" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="' + this.options.svgSource + '" />';
this.options.svgContent += '</svg>';
this.$elm.addClass("hide");
this.$elm.after(this.options.svgContent);
$("html").addClass("inverted");
},
//Check CSS Support using Modernizr
cssSupport: function (prop) {
if (Modernizr && Modernizr[prop]) return true;
return false;
},
//Destroy the SVG Built
destroy: function(){
var $elm = this.$elm;
$("html").removeClass("inverted");
$elm.next("svg").remove();
$elm.removeClass("hide");
$elm.removeData("invertImages");
}
};
$.fn.invertImages = function (options) {
return this.each(function () {
var $this = $(this),
data = $(this).data("invertImages"),
option = options;
if(!data && typeof option === "object") $.data(this, "invertImages", new InvertImages(this, option));
if(data && typeof option === "string") data[option]();
});
};
})(jQuery, window, document);