css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <canvas id="canvas" height="300" width="300"></canvas>
            
          
!
            
              body {
  background: #000;
  text-align: center;
}

* {
  margin: 0;
  padding: 0;
}

canvas {
  margin: 0 auto;
  width: 300px;
}

            
          
!
            
              const precision = 8
const PI2 = 6.283185307179586

const power = (base, exponent) => {
	let val = base
	let i = 0

	while (i < exponent - 1) {
		val *= base
		i += 1
	}

	return val
}

const factorial = n => {
	let val = n

	while (n > 2) {
		val *= n - 1
		n -= 1
	}

	return val
}

const series = (x, offset, precision) => {
	// Constrain x to PI * 2 so that we don't need
	// to add more precision as x increases
	x %= PI2

	// Count the steps with `n`
	let n = 0

	// Set the impulse force
	let val = x

	// When calculating Cosine, contrain the value
	// to `1` (no initial impulse)
	if (offset === 2) {
		val = 1
	}

	// Until we reach our precision...
	while (n < precision) {
		// Calculate the step: [3, 5, 7, ...], [2, 4, 8, ...]
		const step = offset + (n * 2)

		// Calculate the sum for the current step in the series
		const pow = power(x, step)
		const fact = factorial(step)
		const sum = pow / fact

		// Apply the sum to the value as either an
		// Impulse Force or Restorative Force
		if (n % 2) {
			val += sum
		} else {
			val -= sum
		}

		n += 1
	}

	// Return the value after reaching our desired precision
	return val
}

const sin = x => {
	const sineOffset = 3
	const value = series(x, sineOffset, precision)
	return value

	// Example:
	//     return x -
	//         (power(x, 3) / factorial(3)) +
	//         (power(x, 5) / factorial(5)) -
	//         (power(x, 7) / factorial(7)) +
	//         (power(x, 9) / factorial(9)) -
	//         (power(x, 11) / factorial(11)) +
	//         (power(x, 13) / factorial(13)) -
	//         (power(x, 15) / factorial(15)) +
	//         (power(x, 17) / factorial(17)) -
	//         (power(x, 19) / factorial(19))
}

const cos = x => {
	const cosineOffset = 2
	return series(x, cosineOffset, precision)
}

const canvas = document.getElementsByTagName('canvas')[0]
canvas.width = 300
canvas.height = 300
const ctx = canvas.getContext('2d')
ctx.lineWidth = 3

const x = canvas.width / 2
const y = canvas.height / 2

const mag = 100
const rate = 0.3
let t = 0

ctx.fillStyle = 'RGBA(0, 0, 0, 0.1)'
ctx.strokeStyle = 'RGBA(255, 255, 0, 1)'

const draw = () => {
	ctx.fillRect(0, 0, canvas.width, canvas.height)
	const px1 = x + (sin(t) * mag)
	const py1 = y + (cos(t) * mag)

	t += rate

	const px2 = x + (sin(t) * mag)
	const py2 = y + (cos(t) * mag)

	ctx.beginPath()
	ctx.moveTo(px1, py1)
	ctx.lineTo(px2, py2)
	ctx.stroke()
	ctx.closePath()
}

setInterval(draw, 40)

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console