• Stars
    star
    204
  • Rank 192,063 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A custom element for rendering inline 3D models in a HTML document and allowing them to be transformed with CSS.

3D model custom element

This is a experimental custom element that allows 3D objects (currently, OBJ and gLTF format) to be loaded into a document and rendered inline, just like any other external resource. Additionally, any CSS transforms applied to the element will be passed on to the 3D model, allowing complex objects to be placed and maniplulated using just CSS.

Screengrab of transformed models

Examples

  • Hello World - A barebones example.
  • Transform tests - A set of rendering test cases.
  • 3D Scene - An example of mixing standard HTML elements and models to create a 3D scene.

Limitations

  • Support for the gLTF and OBJ model formats - other formats will be added in the future.
  • File format is currently determined by file extension (.obj for OBJ and .gltf/.glb for gLTF).
  • At the moment Safari doesn't scroll models because of a bug with scrollTop.
  • transform-style: flat isn't supported yet.

Getting started

To use this custom element you'll need to include the three.js library (r102+), its accompanying OBJ loader and/or gLTF loader along with the custom element script:

<script src="js/three.min.js"></script>
<script src="js/three-obj-loader.js"></script>
<script src="js/model-element.min.js"></script>

To bolster up browser support, you can also include a web components polyfill.

Using the <x-model> element

Adding a model to a page is as simple as adding the element and setting its src attribute:

<x-model src="/path/to/my-model.obj"></x-model>

Here's a cut-and-paste example:

<!DOCTYPE html>
<html>
<head>
  <style>
    .model {
      width: 400px;
      height: 400px;
    }
  </style>
</head>

<body>
  <x-model class="model" src="/path/to/my-model.obj"></x-model>

  <script src="js/three.min.js"></script>
  <script src="js/three-obj-loader.min.js"></script>
  <script src="js/model-element.min.js"></script>
</body>
</html>

Positioning models

Models can be positioned and rotated using CSS transforms. Any transforms applied to a model element will also be applied to the model.

.model {
  transform: translateZ(-200px) rotateY(45deg);
}

If a model element has an ancestor with a valid perspective / perspective-origin style property, the model will be renderered using perspective projection. Omitting perspective (or setting it to zero) will result in objects being rendered with orthographic projection.


How it works

The model-element script creates a camera, scene, light source and a WebGL renderer. The DOM node returned by the renderer (a <canvas> element) is added to the document and configured to fill the viewport and sit above all other content. Additionally, pointer-events: none is set, allowing elements below to be interacted with.

Adding <x-model> elements to the DOM results in the model being loaded and added to the underlying scene. Removing an element from the DOM will remove it from the scene.

The scene is re-rendered every frame. For each object in the scene, the renderer finds it's host node and walks up the DOM tree resolving any transforms, positions and scroll offsets. The resulting transform matrix is then applied to the object in the scene. Once all objects are updated the renderer repaints the scene to the layer. Objects now appear on-screen, synchronised with their host DOM node.


Contributing

Requirements

  • Node / NPM

Setup

  1. Clone this repo.
  2. Install dependencies: npm install
  3. Build the project with the watch task: npm run dev
  4. Start editing...

Other build options

  • npm run dist - builds the both the unminified and minified distribution files to the /dist/ folder.

More Repositories

1

selectivizr

selectivizr is a JavaScript utility that emulates CSS3 pseudo-classes and attribute selectors in Internet Explorer 6-8.
JavaScript
1,715
star
2

ZzFXM

A super small music generator for use in size-limited JavaScript productions
JavaScript
382
star
3

gadebugger

A Chrome, Firefox & Opera devtools extension for debugging Google Analytics tracking code
JavaScript
238
star
4

css-feature-toggle-devtools-extension

A devtools extension for toggling CSS features allowing developers to see how pages/apps render in browsers that don't support modern CSS features
JavaScript
157
star
5

fuse-devtools

A boilerplate for creating a devtools extension for multiple browsers from a single code base.
JavaScript
134
star
6

JQuery-Extended-Selectors

Extends jQuery to add support for CSS3 xxx-of-type selectors
134
star
7

cssvr

CSSVR is an experiment that simulates browser support for creating VR experiences using media queries and CSS transforms.
JavaScript
82
star
8

ComputedStyleObserver

Prototype implementation of a `computedStyle` observer for DOM elements
JavaScript
19
star
9

surrogate

This is a tiny JavaScript library targeted at modern web browsers and designed to mimic the common parts of jQuery's funtions and syntax using modern ECMAScript and DOM implementations
JavaScript
4
star
10

imagedata

A collection of packages for working with images in JavaScript environments that don't implement the ImageData interface.
JavaScript
2
star
11

zzfxm-songs

A collection of songs for use with ZzFXM
2
star
12

sculpt

JavaScript
2
star
13

stylescribe

Stylescribe is a CSS documentation generator. It's goal is to produce a reference document that can be used to quickly build web pages without having to dive into the inner workings of a projects CSS.
JavaScript
2
star
14

atari-chunked-blits

Testing the feasibility of automatically breaking HOG blits into smaller chunks, allowing large objects to be drawn without interfering with interrupts
Assembly
2
star
15

selection-element

A web component for selecting HTML elements or other web components.
JavaScript
1
star