Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mouseenter and mouseleave #127

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions examples/hover/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/** @jsx React.DOM */

var React = require('react');
var ReactDOM = require('react-dom');
var ReactCanvas = require('react-canvas');

var Surface = ReactCanvas.Surface;
var Group = ReactCanvas.Group;
var Image = ReactCanvas.Image;
var Text = ReactCanvas.Text;
var FontFace = ReactCanvas.FontFace;

var App = React.createClass({
// Hover declaration
// =================
getInitialState: function () {
return {hovered: false};
},
handleMouseEnter: function () {
this.setState({hovered: true});
},
handleMouseLeave: function () {
this.setState({hovered: false});
},
componentDidMount: function () {
window.addEventListener('resize', this.handleResize, true);
},
render: function () {
var size = this.getSize();
return (
<Surface top={0} left={0} width={size.width} height={size.height} enableCSSLayout={true}>
<Group style={this.getPageStyle()} >
<Group style={this.getBoxStyle()} onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave} >
<Text style={this.getBoxTitleStyle()}>
Hover me!
</Text>
</Group>
</Group>
</Surface>
);
},

// Styles
// ======

getSize: function () {
return document.getElementById('main').getBoundingClientRect();
},

getPageStyle: function () {
var size = this.getSize();
return {
position: 'relative',
padding: 14,
width: size.width,
height: size.height,
backgroundColor: '#f7f7f7',
flexDirection: 'column'
};
},

getBoxStyle: function () {
return {
position: 'relative',
margin: 50,
flex: 1,
backgroundColor: this.state.hovered ? 'red' : '#eee'
};
},


getBoxTitleStyle: function () {
return {
fontFace: FontFace('Georgia'),
fontSize: 22,
lineHeight: 28,
height: 28,
marginTop: 10,
color: '#333',
textAlign: 'center'
};
},

// Events
// ======

handleResize: function () {
this.forceUpdate();
}

});

ReactDOM.render(<App />, document.getElementById('main'));
13 changes: 13 additions & 0 deletions examples/hover/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>ReactCanvas: hover</title>
<link rel="stylesheet" type="text/css" href="/examples/common/examples.css">
</head>
<body>
<div id="main"></div>
<script src="/build/hover.js"></script>
</body>
</html>
4 changes: 3 additions & 1 deletion lib/EventTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ module.exports = {
onTouchCancel: 'touchcancel',
onClick: 'click',
onContextMenu: 'contextmenu',
onDoubleClick: 'dblclick'
onDoubleClick: 'dblclick',
onMouseEnter: 'mouseenter',
onMouseLeave: 'mouseleave'
};
14 changes: 13 additions & 1 deletion lib/FrameUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ function union (frame, otherFrame) {
return make(x1, y1, x2 - x1, y2 - y1);
}


/**
* Get unique ID. TODO: Rewrite with yeild on es7.
*
* @return {Number}
*/
var iterator = 0;
function generateId () {
return iterator++;
}

/**
* Determine if 2 frames intersect each other
*
Expand All @@ -126,6 +137,7 @@ module.exports = {
inset: inset,
intersection: intersection,
intersects: intersects,
union: union
union: union,
generateId: generateId
};

1 change: 1 addition & 0 deletions lib/RenderLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var DrawingUtils = require('./DrawingUtils');
var EventTypes = require('./EventTypes');

function RenderLayer () {
this.id = FrameUtils.generateId();
this.children = [];
this.frame = FrameUtils.zero();
}
Expand Down
50 changes: 50 additions & 0 deletions lib/Surface.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ var Surface = React.createClass({
onTouchEnd: this.handleTouchEnd,
onTouchCancel: this.handleTouchEnd,
onClick: this.handleClick,
onMouseOver: this.handleMouseOver,
onMouseOut: this.handleMouseOut,
onMouseMove: this.handleMouseMove,
onContextMenu: this.handleContextMenu,
onDoubleClick: this.handleDoubleClick})
);
Expand Down Expand Up @@ -216,6 +219,53 @@ var Surface = React.createClass({
this.hitTest(e);
},

hitEvent: function (hitTarget, type) {
var type = hitTest.getHitHandle(type);
if (typeof hitTarget[type] === "function") {
hitTarget[type]();
}
},

handleMouseMove: function (e) {
var hitTarget = hitTest(e, this.node, this.refs.canvas);
var oldHoveredSet = this._hovered || {}
var self = this;
var newHoveredSet = {};
if (hitTarget) {
var id;
do {
id = hitTarget.id;
if (oldHoveredSet.hasOwnProperty(id)) {
// remove from mouseout
delete oldHoveredSet[id];
} else {
this.hitEvent(hitTarget, 'mouseenter');
}
newHoveredSet[id] = hitTarget;
hitTarget = hitTarget.parentLayer;
} while (hitTarget);
}
this._hovered = newHoveredSet;
Object.keys(oldHoveredSet).forEach(function (id) {
self.hitEvent(oldHoveredSet[id], 'mouseleave');
});
},

handleMouseOut: function (e) {
var self = this;
if (this._hovered) {
Object.keys(this._hovered).forEach(function (id) {
self.hitEvent(self._hovered[id], 'mouseleave');
});
}
this._hovered = {};
},

handleMouseOver: function (e) {
// just mousemove run mousemove event
this.handleMouseMove(e);
},

handleContextMenu: function (e) {
this.hitTest(e);
},
Expand Down
2 changes: 1 addition & 1 deletion lib/hitTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function getLayerAtPoint (root, type, point, tx, ty) {
}

// No child layer at the given point. Try the parent layer.
if (!layer && root[hitHandle] && FrameUtils.intersects(hitFrame, point)) {
if (!layer && (root[hitHandle] || type === "mousemove") && FrameUtils.intersects(hitFrame, point)) {
layer = root;
}

Expand Down
3 changes: 2 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module.exports = {
'listview': ['./examples/listview/app.js'],
'timeline': ['./examples/timeline/app.js'],
'gradient': ['./examples/gradient/app.js'],
'css-layout': ['./examples/css-layout/app.js']
'css-layout': ['./examples/css-layout/app.js'],
'hover': ['./examples/hover/app.js']
},

output: {
Expand Down