Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Initial commit
  • Loading branch information
maxkrieger committed Feb 19, 2016
0 parents commit ff4ada7
Show file tree
Hide file tree
Showing 16 changed files with 537 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
.sw[ponm]
examples/build.js
examples/node_modules/
gh-pages
node_modules/
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Kevin Ngo

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.

52 changes: 52 additions & 0 deletions README.md
@@ -0,0 +1,52 @@
# AFrame Text Wrap Component

Wrappable text as a material for an [A-Frame](https://aframe.io) VR entity. Requires [`draw`](https://github.com/maxkrieger/aframe-draw-component).

![demo](demo.png)

All attributes are listed below.

## Installation
```
npm i aframe-draw-component --save
npm i aframe-textwrap-component --save
```
##Usage

Register the component with AFrame:

```js
var AFRAME = require("aframe-core");
var draw = require("aframe-draw-component").component;
var textwrap = require("aframe-textwrap-component").component;
AFRAME.registerComponent("draw", draw);
AFRAME.registerComponent("textwrap", textwrap);
```

Then integrate it on an entity:

```html
<a-entity id="box" geometry="primitive: box" position="0 2 0" draw="background: #D7E8FF" textwrap="textAlign: center; x: 128; y: 128; text: Hello world!"></a-entity>
```


##Properties

All properties have a default! :)

|Property|Description|
|------|-------|
|`text`|the text that should be wrapped. **If your text contains a colon (`:`) or a semicolon (`;`), you can escape it using `url(my:text;)`.**|
|`x`|canvas x position of the text (upper left corner)|
|`y`|canvas y position of the text (upper left corner)|
|`font`|css `font` string ([see here](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/font))|
|`color`|color of the text|
|`textAlign`|text alignment ([see here](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textAlign))|
|`textBaseline`|text baseline ([see here](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textBaseline))|
|`direction`|text direction ([see here](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/direction))|
|`width`|width of text block in pixels|
|`lineHeight`|line height (vertical spacing) in pixels|

##Additional Info

* Wrapping function is from [here](http://www.html5canvastutorials.com/tutorials/html5-canvas-wrap-text-tutorial/).
20 changes: 20 additions & 0 deletions browser.js
@@ -0,0 +1,20 @@
// Browser distribution of the A-Frame component.
(function () {
if (typeof AFRAME === 'undefined') {
console.error('Component attempted to register before AFRAME was available.');
return;
}

// Register all components here.
var components = {
example: require('./index').component
};

Object.keys(components).forEach(function (name) {
if (AFRAME.aframeCore) {
AFRAME.aframeCore.registerComponent(name, components[name]);
} else {
AFRAME.registerComponent(name, components[name]);
}
});
})();
Binary file added demo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 99 additions & 0 deletions dist/aframe-example-component.js
@@ -0,0 +1,99 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};

/******/ // The require function
/******/ function __webpack_require__(moduleId) {

/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;

/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };

/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/ // Flag the module as loaded
/******/ module.loaded = true;

/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }


/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;

/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;

/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";

/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {

// Browser distrubution of the A-Frame component.
(function () {
if (!AFRAME) {
console.error('Component attempted to register before AFRAME was available.');
return;
}

// Register all components here.
var components = {
example: __webpack_require__(1).component
};

Object.keys(components).forEach(function (name) {
if (AFRAME.aframeCore) {
AFRAME.aframeCore.registerComponent(name, components[name]);
} else {
AFRAME.registerComponent(name, components[name]);
}
});
})();


/***/ },
/* 1 */
/***/ function(module, exports) {

/**
* Example component for A-Frame.
*/
module.exports.component = {
schema: { },

/**
* Called once when component is attached. Generally for initial setup.
*/
init: function () { },

/**
* Called when component is attached and when component data changes.
* Generally modifies the entity based on the data.
*/
update: function (oldData) { },

/**
* Called when a component is removed (e.g., via removeAttribute).
* Generally undoes all modifications to the entity.
*/
remove: function () { }
};


/***/ }
/******/ ]);
1 change: 1 addition & 0 deletions dist/aframe-example-component.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions examples/basic/index.html
@@ -0,0 +1,11 @@
<html>
<head>
<script src="../build.js"></script>
</head>
<body>
<a-assets></a-assets>
<a-scene>
<a-entity id="box" geometry="primitive: box" position="0 2 0" draw="background: #D7E8FF" textwrap="textAlign: center; x: 128; text: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu"></a-entity>
</a-scene>
</body>
</html>
33 changes: 33 additions & 0 deletions examples/index.html
@@ -0,0 +1,33 @@
<html>
<head>
<title>A-Frame Example Component</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-fork-ribbon-css/0.1.1/gh-fork-ribbon.min.css"/>
<style>
html {
background: #EF2D5E;
color: #FAFAFA;
font-family: monospace;
font-size: 20px;
padding: 10px 20px;
}
h1 {
font-weight: 300;
}
a {
color: #FAFAFA;
display: block;
padding: 15px 0;
}
</style>
</head>
<body>
<h1>A-Frame Example Component</h1>
<a href="basic/index.html">Basic</a>

