cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <div id="controls">
  <button id="moveUp">Move selected up</button>
  <button id="moveDown">Move selected down</button>
  <button id="moveToTop">Move selected to top</button>
  <button id="moveToBottom">Move selected to bottom</button>
  <button id="destroy">Destroy selected</button>
  <button id="download">Download</button>
</div>
<div id="concreteContainer"></div>
            
          
!
            
              // concrete.js downloads and documentation can be found at http://www.concretejs.com

function drawSceneCircles() {
  circles.forEach(function(circle) {
    drawSceneCircle(circle);
  });
  viewport.render();
}

function drawHitCircles() {
  circles.forEach(function(circle) {
    drawHitCircle(circle);
  });
}

function drawSceneCircle(config) {
  var scene = config.layer.scene,
      context = scene.context;

  scene.clear();
  context.save();
  context.beginPath();
  context.arc(config.x, config.y, 60, 0, Math.PI * 2, false);
  context.fillStyle = config.color;
  context.fill();

  if (config.selected) {
    context.strokeStyle = 'black';
    context.lineWidth = 6;
    context.stroke();
  }

  if (config.hovered) {
    context.strokeStyle = 'green';
    context.lineWidth = 2;
    context.stroke();
  }
  context.restore();
}

function drawHitCircle(config) {
  var hit = config.layer.hit,
      context = hit.context;

  hit.clear();
  context.save();
  context.beginPath();
  context.arc(config.x, config.y, 60, 0, Math.PI * 2, false);
  context.fillStyle = hit.getColorFromKey(config.key);
  context.fill();
  context.restore();
}

function getSelectedCircle() {
  var len = circles.length,
      n, circle;

  for (n = 0; n < len; n++) {
    circle = circles[n];
    if (circle.selected) {
      return circle;
    }
  }

  return null;
}

function destroyCircle(key) {
  var len = circles.length,
      n, circle;

  for (n = 0; n < len; n++) {
    circle = circles[n];
    if (circle.key === key) {
      circles.splice(n, 1);
      drawSceneCircles();
      drawHitCircles();
      return true;
    }
  }

  return false;
}

var concreteContainer = document.getElementById('concreteContainer');

// create wrapper
var viewport = new Concrete.Viewport({
  width: 400,
  height: 200,
  container: concreteContainer
});

// create layers
var layer1 = new Concrete.Layer();
var layer2 = new Concrete.Layer();
var layer3 = new Concrete.Layer();

// add layers
viewport.add(layer1).add(layer2).add(layer3);

// create a scene graph (you don't need a scene graph for Concrete, but in this
// example we will use one
var circles = [
  {
    x: viewport.width / 2,
    y: viewport.height * 2 / 3,
    hovered: false,
    selected: true,
    layer: layer1,
    color: 'red',
    key: 'red'
  }, {
    x: viewport.width * 2 / 5,
    y: viewport.height / 3,
    hovered: false,
    selected: false,
    layer: layer2,
    color: 'yellow',
    key: 'yellow'
  }, {
    x: viewport.width * 3 / 5,
    y: viewport.height / 3,
    hovered: false,
    selected: false,
    layer: layer3,
    color: 'orange',
    key: 'orange'
  }
];

// for convenience, make the circles array also a hash
circles.forEach(function(circle) {
  circles[circle.key] = circle;
});

// draw circles onto hit canvases for mouse detection
drawHitCircles();

// draw visible circles
drawSceneCircles();

// add concrete container handlers
concreteContainer.addEventListener('mousemove', _.throttle(function(evt) {
  var boundingRect = concreteContainer.getBoundingClientRect(),
      x = evt.clientX - boundingRect.left,
      y = evt.clientY - boundingRect.top,
      key = viewport.getIntersection(x, y),
      circle;

  // unhover all circles
  circles.forEach(function(circle) {
    circle.hovered = false;
  });

  if (key) {
    circles[key].hovered = true;
  }

  drawSceneCircles();
}, 50));

concreteContainer.addEventListener('click', function(evt) {
  var boundingRect = concreteContainer.getBoundingClientRect(),
      x = evt.clientX - boundingRect.left,
      y = evt.clientY - boundingRect.top,
      key = viewport.getIntersection(x, y),
      circle;

  // unhover all circles
  circles.forEach(function(circle) {
    circle.selected = false;
  });

  if (key) {
    circles[key].selected = true;
  }

  drawSceneCircles();
});

// add button handlers
document.getElementById('moveUp').addEventListener('click', function() {
  var circle = getSelectedCircle();

  if (circle) {
    circle.layer.moveUp();
    viewport.render();
  }
});

document.getElementById('moveDown').addEventListener('click', function() {
  var circle = getSelectedCircle();

  if (circle) {
    circle.layer.moveDown();
    viewport.render();
  }
});

document.getElementById('moveToTop').addEventListener('click', function() {
  var circle = getSelectedCircle();

  if (circle) {
    circle.layer.moveToTop();
    viewport.render();
  }
});

document.getElementById('moveToBottom').addEventListener('click', function() {
  var circle = getSelectedCircle();

  if (circle) {
    circle.layer.moveToBottom();
    viewport.render();
  }
});

document.getElementById('destroy').addEventListener('click', function() {
  var circle = getSelectedCircle();

  if (circle) {
    circle.layer.destroy();
    viewport.render();
  }
});

document.getElementById('download').addEventListener('click', function() {
  viewport.scene.download({
    fileName: 'three-circles.png'
  });
});
            
          
!
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