cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

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, html {
  margin: 0;
  background-color: black;
}

canvas {
  display: block;
}

@keyframes hue {
  to {
    filter: hue-rotate(360deg);
  }
}

.hueRotate {
  animation-timing-function: linear;
  animation-duration: 3s;
  animation-name: hue;
  animation-iteration-count: infinite;
}
            
          
!
            
              /*
  Clifford Attractor Explorer
  Johan Karlsson (DonKarlssonSan)
  x = sin(a * oldY) + c * cos(a * oldX)
  y = sin(b * oldX) + d * cos(b * oldY)
*/
var a = 2.2;
var b = 1.6;
var c = 1;
var d = 0.7;
var x, oldX;
var y, oldY;
var zoom = 150;
var nrOfPoints = 300000;
var rainbow = true;
var hueRotate = false;
var colorHue = 0;
var colorChangeRate = 200;
var colorRange = 180;
var colorStart = 0;
var alpha = 0.08;
var lightness = 50;
var width = 800;
var height = 600
var canvas;
var ctx;
var settings;
var colorSettings;

function init() {
  canvas = document.getElementById("canvas");
  ctx = canvas.getContext("2d");
  //ctx.shadowColor = colorHue;
  //ctx.shadowBlur = 20; 
  ctx.globalCompositeOperation = "lighter";
}

function randomParameters() {
  a = random(-5, 5);  
  b = random(-5, 5);  
  c = random(-5, 5);  
  d = random(-5, 5);  
  
  settings.setNumberValue("a", a);
  settings.setNumberValue("b", b);
  settings.setNumberValue("c", c);
  settings.setNumberValue("d", d);
}

function random(min, max) {
  return Math.random()*(max-min) + min;
}

function randomInt(min, max) {
  return Math.round(random(min, max));
}

function draw() {
  x = 0;
  y = 0;
  canvas.width = width;
  canvas.height = height;
  ctx.translate(width/2, height/2);
  
  var temp = ctx.globalCompositeOperation;
  ctx.globalCompositeOperation = "source-over";
  ctx.fillStyle = "black";
  ctx.fillRect(-width/2, -height/2, width, height);
  ctx.globalCompositeOperation = temp;
  
  ctx.fillStyle = `hsla(${colorHue}, 50%, ${lightness}%, ${alpha})`;
  ctx.fillStyle = "";
  for(var i = 0; i < nrOfPoints; i++) {
    if(rainbow) {
      colorHue = x*colorChangeRate + colorStart;
      ctx.fillStyle = `hsla(${colorHue}, 50%, ${lightness}%, ${alpha})`;
    }
    oldX = x;
    oldY = y;
    x = Math.sin(a * oldY) + c * Math.cos(a * oldX);
    y = Math.sin(b * oldX) + d * Math.cos(b * oldY);
    ctx.fillRect(x*zoom, y*zoom, 1, 1);
  }
}

function initSettings() {
  settings = QuickSettings.create(0, 0, "Attractor")
    .addRange("a", -5, 5, a, 0.1, val => a = val)
    .addRange("b", -5, 5, b, 0.1, val => b = val)
    .addRange("c", -5, 5, c, 0.1, val => c = val)
    .addRange("d", -5, 5, d, 0.1, val => d = val)
    .addRange("zoom", 1, 200, zoom, 1, val => zoom = val)
    .addRange("Number of points", 10000, 9000000, nrOfPoints, 10000, val => nrOfPoints = val)
    .addButton("Random a, b, c, d", randomParameters)
    .addButton("Draw", draw)
    .collapse(); 
  
  settings.hideControl("Color");

  var screenSettings = QuickSettings.create(width/2, 0, "Canvas Size")
    .addNumber("Width", 400, 2000, width, 100, val => width = val)
    .addNumber("Height", 400, 2000, height, 100, val => height = val)
    .addButton("Apply", draw)
    .collapse(); 

  var compositeOperations = ["lighter", "source-over", "xor", "screen"];

  colorSettings = QuickSettings.create(width - 100, 0, "Color")
    .addBoolean("Hue Rotate", hueRotate, toggleHueRotate)
    .addBoolean("Rainbow Colors", rainbow, toggleRainbow)
    .addRange("Color Change Rate", 1, 500, colorChangeRate, 1, val => colorChangeRate = val)
    .addRange("Color", 0, 360, colorHue, 1, val => colorHue = val)
    .addRange("Color Start", 0, 360, colorStart, 1, val => colorStart = val)
    .addRange("alpha", 0.01, 1, alpha, 0.01, val => alpha = val)
    .addRange("Lightness", 0, 100, lightness, 1, val => lightness = val)
    .addDropDown("globalCompositeOperation", compositeOperations, val => {
      ctx.globalCompositeOperation = val.value;
    })
    .collapse(); 
} 

function toggleRainbow() {
  rainbow = !rainbow;
  if(rainbow) {
    colorSettings.hideControl("Color");
    colorSettings.showControl("Color Start");
    colorSettings.showControl("Color Change Rate");
  } else {
    colorSettings.showControl("Color");
    colorSettings.hideControl("Color Start");
    colorSettings.hideControl("Color Change Rate");
  }
}

function toggleHueRotate() {
  canvas.classList.toggle("hueRotate");
}

init();
initSettings();
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