Kinetic JS, touch/hover this, affect all other objects - hover

So, I'll try to explain this as best as I can: I have a canvas with a bunch of objects to touch have a highlighted state (swapping a background image as you can see here). I'm only showing 2 examples here for the objects 'applications' and 'enterprises', but there are many of these objects. My goal is to allow one object to have its hover image, while the rest do not. Once I click on another object, I want that object's hover image to activate, and hover images for all other objects removed (essentially getting rid of the 'touched mouseout' function.
I'd like to say something like:
'If touchstart/mouseover this object, use its hover image, and disable all hover images on all other objects.
applications.on('touchstart mouseover', function() {
writeMessage(messageLayer, 'touchstart applications circle');
this.setFill({ image: images.applicationshover});
layer.draw();
});
applications.on('touchend mouseout', function() {
writeMessage(messageLayer, 'Mouseup applications circle');
this.setFill({ image: images.applicationsimage});
layer.draw();
});
enterprises.on('touchstart mouseover', function() {
writeMessage(messageLayer, 'touchstart enterprises circle');
this.setFill({ image: images.enterpriseshover});
layer.draw();
});
enterprises.on('touchend mouseout', function() {
writeMessage(messageLayer, 'Mouseup enterprises circle');
this.setFill({ image: images.enterprisesimage});
layer.draw();
});

I hate answering questions to something I know little of without an example to tweak, but here goes....
Instead of what ever your doing, why not try this....
When you create your shapes/images add an attribute to them that is the image to use for the hover and not hover state.
Kinda like this....
var thing = new Kinetic.Image({
x: 0,
y: 0,
image: imageObj,
hoverImage: hoverImageObj,
notHoverImage: imageObj,
name: 'image'
});
Then you can have one routine for all images...
var hover = function() {
writeMessage(messageLayer, 'touchstart enterprises circle');
this.setFill({
image: this.attrs.hoverImage
});
layer.draw();
}
var notHover = function() {
writeMessage(messageLayer, 'Mouseup enterprises circle');
this.setFill({
image: this.attrs.notHoverImage
});
layer.draw();
}
icons.on('touchstart mouseover', hover);
icons.on('touchend mouseout', notHover);
There could be better ways to do this, Ive really got to go read the docs ;)

Related

i have a geojson layer button and when i click i have to zoom to the particular layer

this is my layer and i have assigned it to a button, but the zoom is not working when I click the layer button. i tried adding the zoom inside the layer but its not working.
rainfall1 = new ol.layer.Vector({
//title: 'CA_DEVELOPMENT_PLAN',
// extent: [-180, -90, -180, 90],
visible:false,
source: new ol.source.Vector({
url:"./data/village.geojson",
zoom: 12,
format: new ol.format.GeoJSON()
}),
style:function(feature) {
labelStyle.getText().setText(feature.getProperties().CA_NAME);
return style1;
},
declutter: true,
});
document.getElementById("lyr").onclick = function() {
layer1.setVisible(!rainfall1.getVisible());
};
var bindLayerButtonToggle = function (lyr, layer) {
document.getElementById(lyr).onclick = function() {
layer.setVisible(!layer.getVisible());
};
}
bindLayerButtonToggle("lyr", rainfall1);
setVisible will not zoom to a layer, it just turns it on or off.
Instead, you would have to update the view extent, and match it with the layer extent
map.getView().fit(rainfall1.getSource().getExtent());
#JGH's answer might work in some cases but if this is the first time the layer is made visible the source will not be loaded, so if there are no features you will need to wait for it to load before zooming.
if (rainfall1.getSource().getFeatures().length > 0) {
map.getView().fit(rainfall1.getSource().getExtent());
} else {
rainfall1.getSource().once('featuresloadend', function() [
map.getView().fit(rainfall1.getSource().getExtent());
});
}

Change Image on Hover in KineticJS

