cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmloctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              body
	background: #eee

canvas
	position: absolute
	top: 50%
	left: 50%
	transform: translate3d(-50%, -50%, 0)
	background: #fff
	box-shadow: 0 0 24px 8px rgba(0, 0, 0, 0.2)

            
          
!
            
              const {cos, PI, sin} = Math;

// Fun to change:
const SPEED = 1/540;
const CELLS = 14;
const LINE_LENGTH = 32;
const LINE_WIDTH = 32;
const PX = 600;

// Derived.
const PX_PER_CELL = PX / CELLS;

const canvas = document.createElement('canvas');
canvas.height = canvas.width = PX;
canvas.style.height = canvas.style.width = `${PX / 2}px`;
document.body.appendChild(canvas);

const ctx = canvas.getContext('2d');
ctx.lineWidth = LINE_WIDTH;
ctx.strokeStyle = '#222';

// Store the mouse position as state.
const mouse = {x: PX, y: PX};
function move (e) {
	const {clientX, clientY} = e;
	const {left, top} = canvas.getBoundingClientRect();
	mouse.x = (clientX - left) * 2;
	mouse.y = (clientY - top) * 2;
	e.preventDefault();
}
canvas.addEventListener('mousemove', move, false);
canvas.addEventListener('touchmove', e => move({
	...e.changedTouches[0],
	preventDefault: e.preventDefault.bind(e),
}), false);
canvas.addEventListener('click', move, false);

(function draw () {
	ctx.clearRect(0, 0, PX, PX);
	
	ctx.beginPath();
	for (let y = 0; y <= PX; y += PX_PER_CELL) {
		for (let x = 0; x <= PX; x += PX_PER_CELL) {
			const angle = Math.atan2(y - mouse.y, x - mouse.x) + PI/2;
			const dx = LINE_LENGTH/2 * cos(angle);
			const dy = LINE_LENGTH/2 * sin(angle);
			
			ctx.moveTo(x - dx, y - dy);
			ctx.lineTo(x + dx, y + dy);
		}
	}
	ctx.stroke();
	
	requestAnimationFrame(draw);
})();

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console