I am facing an issue while trying to incorporate three.js with WordPress using webpack. The problem lies in the fact that the 3D cube is not showing up inside the canvas.
<div class="div" id="canvas"></div>
appears to be empty.
If I were to insert the app.js code within <script>
tags directly into page.php using a CDN source, it works without any problems (this method does not involve bundling).
However, I wanted to bundle it just like all the other scripts.
Here is the source code:
app.js:
import "./example.js";
example.js:
import * as THREE from 'three';
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);
$("#canvas").append(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;
function animate() {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
};
animate();
page.php:
<?php
get_header();
?>
<main id="home-primary" class="site-main">
<div class="div" id="canvas"></div>
</main><!-- #main -->
<?php
get_footer();
webpack.config.js:
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
var path = require('path');
// adjust these variables to match your project
const jsPath= './js';
const cssPath = './css';
const outputPath = 'dist';
const localDomain = 'http://localhost:8888/jakubtrz-portfolio';
const entryPoints = {
'app': jsPath + '/app.js',
'style': cssPath + '/main.scss',
};
// rest of the configuration file...
package.json:
"devDependencies": {
// dependencies listed here
},
// scripts section and additional configurations...
"dependencies": {
"three": "^0.137.5"
}
No errors are being displayed on the console.