HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div id="container"></div>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/61062/minion_texture.jpg" id="texture_minion" crossOrigin="anonymous">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/61062/minion_spec_texture.jpg" id="texture_spec_minion" crossOrigin="anonymous">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/61062/minion_bump_texture.jpg" id="texture_bump_minion" crossOrigin="anonymous">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/61062/minion_yellow_texture.jpg" id="texture_yellow_minion" crossOrigin="anonymous">
*{
margin:0;
padding:0;
box-sizing:border-box;
}
html,body{
width:100%;
height:100%;
}
body{
overflow:hidden;
background:#fff;
font-family: 'Lato', sans-serif;
font-size: 80%;
}
img{
display:none;
position:absolute;
}
#info{
position:absolute;
width:100%;
bottom:50px;
text-align:center;
}
var container;
var camera, camera_helper, scene, controls;
var raycaster = new THREE.Raycaster();
var renderer;
var clock = new THREE.Clock();
var time = 0;
var duration = 100;
var keyframes = 4;
var interpolation = duration / keyframes;
var currentKeyframe = 0;
var lastKeyframe = 0;
var animOffset = 1;
var radius = 600;
var theta = 0;
var prevTime = Date.now();
var lamp_light, light, fake_light;
var mouseX = 0,
mouseY = 0;
var animation = {
};
var hair;
var mesh, circle, controller_animation, helper;
var morph_logic;
var banane;
var floor;
var texture1;
var tv_screen, tv_context;
var å = {
ready: {
count: 0
},
current_skeleton: 'default',
keymap: {
37: false,
38: false,
39: false,
40: false
},
mouse: {
x: 0,
y: 0,
z: 0.5
},
models: {
minion: "https://s3-us-west-2.amazonaws.com/s.cdpn.io/61062/minion.json"
},
targetList: []
}
var yellow_image = document.getElementById('texture_yellow_minion');
var minion_yellow_texture = new THREE.Texture(yellow_image);
minion_yellow_texture.minFilter = THREE.LinearFilter;
minion_yellow_texture.needsUpdate = true;
var minion_image = document.getElementById('texture_minion');
var minion_texture = new THREE.Texture(minion_image);
minion_texture.minFilter = THREE.LinearFilter;
minion_texture.needsUpdate = true;
var minion_spec_image = document.getElementById('texture_spec_minion');
var minion_spec_texture = new THREE.Texture(minion_spec_image);
minion_spec_texture.minFilter = THREE.LinearFilter;
minion_spec_texture.needsUpdate = true;
var minion_bump_image = document.getElementById('texture_bump_minion');
var minion_bump_texture = new THREE.Texture(minion_bump_image);
minion_bump_texture.minFilter = THREE.LinearFilter;
minion_bump_texture.needsUpdate = true;
//___________________________________________
function getRandomColor() {
var letters = '0123433333000ccc'.split('');
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
//___________________________________________ KICK IT OFF
init();
animate();
//___________________________________________ INIT
function init() {
container = document.getElementById('container');
camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, .1, 10000);
camera.position.x = 0;
camera.position.y = 0;
camera.position.z = 5;
camera.update = function(time) {
}
camera.move_direction = 1;
scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0xffffff, 0.0015);
scene.add(camera);
var material = new THREE.MeshBasicMaterial({
color: 0x0000ff,
transparent: true,
opacity: 0
});
var radius = 50;
var segments = 32;
var circleGeometry = new THREE.CircleGeometry(radius, segments);
camera_helper = new THREE.Mesh(circleGeometry, material);
scene.add(camera_helper);
camera_helper.add(camera);
camera_helper.visible = true;
camera.position.set(50, 20, 50);
//___________________________________________ CONTROLS
controls = new THREE.OrbitControls(camera);
controls.damping = 0.2;
controls.addEventListener('change', render);
camera.lookAt(new THREE.Vector3(0, 5, 0));
controls.target = new THREE.Vector3(0, 5, 0);
controls.minDistance = 50;
controls.maxDistance = 160;
controls.minPolarAngle = 20 * Math.PI / 180;
controls.maxPolarAngle = 90 * Math.PI / 180;
controls.minAzimuthAngle = -30 * Math.PI / 180;
controls.maxAzimuthAngle = 30 * Math.PI / 180;
//___________________________________________ HEMI
var ambient = new THREE.AmbientLight(0xffffff);
scene.add(ambient);
//___________________________________________ FAKE LIGHT
fake_light = new THREE.SpotLight(0xffffff, .5);
fake_light.position.set(0, 50, 120);
fake_light.castShadow = true;
//
fake_light.shadowMapWidth = 1024 * 2;
fake_light.shadowMapHeight = 1024 * 2;
//
var d = 3500;
fake_light.shadowCameraLeft = -d;
fake_light.shadowCameraRight = d;
fake_light.shadowCameraTop = d * 2.8;
fake_light.shadowCameraBottom = -d;
fake_light.shadowCameraNear = 0.1;
//scene.add( fake_light );
light = new THREE.SpotLight(0xffffff, .1);
light.castShadow = true;
light.shadowMapWidth = 1024 * 2;
light.shadowMapHeight = 1024 * 2;
var d = 3500;
light.shadowCameraLeft = -d;
light.shadowCameraRight = d;
light.shadowCameraTop = d * 2.8;
light.shadowCameraBottom = -d;
light.shadowCameraNear = 0.1;
light.position.set(0, 100, 100);
light.target.updateMatrixWorld();
light.update = function(time) {
this.position = camera_helper.position;
//light.position.updateMatrixWorld();
this.target.position.set(camera_helper.position.x, 0, camera_helper.position.z);
this.target.updateMatrixWorld();
}
camera_helper.add(light);
corr_light = new THREE.SpotLight(0xffffff, .1);
corr_light.position.set(0, 12, 20);
corr_light.target.position.set(0, 6, 0);
corr_light.target.updateMatrixWorld();
//corr_light.castShadow = true;
scene.add(corr_light);
// RENDERER
renderer = new THREE.WebGLRenderer({
antialias: true,
transparent: true,
alpha: true
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
renderer.shadowMapEnabled = true;
//__________________________________ LOAD MODEL
var loader = new THREE.JSONLoader();
loader.load(å.models.minion, function(geometry, material) {
material.forEach(function(mat, index) {
mat.skinning = true;
});
var main_material = new THREE.MeshPhongMaterial({
shading: THREE.SmoothShading,
skinning: true,
shininess: 200,
map: minion_texture,
side: THREE.DoubleSide,
specularMap: minion_spec_texture,
bumpMap: minion_bump_texture,
bumpScale: .005,
});
var yellow_material = new THREE.MeshPhongMaterial({
shading: THREE.SmoothShading,
skinning: true,
shininess: 20,
map: minion_yellow_texture,
side: THREE.DoubleSide,
specularMap: minion_spec_texture
});
var transparent_material = new THREE.MeshPhongMaterial({
color: 0x3BB8E7,
shading: THREE.SmoothShading,
shininess: 200,
skinning: true,
side: THREE.DoubleSide,
transparent: true,
opacity: .2
});
var hair_material = new THREE.MeshPhongMaterial({
color: 0x000000,
shading: THREE.SmoothShading,
shininess: 200,
skinning: true,
side: THREE.DoubleSide
});
var minion_material = new THREE.MeshFaceMaterial([main_material, yellow_material, transparent_material, hair_material]);
var colors = [];
minion = new THREE.SkinnedMesh(geometry, minion_material);
minion.geometry.colors = colors;
minion.geometry.colorsNeedUpdate = true;
minion.position.set(0, 0, 0);
minion.scale.set(10, 10, 10);
minion.receiveShadow = true;
minion.castShadow = true;
minion.direction = 1;
minion.update = function(time) {
animation.jump.timeScale = 0;
//animation.walk.timeScale = 0;
minion_movement(å.keymap);
camera_helper.position.x = this.position.x;
camera_helper.position.z = this.position.z;
floor.position.x = this.position.x;
floor.position.z = this.position.z;
}
scene.add(minion);
//___________________________________________ Animation
animation.jump = new THREE.Animation(minion, geometry.animation[1]);
animation.jump.play();
animation.jump.timeScale = 1;
animation.walk = new THREE.Animation(minion, geometry.animation[0]);
animation.walk.play();
animation.walk.timeScale = 5;
helper = new THREE.SkeletonHelper(minion);
helper.material.linewidth = 1;
helper.visible = false;
scene.add(helper);
});
//___________________________________________ FLOOR
var plane_geo = new THREE.PlaneBufferGeometry(100, 100, 32);
var plane_mat = new THREE.MeshBasicMaterial({
color: 0xffffff,
side: THREE.DoubleSide
})
floor = new THREE.Mesh(plane_geo, plane_mat);
floor.rotation.x = -90 * Math.PI / 180;
floor.receiveShadow = true;
scene.add(floor);
}
// __________________________________ Only for Skinned Mesh Animation
function ensureLoop(animation) {
//animation.hierarchy.shift();
for (var i = 0; i < animation.hierarchy.length; i++) {
var bone = animation.hierarchy[i];
var first = animation.hierarchy[0];
var last = animation.hierarchy[animation.hierarchy.length - 1];
last.pos = first.pos;
last.rot = first.rot;
last.scl = first.scl;
}
}
function boneLookAt(bone, p) {
var target = new THREE.Vector3(
p.x - bone.matrixWorld.elements[12],
p.y - bone.matrixWorld.elements[13],
p.z - bone.matrixWorld.elements[14]
).normalize();
var v = new THREE.Vector3(0, 0, 1);
var q = new THREE.Quaternion().setFromUnitVectors(v, target);
q.x += .1;
bone.quaternion.copy(q);
}
//___________________________________________ Event in Space
window.addEventListener('resize', onWindowResize, false);
window.addEventListener('keydown',keymapper,false);
window.addEventListener('keyup',keymapper,false);
function keymapper(e) {
controls.enabled = false;
e.preventDefault(e);
e = e || event; // to deal with IE
å.keymap[e.keyCode] = e.type == 'keydown';
}
function minion_movement(keymap) {
for (key in keymap) {
if (keymap[key]) {
switch (key) {
//SPACE
case '32':
animation.walk.timeScale = (0);
animation.jump.timeScale = (5);
å.current_skeleton = 'jump';
minion.direction = 1;
break;
//Forwards
case '38':
minion.translateZ(0.3);
animation.walk.timeScale = (5);
å.current_skeleton = 'walk';
minion.direction = 1;
break;
//Backwards
case '40':
minion.translateZ(-0.3);
animation.walk.timeScale = (-5);
å.current_skeleton = 'walk';
minion.direction = -1;
break;
//left
case '37':
minion.rotation.y += .05;
camera_helper.rotation.y += .05;
å.current_skeleton = 'walk';
break;
//right
case '39':
minion.rotation.y -= .05;
camera_helper.rotation.y -= .05;
å.current_skeleton = 'walk';
break;
}
}
}
}
//___________________________________________ click
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
//___________________________________________ RENDER
var eye_options = {
current: {
top: 0,
bottom: 0
},
state: 'bored',
switchState: function() {
var states = ['close', 'bored', 'angry', 'open', 'complete_closed'];
return states[Math.floor(Math.random() * states.length)];
},
speed: 2,
direction: {
top: 1,
bottom: 1
},
stops: {
close: {
top: 65,
bottom: 65
}
}
}
var current_eye = 0;
function eye_movement(mesh, point) {
var private_point = point.clone();
private_point.x += camera_helper.position.x;
private_point.y += camera_helper.position.y;
private_point.z += camera_helper.position.z;
boneLookAt(mesh.skeleton.bones[4], private_point);
switch (eye_options.state) {
case 'close':
eye_options.current.top += (eye_options.speed * eye_options.direction.top);
eye_options.current.bottom += (eye_options.speed * eye_options.direction.bottom);
if (eye_options.current.top > eye_options.stops.close.top || eye_options.current.top < 0) {
eye_options.direction.top *= (-1);
}
if (eye_options.current.bottom > eye_options.stops.close.bottom || eye_options.current.bottom < 0) {
eye_options.direction.bottom *= (-1);
}
mesh.skeleton.bones[6].rotation.x = eye_options.current.top * Math.PI / 180;
mesh.skeleton.bones[7].rotation.x = -eye_options.current.bottom * Math.PI / 180;
break;
case 'bored':
mesh.skeleton.bones[6].rotation.x = 60 * Math.PI / 180;
mesh.skeleton.bones[7].rotation.x = 0 * Math.PI / 180;
break;
case 'angry':
mesh.skeleton.bones[6].rotation.x = 55 * Math.PI / 180;
mesh.skeleton.bones[7].rotation.x = -55 * Math.PI / 180;
break;
case 'open':
mesh.skeleton.bones[6].rotation.x = 0 * Math.PI / 180;
mesh.skeleton.bones[7].rotation.x = -0 * Math.PI / 180;
break;
case 'complete_closed':
mesh.skeleton.bones[6].rotation.x = 65 * Math.PI / 180;
mesh.skeleton.bones[7].rotation.x = -65 * Math.PI / 180;
break;
}
}
$('body').click(function(e) {
eye_options.state = eye_options.switchState();
})
function animate() {
requestAnimationFrame(animate);
render();
}
var fps = 24;
var keyframes = 58;
var duration = keyframes / fps;
var interpolation = duration / keyframes;
function render(time) {
theta += 0.1;
var delta = clock.getDelta();
// update morph
if (typeof minion !== "undefined") {
var keyframe = Math.floor(time / interpolation) + animOffset;
if (helper !== undefined) {
helper.update();
}
THREE.AnimationHandler.update(interpolation);
minion.update(time);
camera.update(time);
light.update(time);
// ___________________________________________ LOOKAt Point
eye_movement(minion, camera.position);
//
var time = Date.now();
prevTime = time;
}
renderer.render(scene, camera);
}
Also see: Tab Triggers