Skip to content

Instantly share code, notes, and snippets.

@valex
Created September 25, 2018 17:06
Show Gist options
  • Save valex/6b20d940d8a7a715f71db8d623f7b410 to your computer and use it in GitHub Desktop.
Save valex/6b20d940d8a7a715f71db8d623f7b410 to your computer and use it in GitHub Desktop.
Using the canvas as a texture + literally
<!DOCTYPE html>
<html>
<head>
<title>Example 10.09 - Canvas texture</title>
<link href="../libs96/literallycanvas-0.4.14/css/literallycanvas.css" rel="stylesheet">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/three.js/96/three.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/stats.js/r16/Stats.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.2/dat.gui.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-with-addons.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.js"></script>
<script type="text/javascript" src="../libs96/literallycanvas-0.4.14/js/literallycanvas.js"></script>
<style>
body {
/* set margin to 0 and overflow to hidden, to go fullscreen */
margin: 0;
overflow: hidden;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.fs-container {
width: 600px;
height: 380px;
margin: auto;
bottom: 20px;
position: absolute;
x: 0;
y: 0;
"
}
#canvas-output {
width: 600px;
height: 380px;
}
</style>
</head>
<body>
<div id="Stats-output">
</div>
<!-- Div which will hold the Output -->
<div class="fs-container">
<div id="canvas-output" style="float:left">
</div>
</div>
<!-- Div which will hold the Output -->
<div id="WebGL-output">
</div>
<!-- Javascript code that runs our Three.js examples -->
<script type="text/javascript">
var lc = LC.init(document.getElementById('canvas-output'), {
imageURLPrefix: '../libs96/literallycanvas-0.4.14/img',
primaryColor: 'lightgreen',
backgroundColor: '#000000',
});
// once everything is loaded, we run our Three.js stuff.
function init() {
var stats = initStats();
// create a scene, that will hold all our elements such as objects, cameras and lights.
var scene = new THREE.Scene();
// create a camera, which defines where we're looking at.
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
// create a render and set the size
var webGLRenderer = new THREE.WebGLRenderer();
webGLRenderer.setClearColor(new THREE.Color(0xbbbbbb));
webGLRenderer.setSize(window.innerWidth, window.innerHeight);
webGLRenderer.shadowMap.enabled = true;
webGLRenderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap
var cube = createMesh(new THREE.BoxGeometry(10, 10, 10));
cube.position.x = 0;
scene.add(cube);
// position and point the camera to the center of the scene
camera.position.x = 0;
camera.position.y = 12;
camera.position.z = 28;
camera.lookAt(new THREE.Vector3(0, 0, 0));
var ambiLight = new THREE.AmbientLight(0x141414);
scene.add(ambiLight);
var light = new THREE.DirectionalLight();
light.position.set(0, 30, 20);
scene.add(light);
// add the output of the renderer to the html element
document.getElementById("WebGL-output").appendChild(webGLRenderer.domElement);
// call the render function
var step = 0;
// var polyhedron = createMesh(new THREE.PolyhedronGeometry(vertices, faces, controls.radius, controls.detail));
// setup the control gui
var controls = new function () {
this.showTexture = true;
this.showCanvas = function () {
if (controls.showTexture) {
$('.fs-container').show();
} else {
$('.fs-container').hide();
}
};
};
var gui = new dat.GUI();
gui.add(controls, "showTexture").onChange(controls.showCanvas);
render();
function createMesh(geom) {
var canvasMap = new THREE.Texture($("#canvas-output canvas").get(1));
var mat = new THREE.MeshPhongMaterial();
mat.map = canvasMap;
console.log($("#canvas-output canvas").get(1));
var mesh = new THREE.Mesh(geom, mat);
return mesh;
}
function render() {
stats.update();
cube.rotation.y += 0.01;
cube.rotation.x += 0.01;
cube.material.map.needsUpdate = true;
// render using requestAnimationFrame
requestAnimationFrame(render);
webGLRenderer.render(scene, camera);
}
function initStats() {
var stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
// Align top-left
stats.domElement.style.position = 'absolute';
stats.domElement.style.left = '0px';
stats.domElement.style.top = '0px';
document.getElementById("Stats-output").appendChild(stats.domElement);
return stats;
}
}
window.onload = init;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment