From 84e7e714618acebc14b3ae6ea3be8bed2d9b6fa0 Mon Sep 17 00:00:00 2001 From: Eric Rowell Date: Sun, 18 Mar 2012 11:24:57 -0700 Subject: [PATCH] improved mouseover and mouseout event handling by ignoring parent handlers if mouse moves from one child to another --- dist/kinetic-core.js | 108 ++++++++++++++++++++++++++---------- dist/kinetic-core.min.js | 4 +- src/Node.js | 45 ++++++++++----- src/Stage.js | 61 +++++++++++++++----- tests/js/functionalTests.js | 102 +++++++++++++++++++++++++--------- 5 files changed, 237 insertions(+), 83 deletions(-) diff --git a/dist/kinetic-core.js b/dist/kinetic-core.js index 2b0b7038..66d7d0ed 100644 --- a/dist/kinetic-core.js +++ b/dist/kinetic-core.js @@ -3,7 +3,7 @@ * http://www.kineticjs.com/ * Copyright 2012, Eric Rowell * Licensed under the MIT or GPL Version 2 licenses. - * Date: Mar 17 2012 + * Date: Mar 18 2012 * * Copyright (C) 2011 - 2012 by Eric Rowell * @@ -733,22 +733,41 @@ Kinetic.Node.prototype = { * @param {Event} evt */ _handleEvents: function(eventType, evt) { - // generic events handler - function handle(obj) { - var el = obj.eventListeners; - if(el[eventType]) { - var events = el[eventType]; - for(var i = 0; i < events.length; i++) { - events[i].handler.apply(obj, [evt]); - } - } + var stage = this.getStage(); + this._handleEvent(this, stage.mouseoverShape, stage.mouseoutShape, eventType, evt); + }, + /** + * handle node event + */ + _handleEvent: function(node, mouseoverNode, mouseoutNode, eventType, evt) { + var el = node.eventListeners; + var okayToRun = true; - // simulate event bubbling - if(!evt.cancelBubble && obj.parent.className !== 'Stage') { - handle(obj.parent); + /* + * determine if event handler should be skipped by comparing + * parent nodes + */ + if(eventType === 'onmouseover' && mouseoutNode && mouseoutNode.id === node.id) { + okayToRun = false; + } + else if(eventType === 'onmouseout' && mouseoverNode && mouseoverNode.id === node.id) { + okayToRun = false; + } + + if(el[eventType] && okayToRun) { + var events = el[eventType]; + for(var i = 0; i < events.length; i++) { + events[i].handler.apply(node, [evt]); } } - handle(this); + + var mouseoverParent = mouseoverNode ? mouseoverNode.parent : undefined; + var mouseoutParent = mouseoutNode ? mouseoutNode.parent : undefined; + + // simulate event bubbling + if(!evt.cancelBubble && node.parent.className !== 'Stage') { + this._handleEvent(node.parent, mouseoverParent, mouseoutParent, eventType, evt); + } } }; @@ -882,9 +901,11 @@ Kinetic.Stage = function(cont, width, height) { y: 1 }; this.dblClickWindow = 400; - this.targetShape = undefined; this.clickStart = false; + this.targetShape = undefined; this.targetFound = false; + this.mouseoverShape = undefined; + this.mouseoutShape = undefined; // desktop flags this.mousePos = undefined; @@ -1199,12 +1220,25 @@ Kinetic.Stage.prototype = { } /* - * NOTE: these event handlers require target shape - * handling - */ + * NOTE: these event handlers require target shape + * handling + */ + + // handle onmouseover else if(!isDragging && this._isNewTarget(shape, evt)) { - // handle onmouseover + /* + * check to see if there are stored mouseout events first. + * if there are, run those before running the onmouseover + * events + */ + if(this.mouseoutShape) { + this.mouseoverShape = shape; + this.mouseoutShape._handleEvents('onmouseout', evt); + this.mouseoverShape = undefined; + } + shape._handleEvents('onmouseover', evt); + this._setTarget(shape); return true; } @@ -1217,13 +1251,24 @@ Kinetic.Stage.prototype = { } // handle mouseout condition else if(!isDragging && this.targetShape && this.targetShape.id === shape.id) { - this.targetShape = undefined; - shape._handleEvents('onmouseout', evt); + this._setTarget(undefined); + this.mouseoutShape = shape; + //shape._handleEvents('onmouseout', evt); return true; } return false; }, + /** + * set new target + */ + _setTarget: function(shape) { + this.targetShape = shape; + this.targetFound = true; + }, + /** + * check if shape should be a new target + */ _isNewTarget: function(shape, evt) { if(!this.targetShape || (!this.targetFound && shape.id !== this.targetShape.id)) { /* @@ -1232,14 +1277,10 @@ Kinetic.Stage.prototype = { if(this.targetShape) { var oldEl = this.targetShape.eventListeners; if(oldEl) { - this.targetShape._handleEvents('onmouseout', evt); + this.mouseoutShape = this.targetShape; + //this.targetShape._handleEvents('onmouseout', evt); } } - - // set new target shape - this.targetShape = shape; - this.targetFound = true; - return true; } else { @@ -1293,15 +1334,26 @@ Kinetic.Stage.prototype = { * three nested loops */ this.targetFound = false; - + var shapeDetected = false; for(var n = this.children.length - 1; n >= 0; n--) { var layer = this.children[n]; if(layer.visible && n >= 0 && layer.isListening) { if(this._traverseChildren(layer, evt)) { n = -1; + shapeDetected = true; } } } + + /* + * if no shape was detected and a mouseout shape has been stored, + * then run the onmouseout event handlers + */ + if(!shapeDetected && this.mouseoutShape) { + this.mouseoutShape._handleEvents('onmouseout', evt); + this.mouseoutShape = undefined; + + } }, /** * begin listening for events by adding event handlers diff --git a/dist/kinetic-core.min.js b/dist/kinetic-core.min.js index 35f0d0d1..1d208a2e 100644 --- a/dist/kinetic-core.min.js +++ b/dist/kinetic-core.min.js @@ -3,7 +3,7 @@ * http://www.kineticjs.com/ * Copyright 2012, Eric Rowell * Licensed under the MIT or GPL Version 2 licenses. - * Date: Mar 17 2012 + * Date: Mar 18 2012 * * Copyright (C) 2011 - 2012 by Eric Rowell * @@ -25,4 +25,4 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -var Kinetic={};Kinetic.GlobalObject={stages:[],idCounter:0,isAnimating:!1,frame:{time:0,timeDiff:0,lastTime:0},drag:{moving:!1,node:undefined,offset:{x:0,y:0}},extend:function(a,b){for(var c in b.prototype)b.prototype.hasOwnProperty(c)&&a.prototype[c]===undefined&&(a.prototype[c]=b.prototype[c])},_isaCanvasAnimating:function(){for(var a=0;a=h.config.duration*1e3?(this._endTransition.apply(h),this._removeTransition(h),h.config.callback!==undefined&&h.config.callback()):this._linearTransition.apply(h,[this.frame])}f&&e.draw()}}},_updateFrameObject:function(){var a=new Date,b=a.getTime();this.frame.lastTime===0?this.frame.lastTime=b:(this.frame.timeDiff=b-this.frame.lastTime,this.frame.lastTime=b,this.frame.time+=this.frame.timeDiff)},_animationLoop:function(){if(this.isAnimating){this._updateFrameObject(),this._runFrames();var a=this;requestAnimFrame(function(){a._animationLoop()})}},_handleAnimation:function(){var a=this;!this.isAnimating&&this._isaCanvasAnimating()?(this.isAnimating=!0,a._animationLoop()):this.isAnimating&&!this._isaCanvasAnimating()&&(this.isAnimating=!1,this.frame.lastTime=0)}},window.requestAnimFrame=function(a){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(a){window.setTimeout(a,1e3/60)}}(),Kinetic.Node=function(a){this.visible=!0,this.isListening=!0,this.name=undefined,this.alpha=1,this.x=0,this.y=0,this.scale={x:1,y:1},this.rotation=0,this.centerOffset={x:0,y:0},this.eventListeners={},this.dragConstraint="none",this.dragBounds={},this._draggable=!1;if(a)for(var b in a)switch(b){case"draggable":this.draggable(a[b]);break;case"listen":this.listen(a[b]);break;case"rotationDeg":this.rotation=a[b]*Math.PI/180;break;default:this[b]=a[b]}this.centerOffset.x===undefined&&(this.centerOffset.x=0),this.centerOffset.y===undefined&&(this.centerOffset.y=0)},Kinetic.Node.prototype={on:function(a,b){var c=a.split(" ");for(var d=0;d1?g[1]:"";this.eventListeners[h]||(this.eventListeners[h]=[]),this.eventListeners[h].push({name:i,handler:b})}},off:function(a){var b=a.split(" ");for(var c=0;c1){var h=f[1];for(var i=0;i0&&(this.parent.children.splice(a,1),this.parent.children.splice(a-1,0,this),this.parent._setChildrenIndices())},moveToBottom:function(){var a=this.index;this.parent.children.splice(a,1),this.parent.children.unshift(this),this.parent._setChildrenIndices()},setZIndex:function(a){var b=this.index;this.parent.children.splice(b,1),this.parent.children.splice(a,0,this),this.parent._setChildrenIndices()},setAlpha:function(a){this.alpha=a},getAlpha:function(){return this.alpha},getAbsoluteAlpha:function(){var a=1,b=this;while(b.className!=="Stage")a*=b.alpha,b=b.parent;return a},draggable:function(a){this.draggable!==a&&(a?this._initDrag():this._dragCleanup(),this._draggable=a)},isDragging:function(){var a=Kinetic.GlobalObject;return a.drag.node!==undefined&&a.drag.node.id===this.id&&a.drag.moving},moveTo:function(a){var b=this.parent;b.children.splice(this.index,1),b._setChildrenIndices(),a.children.push(this),this.index=a.children.length-1,this.parent=a,a._setChildrenIndices(),this.name&&(b.childrenNames[this.name]=undefined,a.childrenNames[this.name]=this)},getParent:function(){return this.parent},getLayer:function(){return this.className==="Layer"?this:this.getParent().getLayer()},getStage:function(){return this.className==="Stage"?this:this.getParent().getStage()},getName:function(){return this.name},setCenterOffset:function(a,b){this.centerOffset.x=a,this.centerOffset.y=b},getCenterOffset:function(){return this.centerOffset},transitionTo:function(a){var b=this.getLayer(),c=this,d=a.duration*1e3,e={};for(var f in a)if(a.hasOwnProperty(f))if(a[f].x!==undefined||a[f].y!==undefined){e[f]={};var g=["x","y"];for(var h=0;h0)this.remove(this.children[0])},_remove:function(a){a.name!==undefined&&(this.childrenNames[a.name]=undefined),this.children.splice(a.index,1),this._setChildrenIndices(),a=undefined},_drawChildren:function(){var a=this.children;for(var b=0;b=0;d--){var e=c[d];if(e.className==="Shape"){var f=this._detectEvent(e,b);if(f)return!0}else{var f=this._traverseChildren(e,b);if(f)return!0}}return!1},_handleEvent:function(a){var b=Kinetic.GlobalObject;a||(a=window.event),this._setMousePosition(a),this._setTouchPosition(a);var c=this.backstageLayer;c.clear(),this.targetFound=!1;for(var d=this.children.length-1;d>=0;d--){var e=this.children[d];e.visible&&d>=0&&e.isListening&&this._traverseChildren(e,a)&&(d=-1)}},_listen:function(){var a=this;this.container.addEventListener("mousedown",function(b){a.mouseDown=!0,a._handleEvent(b)},!1),this.container.addEventListener("mousemove",function(b){a.mouseUp=!1,a.mouseDown=!1,a._handleEvent(b)},!1),this.container.addEventListener("mouseup",function(b){a.mouseUp=!0,a.mouseDown=!1,a._handleEvent(b),a.clickStart=!1},!1),this.container.addEventListener("mouseover",function(b){a._handleEvent(b)},!1),this.container.addEventListener("mouseout",function(b){a.mousePos=undefined},!1),this.container.addEventListener("touchstart",function(b){b.preventDefault(),a.touchStart=!0,a._handleEvent(b)},!1),this.container.addEventListener("touchmove",function(b){b.preventDefault(),a._handleEvent(b)},!1),this.container.addEventListener("touchend",function(b){b.preventDefault(),a.touchEnd=!0,a._handleEvent(b)},!1)},_setMousePosition:function(a){var b=a.clientX-this._getContainerPosition().left+window.pageXOffset,c=a.clientY-this._getContainerPosition().top+window.pageYOffset;this.mousePos={x:b,y:c}},_setTouchPosition:function(a){if(a.touches!==undefined&&a.touches.length===1){var b=a.touches[0],c=b.clientX-this._getContainerPosition().left+window.pageXOffset,d=b.clientY-this._getContainerPosition().top+window.pageYOffset;this.touchPos={x:c,y:d}}},_getContainerPosition:function(){var a=this.container,b=0,c=0;while(a&&a.tagName!=="BODY")b+=a.offsetTop-a.scrollTop,c+=a.offsetLeft-a.scrollLeft,a=a.offsetParent;return{top:b,left:c}},_stripLayer:function(a){a.context.stroke=function(){},a.context.fill=function(){},a.context.fillRect=function(b,c,d,e){a.context.rect(b,c,d,e)},a.context.strokeRect=function(b,c,d,e){a.context.rect(b,c,d,e)},a.context.drawImage=function(){},a.context.fillText=function(){},a.context.strokeText=function(){}},_endDrag:function(a){var b=Kinetic.GlobalObject;b.drag.node&&b.drag.moving&&(b.drag.moving=!1,b.drag.node._handleEvents("ondragend",a)),b.drag.node=undefined},_prepareDrag:function(){var a=this;this.on("mousemove touchmove",function(b){var c=Kinetic.GlobalObject,d=c.drag.node;if(d){var e=a.getUserPosition(),f=d.dragConstraint,g=d.dragBounds;if(f==="none"||f==="horizontal"){var h=e.x-c.drag.offset.x;(g.left===undefined||g.lefth)&&(d.x=h)}if(f==="none"||f==="vertical"){var i=e.y-c.drag.offset.y;(g.top===undefined||g.topi)&&(d.y=i)}c.drag.node.getLayer().draw(),c.drag.moving||(c.drag.moving=!0,c.drag.node._handleEvents("ondragstart",b)),c.drag.node._handleEvents("ondragmove",b)}},!1),this.on("mouseup touchend mouseout",function(b){a._endDrag(b)})},_buildDOM:function(){this.content.style.width=this.width+"px",this.content.style.height=this.height+"px",this.content.style.position="relative",this.content.style.display="inline-block",this.content.className="kineticjs-content",this.container.appendChild(this.content),this.bufferLayer=new Kinetic.Layer,this.backstageLayer=new Kinetic.Layer,this.bufferLayer.parent=this,this.backstageLayer.parent=this,this._stripLayer(this.backstageLayer),this.bufferLayer.getCanvas().style.display="none",this.backstageLayer.getCanvas().style.display="none",this.bufferLayer.canvas.width=this.width,this.bufferLayer.canvas.height=this.height,this.content.appendChild(this.bufferLayer.canvas),this.backstageLayer.canvas.width=this.width,this.backstageLayer.canvas.height=this.height,this.content.appendChild(this.backstageLayer.canvas)}},Kinetic.GlobalObject.extend(Kinetic.Stage,Kinetic.Container),Kinetic.Layer=function(a){this.className="Layer",this.canvas=document.createElement("canvas"),this.context=this.canvas.getContext("2d"),this.canvas.style.position="absolute",this.transitions=[],this.transitionIdCounter=0,Kinetic.Container.apply(this,[]),Kinetic.Node.apply(this,[a])},Kinetic.Layer.prototype={draw:function(){this._draw()},clear:function(){var a=this.getContext(),b=this.getCanvas();a.clearRect(0,0,b.width,b.height)},getCanvas:function(){return this.canvas},getContext:function(){return this.context},add:function(a){this._add(a)},remove:function(a){this._remove(a)},_draw:function(){this.clear(),this.visible&&this._drawChildren()}},Kinetic.GlobalObject.extend(Kinetic.Layer,Kinetic.Container),Kinetic.GlobalObject.extend(Kinetic.Layer,Kinetic.Node),Kinetic.Group=function(a){this.className="Group",Kinetic.Container.apply(this,[]),Kinetic.Node.apply(this,[a])},Kinetic.Group.prototype={add:function(a){this._add(a)},remove:function(a){this._remove(a)},_draw:function(){this.visible&&this._drawChildren()}},Kinetic.GlobalObject.extend(Kinetic.Group,Kinetic.Container),Kinetic.GlobalObject.extend(Kinetic.Group,Kinetic.Node),Kinetic.Shape=function(a){this.className="Shape";if(a.stroke!==undefined||a.strokeWidth!==undefined)a.stroke===undefined?a.stroke="black":a.strokeWidth===undefined&&(a.strokeWidth=2);this.drawFunc=a.drawFunc,Kinetic.Node.apply(this,[a])},Kinetic.Shape.prototype={getContext:function(){return this.tempLayer.getContext()},getCanvas:function(){return this.tempLayer.getCanvas()},fillStroke:function(){var a=this.getContext();this.fill!==undefined&&(a.fillStyle=this.fill,a.fill()),this.stroke!==undefined&&(a.lineWidth=this.strokeWidth===undefined?1:this.strokeWidth,a.strokeStyle=this.stroke,a.stroke())},setFill:function(a){this.fill=a},getFill:function(){return this.fill},setStroke:function(a){this.stroke=a},getStroke:function(){return this.stroke},setStrokeWidth:function(a){this.strokeWidth=a},getStrokeWidth:function(){return this.strokeWidth},_draw:function(a){if(this.visible){var b=a.getStage(),c=a.getContext(),d=[];d.unshift(this);var e=this.parent;while(e.className!=="Stage")d.unshift(e),e=e.parent;for(var f=0;f=h.config.duration*1e3?(this._endTransition.apply(h),this._removeTransition(h),h.config.callback!==undefined&&h.config.callback()):this._linearTransition.apply(h,[this.frame])}f&&e.draw()}}},_updateFrameObject:function(){var a=new Date,b=a.getTime();this.frame.lastTime===0?this.frame.lastTime=b:(this.frame.timeDiff=b-this.frame.lastTime,this.frame.lastTime=b,this.frame.time+=this.frame.timeDiff)},_animationLoop:function(){if(this.isAnimating){this._updateFrameObject(),this._runFrames();var a=this;requestAnimFrame(function(){a._animationLoop()})}},_handleAnimation:function(){var a=this;!this.isAnimating&&this._isaCanvasAnimating()?(this.isAnimating=!0,a._animationLoop()):this.isAnimating&&!this._isaCanvasAnimating()&&(this.isAnimating=!1,this.frame.lastTime=0)}},window.requestAnimFrame=function(a){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(a){window.setTimeout(a,1e3/60)}}(),Kinetic.Node=function(a){this.visible=!0,this.isListening=!0,this.name=undefined,this.alpha=1,this.x=0,this.y=0,this.scale={x:1,y:1},this.rotation=0,this.centerOffset={x:0,y:0},this.eventListeners={},this.dragConstraint="none",this.dragBounds={},this._draggable=!1;if(a)for(var b in a)switch(b){case"draggable":this.draggable(a[b]);break;case"listen":this.listen(a[b]);break;case"rotationDeg":this.rotation=a[b]*Math.PI/180;break;default:this[b]=a[b]}this.centerOffset.x===undefined&&(this.centerOffset.x=0),this.centerOffset.y===undefined&&(this.centerOffset.y=0)},Kinetic.Node.prototype={on:function(a,b){var c=a.split(" ");for(var d=0;d1?g[1]:"";this.eventListeners[h]||(this.eventListeners[h]=[]),this.eventListeners[h].push({name:i,handler:b})}},off:function(a){var b=a.split(" ");for(var c=0;c1){var h=f[1];for(var i=0;i0&&(this.parent.children.splice(a,1),this.parent.children.splice(a-1,0,this),this.parent._setChildrenIndices())},moveToBottom:function(){var a=this.index;this.parent.children.splice(a,1),this.parent.children.unshift(this),this.parent._setChildrenIndices()},setZIndex:function(a){var b=this.index;this.parent.children.splice(b,1),this.parent.children.splice(a,0,this),this.parent._setChildrenIndices()},setAlpha:function(a){this.alpha=a},getAlpha:function(){return this.alpha},getAbsoluteAlpha:function(){var a=1,b=this;while(b.className!=="Stage")a*=b.alpha,b=b.parent;return a},draggable:function(a){this.draggable!==a&&(a?this._initDrag():this._dragCleanup(),this._draggable=a)},isDragging:function(){var a=Kinetic.GlobalObject;return a.drag.node!==undefined&&a.drag.node.id===this.id&&a.drag.moving},moveTo:function(a){var b=this.parent;b.children.splice(this.index,1),b._setChildrenIndices(),a.children.push(this),this.index=a.children.length-1,this.parent=a,a._setChildrenIndices(),this.name&&(b.childrenNames[this.name]=undefined,a.childrenNames[this.name]=this)},getParent:function(){return this.parent},getLayer:function(){return this.className==="Layer"?this:this.getParent().getLayer()},getStage:function(){return this.className==="Stage"?this:this.getParent().getStage()},getName:function(){return this.name},setCenterOffset:function(a,b){this.centerOffset.x=a,this.centerOffset.y=b},getCenterOffset:function(){return this.centerOffset},transitionTo:function(a){var b=this.getLayer(),c=this,d=a.duration*1e3,e={};for(var f in a)if(a.hasOwnProperty(f))if(a[f].x!==undefined||a[f].y!==undefined){e[f]={};var g=["x","y"];for(var h=0;h0)this.remove(this.children[0])},_remove:function(a){a.name!==undefined&&(this.childrenNames[a.name]=undefined),this.children.splice(a.index,1),this._setChildrenIndices(),a=undefined},_drawChildren:function(){var a=this.children;for(var b=0;b=0;d--){var e=c[d];if(e.className==="Shape"){var f=this._detectEvent(e,b);if(f)return!0}else{var f=this._traverseChildren(e,b);if(f)return!0}}return!1},_handleEvent:function(a){var b=Kinetic.GlobalObject;a||(a=window.event),this._setMousePosition(a),this._setTouchPosition(a);var c=this.backstageLayer;c.clear(),this.targetFound=!1;var d=!1;for(var e=this.children.length-1;e>=0;e--){var f=this.children[e];f.visible&&e>=0&&f.isListening&&this._traverseChildren(f,a)&&(e=-1,d=!0)}!d&&this.mouseoutShape&&(this.mouseoutShape._handleEvents("onmouseout",a),this.mouseoutShape=undefined)},_listen:function(){var a=this;this.container.addEventListener("mousedown",function(b){a.mouseDown=!0,a._handleEvent(b)},!1),this.container.addEventListener("mousemove",function(b){a.mouseUp=!1,a.mouseDown=!1,a._handleEvent(b)},!1),this.container.addEventListener("mouseup",function(b){a.mouseUp=!0,a.mouseDown=!1,a._handleEvent(b),a.clickStart=!1},!1),this.container.addEventListener("mouseover",function(b){a._handleEvent(b)},!1),this.container.addEventListener("mouseout",function(b){a.mousePos=undefined},!1),this.container.addEventListener("touchstart",function(b){b.preventDefault(),a.touchStart=!0,a._handleEvent(b)},!1),this.container.addEventListener("touchmove",function(b){b.preventDefault(),a._handleEvent(b)},!1),this.container.addEventListener("touchend",function(b){b.preventDefault(),a.touchEnd=!0,a._handleEvent(b)},!1)},_setMousePosition:function(a){var b=a.clientX-this._getContainerPosition().left+window.pageXOffset,c=a.clientY-this._getContainerPosition().top+window.pageYOffset;this.mousePos={x:b,y:c}},_setTouchPosition:function(a){if(a.touches!==undefined&&a.touches.length===1){var b=a.touches[0],c=b.clientX-this._getContainerPosition().left+window.pageXOffset,d=b.clientY-this._getContainerPosition().top+window.pageYOffset;this.touchPos={x:c,y:d}}},_getContainerPosition:function(){var a=this.container,b=0,c=0;while(a&&a.tagName!=="BODY")b+=a.offsetTop-a.scrollTop,c+=a.offsetLeft-a.scrollLeft,a=a.offsetParent;return{top:b,left:c}},_stripLayer:function(a){a.context.stroke=function(){},a.context.fill=function(){},a.context.fillRect=function(b,c,d,e){a.context.rect(b,c,d,e)},a.context.strokeRect=function(b,c,d,e){a.context.rect(b,c,d,e)},a.context.drawImage=function(){},a.context.fillText=function(){},a.context.strokeText=function(){}},_endDrag:function(a){var b=Kinetic.GlobalObject;b.drag.node&&b.drag.moving&&(b.drag.moving=!1,b.drag.node._handleEvents("ondragend",a)),b.drag.node=undefined},_prepareDrag:function(){var a=this;this.on("mousemove touchmove",function(b){var c=Kinetic.GlobalObject,d=c.drag.node;if(d){var e=a.getUserPosition(),f=d.dragConstraint,g=d.dragBounds;if(f==="none"||f==="horizontal"){var h=e.x-c.drag.offset.x;(g.left===undefined||g.lefth)&&(d.x=h)}if(f==="none"||f==="vertical"){var i=e.y-c.drag.offset.y;(g.top===undefined||g.topi)&&(d.y=i)}c.drag.node.getLayer().draw(),c.drag.moving||(c.drag.moving=!0,c.drag.node._handleEvents("ondragstart",b)),c.drag.node._handleEvents("ondragmove",b)}},!1),this.on("mouseup touchend mouseout",function(b){a._endDrag(b)})},_buildDOM:function(){this.content.style.width=this.width+"px",this.content.style.height=this.height+"px",this.content.style.position="relative",this.content.style.display="inline-block",this.content.className="kineticjs-content",this.container.appendChild(this.content),this.bufferLayer=new Kinetic.Layer,this.backstageLayer=new Kinetic.Layer,this.bufferLayer.parent=this,this.backstageLayer.parent=this,this._stripLayer(this.backstageLayer),this.bufferLayer.getCanvas().style.display="none",this.backstageLayer.getCanvas().style.display="none",this.bufferLayer.canvas.width=this.width,this.bufferLayer.canvas.height=this.height,this.content.appendChild(this.bufferLayer.canvas),this.backstageLayer.canvas.width=this.width,this.backstageLayer.canvas.height=this.height,this.content.appendChild(this.backstageLayer.canvas)}},Kinetic.GlobalObject.extend(Kinetic.Stage,Kinetic.Container),Kinetic.Layer=function(a){this.className="Layer",this.canvas=document.createElement("canvas"),this.context=this.canvas.getContext("2d"),this.canvas.style.position="absolute",this.transitions=[],this.transitionIdCounter=0,Kinetic.Container.apply(this,[]),Kinetic.Node.apply(this,[a])},Kinetic.Layer.prototype={draw:function(){this._draw()},clear:function(){var a=this.getContext(),b=this.getCanvas();a.clearRect(0,0,b.width,b.height)},getCanvas:function(){return this.canvas},getContext:function(){return this.context},add:function(a){this._add(a)},remove:function(a){this._remove(a)},_draw:function(){this.clear(),this.visible&&this._drawChildren()}},Kinetic.GlobalObject.extend(Kinetic.Layer,Kinetic.Container),Kinetic.GlobalObject.extend(Kinetic.Layer,Kinetic.Node),Kinetic.Group=function(a){this.className="Group",Kinetic.Container.apply(this,[]),Kinetic.Node.apply(this,[a])},Kinetic.Group.prototype={add:function(a){this._add(a)},remove:function(a){this._remove(a)},_draw:function(){this.visible&&this._drawChildren()}},Kinetic.GlobalObject.extend(Kinetic.Group,Kinetic.Container),Kinetic.GlobalObject.extend(Kinetic.Group,Kinetic.Node),Kinetic.Shape=function(a){this.className="Shape";if(a.stroke!==undefined||a.strokeWidth!==undefined)a.stroke===undefined?a.stroke="black":a.strokeWidth===undefined&&(a.strokeWidth=2);this.drawFunc=a.drawFunc,Kinetic.Node.apply(this,[a])},Kinetic.Shape.prototype={getContext:function(){return this.tempLayer.getContext()},getCanvas:function(){return this.tempLayer.getCanvas()},fillStroke:function(){var a=this.getContext();this.fill!==undefined&&(a.fillStyle=this.fill,a.fill()),this.stroke!==undefined&&(a.lineWidth=this.strokeWidth===undefined?1:this.strokeWidth,a.strokeStyle=this.stroke,a.stroke())},setFill:function(a){this.fill=a},getFill:function(){return this.fill},setStroke:function(a){this.stroke=a},getStroke:function(){return this.stroke},setStrokeWidth:function(a){this.strokeWidth=a},getStrokeWidth:function(){return this.strokeWidth},_draw:function(a){if(this.visible){var b=a.getStage(),c=a.getContext(),d=[];d.unshift(this);var e=this.parent;while(e.className!=="Stage")d.unshift(e),e=e.parent;for(var f=0;f= 0; n--) { var layer = this.children[n]; if(layer.visible && n >= 0 && layer.isListening) { if(this._traverseChildren(layer, evt)) { n = -1; + shapeDetected = true; } } } + + /* + * if no shape was detected and a mouseout shape has been stored, + * then run the onmouseout event handlers + */ + if(!shapeDetected && this.mouseoutShape) { + this.mouseoutShape._handleEvents('onmouseout', evt); + this.mouseoutShape = undefined; + + } }, /** * begin listening for events by adding event handlers diff --git a/tests/js/functionalTests.js b/tests/js/functionalTests.js index 1092358c..d8823fb8 100644 --- a/tests/js/functionalTests.js +++ b/tests/js/functionalTests.js @@ -458,28 +458,54 @@ Test.prototype.tests = { stage.add(layer); }, - 'EVENTS - event bubbling': function(containerId) { + 'EVENTS - group click events': function(containerId) { var stage = new Kinetic.Stage(containerId, 578, 200); var layer = new Kinetic.Layer(); var group = new Kinetic.Group(); - layer.on('mouseover', function() { - log('mouseover layer'); + + + group.on('click', function() { + log('click group'); //console.log(this); }); - layer.on('mouseout', function() { - log('mouseout layer'); - //console.log(this); + + var redCircle = new Kinetic.Circle({ + x: stage.width / 2, + y: stage.height / 2, + radius: 80, + strokeWidth: 4, + fill: 'red', + stroke: 'black', + name: 'red' }); - group.on('mouseover', function() { - log('mouseover group'); - //console.log(this); - }); - group.on('mouseout', function() { - log('mouseout group'); + var greenCircle = new Kinetic.Circle({ + x: stage.width / 2, + y: stage.height / 2, + radius: 40, + strokeWidth: 4, + fill: 'green', + stroke: 'black', + name: 'green' + }); + + group.add(redCircle); + group.add(greenCircle); + + layer.add(group); + stage.add(layer); + }, + 'EVENTS - group mousemove events': function(containerId) { + var stage = new Kinetic.Stage(containerId, 578, 200); + var layer = new Kinetic.Layer(); + var group = new Kinetic.Group(); + + group.on('mousemove', function() { + log('mousemove group'); //console.log(this); }); + var redCircle = new Kinetic.Circle({ x: stage.width / 2, y: stage.height / 2, @@ -489,14 +515,6 @@ Test.prototype.tests = { stroke: 'black' }); - redCircle.on('mouseover', function() { - log('mouseover red circle'); - //console.log(this); - }); - redCircle.on('mouseout', function() { - log('mouseout red circle'); - //console.log(this); - }); var greenCircle = new Kinetic.Circle({ x: stage.width / 2, y: stage.height / 2, @@ -506,13 +524,45 @@ Test.prototype.tests = { stroke: 'black' }); - greenCircle.on('mouseover', function() { - log('mouseover green circle'); - //console.log(this); + group.add(redCircle); + group.add(greenCircle); + + layer.add(group); + stage.add(layer); + }, + 'EVENTS - group mouseover events': function(containerId) { + var stage = new Kinetic.Stage(containerId, 578, 200); + var layer = new Kinetic.Layer(); + var group = new Kinetic.Group({ + name: 'group' }); - greenCircle.on('mouseout', function() { - log('mouseout green circle'); - //console.log(this); + + group.on('mouseover', function() { + log('mouseover group'); + }); + + group.on('mouseout', function() { + log('mouseout group'); + }); + + var redCircle = new Kinetic.Circle({ + x: stage.width / 2, + y: stage.height / 2, + radius: 80, + strokeWidth: 4, + fill: 'red', + stroke: 'black', + name: 'red' + }); + + var greenCircle = new Kinetic.Circle({ + x: stage.width / 2, + y: stage.height / 2, + radius: 40, + strokeWidth: 4, + fill: 'green', + stroke: 'black', + name: 'green' }); group.add(redCircle);