Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas class="webgl"></canvas>
<div class="input-group">
	<label for="armAngle">Move arms</label>
	<input type="range" min="0" max="90" data-input id="armAngle">
</div>
              
            
!

CSS

              
                * {
	box-sizing: border-box;
}

body {
	margin: 0;
	min-height: 100vh;
	font-family: 'Helvetica', sans-serif;
	accent-color: deeppink;
}

.input-group {
	position: absolute;
	top: 0;
	left: 0;
	padding: 1rem;
	color: white;
	
	> * {
		display: block;
	}
	
	> * + * {
		margin-top: 0.5rem;
	}
}
              
            
!

JS

              
                const canvas = document.querySelector('.webgl')
const input = document.querySelector('[data-input]')

const scene = new THREE.Scene()

const degreesToRadians = (degrees) => {
	return degrees * (Math.PI / 180)
}

const sizes = {
  width: window.innerWidth,
  height: window.innerHeight
}

/**
 * Renderer
 */
const renderer = new THREE.WebGLRenderer({ canvas })

const render = () => {
	renderer.setSize(sizes.width, sizes.height)
	renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
	renderer.render(scene, camera)
}

// Camera
const camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height, 0.1, 1000)
camera.position.z = 5
scene.add(camera)

window.addEventListener('resize', () =>
{
    // Update sizes
    sizes.width = window.innerWidth
    sizes.height = window.innerHeight

    // Update camera
    camera.aspect = sizes.width / sizes.height
    camera.updateProjectionMatrix()

    // Update renderer
    render(renderer)
})

// Material
const material = new THREE.MeshLambertMaterial({ color: 0xffffff })

// Lighting
const lightAmbient = new THREE.AmbientLight(0x9eaeff, 0.5)
scene.add(lightAmbient)

const lightDirectional = new THREE.DirectionalLight(0xffffff, 0.6)
scene.add(lightDirectional)

// Move the light source towards us
lightDirectional.position.set(5, 5, 5)

// Figure
class Figure {
	constructor(params) {
		this.params = {
			x: 0,
			y: -1,
			z: 0,
			ry: 0,
			angle: 0,
			...params
		}
		
		// Create group and add to scene
		this.group = new THREE.Group()
		scene.add(this.group)
		
		// Position according to params
		this.group.position.x = this.params.x
		this.group.position.y = this.params.y
		this.group.position.z = this.params.z
		this.group.rotation.y = this.params.ry
		
		this.arms = []
	}
	
	createBody() {
		const geometry = new THREE.BoxGeometry(1, 1.5, 1)
		const body = new THREE.Mesh(geometry, material)
		this.group.add(body)
	}
	
	createHead() {
		const geometry = new THREE.BoxGeometry(1.4, 1.4, 1.4)
		const head = new THREE.Mesh(geometry, material)
		this.group.add(head)
		
		head.position.y = 1.65
	}
	
	createArms() {
		const geometry = new THREE.BoxGeometry(0.25, 1, 0.25)
			
		let i
		
		for(i = 0; i < 2; i++) {
			const arm = new THREE.Mesh(geometry, material)
			const m = i % 2 === 0 ? 1 : -1
			
			this.group.add(arm)
			this.arms.push(arm)
			arm.position.x = m * 0.8
			arm.position.y = 0.17
		}
	}
	
	moveArms() {
		this.arms.forEach((arm, i) => {
			const m = i % 2 === 0 ? 1 : -1
			arm.rotation.z = degreesToRadians(this.params.angle * m)
		})
	}
	
	init() {
		this.createBody()
		this.createHead()
		this.createArms()
	}
}

const fig1 = new Figure()
fig1.init()
fig1.group.rotation.y = degreesToRadians(-15)

render()

input.addEventListener('input', (e) => {
	fig1.params.angle = e.target.value
	fig1.moveArms()
	render()
})
              
            
!
999px

Console