resize: no more polling loop (all resize can be predicted and manually triggered); don't trigger jquery event, just store the handler in the dom object and call it

This commit is contained in:
jean-emmanuel 2016-12-12 14:55:51 +01:00
parent c261a9d630
commit 3f61d34722
2 changed files with 8 additions and 25 deletions

View file

@ -18,7 +18,6 @@
self.attr('tabindex',"0")
canvas.resize(function(e,width,height){
e.stopPropagation()
var width = width*2,
height = height*2

View file

@ -4,32 +4,21 @@
*/
(function($){
var elems = [],
resizedElems = [],
pollInterval = 250,
resizeTimeout
var elems = []
$.event.special.resize = {
setup: function() {
setup: function(data, namespaces, handler) {
if (this===window) return false
var elem = this
// Add this element to the list of internal elements to monitor.
elems.push( elem )
elems.push(this)
// If this is the first element added, start the polling loop.
if ( elems.length === 1 ) {
resizePollingLoop()
}
},
teardown: function() {
if (this===window) return false
var elem = this
// Remove this element from the list of internal elements to monitor.
for (var i = elems.length - 1; i >= 0; i--) {
if(elems[i] == this){
@ -44,7 +33,6 @@
},
add: function(handleObj) {
if (this===window) return false
// Save a reference to the bound event handler.
@ -54,12 +42,15 @@
// Call the originally-bound event handler and return its result.
return old_handler.apply(this, arguments)
}
this.resizeHandler = handleObj.handler
}
}
function checkResizes(){
// Iterate over all elements to which the 'resize' event is bound.
resizedElems = []
var resizedElems = []
for (var i = elems.length - 1; i >= 0; i--) {
var elem = elems[i]
@ -79,18 +70,11 @@
}
}
for (var i = resizedElems.length - 1; i >= 0; i--) {
$(resizedElems[i]).triggerHandler('resize',[resizedElems[i].resizedataw,resizedElems[i].resizedatah])
resizedElems[i].resizeHandler(undefined, resizedElems[i].resizedataw, resizedElems[i].resizedatah)
}
}
function resizePollingLoop() {
// yep, setTimeout, 'cuz requestAnimation doesn't seem to perform that well
checkResizes()
resizeTimeout = window.setTimeout(resizePollingLoop, pollInterval)
}
$(window).resize(checkResizes)
})(jQuery)