Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
richardanaya committed Jun 13, 2016
0 parents commit c32ed42
Show file tree
Hide file tree
Showing 14 changed files with 261 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015", "react"]
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
*.log
node_modules
build
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
examples
.idea
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Richard Anaya

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#aframe-html

This is a component for easily adding HTML as a material to your 3d objects in aframe VR.

Features
* Load content from a url (note: you'll be restricted by what urls you use unless you use CORS or proxy)
* Modify content directly
* Simulate mouse events ("mousedown","mouseup","mousemove","click")
5 changes: 5 additions & 0 deletions examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require("aframe")
require("aframe-canvas")
require("./index.js")
window.React = require("react")
window.ReactDOM = require("react-dom")
11 changes: 11 additions & 0 deletions examples/00_simple/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<script src="../../build/bundle.js"></script>
</head>
<body>
<a-scene>
<a-sky color="#999999"></a-sky>
<a-plane position="0 1.25 -1" scale="3 3 3" html-material="url:test.html;width:200;height:200"/>
</a-scene>
</body>
</html>
5 changes: 5 additions & 0 deletions examples/00_simple/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h1>This is a <b>test</b></h1>
</body>
</html>
16 changes: 16 additions & 0 deletions examples/01_innerHTML/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<head>
<script src="../../build/bundle.js"></script>
</head>
<body>
<a-scene>
<a-sky color="#999999"></a-sky>
<a-plane id="screen" position="0 1.25 -1" scale="3 3 3" html-material="width:200;height:200"/>
</a-scene>
</body>
<script>
var component = document.querySelector("#screen").components["html-material"];
component.getDocument().body.innerHTML = "Hello World!"
component.updateTexture();
</script>
</html>
19 changes: 19 additions & 0 deletions examples/02_react/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<html>
<head>
<script src="../../build/bundle.js"></script>
</head>
<body>
<a-scene>
<a-sky color="#999999"></a-sky>
<a-plane id="screen" position="0 1.25 -1" scale="3 3 3" html-material="width:200;height:200"/>
</a-scene>
</body>
<script>
var component = document.querySelector("#screen").components["html-material"];
ReactDOM.render(
React.createElement("div",null,"Hello React!"),
component.getDocument().body
);
component.updateTexture();
</script>
</html>
34 changes: 34 additions & 0 deletions examples/03_mouse/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<html>
<head>
<script src="../../build/bundle.js"></script>
</head>
<body>
<a-scene>
<a-sky color="#999999"></a-sky>
<a-plane id="screen" position="0 1.25 -1" scale="3 3 3" html-material="width:200;height:200"/>
</a-scene>
</body>
<script>
var component = document.querySelector("#screen").components["html-material"];

var clicked = false;

var render = function(){
ReactDOM.render(
React.createElement("button",{style:{position:"absolute",top:"100px",left:"100px",background:clicked?"red":"auto"},onClick:buttonClicked},"Click Me"),
component.getDocument().body
);
component.updateTexture();
}

var buttonClicked = function(){
clicked = true;
render();
}
render();

setTimeout(function(){
component.triggerEventAt("click",101,101);
},3000)
</script>
</html>
84 changes: 84 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// aframe-html.js
// repo : https://github.com/richardanaya/aframe-html
// license : MIT

(function (window, module, AFRAME, rasterizeHTML) {
"use strict";
AFRAME = AFRAME.aframeCore || AFRAME;

AFRAME.registerComponent('html-material', {
schema: {
url: {
type: 'string',
default: ""
},
width: {
type: 'int',
default: 0
},
height: {
type: 'int',
default: 0
}
},

update: function () {
var _this = this;
if(!this.canvas){
this.canvas = document.createElement("canvas");
}
this.canvas.width = this.data.width;
this.canvas.height = this.data.height;
var _this = this;
var ctx = this.canvas.getContext("2d")
ctx.clearRect(0,0,this.data.width,this.data.height);
var texture = new THREE.Texture(this.canvas);
var material = new THREE.MeshBasicMaterial({ map: texture, transparent: true });
this.el.object3D.children[0].material = material;

if(!this.frame) {
this.frame = document.createElement("iframe");
this.el.appendChild(this.frame);
}

var frameDocument = _this.frame.contentWindow.document;

this.getDocument = function(){
return frameDocument;
}
this.updateTexture = function(){
rasterizeHTML.drawDocument(frameDocument, ctx.canvas, {width: _this.data.width}).then(function success(renderResult) {
texture.needsUpdate = true;
}, function error(e) {
console.log(e)
});
}

function triggerMouseEvent (node, eventType) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent (eventType, true, true);
node.dispatchEvent (clickEvent);
}

this.triggerEventAt = function(e,x,y){
var target = frameDocument.elementFromPoint(x,y);
triggerMouseEvent(target,e);
}

if(this.data.url != ""){
this.frame.src = this.data.url;
this.frame.addEventListener("load",function(){
frameDocument = _this.frame.contentWindow.document;
_this.updateTexture();
})
}


}
});
})(
typeof window !== "undefined" ? window : {},
typeof module !== "undefined" ? module : {},
typeof require !== "undefined" ? require("aframe") : (AFRAME || window.AFRAME),
typeof require !== "undefined" ? require("rasterizehtml") : rasterizeHTML
);
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "aframe-html",
"version": "0.0.1",
"description": "A component rendering and interacting with HTML in VR",
"main": "index.js",
"scripts": {
"dev": "node node_modules/concurrently/src/main.js --kill-others \"./node_modules/webpack/bin/webpack.js --progress --colors --watch\" \"./node_modules/http-server/bin/http-server -s -c-1 -p 9998 .\" \"echo LISTENING ON localhost:9998\""
},
"repository": {
"type": "git",
"url": "git+https://github.com/richardanaya/aframe-html.git"
},
"keywords": [
"aframe",
"html",
"vr",
"3d"
],
"author": "Richard Anaya <[email protected]> (http://www.richardanaya.com/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/richardanaya/aframe-html/issues"
},
"homepage": "https://github.com/richardanaya/aframe-html#readme",
"dependencies": {
"aframe": "latest",
"rasterizehtml": "^1.2.2"
},
"devDependencies": {
"concurrently": "^2.1.0",
"http-server": "^0.9.0",
"nodemon": "^1.9.2",
"webpack": "^1.13.1",
"react": "^15.1.0",
"react-dom": "^15.1.0"
}
}
12 changes: 12 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
entry: "./examples.js",
output: {
path: __dirname+"/build/",
filename: "bundle.js"
},
module: {
loaders: [

]
}
};

0 comments on commit c32ed42

Please sign in to comment.