ChristopherKRad

ChristopherKRad

Having some difficulty adding three.js to a Phoenix project

I’m having some difficulty adding the npm package “three” to my phx project.

Specifically, I am getting blank screen on the /3D route that I have set, however, I am getting the hello word. The div seems to be recognized, but I think there may be something wrong with esbuild?

I’ve tried vendoring three.js using the minified link here with no luck either: https://raw.githubusercontent.com/mrdoob/three.js/dev/build/three.min.js

My current config can be found in the image attached.

I am more familiar with Phx Backend than front end, but would still consider myself a beginner - especially as it relates to adding JS deps.

Any help is appreciated :heart:

First 10 of 12 Posts Switch mode

03juan

03juan

Hi Christopher, welcome to the forum.

The screen grab is too low res to see anything, it would be better if you posted the relevant code directly as text.

ChristopherKRad

ChristopherKRad OP

oops! You’re right.

Let me try that again

ChristopherKRad

ChristopherKRad OP

Updated the image

ChrisYammine

ChrisYammine

it looks like your func createThreeJSScene is never called, have you tried invoking it?

ChristopherKRad

ChristopherKRad OP

I ended up vendoring three js in assets/vendor/three.min.js

Then I created a Hook in my app.js as follows:

import * as THREE from "../vendor/three.min.js";

window.THREE = THREE;

let Hooks = {};
Hooks.ThreeInit = {
  mounted() {
    if (this.el.dataset.initialized === "false") {
      this.initThreeScene();
      this.el.dataset.initialized = "true";
    }
  },
  initThreeScene() {
    // Initialize your three.js scene here, using `THREE` object.
    // For example:
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
    const renderer = new THREE.WebGLRenderer();

    renderer.setSize(window.innerWidth, window.innerHeight);
    this.el.appendChild(renderer.domElement);

    const geometry = new THREE.BoxGeometry();
    const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
    const cube = new THREE.Mesh(geometry, material);

    scene.add(cube);
    camera.position.z = 5;

    const animate = function () {
      requestAnimationFrame(animate);

      cube.rotation.x += 0.01;
      cube.rotation.y += 0.01;

      renderer.render(scene, camera);
    };

    animate();
  }
};

let liveSocket = new LiveSocket("/live", Socket, {
  params: {_csrf_token: csrfToken},
  hooks: Hooks,
  dom: {
    onBeforeElUpdated(from, to) {
      if (from._x_dataStack) {
        window.Alpine.clone(from, to)
      }
    }
  }
})

//other Alpine js stuff here

// connect if there are any LiveViews on the page
liveSocket.connect()

Then tried calling the phx-hook in my three_js_live.ex as follows:

defmodule PortfolioWeb.ThreeJSLive do
  use Phoenix.LiveView

  def mount(_params, _session, socket) do
    {:ok, socket}
  end



  def render(assigns) do
    ~L"""
    <h1>Hello, World!</h1>
    <div id="three-container" phx-hook="ThreeInit"></div>
    """
  end
end

At the moment, the only thing that renders is the Hello World

03juan

03juan

This won’t succeed because it returns undefined when the attribute is missing.

However you also will need to add phx-update="ignore" to the element, otherwise your scene will be removed on the next update from the server.

DOM patching & temporary assigns

The “ignore” behaviour is frequently used when you need to integrate with another JS library. Note only the element contents are ignored, its attributes can still be updated.

ChristopherKRad

ChristopherKRad OP

So, I’ve reduce the mounted() it down to:

mounted() {
    this.initThreeScene();
},

added the following to the

:

defmodule PortfolioWeb.ThreeJSLive do
  use Phoenix.LiveView

  def mount(_params, _session, socket) do
    {:ok, socket}
  end



  def render(assigns) do
    ~L"""
    <h1>Hello, World!</h1>
    <div id="three-container" phx-hook="ThreeInit" phx-update="ignore"></div>
    """
  end
end

Still no cube unfortunately.

03juan

03juan

Import from three.module.js

I’m curious what version of Phoenix you’re using, since you’re rendering the view with a ~L leex sigil instead of ~H? Did you not get something like this error during the asset build pipeline?

[WARNING] Import "Scene" will always be undefined because the file "vendor/three.min.js" has no exports [import-is-undefined]

    js/app.js:14:28:
      14 |     const scene = new THREE.Scene();
         |                             ~~~~~
ChristopherKRad

ChristopherKRad OP

I’m running 0.17.5 Live view & 1.6.13 Phoenix.

I do not see such an error on my side.

Is it possible for me to view the project you’ve created so that I can figure out what went wrong on my end?

03juan

03juan

Here’s a link to my github repo 03juan/three_cube

My previous answer was actually misleading, sorry about that. I actually got the error after installing the three module from npm into the vendor directory, and then importing from ../vendor/node_modules/three/build/three.min.js, not directly from ../vendor/three.min.js. I assumed that the error would also show in the second case but didn’t check before posting.

Have a look at app.js in the repo. In its current state importing from node_modules I get the error when building the asset, and there is no cube. However using the import in the second line does show the cube, and the console only shows the warning:
Scripts "build/three.js" and "build/three.min.js" are deprecated with r150+, and will be removed with r160. Please use ES Modules or alternatives

I tried to build the bundle with esbuild 0.14.29 used in Phoenix 1.6.13 and that also works when importing three.min.js directly from vendor but not from within node_modules.

Not sure why it works with one but not the other. My guess is something to do with it being a inside node package, but don’t have enough experience with js bundling to really know. :thinking:

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement