Coder Social home page Coder Social logo

aframe-draw-component's Introduction

AFrame Draw Component

HTML5 canvas as a material for an A-Frame VR entity.

demo

Fully extendable with components which utilize the canvas api.

Note: if you can't get the component to work with the latest A-Frame version, check out Mayo's shader variations.

Installation

npm i aframe-draw-component --save

##Usage

Register the component with AFrame:

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

Then, you can implement it with third party components which utilize the draw component. Just put it after the draw prop, like this example using a square component:

<a-entity position="0 1 0" geometry="primitive: box" draw="width: 256; height: 256" square="text: Hello">
</a-entity>

Or, if you want direct access to the Canvas API, write a quick component yourself:

AFRAME.registerComponent("square", {
	dependencies: ["draw"],

	update: function() {
		var draw = this.el.components.draw; //get access to the draw component
		var ctx = draw.ctx;
		var canvas = draw.canvas;
		ctx.fillStyle = "#AFC5FF";
		ctx.fillRect(0, 0, canvas.width, canvas.height);
		ctx.fillStyle = "blue";
		ctx.fillRect(68, 68, 120, 120);
		ctx.fillStyle = "white";
		ctx.font = "36px Georgia";
		ctx.fillText(this.data.text, 80, 140);
		draw.render(); //tell it to update the texture
	}
});

Note: the example above is not ideal for third-party components, or when you're using multiple components in your project which use draw. See below:

##Advanced Usage & Render Binding

When writing a generic third party component, you will more than likely want to re-render the canvas. This may also require a clearing of the canvas itself. Because of this, it's imperative that every third party component encapsulate their use of the canvas into their own local render functions, and then register these functions into draw.

draw can then put every component into a call stack, and then re-render each component in the exact order that they were placed in. This also potentially allows for layering, although a-frame can handle this on its own using component updates pretty well.

An example of this uses Object.bind:

AFRAME.registerComponent("square", {
	dependencies: ["draw"],
	init: function() {
		this.draw = this.el.components.draw;
		this.draw.register(this.render.bind(this));
	},
	update: function () {
		this.draw.render();
	},
	render: function () {
		var ctx = this.draw.ctx;
		var canvas = this.draw.canvas;
		ctx.fillStyle = "#AFC5FF";
		ctx.fillRect(0, 0, canvas.width, canvas.height);
		ctx.fillStyle = this.data.color;
		ctx.fillRect(68, 68, 120, 120);
	}
});

AFRAME.registerComponent("greeting", {
	dependencies: ["draw"],
	init: function() {
		this.draw = this.el.components.draw;
		this.draw.register(this.render.bind(this));
	},
	update: function () {
		this.draw.render();
	},
	render: function () {
		var ctx = this.draw.ctx;
		ctx.fillStyle = "white";
		ctx.font = "36px Georgia";
		ctx.fillText(this.data.text, 80, 140);
	}
});

After each component is initialized, it registers its own render function with draw. If its own data is changed (within the update function), it will tell draw to re-render the entire canvas, and call both the square and greeting's render functions in order.

##Methods & Component Properties

  • These are only needed for writing your own components which utilize draw.
Property Description
.canvas hidden canvas to perform methods on
.ctx hidden ctx to perform methods on
.register([func] render) add your component's own render function to the registry so that it will re-render on any render call.
.render() update the material with the new canvas

##Properties

Property Description
width width of canvas (should match ratio of a face of the entity)
height height of canvas (should match ratio of a face of the entity)
background background color of canvas

##Featured Components

##Additional Info

  • Thanks to ngokevin and RangerMauve for their help.
  • As this is meant to be an extendable component, PR's to the readme are welcome.

aframe-draw-component's People

Contributors

goblinlordx avatar maxkrieger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

aframe-draw-component's Issues

Does not work with latest aframe branch

Hi,

The components works perfectly if using aframe-core, as you have in your package
"aframe-core": "0.1.2",
but the with the latest aframe branch
"aframe": "aframevr/aframe#dev"
The canvas is not drawn on the entity and I didn't get any message on the console about it.

Build dist files

npm run dist should build dist files so we can download droppable files into HTML.

how to use it with es6 import?

I want to do it by es6 import:

import 'aframe'
import 'aframe-draw-component'

but I fount that It didn't add draw in AFRAME components.
How to import it and use it with advanced usage?

not updating element

Hi,

Great component.. thanks.

I've picked this up from en existing project and I'm trying to enable updating an element, This component is working the first time, but after I try and update it just turns into a white box.

Here is my component:

`AFRAME.registerComponent('hotspots-button', {
  dependencies: ['draw'],
  schema: {
    text: {type: 'string', default: ''},
    icon: {type: 'string', default: ''},
  },
  _imageURL: '',

  init: function() {
    this.draw = this.el.components.draw
    this.draw.register(this.render.bind(this))

  },

  update: function () {
    this.draw.render()
    this.el.children[0].setAttribute('src', this._imageURL)
  },

  render: function () {
    let ctx = this.draw.ctx
    let canvas = this.draw.canvas
    blackCurvedImage(canvas, ctx, canvas.width, canvas.height, this.data.text)
    this._imageURL = canvas.toDataURL('image/png',1.0)
  },
})`

The blackCurvedImage function makes the changes to the canvas i.e.

export function blackCurvedImage(canvas, ctx, width, height, text) {
  // Clean context
  ctx.clearRect(0, 0, width, height)
  // Container
  ctx.strokeStyle = classicWhiteOutlineRGBA
  ctx.fillStyle = classicBlackFillRGBA
  roundRect(ctx, 0, 0, width, height, 20, true) 
...

However changes are not being reflected.

Currently it's just turning the canvas white.

Any ideas on how to manage updates?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.