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 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.

            
              <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
🕑 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