JSFiddle - React, Tailwind, and code Playground

by mattdlockyer

HTML

<canvas id="canvas" style=""></canvas>

JavaScript

var materials = [];
var textures = [];
var width = window.innerWidth;
var height = window.innerHeight / 2;

var cube;

var size = 257;

window.onmousemove = draw;

var pMouse = {x:0, y:0};

function draw(e) {
    var canvas = document.getElementById('canvas');
    var context = canvas.getContext('2d');
    context.beginPath();
      context.moveTo(pMouse.x, pMouse.y);
      context.lineTo(e.pageX, e.pageY);
      context.stroke();
    pMouse.x = e.pageX;
    pMouse.y = e.pageY;
}

function init() {
    scene = new THREE.Scene();
    camera = new THREE.PerspectiveCamera(70, width / height, 1, 1000);
    camera.position.y = 100;
    camera.position.z = 500;
    scene.add(camera);

    for (var i = 0; i < 6; i++) {
        var texture = new THREE.Texture(changeCanvas());
        textures.push(texture);

        var material = new THREE.MeshBasicMaterial({ map: texture });
        materials.push(material);
    }

    cube = new THREE.Mesh(new THREE.CubeGeometry(size, size, size, 1, 1, 1, materials), new THREE.MeshFaceMaterial());
    cube.position.y = 150;
    cube.rotation.y = 10;
    scene.add(cube);

    renderer = new THREE.WebGLRenderer();
    renderer.setSize(width, height);
    document.body.appendChild(renderer.domElement);
}

function animate() {
    requestAnimationFrame(animate);
    render();
}

function render() {
    for (var i = 0; i < textures.length; i++) {
        textures[i].needsUpdate = true;
    }
    
    cube.rotation.x += 0.02;

    renderer.render(scene, camera);
}

init();
animate();