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

              
                

    <div id='container'>

    </div>

    <img class="icons" id="linkedin" src="http://experiments.crma.ninja/repos/samuel-honigstein/images/linkedin.png">
    <img class="icons" id="facebook" src="http://experiments.crma.ninja/repos/samuel-honigstein/images/facebook.png">
    <img class="icons" id="twitter" src="http://experiments.crma.ninja/repos/samuel-honigstein/images/twitter.png">
    <img class="icons" id="plus" src="http://experiments.crma.ninja/repos/samuel-honigstein/images/plus.png">

<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r70/three.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenLite.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>

              
            
!

CSS

              
                html {
  height: 100%;
  overflow: hidden;
}


body {
  height: 100%;
  overflow: hidden;
  background-color: black;
  font-family: 'gotham', sans-serif;
}


.icons {
  position: absolute;
  width: 40px;
  height: 40px;
  bottom: 90px;
  cursor: pointer;
  z-index: 3000;
}


#facebook {
  right: 60px;
}


#twitter {
  right: 120px;
}

#linkedin {
  right: 180px;
}

#plus {
  right: 240px;
}


#container {
  width: 100%;
  height: 100%;
}

#container canvas {
  background-color: white;
}

              
            
!

JS

              
                class window.App


	container		: null

	stats 			: null

	camera 			: null

	scene 			: null

	renderer 		: null

	windowHalfX 	: null

	windowHalfY 	: null

	extrudeSettings : null

	geometry 		: null
  
	cameraLookatVector	 : null
  	
	constructor: ->

		fb = document.getElementById('facebook');
		twt = document.getElementById('twitter');
		lin = document.getElementById('linkedin');
		plus = document.getElementById('plus');

		fb.addEventListener('click', ()=>

		    window.open("https://www.facebook.com/Pierre.de.miel", '_blank');

		, false)

		twt.addEventListener('click', ()=>

		    window.open("https://twitter.com/Samsyyyy", '_blank');

		, false)

		lin.addEventListener('click', ()=>

		    window.open("https://www.linkedin.com/profile/view?id=182449324&trk=nav_responsive_tab_profile", '_blank');

		, false)


		plus.addEventListener('click', ()=>

		    window.open("http://experiments.crma.ninja/repos/samuel-honigstein/", '_blank');

		, false)

		@windowHalfX = window.innerWidth / 2;

		@windowHalfY = window.innerHeight / 2;

		@camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100000 );

		@scene = new THREE.Scene();
    
		@cameraLookatVector = new THREE.Vector3( 0, 0, -500 )

		@renderer = new THREE.WebGLRenderer({antialias : false});

		@renderer.setSize( window.innerWidth, window.innerHeight );
    
		@container = document.getElementById('container')

		@container.appendChild( @renderer.domElement );		

		window.addEventListener( 'resize', @onWindowResize, false );
    
		window.addEventListener( 'mousemove', @onDocumentMouseMove, false );
    
		LSystem = new window.LSystem(
                {size: {value:25, growth: 0.143333},angle: {value:-3787.3, growth: 0.05}},
                { size: {value:12.27}, angle: {value:4187.5}},
                "L : SYS\nS : F-[F-Y[S]]\nY : [|FF[-((>S]+Y]",10, @scene
            )

    
		@animate()

	###	@cameraLookatVector = new THREE.Vector3( 0, 0, -300 )
		###
		
	


	onDocumentMouseMove:(event)=>

		event.preventDefault();
		
		@camera.position.x = ( event.clientX / window.innerWidth ) * 1400 - 700;
		@camera.position.y = - ( event.clientY / window.innerHeight ) * 1400 + 700;

	animate: =>

		requestAnimationFrame( @animate );
    
		@camera.lookAt(@cameraLookatVector);
   
		@renderer.render( @scene, @camera );

				
	onWindowResize:=>


		@camera.aspect = window.innerWidth / window.innerHeight;

		@camera.updateProjectionMatrix();

		@renderer.setSize( window.innerWidth, window.innerHeight );

    

