Svelte A-Frame Web Components - high performance reactive development kit for A-Frame

Related tags

Boilerplate svawc
Overview

logo - cartoon seagul with a wide open beak and the letters S V A W C

SVAWC

Svelte A-Frame Web Components

SVAWC brings modern reactive development and HTML templating to A-Frame component development without compromising on speed, usability, or bundle size.

How it works

  1. Write reactive template code using Svelte
  2. Svelte compiles that down to efficient createElement, setAttibute, et c. calls (no virtual DOM or unecessary entity recreation)
  3. SVAWC packages it into Web Components for distribution
  4. Link the packaged script and then use the Web Component in any A-Frame scene, works with bundled apps and vanilla JS & HTML

What it looks like

Svelte reactive template source:

<!-- APerson.svelte -->
<script>
  // props, converted to dash case on WebComponent, e.g. shirt-color
  export let skinColor = 'burlywood'
  export let shirtColor = 'seagreen'
  export let pantsColor = 'slateblue'
  // computed variables
  $: skinMaterial = { color: skinColor, roughness: 0.9 }
  $: shirtMaterial = { color: shirtColor }
  $: pantsMaterial = { color: pantsColor }
  const limbs = [-1, 1]
</script>

<a-entity
  class="head" 
  position={{ x: 0, y: 1.6, z: 0 }}
  geometry={{ primitive: 'sphere', radius: 0.2 }}
  material={skinMaterial}
  shadow
/>
<a-entity
  class="body"
  position={{ x: 0, y: 1.05, z: 0 }}
  geometry={{primitive: 'cylinder', radius: 0.25, height: 0.7 }}
  material={shirtMaterial}
  shadow
