Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ 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

Auto Save

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 class="wrapper">
	<canvas></canvas>
	<p class="instruction">Stroke with your finger / mouse. Or just watch.<p>
</div>

<div class="wrapper options">
	<label id="blustery-label" for="blustery"></label>
	<input type="range" min="10" max="300" id="blustery">
</div>
<div class="wrapper options">
	<label id="speed-label" for="speed"></label>
	<input type="range" min="0.1" max="5" id="speed" step=".1">
</div>
              
            
!

CSS

              
                $left: 27%;
$top: 33%;
body{ 
	background: #212121;
	color: whitesmoke;
	font-family: sans-serif;
}
canvas{
	background: linear-gradient(0deg, #e1a455, #e1a455 10%, #bf8642 30%, #8b6231 33%, transparent 34%),
		radial-gradient(circle at $left $top, #fefdf8, #faf1c2 20%, transparent 40%), 
		linear-gradient(0deg, #fefdf8 10%, #bf8642 33%, #fefdf8 34%, #fbf3c6 40%, #f9d98e);
	border: 1px solid #aa5;
}
.wrapper{
	margin: 0 auto;
	p{
		pointer-events: none;
		position: absolute;
		text-align: center;
		top: 0px;
		width: inherit;
		&.instruction{
			color: hsl(0,100%,35%);
			font-weight: bold;
		}
	}
}
.options{
	padding-top:10px;
	label{
		vertical-align: top;
	}
	input{
		margin-bottom:5px;
		width: 100%;
	}
}
p{
	
}
              
            
!

JS

              
                let canvas = document.querySelector('canvas'),
	ctx = canvas.getContext('2d'),
	width = 400,
	height = 500,
	gravity = .2,
	windshear = .2,
	windshearOld = windshear,
	windshearTarget = windshear,
	windChangeRate = 45,
	windMax = 2.5,
	windTick = 0,
	prevX = width * .7, 
	prevY = height * .08, 
	mouseX = prevX, 
	mouseY = prevY, 
	curX, curY, 
	nextX, nextY,
	followDelayFactor = .3, // smaller is looser follow
	dx, dy,
	lineAngleRadians,
	ribbonWidth = 8,
	ptA = {x:prevX, y:prevY}, ptB = {x:prevX, y:prevY}, 
	tempPtA, tempPtB, 
    upPointsHistory = [],
	downPointsHistory = [], 
	maxHistory = 18, // ie length of ribbon
	minHistory = 3,
	colourTick = 0,
	ribbonColour = 'hsla(0,100%,35%,.7)',
	ribbonEdgeColour = '#aa5',
	instructionHidden = false;

const matan2 = Math.atan2,
	  msqrt = Math.sqrt,
	  mcos = Math.cos,
	  msin = Math.sin,
	  mmin = Math.min,
	  mmax = Math.max,
	  mrandom = Math.random,
	  mPI = Math.PI;

canvas.width = width;
canvas.height = height;
ctx.strokeStyle = ribbonEdgeColour;
ctx.fillStyle = ribbonColour;

let wrappers = [... document.querySelectorAll('.wrapper')];
for(let i = 0; i < wrappers.length; i++){
	wrappers[i].style.width = width + 'px';
}

function update(){
	if( upPointsHistory.push({x:ptA.x, y:ptA.y}) > maxHistory){
		upPointsHistory.shift();
	}
	if( downPointsHistory.push({x:ptB.x, y:ptB.y}) > maxHistory){
		downPointsHistory.shift();
	}
	for(let loop = upPointsHistory.length, i = 0; i < loop; i++){
		// Apply gravity
		upPointsHistory[i].y += gravity * (loop - i);
		downPointsHistory[i].y += gravity * (loop - i);
		// Apply windshear
		upPointsHistory[i].x += windshear * (loop - i);
		downPointsHistory[i].x += windshear * (loop - i);
	}
	if(windTick++ === windChangeRate){
		changeWind();
	}
	windshear = easeInOutSine(windTick, windshearOld, windshearTarget, windChangeRate);
}
function changeWind(){
	windTick = 0;
	windshearOld = windshearTarget;
	windshearTarget = mrandom() * windMax - (windMax * .5);
}
function draw(){
	ctx.clearRect(0, 0, width, height);
	dx = (mouseX-prevX) * followDelayFactor;
	dy = (mouseY-prevY) * followDelayFactor;
	
	// Variable gravity, based on mouse velocity
	// Faster movement lifts the ribbon
	gravity = msqrt(dx*dx + dy*dy).map(0,50, 2,-0.9);

	nextX = prevX + dx;
	nextY = prevY + dy;

	if(ribbonWidth > 0){
		lineAngleRadians = matan2(dy, dx);
		// points either side of line
		tempPtA = rotate_point(nextX, nextY-ribbonWidth, nextX, nextY, lineAngleRadians);
		ptA.x = tempPtA.x;
		ptA.y = tempPtA.y;
		tempPtB = rotate_point(nextX, nextY+ribbonWidth, nextX, nextY, lineAngleRadians);
		ptB.x = tempPtB.x;
		ptB.y = tempPtB.y;

		if(upPointsHistory.length > minHistory){
			let loopLength = upPointsHistory.length - 1;
			ctx.beginPath();
			ctx.moveTo(upPointsHistory[0].x, upPointsHistory[0].y);
			for(let i=0; i<loopLength; i++){
				ctx.lineTo(upPointsHistory[i].x, upPointsHistory[i].y);
			}
			for(let i=loopLength; i>=0; i--){
				ctx.lineTo(downPointsHistory[i].x, downPointsHistory[i].y);
			}
			ctx.stroke();
			ctx.fill();
		}
		prevX = nextX;
		prevY = nextY;
	}

}

function rotate_point(pointX, pointY, originX, originY, radians) {
	let distX = pointX - originX,
		distY = pointY - originY;
	return {
		x: mcos(radians) * distX - msin(radians) * distY + originX,
		y: msin(radians) * distX + mcos(radians) * distY + originY
	};
}
function getColour(){
	return 'hsla(' + (colourTick += .3) + ', 100%, 50%, .5)';
}
const easeInOutSine = function (t, b, c, d) {
	return c/2 * (1 - mcos(mPI*t/d)) + b;
}

// minIn, maxIn, minOut, maxOut
Number.prototype.map = function(a,b,c,d){
	return c+(d-c)*((this-a)/(b-a))
};

function animate(){
	update();
	draw();
	requestAnimationFrame(animate);
}
function setUpEvents(){
	function getMousePos(canvas, e) {
		let rect = canvas.getBoundingClientRect();
		return {
			x: e.clientX - rect.left,
			y: e.clientY - rect.top
		};
	}
	
	/* TOUCH EVENTS */
	canvas.addEventListener('touchstart', function(e){
		e.preventDefault();
		let touchobj = e.changedTouches[0];
		mouseX = touchobj.pageX;
		mouseY = touchobj.pageY;
		hideInstructions();
	}, false);
	canvas.addEventListener('touchmove', function(e){
		e.preventDefault();
		let touchobj = e.changedTouches[0];
		mouseX = touchobj.pageX;
		mouseY = touchobj.pageY;
	}, false);
	
	
	canvas.addEventListener('mousemove', function(e) {
		let mousePos = getMousePos(canvas, e);
		mouseX = mousePos.x;
		mouseY = mousePos.y;
		hideInstructions();
	}, false);
}
function hideInstructions(){
	if(!instructionHidden){
		document.querySelector('.instruction').style.display = 'none';
		instructionHidden = true;
	}
}

setUpEvents();
animate();


/* UI */
let speedSelect = document.querySelector('#speed'),
	speedLabel = document.querySelector('#speed-label'),
	blusterySelect = document.querySelector('#blustery'),
	blusteryLabel = document.querySelector('#blustery-label');
speedSelect.value = windMax;
blusterySelect.value = windChangeRate;

speedSelect.oninput = function(e) {
	windMax = e.target.valueAsNumber;
	changeWind();
	setSpeedLabel();
}
blusterySelect.oninput = function(e) {
	windChangeRate = e.target.valueAsNumber;
	if(windTick >= windChangeRate){
		changeWind();
		windTick = 0;
	}
	setBlusteryLabel();
}

function setSpeedLabel(){
	speedLabel.innerHTML = 'Maximum wind speed (' + windMax + ')';
}
function setBlusteryLabel(){
	blusteryLabel.innerHTML = 'How changeable is the wind? Every ' + (windChangeRate/60).toPrecision(1) + 's';
}
setSpeedLabel();
setBlusteryLabel();
              
            
!
999px

Console