class window.LSystem

	rules : null

	params: null

	rules: null

	sensitivities : null

	SystemManager: null

	grow: {

			value: -3787.3
	}

	renderer: null

	geometry: null

	mat: null

	line : null

	scene : null

	constructor: (params, sensitivities, rules, iterations, scene) ->

		@rules = rules;

		@iterations = iterations

		@scene = scene

		@params = params

		@sensitivities = sensitivities

		textRules = @rules.split("\n").map (r) -> (r.replace(/\ /g, '')).split(':')

		ruleMap = Util.toObj(textRules)

		@systemManager = new SystemManager()

		@renderer = new Renderer()

		@systemManager.activate(@)

		elems = @systemManager.getInstructions()

		if elems

			@renderer.render(elems, @)

	 	@geometry = @renderer.geometry

	 	@systemManager.activate(@)

	 	elems = @systemManager.getInstructions()

	 	@renderer.render(elems, @) if elems

	 	@geometry = @renderer.geometry

	 	@mat = new THREE.LineBasicMaterial( { color: 0xffffff, linewidth: 0.0001, wireframe: true} )
	 	@mat.transparent = true;
	 	@mat.opacity = 0.11;

	 	@line = new THREE.Line( @geometry, @mat);

	 	@line.position.z = -500;


	 	@scene.add(@line);

	 	console.log(@mat)

		TweenLite.to(@params.angle, 80000, { growth: 100 ,ease: Linear.None, onUpdate:()=>

			@systemManager.activate(@)

			elems = @systemManager.getInstructions()

			@renderer.render(elems, @) if elems

			@geometry = @renderer.geometry

			@updateGeom();

		})

	updateGeom:() =>

		@line.geometry.verticesNeedUpdate = true;

		@line.geometry.vertices = @geometry.vertices

	isIsomorphicTo:(system) ->

			if (!system) then false else @rules == system.rules and @iterations == system.iterations


	merge: (system) ->
		_.extend(@, system) if system

class Renderer
	context:null
	g:null
	stack:[]
	isDrawing:false
	canvas: null
	geometry: null
	refX: 0
	refY: 0

	constructor: () ->

		
		@context = new RenderingContext()

	clearCanvas: =>
		

	reset: (system) =>

		@geometry = new THREE.Geometry();
		@clearCanvas()
		@context.reset(system)


	render: (elems, system) =>


		@isDrawing = true
		start = new Date
    
		@reset(system)

		refx = @context.state.x

		refy = @context.state.y

		@refX = @context.state.x
		@refY = @context.state.y
		#initialise lower-bounds
		[s,b] = [@context.state, @context.bounding]

		i = 0

		_.each elems, (e) =>

			@definitions[e](@context.state, system.params, @context,@geometry, @refX, @refY)

		@isDrawing = false

		# return (new Date - start)

	#this really *really* doesn't belong here.. but it requires a hell of a lot more
	#dereferences if I put it anywhere else. Considering it's looked up on the order
	#of hundreds of thousands of times, I'm leaving it here for the time being...
	definitions: (() =>
		[cos,sin,pi,min,max] = [Math.cos, Math.sin, Math.PI,Math.min,Math.max]
		len = ang = s = c = 0
		# expanded (explicit) for efficiency
		cloneState = (c) -> {
			x:              c.x
			y:              c.y
			orientation:    c.orientation
			stepAngle:      c.stepAngle
			stepSize:       c.stepSize
		}
		return {
		"F": (state, params, context, geometry, refX, refY) =>

			ang = ((state.orientation%360) / 180) * pi #todo - stop storing degrees?!

			state.x += cos(ang)*state.stepSize
			state.y += sin(ang)*state.stepSize
      
			geometry.vertices.push( new THREE.Vector3(state.x - refX, state.y - refY,0) );

		"+": (state) -> state.orientation += state.stepAngle
		"-": (state) -> state.orientation -= state.stepAngle
		"|": (state) -> state.orientation += 180

		"[": (state, params, context, geometry) ->
			context.stack.push(cloneState state);

		"]": (state, params, context, geometry,refX, refY) ->

			context.state = state = context.stack.pop();
			geometry.vertices.push( new THREE.Vector3(state.x - refX, state.y - refY,0) );

		"!": (state) -> state.stepAngle *= -1

		"(": (state, params) -> state.stepAngle *= (1 - params.angle.growth)

		")": (state, params) -> state.stepAngle *= (1 + params.angle.growth)

		"<": (state, params) -> state.stepSize *= (1 + params.size.growth);

		">": (state, params) -> state.stepSize *= (1 - params.size.growth);
		}
	)()

class RenderingContext
	initialised:false
	state:null
	bounding:null
	stack:[]
	constructor: () ->
    
    @state =
      x:              0
      y:              0
      orientation:    -90
      stepAngle:      0
      stepSize:       0

	reset: (system) =>

		@initialised = true
		@state =
			x:              0
			y:              0
			orientation:    -90
			stepAngle:      system.params.angle.value
			stepSize:       system.params.size.value

		@bounding = new Bounding
		@stack = []