<div class="github-fork-ribbon-wrapper right">
<div class="github-fork-ribbon" style="background: #3482AA">
<a href="https://github.com/ngokevin/aframe-component-boilerplate">Fork me on GitHub</a>
</div>
</div>
</body>
</html>
8 changes: 8 additions & 0 deletions examples/main.js
@@ -0,0 +1,8 @@
var AFRAME = require("aframe-core");
var drawComponent = require("aframe-draw-component").component;
var textwrapComponent = require("../index.js").component;
AFRAME.registerComponent("draw", drawComponent);
AFRAME.registerComponent("textwrap", textwrapComponent);
window.setTimeout(function() {
document.querySelector("#box").setAttribute("textwrap", "text", "fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
}, 2000);
103 changes: 103 additions & 0 deletions index.js
@@ -0,0 +1,103 @@
/**
* Example component for A-Frame.
*/
module.exports.component = {
dependencies: ["draw"],
schema: {
text: {
default: "Sample Text"
},
x: {
default: 5
},
y: {
default: 20
},
font: {
default: "20px sans-serif"
},
color: {
default: "#000000"
},
textAlign: {
default: "start"
},
textBaseline: {
default: "alphabetic"
},
direction: {
default: "inherit"
},
width: {
default: 256
},
lineHeight: {
default: 20
}
},

/**
* Called once when component is attached. Generally for initial setup.
*/
init: function () {
this.draw = this.el.components.draw;
this.draw.register(this.render.bind(this));
},

/**
* Called when component is attached and when component data changes.
* Generally modifies the entity based on the data.
*/
update: function () {
this.filterEscapeUrl(); //for escaping colons, semicolons, etc
this.draw.render();
},

filterEscapeUrl: function () {
var match = this.data.text.match(/^url\((.*)\)$/);
if (match) this.data.text = match[1];
},

render: function () {
var ctx = this.draw.ctx;
var canvas = this.draw.canvas;

if (this.data.bg) {
ctx.fillStyle = this.data.bg;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}

ctx.fillStyle = this.data.color;
ctx.font = this.data.font;
ctx.textAlign = this.data.textAlign;
ctx.textBaseline = this.data.textBaseline;
ctx.direction = this.data.direction;
wrapText(ctx, this.data.text, this.data.x, this.data.y, this.data.width, this.data.lineHeight);

//stolen from http://www.html5canvastutorials.com/tutorials/html5-canvas-wrap-text-tutorial/
function wrapText(context, text, x, y, maxWidth, lineHeight) {
var words = text.split(" ");
var line = "";

for (var n = 0; n < words.length; n++) {
var testLine = line + words[n] + " ";
var metrics = context.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > maxWidth && n > 0) {
context.fillText(line, x, y);
line = words[n] + " ";
y += lineHeight;
} else {
line = testLine;
}
}
context.fillText(line, x, y);
}
},

/**
* Called when a component is removed (e.g., via removeAttribute).
* Generally undoes all modifications to the entity.
*/
remove: function () {}
};

0 comments on commit ff4ada7

Please sign in to comment.