>
  <!-- loops -->
  {#each limbs as side (side)}
    <a-entity
      class="arm"
      position={{ x: side *  0.3, y: 0.05, z: 0 }}
      rotation={{ x: 0, y: 0, z: side * 30 }}
      geometry={{ primitive: 'cylinder', radius: 0.1, height: 0.7 }}
      material={shirtMaterial}
      shadow
      />
  {/each}
</a-entity>
{#each limbs as side (side)}
  <a-entity
    class="leg"
    position={{ x: side * 0.1, y: 0.35, z: 0 }}
    rotation={{ x: 0, y: 0, z: side * 10 }}
    geometry={{ primitive: 'cylinder', radius: 0.15, height: 0.7 }}
    material={pantsMaterial}
    shadow
    />
{/each}

The above is just standard Svelte code. Check out their guide if you're not already familiar.

SVAWC Wrapper:

import { registerWebComponent } from 'svawc'
import APerson from "./APerson.svelte"
registerWebComponent({Component: APerson, tagname: "a-person", props: ["skinColor", "shirtColor", "pantsColor"] })

Usage in A-Frame Scene:

<head>
  <script src="https://aframe.io/releases/1.4.1/aframe.js"></script>
  <script src='https://cdn.jsdelivr.net/npm/svawc-template'></script>
</head>
<body>
  <a-scene>
    <a-person position="0 0 -3"></a-person>
    <a-person position="1 0 -3" skin-color="peachpuff" shirt-color="grey" pants-color="darkgrey"></a-person>
    <a-person position="-1 0 -3" skin-color="sienna" shirt-color="pink" pants-color="white"></a-person>
  </a-scene>
</body>

Try it out

Why it's useful

I love A-Frame, but the recurring pain points for me in large apps are handling complex reactive state and making nested entity structures re-usable.

Solutions for the reactive state generally involve meta-components like event-set or the creation of one-off 'components' that just handle business logic. These tend to spread your logic around and make a large codebase harder to maintain. For re-usable structures, you're either stuck with HTML templates, which are awkward to use, bloat your index.html, and again serve to keep your structure far from your logic, or you've got to write tons of tedious createElement and setAttribute calls.

SVAWC lets you write the organized, concise code we're accustomed to from modern reactive frameworks and integrate it seamlessly in any A-Frame project. SVAWC is the A-Frame answer to React Three Fiber, which is a lovely and powerful framework, but never feels quite right to me due the lack of ECS.

API documentation

View the full API documentation at https://immers-space.github.io/svawc

Get Started

The svawc-template repo has everything you need to start building and publishing SVAWCs. Click here to create a copy of it.

Feature status

This library is fully functional, but some of the features still need some polish

πŸ™‚ Svelte props as HTML Attributes
Svelte props become attributes on the custom element, converting camelCase to dash-case automatically. For now, the props must be explicitly listed in the `props` option, but I'd like to be able to infer them automatically in the future.
πŸ˜€ Light DOM
All component output is rendered to the light DOM as children of the custom element. Shadow DOM is not available as the boundary breaks A-Frame's scene graph logic, and the benefits of Shadow DOM are primarily CSS encapsulation which isn't relevant here.
😐 Slots
Full slot functionality is available including default and named slots. There's an issue with A-Frame compatibility that generates console errors from slotted entities trying to initialize within a document fragment, but it doesn't seem to cause any issues. Will work on fixes to avoid those errors.
😦 Dependency Injection
Not available yet, but I'd like to have it work where dependencies on A-Frame components can be re-used from the consuming app if already installed or injected via CDN if not so that we don't have bundle extra code in our SVAWCs nor worry about duplicate component registration.

Code adapted from svelte-tag by Chris Ward.

Logo is CC-BY-NC-SA, adapted from a photo by Leonard J Matthews.

You might also like...
Berry free react material-ui admin template for easing and faster web development.
Berry free react material-ui admin template for easing and faster web development.

Berry is a creative free react admin template build using the Material-UI. It is meant to be the best User Experience with highly customizable feature-riched pages. It is a complete game-changer React Dashboard Template with easy and intuitive responsive design as on retina screens or laptops.

Modularization best practice to common web development scenarios, applies to vue/react/... any framework

Vite How-to Guide There are plenty of tutorials on how to use vite to compile code written in latest and hottest technology, this guide is different.

GeekTutor is your best source for web development tutoring and assistance
GeekTutor is your best source for web development tutoring and assistance

GeekTutor is your best source for web development tutoring and assistance. Each tutor has expertise on a variety of subjects. Users can view articles written by the tutors, and scheule sessions via email.

LightCode is a powerful web-based development tool allowing multiple users to modify the same codebase in real-time. It is built with React.js, Node.js, Socket.IO, and Tailwind CSS.

LightCode Introduction LightCode is a powerful web-based development tool allowing multiple users to modify the same codebase in real-time. It is buil

A monorepo boilerplate for react web and react native. Uses react-native-web to reduce time invested in making the same components for mobile and web

Monorepo template Get Started Run the following command to install all the dependencies in the right location yarn lerna bootstrap yarn workspace @mar

Free Open Source High Quality Dashboard based on Bootstrap 4 & React 16
Free Open Source High Quality Dashboard based on Bootstrap 4 & React 16

Airframe React High Quality Dashboard / Admin / Analytics template that works great on any smartphone, tablet or desktop. Available as Open Source as

A Frontend boilerplate for High productivity

Stapp frontend React 16, Redux 4, Router 4, Webpack 4 Domain-Driven File Structuring React/Redux No CSS frameworks (minimal styles starter) HMR suppor

A library of high-quality primitives that extend SolidJS reactivity.

Solid Primitives A project that strives to develop high-quality, community contributed Solid primitives. All utilities are well tested and continuousl

Angular - Best Practices of Reactive Programming.

This project is a study system in Angular and implementing using the best practices of reactive programming.

Owner
Immers Space
Immers Space
Tail-kit is a free and open source components and templates kit fully coded with Tailwind css 2.0.

Tail-Kit A beautiful and large components and templates kit for TailwindCSS 2.0 Tail-Kit is Free and Open Source. It does not change or add any CSS to

null 2.5k Jan 4, 2023
Now UI Kit React - Free Bootstrap 4, React, React Hooks and Reactstrap UI Kit

Now UI Kit React is a free Bootstrap 4, React, React Hooks and Reactstrap UI Kit provided for free by Invision and Creative Tim. It is a beautiful cross-platform UI kit featuring over 50 elements and 3 templates.

Creative Tim 132 Dec 19, 2022
Ts-next-chakra-motion-kit - Starter kit with Next.js, Chakra-UI, Framer-Motion in Typescript

Typescript Next.js Chakra-UI Framer-Motion Starter Kit Start with a powerful tem

Alexandre Gossard 39 Oct 14, 2022
Start your development with a Free Tailwind CSS and NextJS UI Kit and Admin.

Notus NextJS A beautiful UI Kit and Admin for Tailwind CSS and NextJS. Start your development with a Free Tailwind CSS and NextJS UI Kit and Admin. Le

Creative Tim 827 Dec 31, 2022
Fluent Blocks makes it easy to use Fluent & UI Kit designs in app development.

Fluent Blocks makes it easy to use Fluent & UI Kit designs in app development. This repository provides a set of packages developers can use to build

Office Developer 19 Nov 22, 2022
Small ES6 Native Speed jQuery for Svelte, Vue3, React, Angular, and WEB

sQuery This is a Very Small ES6 Native Speed jQuery for Svelte, Vue3, React, Angular, and WEB. Are you fed up with the modern js frameworks? But you'r

Exis 2 Sep 24, 2022
Micro frontend template for starter using qiankun + Vite + TypeScript + Vue3 + React.js + Svelte πŸš€

Micro frontend template for starter using qiankun + Vite + TypeScript + Vue3 + React.js + Svelte ??

Yuga Sun 28 Dec 20, 2022
Secure boilerplate for Electron app based on Vite. TypeScript + Vue/React/Angular/Svelte/Vanilla

Vite Electron Builder Boilerplate Vite+Electron = ?? This is a template for secure electron applications. Written following the latest safety requirem

Alex Kozack 1.5k Jan 4, 2023
A tiny (258 bytes) state manager for React/RN/Preact/Vue/Svelte with many atomic tree-shakable stores

Nano Stores A tiny state manager for React, React Native, Preact, Vue, Svelte, and vanilla JS. It uses many atomic stores and direct manipulation. Sma

Nano Stores 2.2k Dec 28, 2022
IngredientRecipeFinder - Web app built with react using Edamam API to find any recipe for an ingredient search with nutrition filters (high protein, low carb,etc)

Ingredient Recipe Finder Web app This web app built with the use of Edamam API allows you to find any type of recipe for a specific ingredient you are

null 1 Jan 4, 2022