claygl/example/app_event.html

64 lines
1.9 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="../dist/claygl.js"></script>
<title>Basic Cube</title>
</head>
<body>
<style>
html, body, #main {
height: 100%;
margin: 0;
}
</style>
<div id="main"></div>
<script>
var app = clay.application.create('#main', {
event: true,
init: function (app) {
// Create camera
this._camera = app.createCamera([0, 2, 5], [0, 0, 0]);
// Create cube
this._cube = app.createCube();
['mouseover', 'mousemove', 'mouseout', 'click', 'dbclick'].forEach(function (eveType) {
this._cube.on(eveType, function (event) {
console.log('Event: ' + event.type);
});
}, this);
function makeRandomColor() {
return [Math.random(), Math.random(), Math.random()];
}
var randomColor = makeRandomColor();
this._cube.on('mouseover', function () {
this._cube.material.set('color', randomColor);
randomColor = makeRandomColor();
}, this).on('mouseout', function () {
this._cube.material.set('color', randomColor);
randomColor = makeRandomColor();
}, this);
// Create light
this._mainLight = app.createDirectionalLight([-1, -1, -1]);
},
loop: function (app) {
this._cube.rotation.rotateY(app.frameTime / 1000);
}
});
window.onresize = function () {
app.resize()
};
</script>
</body>
</html>