#================================================================

class Bounding
	x1: Infinity
	y1: Infinity
	x2: -Infinity
	y2: -Infinity
	width: => (@x2-@x1)
	height: => (@y2-@y1)
	constrain: (x,y) ->
		@x1 = Math.max(@x1, 0)
		@y1 = Math.max(@y1, 0)
		@x2 = Math.min(@x2, x)
		@y2 = Math.min(@y2, y)

#================================================================

# =========================================
class CompiledSystem
	constructor: (@system, @elements) ->

# =========================================
class SystemCompiler
	_halt: false

	halt: -> @_halt = true;

	compile: (system) ->

		@_halt = false
		CHUNK_SIZE = 400000
		def = $.Deferred()
		def.notify(0) # zero progress

		textRules = system.rules.split("\n").map (r) -> (r.replace(/\ /g, '')).split(':')

		ruleMap = Util.toObj(textRules)
		seed = textRules[0][0] #choose first rule as system initialiser

		removeNonInstructions = (expr) -> expr.split('').filter((e) -> true if (Renderer.prototype.definitions[e]))



		# todo: this is absolutely horrifying. Sort it out.
		# note to any bypassers - this used to be a single reduce operation,
		# then I decided to make compilation interruptible (or I'll be crashing people's browsers...)
		# Sigh.
		expandChunk = (levelNum,levelExpr, acc, start, processed, count) =>
			while( processed < count )
				if (@._halt)
					def.reject()
					return
				else if (levelNum == 0)
					def.resolve(removeNonInstructions(levelExpr))
					return
				remaining = count - processed
				reachesEndOfLevel = remaining >= (levelExpr.length - start)
				if (reachesEndOfLevel) then remaining = levelExpr.length - start
				i = start
				end = start + remaining
				while ( i < end)
					symbol = levelExpr[i]
					acc += ruleMap[symbol] || symbol
					i++
				processed += remaining
				start += remaining
				if (reachesEndOfLevel)
					levelNum--
					levelExpr = acc
					acc = ''
					start = 0

			def.notify((system.iterations - levelNum) / system.iterations)
			setTimeout(( -> expandChunk(levelNum,levelExpr,acc,start,0,count)),0)

		expandChunk(system.iterations, seed, '', 0,0,CHUNK_SIZE)
		return def.promise()

# =========================================
class SystemManager
	compiler: new SystemCompiler
	stagedSystem: null # system pending compilation (replaces active when compiled)
	activeSystem: null
	compiledElements: null

	activate: (system) ->

		if (@promise and @stagedSystem?.isIsomorphicTo(system))
			@activeSystem.merge(system)
			@promise
		else if (@promise?.state() == 'pending')
			@compiler.halt()
			@promise.fail( => @_recompile(system))
		else @_recompile(system)




	_recompile: (system) ->
		@stagedSystem = system
		@promise = @compiler.compile(system)
		@promise.fail( => @stagedSystem = @activeSystem )
		@promise.pipe( (elements)  =>
			@activeSystem = system
			@compiledElements = elements
			return elements
		)

	getInstructions: =>
		return @compiledElements


  
class Util

  @log:(x) -> console.log(x)

  @control:(name) -> document.getElementById(name)

  @value: (name) => parseFloat(Util.stringvalue(name))

  @stringvalue: (name) -> Util.control(name).value

  @clone:(x) -> JSON.parse(JSON.stringify(x))

  @toObj:(kvPairs) ->

    obj = {}

    obj[k] = v for [k,v] in kvPairs

    return obj

  @map: (obj, fn) ->

    result = {}
    
    for key of obj then do ->
      
      result[key] = fn(obj[key], key)
      
    return result
    
  @merge: (a,b,c) -> $.extend(true, a,b,c)
  
  @round: (n,d) ->
    
    pow = Math.pow(10,d)
    
    Math.round(n*pow) / pow
    
  @time: (n,f) ->
    
    f = n if n instanceof Function
    
    s = new Date; f(); (new Date - s)
    

  @canvasWidth = (canvas) => canvas.width / (window.devicePixelRatio or 1)
  
  @canvasHeight = (canvas) => canvas.height / (window.devicePixelRatio or 1)
  



App = new window.App();


              
            
!
999px

Console