-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.bb-pixelify.js
126 lines (102 loc) · 4.06 KB
/
jquery.bb-pixelify.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
/*!
* BB Pixelify 1.1.1
* Transform single images into a grid of tiles.
* https://github.com/bobbybol/pixelify
* @license MIT licensed
*
* Copyright (C) 2016 bobbybol.com - A project by Bobby Bol
*/
;(function ($) {
"use strict";
/**
* Defining the Plugin
*/
$.fn.bbPixelify = function(options) {
/**
* Setting the Defaults
*/
var settings = {
columns : 10,
rows : 10,
responsive : false // this is work in progress for v1.2.0
};
// Settings extendable with options
$.extend(settings, options);
/**
* Set up the image grid for each element
*/
return this.each(function() {
// Variables
var container = $(this);
if (!container.find('img').length){
return;
}
var originalImage = container.find('img');
var imageSource = 'url(' + originalImage.attr('src') + ')';
// Set image to block before measuring
originalImage.css({
display: "block",
padding: 0,
margin: 0
});
var targetWidth = originalImage.width();
var targetHeight = originalImage.height();
var tileWidth = targetWidth / settings.columns;
var tileHeight = targetHeight / settings.rows;
var i;
var j;
// Build the tile blueprint
var tile = jQuery('<div/>', {
class: 'bbTile',
css: {
position : "absolute",
width : tileWidth,
height : tileHeight,
backgroundImage : imageSource
}
});
// Remove the original
originalImage.remove();
// Set the container to relative if static
if ( container.css('position') === 'static' ) {
container.css('position', 'relative');
}
// Set the container to correct width and height
if (!settings.reponsive) {
container.css({
width : targetWidth,
height : targetHeight
});
}
// #TODO v1.1.5 refactor functionally
// and test performance
// #TODO v1.2.0 add debounced resize listener
// and re-build grid when responsive set to true
// #TODO v1.3.0 remove subpixel values
// by alternatively rounding the pixel values
// up on even tiles and down on uneven tiles
// Build grid of tiles
for (i = 0; i < settings.rows; i++) {
for (j = 0; j < settings.columns; j++) {
tile
.clone()
.css({
left : j * tileWidth,
top : i * tileHeight,
backgroundPosition : j * -tileWidth + 'px ' + i * -tileHeight + 'px',
backgroundSize : targetWidth + 'px ' + targetHeight + 'px',
backgroundRepeat : 'no-repeat'
})
.appendTo(container);
}
}
// Add 'pixelified' class to container for animation hook
container.addClass('pixelified');
// Add some useful data to jQuery object
container.data("bbMatrixInfo", {
rows : settings.rows,
columns : settings.columns
});
});
};
}(jQuery));