I'm looking to change an image on hover in KineticJS. What happens now is that the image just appends to the layer, doesn't actually swap the image. Without removing the layer, I want to swap the image with another. Here's my code.
imgObj.onload = function () {
var image = new Kinetic.Image({
x: 10,
y: 10,
image: imgObj,
width: 28,
height: 28,
id: 'myImage'
});
image.on("mouseenter", function (e) {
document.body.style.cursor = 'pointer';
imgObj.src = 'myHoverImage.png';
layer.draw();
});
image.on("mouseleave", function () {
document.body.style.cursor = 'default';
imgObj.src = 'myImage.png';
});
Use .setImage on the Kinetic.Image to swap to a different image:
Preload both images into image objects with var hoverImage=new Image() ... and var blurImage=new Image() ....
Important: Be sure both images have fully loaded using the .onload method of the image objects.
In the event handlers, swap images with image.setImage(hoverImage) or image.setImage(blurImage).
Display the swapped image with layer.draw().

Jquery mobile doesn't follow a link comming from another JQM page

I'm using the jquery-ui-map plugin and my test page (test.html) loads a Google Map correctly when I click the button. This SAME test.html don't work if a load it from another jquery mobile page. What I'm doing bad or what I'm missing? Next the significant code:
Javascript in the header (like in the example for the basic map):
var mobileDemo = { 'center': '57.7973333,12.0502107', 'zoom': 10 };
$('#basic_map').live('pageinit', function() {
demo.add('basic_map', function() {
$('#map_canvas').gmap({
'center': mobileDemo.center,
'zoom': mobileDemo.zoom,
'disableDefaultUI':true,
'callback': function() {
var self = this;
self.addMarker({'position': this.get('map').getCenter() }).click(function() {
self.openInfoWindow({ 'content': 'Hello World!' }, this);
});
}});
}).load('basic_map');
});
$('#basic_map').live('pageshow', function() {
demo.add('basic_map', function() { $('#map_canvas').gmap('refresh'); }).load('basic_map');
});
And the html (sorry, I can't post the HTML code because it's interpretatded, but the link is below):
http://www.medlifesolutions.com.mx/locations/mobile/test.html
As I said, this works perfect if I write test.html in the browser directly but if comes from another page:
http://www.medlifesolutions.com.mx/locations/mobile/main.php
it simply ignores a "click" or touching the button to show the map. Thanks in advance for your help.
One problem I see in your code is the use of live. It is recommended to replace live with on.

how do i add in mootools an onComplete event on a morph attached to a mouseleave?

here is my code:
$('myButton').addEvents({
mouseenter: function(){
$('myImage').setStyle('display','block');
$('myImage').morph({
'duration': 500,
'transition': Fx.Transitions.Sine.in,
'opacity': 1,
'top': -205
});
},
mouseleave: function(){
$('myImage').morph({
'opacity': 0,
'top': -175,
'onComplete': hidemyImage
});
}
});
function hidemyImage() {
$('myImage').setStyle('display','none')
}
the onComplete inside the mouseleave does not work as expected... it hides the image immediately when i move away from myButton instead of hiding it after the morph has finished... i tried several solutions but none worked so far. any idea / help? thanks in advance!
you need to work with the instance and not pass on things in the morph function directly, that takes properties to morph and it probably runs your function immediately in the hope it will return a property value. you can do el.set('morph', {onComplete: hideImagefn}) before that and it will work but read on...
one way to do it is to set your morph options/instance once and work with it afterwards like so:
(function() {
var img = document.id('myImage').set('morph', {
duration: 500,
transition: Fx.Transitions.Sine.in,
link: 'cancel',
onStart: function() {
this.element.setStyle('display', 'block');
}
}), fx = img.get('morph');
// of you can just do var fx = new Fx.Morph(img, { options});
document.id('myButton').addEvents({
mouseenter: function(){
fx.start({
opacity: 1,
top: -205
});
},
mouseleave: function(){
fx.addEvent('complete', function() {
this.element.setStyle('display', 'none');
this.removeEvents('complete');
}).start({
opacity: 0,
top: -175
});
}
});
})();
the start ensures its always visible when mouseovered, the link is cancel which means it will stop execution if you mouse out too early and if you do mouseout, it will then hide the image and remove the onComplete event so that if you show it once more, it stays visible when it comes into view.
if you don't plan on being able to bring it back you can clean-up better and even use event pseudos like onComplete:once etc - though thats part of Event.Pseudos from mootools-more.
in general - .get/.set morph is your setup. el.morph() passes values to morphInstance.start()
play here: http://jsfiddle.net/dimitar/NkNHX/

Using jQuery Cycle on Hover Only

I'm trying to get jQuery Cycle to only run when the slideshow is being hovered (which is the opposite of the functionality that they have built in).
Here's where I'm at: http://jsfiddle.net/zSBMU/
$(document).ready(function() {
$('.slideshow').hover(
function() {
$(this).cycle({
fx: 'fade',
speed: 600,
timeout: 300,
pause: 0
});
},
function(){
$(this).cycle('stop');
}
).trigger('hover');
​ });
The first time you hover, it's great, works fine. But if you try to hover the same one again, it only goes through one fade instead of looping through again.
Any ideas?
Please ignore some of the gross code, working with a pretty old theme here, trying to clean it up!
You're using "stop" and recreating the cycle, so you're adding several cycles on the object.
You've to use "pause" and "resume".
Example bellow:
var cycleConfigured = false;
$(document).ready(function() {
$('.slideshow').hover(
function() {
if(cycleConfigured)
$(this).cycle('resume');
else
{
$(this).cycle({
fx: 'fade',
speed: 600,
timeout: 300,
pause: 0
});
cycleConfigured = true;
}
},
function(){
$(this).cycle('pause');
}
).trigger('hover');
});​
The variable cycleConfigured will be used to control our cycle plugin, to check if it was already instantiated. In alternative you can create it on $(document).ready() and then pause it like this:
$(document).ready(function() {
// configure the cycle plugin
$('.slideshow').cycle({
fx: 'fade',
speed: 600,
timeout: 300,
pause: 0
});
$('.slideshow').cycle('pause'); // pause it right away.
$('.slideshow').hover(
function() {
$(this).cycle('resume'); // start playing.
},
function(){
$(this).cycle('pause'); // pause the slideshow.
}
).trigger('hover');
});​
Then everything you need to do is use $(this).cycle('pause') on out and $(this).cycle('resume') on over.
Anything let me know.