What's new

Welcome to the forum 👋, Visitor

To access the forum content and all our services, you must register or log in to the forum. Becoming a member of the forum is completely free.

How to insert js to WordPress post

Tubesec

New member
Joined
Dec 11, 2022
Messages
1
Reaction score
1
HTML Coins
0
I have a WordPress landing page. I'd like to have this animated smoke js code in the background on this landing page, but I'm not sure how.

>>> Codepen <<<

External scripts

Code:
// Smoke Class
// By Marco Biedermann https://codepen.io/marcobiedermann/pen/jympXQ
class Smoke {
    constructor(options) {
        const defaults = {
            width: window.innerWidth,
            height: window.innerHeight
        };

        Object.assign(this, options, defaults);
        this.onResize = this.onResize.bind(this);

        this.addEventListeners();
        this.init();
    }

    init() {
        const { width, height } = this;

        this.clock = new THREE.Clock();

        const renderer = (this.renderer = new THREE.WebGLRenderer());

        renderer.setSize(width, height);

        this.scene = new THREE.Scene();

        const meshGeometry = new THREE.BufferGeometry(200, 200, 200);
        const meshMaterial = new THREE.MeshLambertMaterial({
            color: 0xaa6666,
            wireframe: false
        });
        this.mesh = new THREE.Mesh(meshGeometry, meshMaterial);

        this.cubeSineDriver = 0;

        this.addCamera();
        this.addLights();
        this.addParticles();
        // this.addBackground();

        document.body.appendChild(renderer.domElement);
    }

    evolveSmoke(delta) {
        const { smokeParticles } = this;

        let smokeParticlesLength = smokeParticles.length;

        while (smokeParticlesLength--) {
            smokeParticles[smokeParticlesLength].rotation.z += delta * 0.2;
        }
    }

    addLights() {
        const { scene } = this;
        const light = new THREE.DirectionalLight(0xffffff, 0.75);

        light.position.set(-1, 0, 1);
        scene.add(light);
    }

    addCamera() {
        const { scene } = this;
        const camera = (this.camera = new THREE.PerspectiveCamera(
            75,
            this.width / this.height,
            1,
            10000
        ));

        camera.position.z = 1000;
        scene.add(camera);
    }

    addParticles() {
        const { scene } = this;
        const textureLoader = new THREE.TextureLoader();
        const smokeParticles = (this.smokeParticles = []);

        textureLoader.load(
            "https://raw.githubusercontent.com/marcobiedermann/playground/master/three.js/smoke-particles/dist/assets/images/clouds.png",
            (texture) => {
                const smokeMaterial = new THREE.MeshLambertMaterial({
                    color: 0xffffff,
                    map: texture,
                    transparent: true
                });
                smokeMaterial.map.minFilter = THREE.LinearFilter;
                const smokeGeometry = new THREE.PlaneBufferGeometry(300, 300);

                const smokeMeshes = [];
                let limit = 150;

                while (limit--) {
                    smokeMeshes[limit] = new THREE.Mesh(
                        smokeGeometry,
                        smokeMaterial
                    );
                    smokeMeshes[limit].position.set(
                        Math.random() * 500 - 250,
                        Math.random() * 500 - 250,
                        Math.random() * 1000 - 100
                    );
                    smokeMeshes[limit].rotation.z = Math.random() * 360;
                    smokeParticles.push(smokeMeshes[limit]);
                    scene.add(smokeMeshes[limit]);
                }
            }
        );
    }

    addBackground() {
        const { scene } = this;
        const textureLoader = new THREE.TextureLoader();
        const textGeometry = new THREE.PlaneBufferGeometry(600, 320);

        textureLoader.load("harry-potter-bg.png", (texture) => {
            const textMaterial = new THREE.MeshLambertMaterial({
                blending: THREE.AdditiveBlending,
                color: 0xffffff,
                map: texture,
                opacity: 1,
                transparent: true
            });
            textMaterial.map.minFilter = THREE.LinearFilter;
            const text = new THREE.Mesh(textGeometry, textMaterial);

            text.position.z = 800;
            scene.add(text);
        });
    }

    render() {
        const { mesh } = this;
        let { cubeSineDriver } = this;

        cubeSineDriver += 0.01;

        mesh.rotation.x += 0.005;
        mesh.rotation.y += 0.01;
        mesh.position.z = 100 + Math.sin(cubeSineDriver) * 500;

        this.renderer.render(this.scene, this.camera);
    }

    update() {
        this.evolveSmoke(this.clock.getDelta());
        this.render();

        requestAnimationFrame(this.update.bind(this));
    }

    onResize() {
        const { camera } = this;

        const windowWidth = window.innerWidth;
        const windowHeight = window.innerHeight;

        camera.aspect = windowWidth / windowHeight;
        camera.updateProjectionMatrix();

        this.renderer.setSize(windowWidth, windowHeight);
    }

    addEventListeners() {
        window.addEventListener("resize", this.onResize);
    }
}
const smoke = new Smoke();
smoke.update();
 

Theme customization system

You can customize some areas of the forum theme from this menu.

  • Wide/Narrow view

    You can control a structure that you can use to use your theme wide or narrow.

    Grid view forum list

    You can control the layout of the forum list in a grid or ordinary listing style structure.

    Picture grid mode

    You can control the structure where you can open/close images in the grid forum list.

    Close sidebar

    You can get rid of the crowded view in the forum by closing the sidebar.

    Fixed sidebar

    You can make it more useful and easier to access by pinning the sidebar.

    Close radius

    You can use the radius at the corners of the blocks according to your taste by closing/opening it.

  • Choose the color combination that reflects your taste
    Background images
    Color gradient backgrounds
Back