I have a several divs connected to one another using the endpoint connections to one another. As I do a zoom in or zoom out, I need to repaint all the UI components. The UI components are drawn using the jsPlumb library(jquery/javascript based). There is a method in jsplumb which forces the repaint but it is not working as the way I want it to do. Here it is what I am trying to do.
$("div#explanation .plus").click(function(){
jsPlumb.repaintEverything();
var strongFont = parseFloat($("div.window strong").css('font-size'));
var newStrongFont = strongFont + 2;
$("div.window ").animate({
'height':'+=20px', 'width':'+=20px'
},"medium");
$("div.window strong").animate({
fontSize:newStrongFont
},"medium");
//This is not working
jsPlumb.repaintEverything();
jsPlumb.repaintEverything();
//I am calling here another function to repaint the div windows one by one
repaintWindows();
});
var repaintWindows = function(){
var jjl;
for(jjl=1;jjl<=xmlLength;jjl++)
{
var windo = "window"+jjl;
//alert("Window is ::"+windo);
jsPlumb.repaint(windo);
alert("Window is ::"+windo);
//_addEndpoints(wind, ["LeftMiddle", "BottomCenter"], ["TopCenter", "RightMiddle"]);
}
alert("the value of jjl is ::"+jjl+" \nThe value of xmlLength is::"+xmlLength);
};
The problem here is if I remove the
//alert("Window is ::"+windo);
from the repaintWindows method for loop, the windows get repainted during the next cycle of call. I want the repaint to work at the same time. If I keep the alert, then it does repaint the window at the same time. As well, at the end I dont want any kind of alert over there.
Can somebody please tell me where am I going wrong.
Any help is appreciated.
Thanks!
not sure, but try this one
$(window).resize(function() {
jsPlumb.repaintEverything();
});
Related
I am building an app in Ionic, and there are certain functions that load new sets of data into an existing view.
When I do this, sometimes if I have scrolled down, and the new data takes less space than the previous one, I am left with a blank screen until I scroll back up to where the new data is.
To prevent this, I am running an $ionicScrollDelegate.resize(); which works, but it doesn't refresh. If I swipe (to reload data) twice, it resizes the view and bumps the scroll up if necessary, however if I do it just once, the scroll resizes, however does not bring the user up automatically. They have to start scrolling first, then it snaps them back to a scroll area that actually has data present.
I managed to solve this by using a watch:
$scope.$watch(function(scope) { return scope.projects },
function() {
$ionicScrollDelegate.$getByHandle('taskScroll').resize();
}
);
But I do not want to use a $watch for performance reasons. I've tried using various methods to get this resize to work from the event that calls the data to be reloaded, including $apply, $evalAsync, $timeout...
None of them worked, except $timeout, but then only when the interval was 500ms. This tells me it is an issue with the speed at which the data is loading and that is why $watch worked cause it keeps looking.
Is there anything else I can do to make the resize work immediately? This is the code for the event handler function:
$scope.onSwipeRight = function () {
var operatingDate = appContextService.getOperatingDate()
.subtract(1,'days')
.startOf('day');
var pastDate = moment().subtract(1,'years')
.startOf('year')
.startOf('isoWeek');
if (isSwipeEnabled && operatingDate.isAfter(pastDate)) {
slideInCacheValidation(-1);
}
// Just one attempt at fixing this below, I've tried so many things that do not work...
$scope.$eval( function() {
$ionicScrollDelegate.$getByHandle('taskScroll').resize();
});
};
//Sets up endgame and text variables for later use
var final = '';
var message = '';
var endGame = false;
//Ends the game by disabling input
function endGame(){
feedButton.off('click');
hydrateButton.off('click');
sleepButton.off('click');
entertainButton.off('click');
startButton.off('click');
}
function gameStart(){
healthDrop();
sanityDrop();
patienceDrop();
difficult();
randOne();
randTwo();
randThree();
randFour();
function indicateBegin(){
imgk.setAttrs({fill: 'purple'});
}
function indicateOff(){
imgk.setAttrs({fill: false});
}
var timer = setTimeout(indicateOff, 4000);
}
//Should ready KineticJS buttons for use
startButton.on('click', gameStart);
feedButton.on('click', tamaHibachi.feed);
hydrateButton.on('click', tamaHibachi.hydrate);
sleepButton.on('click', tamaHibachi.sleep);
entertainButton.on('click', tamaHibachi.entertain);
So, I've been trying to create a game in KineticJS for a class project, but I've found that none of my buttons work. I don't know if the problem lies in this part of the code, but I would believe it does. Can anyone help?
Without more info on your code it's hard to say anything. For instance, all my objects have 'ID's... container is a DIV with an id of 'container' so that you can then do this:
container.addEventListener('mousedown', startListening, false );
I can't tell from your code if you have all that set up right. It's probably nothing with Kinetic, because I have a huge project on the go with Kinetic and all the event listeners are working just fine.
The title of the question expresses what I think is the ultimate question behind my particular case.
My case:
Inside a click handler, I want to make an image visible (a 'loading' animation) right before a busy function starts. Then I want to make it invisible again after the function has completed.
Instead of what I expected I realize that the image never becomes visible. I guess that this is due to the browser waiting for the handler to end, before it can do any redrawing (I am sure there are good performance reasons for that).
The code (also in this fiddle: http://jsfiddle.net/JLmh4/2/)
html:
<img id="kitty" src="http://placekitten.com/50/50" style="display:none">
<div>click to see the cat </div>
js:
$(document).ready(function(){
$('#enlace').click(function(){
var kitty = $('#kitty');
kitty.css('display','block');
// see: http://unixpapa.com/js/sleep.html
function sleepStupidly(usec)
{
var endtime= new Date().getTime() + usec;
while (new Date().getTime() < endtime)
;
}
// simulates bussy proccess, calling some function...
sleepStupidly(4000);
// when this triggers the img style do refresh!
// but not before
alert('now you do see it');
kitty.css('display','none');
});
});
I have added the alert call right after the sleepStupidly function to show that in that moment of rest, the browser does redraw, but not before. I innocently expected it to redraw right after setting the 'display' to 'block';
For the record, I have also tried appending html tags, or swapping css classes, instead of the image showing and hiding in this code. Same result.
After all my research I think that what I would need is the ability to force the browser to redraw and stop every other thing until then.
Is it possible? Is it possible in a crossbrowser way? Some plugin I wasn't able to find maybe...?
I thought that maybe something like 'jquery css callback' (as in this question: In JQuery, Is it possible to get callback function after setting new css rule?) would do the trick ... but that doesn't exist.
I have also tried to separte the showing, function call and hiding in different handlers for the same event ... but nothing. Also adding a setTimeout to delay the execution of the function (as recommended here: Force DOM refresh in JavaScript).
Thanks and I hope it also helps others.
javier
EDIT (after setting my preferred answer):
Just to further explain why I selected the window.setTimeout strategy.
In my real use case I have realized that in order to give the browser time enough to redraw the page, I had to give it about 1000 milliseconds (much more than the 50 for the fiddle example). This I believe is due to a deeper DOM tree (in fact, unnecessarily deep).
The setTimeout let approach lets you do that.
Use JQuery show and hide callbacks (or other way to display something like fadeIn/fadeOut).
http://jsfiddle.net/JLmh4/3/
$(document).ready(function () {
$('#enlace').click(function () {
var kitty = $('#kitty');
// see: http://unixpapa.com/js/sleep.html
function sleepStupidly(usec) {
var endtime = new Date().getTime() + usec;
while (new Date().getTime() < endtime);
}
kitty.show(function () {
// simulates bussy proccess, calling some function...
sleepStupidly(4000);
// when this triggers the img style do refresh!
// but not before
alert('now you do see it');
kitty.hide();
});
});
});
Use window.setTimeout() with some short unnoticeable delay to run slow function:
$(document).ready(function() {
$('#enlace').click(function() {
showImage();
window.setTimeout(function() {
sleepStupidly(4000);
alert('now you do see it');
hideImage();
}, 50);
});
});
Live demo
To force redraw, you can use offsetHeight or getComputedStyle().
var foo = window.getComputedStyle(el, null);
or
var bar = el.offsetHeight;
"el" being a DOM element
I do not know if this works in your case (as I have not tested it), but when manipulating CSS with JavaScript/jQuery it is sometimes necessary to force redrawing of a specific element to make changes take effect.
This is done by simply requesting a CSS property.
In your case, I would try putting a kitty.position().left; before the function call prior to messing with setTimeout.
What worked for me is setting the following:
$(element).css('display','none');
After that you can do whatever you want, and eventually you want to do:
$(element).css('display','block');
Heyho,
I´am working on a project in my university and I´d like to use "Hammer.js".
I´ve downloaded the Carousel-Example and it works perfectly for me.
But I would like to start a the middle pane of my code and it´s not so simple I think.
It´s something like this:
http://img203.imageshack.us/img203/6326/schemeas.jpg
so Hammer.js starts always with the green screen. But I like to start with the yellow one.
I´ve added one swipe right to the init function but it looks horrible when the page is loading and could not be the goal ^^
I hope anyone of you have an idea how to solve my problem.
Try calling
carousel.showPane(1);
That will display the second pane instantly. You will want to put this near the bottom, right after where it says.
carousel.init();
If you're feeling adventurous you could try and make it automatically start with that pane as there's a variable inside the Carousel function called current_pane which is set to a default of 0 (the first pane). Altering this may work too but might require more code somewhere else. Experiment!
edit
NULL is right, it does animate it. Here's a more in depth method to set it without animation:
I found that the method responsible for changing which pane is showing was the setContainerOffset mthod which could be passed a variable to animate it. I previously told you to use showPane(2) but that then called
setContainerOffset(offset, true)
which caused the animation occur. What you should do instead is make a slightly different version of showPane...
this.setPane = function( index ) {
// between the bounds
index = Math.max(0, Math.min(index, pane_count-1));
current_pane = index;
var offset = -((100/pane_count)*current_pane);
setContainerOffset(offset, false);
};
You'll find it's almost identical to showPane except for the name and the fact that it calls setContainerOffset with animation: false. This will immediately show the pane of your choice and can be called using
carousel.setPane(index);
What I've done is added this to the init function so that it looks like this:
this.init = function() {
setPaneDimensions();
var c = this;
$(window).on("load resize orientationchange", function() {
setPaneDimensions();
c.setPane(current_pane);
//updateOffset();
})
};
Now you can change
var current_pane = 0;
to whatever you want and the carousel will always start with that pane when it's initialised! simple!
I've a scenario that requires me to detect animation stop of a periodically animated element and trigger a function. I've no control over the element's animation. The animation can be dynamic so I can't use clever setTimeout.
Long Story
The simplified form of the problem is that I'm using a third party jQuery sliding banners plugin that uses some obfuscated JavaScript to slide banners in and out. I'm in need of figuring out a hook on slideComplete sort of event, but all I have is an element id. Take this jsfiddle as an example and imagine that the javascript has been obfuscated. I need to trigger a function when the red box reaches the extremes and stops.
I'm aware of the :animated pseudo selector but I think it will need me to constantly poll the required element. I've gone through this, this, and this, but no avail. I've checked jquery promise but I couldn't figure out to use that in this scenario. This SO question is closest to my requirements but it has no answers.
P.S. Some more information that might be helpful:
The element isn't created by JavaScript, it is present on page load.
I've control over when to apply the plugin (that makes it periodically sliding banner) on the element
Most of the slideshow plugins I have used use changing classes at the end of the animation... You could extend the "addClass" method of jQuery to allow you to capture the class change as long as the plugin you use is using that method like it should:
(function($){
$.each(["addClass","removeClass"],function(i,methodname){
var oldmethod = $.fn[methodname];
$.fn[methodname] = function(){
oldmethod.apply( this, arguments );
this.trigger(methodname+"change");
return this;
}
});
})(jQuery);
I threw together a fiddle here
Even with obfuscated code you should be able to use this method to check how they are sending in the arguments to animate (I use the "options" object when I send arguments to animate usually) and wrap their callback function in an anonymous function that triggers an event...
like this fiddle
Here is the relevant block of script:
(function($){
$.each(["animate"],function(i,methodname){
var oldmethod = $.fn[methodname];
$.fn[methodname] = function(){
var args=arguments;
that=this;
var oldcall=args[2];
args[2]=function(){
oldcall();
console.log("slideFinish");
}
oldmethod.apply( this, args );
return this;
}
});
})(jQuery);
Well since you didn't give any indication as to what kind of animation is being done, I'm going to assume that its a horizontal/vertical translation, although I think this could be applied to other effects as well. Because I don't know how the animation is being accomplished, a setInterval evaluation would be the only way I can guess at how to do this.
var prevPos = 0;
var isAnimating = setInterval(function(){
if($(YOUROBJECT).css('top') == prevPos){
//logic here
}
else{
prevPos = $(YOUROBJECT).css('top');
}
},500);
That will evaluate the vertical position of the object every .5 seconds, and if the current vertical position is equal to the one taken .5 seconds ago, it will assume that animation has stopped and you can execute some code.
edit --
just noticed your jsfiddle had a horizontal translation, so the code for your jsfiddle is here http://jsfiddle.net/wZbNA/3/