Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body>
  <div class="entire-contaier">
    <div class="draw-container" id="main">
      <div class="part" id="left">
        <div class="scrollable-draw">
          <canvas id="left-canvas"></canvas>
          <canvas class = 'draw-canvas' id="blue-square"></canvas>
          <canvas class = 'draw-canvas' id="red-circle"></canvas>
          <canvas class = "draw-canvas listen-canvas" id="listener"></canvs>
        </div>
      
      </div>
      <div class="layer-selection">
        <ul class="layer-list" id="layer-names">
          <li class ="unselected layer-button" id="red" draggable="true">Red</li>
          <li class ="unselected layer-button" id="blue" draggable="true">Blue</li>
        </ul>
      </div>
      <div class = "button-menu">
        <ul class="tools">
          <li class ="unselected tool-button" id="undo">Undo</li>
          <li class ="unselected tool-button" id="redo">Redo</li>
        </ul>
      </div>
    </div>
    <div class="tracker">
      <div class="image-tracker">
        <canvas class="top" id="tracker-top"></canvas>
        <canvas class="bottom" id="tracker-bottom"></canvas>
      </div>
      <div class="list-tracker">
        <ul>
        </ul>
      </div>
    </div>
  </div>
</body>
              
            
!

CSS

              
                #main{
  position: relative;
  background-color:#333;
  width:1000px;
  height:1000px;
}
.entire-container {
  position: relative;
  height: 700px;
  width: 800px;
}
.part{
  position:absolute;
  width:88%;
  height:85%;
  border:5px solid;
  border-color: #ddd;
}
#left {
float:left;
}
#right{
  left:50%;
}
.scrollable-draw{
  height:100%;
  width:100%;
  overflow-y:scroll;
  overflow-x:scroll;
  position:absolute;
}
.draw-canvas{
  position:absolute;
  left:0;
  top:0;
}
.listen-canvas{
  z-index: 1000;
}
.layer-selection{
  background-color:#666;
  float:right;
  height:86.5%;
  width: 12%;
}
.layer-list{
  list-style: none;
}
.layer-list li.unselected {
  text-align: center;
  background-color:#333;
  line-height: 60px;
  color:#999;
  -webkit-user-select:none;
  -moz-user-select:none;
  -ms-user-select:none;
  user-select:none;
  cursor:move;
}

.layer-list li.selected {
  text-align: center;
  line-height: 60px;
  -webkit-user-select:none;
  -moz-user-select:none;
  -ms-user-select:none;
  user-select:none;
  cursor:move;
  background-color:#999;
  color:#333;
}

.layer-list li:hover {
  background-color:#555;
  color:#ddd;
}

.layer-list li.over{
  border: 2px dashed #000;
}

.button-menu{
  background-color:#646;
  float:right;
  position:absolute;
  top: 88%;
  height:12%;
  width: 100%;
}

.tools{
  list-style:none;
}

.tools li {
  line-height: 60px;
  margin:12px;
  -webkit-user-select:none;
  -moz-user-select:none;
  -ms-user-select:none;
  user-select:none;
  cursor:pointer;
  float: left;
}

.tracker{
  float:right;
  position:absolute;
  top:0%;
  left:60%;
  border: 5px solid black;
  width: 30%;
  height: 200%;
}

.tracker .image-tracker{
  float:left;
}
.tracker .image-tracker .top {
  position:absolute;
  top:0;
  left:0;
  width: 50%;
  height: 50%;
  overflow-x:scroll;
  overflow-y:scroll;
  
}
.tracker .image-tracker .bottom {
  position:absolute;
  top:50%;
  left:0;
  width:50%;
  height:50%;
  overflow-x:scroll;
  overflow-y:scroll;
  
  
}
.tracker .list-tracker {
  float:right;
}
.tracker .list-tracker ul{
  list-style:none;
}
              
            
!

JS

              
                class BeforeAndAfter{
  constructor(){
    if (arguments.length === 6){
      this._x = arguments[0] - 2;
      this._y = arguments[1] - 2;
      this._width = arguments[2] + 2;
      this._height = arguments[3] + 2;
      this._beforeCtx = arguments[4];
      var afterCtx = arguments[5];
      this._before = this._beforeCtx.getImageData(this._x, this._y, this._width, this._height);
      var addData = afterCtx.getImageData(this._x, this._y, this._width, this._height);
      var offscreen = document.createElement("canvas");
      var osctx = offscreen.getContext("2d");
      offscreen.width = addData.width;
      offscreen.height = addData.height;
      osctx.putImageData(addData, 0, 0);
      var beforeTracker = document.getElementById("tracker-top");
      var btCtx = beforeTracker.getContext("2d");
      btCtx.putImageData(this._beforeCtx.getImageData(this._x, this._y, this._width, this._height), 0,0);
      
      this._beforeCtx.drawImage(offscreen, this._x, this._y);
      afterCtx.clearRect(this._x, this._y, this._width, this._height);
      this._after = this._beforeCtx.getImageData(this._x, this._y, this._width, this._height);
      var afterTracker = document.getElementById("tracker-bottom");
      var atCtx = afterTracker.getContext("2d");
      atCtx.putImageData(this._beforeCtx.getImageData(this._x, this._y, this._width, this._height), 0, 0);
    }
  }
  
}

class Command {
  constructor(){
    if (new.target === Command){
      throw new TypeError("Connot construct Command; it is abstract");
    }
    if (this.execute === undefined || this.undo == undefined){
      throw new TypeError("Must implement execute()");
    }
  }
}

class DrawCircle extends Command{
  
  constructor(pos, context){
    super();
    this._pos = pos;
    this._context = context; 
  }
  execute (){
      var posx = this._pos.x;
      var posy = this._pos.y;
      this._context.strokeStyle="#f00";
      this._context.globalCompositeOperation = "source-over";
      this._context.beginPath();
      this._context.arc(posx, posy, 10, 0, 2 * Math.PI);
      this._context.stroke();
  }
  
  undo(){
    var posx = this._pos.x;
    var posy = this._pos.y;
    this._context.globalCompositeOperation = "destination-out";
    this._context.beginPath();
    this._context.arc(posx, posy, 11, 0, Math.PI * 2, true);
    this._context.fill();
  }
  
}

class DrawSquare extends Command {
  constructor(pos, context){
    super();
    this._pos = pos;
    this._context = context;
  }
  execute(){
    var posx = this._pos.x;
    var posy = this._pos.y;
    this._context.strokeStyle="#00f";
    this._context.beginPath();
    this._context.rect(posx, posy, 15, 15);
    this._context.stroke();
  }
  undo(){
    var posx = this._pos.x;
    var posy = this._pos.y;
    this._context.clearRect(posx - 1, posy - 1, 18, 18);
  }
}

var commandStack = [];
var beforeAndAfterStack = [];
var lastLoc = -1;
var leftCanvas = document.getElementById('left-canvas');
var leftContext = leftCanvas.getContext('2d');
var leftImageObj = new Image();
var redCircleCanvas = document.getElementById('red-circle');
var blueSquareCanvas = document.getElementById('blue-square');
var selects = document.querySelectorAll("#layer-names li");
var layers = [redCircleCanvas, blueSquareCanvas];
initializeZIndices();
var blueSelect = document.getElementById('blue');
var redSelect = document.getElementById('red');
var undoButton = document.getElementById('undo');
var redoButton = document.getElementById('redo');
var listenerCanvas = document.getElementById("listener");
var selectedLayer = $("ul#layer-names li:first");
var dragLayer = null;
selectedLayer.addClass('selected');
leftImageObj.onload = function() {
  leftContext.clearRect(0, 0, leftCanvas.width, leftCanvas.height);
  leftCanvas.width = leftImageObj.width;
  leftCanvas.height = leftImageObj.height;
  leftContext.drawImage(leftImageObj, 0,0);
  redCircleCanvas.width = leftImageObj.width;
  redCircleCanvas.height = leftImageObj.height;
  blueSquareCanvas.width = leftImageObj.width;
  blueSquareCanvas.height = leftImageObj.height;
  listenerCanvas.width = leftImageObj.width;
  listenerCanvas.height= leftImageObj.height;
}

leftImageObj.src = 'https://static.pexels.com/photos/115045/pexels-photo-115045.jpeg';

/*redCircleCanvas.addEventListener('mousedown', circleEvent, false);
blueSquareCanvas.addEventListener('mousedown', squareEvent, false);*/

listenerCanvas.addEventListener('click', drawEvent, false);
redSelect.addEventListener('click', selectEvent, false);
blueSelect.addEventListener('click', selectEvent, false);
[].forEach.call(selects, function(select){
  select.addEventListener('dragstart', dragStartEvent, false);
  select.addEventListener('dragover', dragOverEvent, false);
  select.addEventListener('dragenter', dragEnterEvent, false);
  select.addEventListener('dragleave', dragLeaveEvent, false);
  select.addEventListener('drop', dragDropEvent, false);
  select.addEventListener('dragend', dragEndEvent, false);
});
undoButton.addEventListener('click', undoEvent, false);
redoButton.addEventListener('click', redoEvent, false);
//redSelect.addEventListener('dragstart', dragStartEvent, false);
//blueSelect.addEventListener('dragstart', dragStartEvent, false);
function drawEvent(e){
  var canvas = e.target;
  var pos = getMousePos(canvas, e);
  var drawLayer = selectedLayer.attr("id");
  if (drawLayer == "red"){
    var context = listenerCanvas.getContext('2d');
    var beforeContext = redCircleCanvas.getContext('2d');
    var dc = new DrawCircle(pos, context);
    //commandStack.push(dc);
    dc.execute();
    var baa = new BeforeAndAfter(pos.x-9, pos.y-9, 22, 22, beforeContext, context);
    beforeAndAfterStack.push(baa);
    lastLoc++;
    //drawCircle(pos, context);
  } else if (drawLayer == "blue"){
    var context = listenerCanvas.getContext('2d');
    var beforeContext = blueSquareCanvas.getContext('2d');
    var ds = new DrawSquare(pos, context);
    //commandStack.push(ds);  
    ds.execute();
    var baa = new BeforeAndAfter(pos.x-4, pos.y-4, 20, 20, beforeContext, context);
    beforeAndAfterStack.push(baa);
    lastLoc++;
    //drawSquare(pos, context);
  }
  //console.log(commandStack[lastLoc]);
}

function selectEvent(e){
  var li = e.target;
  var liId = li.id;
  var slId = selectedLayer.attr("id");
  if (liId !== slId){
    $(li).addClass('selected');
    $(li).removeClass('unselected');
    selectedLayer.addClass('unselected');
    selectedLayer.removeClass('selected');
    selectedLayer = $(li);
  }
  //console.log(selectedLayer.attr("id"));
}
function dragStartEvent(e){
  this.style.opacity = '0.4';
  dragLayer = this;
  e.dataTransfer.effectAllowed = 'move';
  e.dataTransfer.setData('text/html', this.id);
}
function dragOverEvent(e){
  if (e.preventDefault) {
    e.preventDefault(); // Necessary. Allows us to drop.
  }

  e.dataTransfer.dropEffect = 'move';
}
function dragEnterEvent(e){
  this.classList.add('over');
}
function dragLeaveEvent(e){
  this.classList.remove('over');
}
function dragDropEvent(e){
  if (e.stopPropagation) {
    e.stopPropagation(); // stops the browser from redirecting.
  }

  if (dragLayer.id != this.id){
    var obj1 = document.getElementById(e.dataTransfer.getData('text/html'));
    var obj2 = document.getElementById(this.id);
    var index1 = $(obj1).index();
    var index2 = $(obj2).index();
    swapLayers(index1, index2);
    //console.log(obj1.id);
    //console.log(obj2.id);
    swapNodes(obj1, obj2);
  }

  return false;
}
function dragEndEvent(e){
  this.style.opacity = '1.0';
  [].forEach.call(selects, function (select) {
    select.classList.remove('over');
  });
  //console.log(selectedLayer.attr("id"));
  //console.log("drag event ended");
}


function drawCircle(pos, context){
  posx = pos.x;
  posy = pos.y;
  context.strokeStyle="#f00";
  context.beginPath();
  context.arc(posx, posy, 10, 0, 2*Math.PI);
  context.stroke();
}
function drawSquare(pos, context){
  posx = pos.x;
  posy = pos.y;
  context.strokeStyle="#00f";
  context.beginPath();
  context.rect(posx, posy, 15, 15);
  context.stroke();
}
function getMousePos(canvas, evt){
	var rect = canvas.getBoundingClientRect();
    return {
        x: (evt.clientX - rect.left) / (rect.right - rect.left) * canvas.width,
        y: (evt.clientY - rect.top) / (rect.bottom - rect.top) * canvas.height
    };
}
function swapNodes(obj1, obj2){
  var parent2 = obj2.parentNode;
  var next2 = obj2.nextSibling;
  
  if (next2 == obj1){
    parent2.insertBefore(obj1, obj2);
  } else {
    obj1.parentNode.insertBefore(obj2, obj1);
    if (next2){
      parent2.insertBefore(obj1, next2);
    } else {
      parent2.appendChild(obj1);
    }
  }
}
function swapLayers(i1, i2){
  var temp = $(layers[i1]).css('z-index');
  //console.log(temp);
  //console.log($(layers[i2]).css('z-index'));
  $(layers[i1]).css('z-index', $(layers[i2]).css('z-index'));
  $(layers[i2]).css('z-index', temp);
  //console.log($(layers[i1]).css('z-index'));
  //console.log($(layers[i2]).css('z-index'));
}
function initializeZIndices(){
  var counter = 15 + layers.length;
  layers.forEach(function(layer){
    $(layer).css('z-index', counter.toString());
    counter -= 1;
  });
}

function undoEvent(e) {
  if (lastLoc < 0){
    return;
  }
  var baa = beforeAndAfterStack[lastLoc];
  //need to change the context area to before the action was made.
  var ctx = baa._beforeCtx;
  var imageData = baa._before;
  var x = baa._x;
  var y = baa._y;
  var width = baa._width;
  var height = baa._height;
  ctx.clearRect(x, y, width, height);
  ctx.putImageData(imageData, x, y);
  lastLoc--;
  //console.log(lastLoc);
}

function redoEvent(e){
  if (lastLoc < beforeAndAfterStack.length){
    if (beforeAndAfterStack[lastLoc + 1] !== undefined){
      //console.log("gets into the if statement");
      var baa = beforeAndAfterStack[lastLoc + 1];
      //need to change the context to the state after the
      //last action
      var ctx = baa._beforeCtx;
      var imageData = baa._after;
      var x = baa._x;
      var y = baa._y;
      var width = baa._width;
      var height = baa._height;
      ctx.clearRect(x, y, width, height);
      ctx.putImageData(imageData, x, y);
      lastLoc++;
    }
  }
}
/*function circleEvent(e){
  var canvas = e.target
  var context = canvas.getContext('2d');
  var pos = getMousePos(canvas, e);
  drawCircle(pos, context);
}
function squareEvent(e){
  var canvas = e.target;
  var context = canvas.getContext('2d');
  var pos = getMousePos(canvas, e);
  drawSquare(pos, context);
}
function bringToTopEvent(e){
  var li = e.target.id;
  console.log(li);
  if (li == "red"){
    redCircleCanvas.style.zIndex = 5;
    blueSquareCanvas.style.zIndex = 1;
  } else if (li == "blue") {
    blueSquareCanvas.style.zIndex = 5;
    redCircleCanvas.style.zIndex = 1;
  }
  console.log(redCircleCanvas.style.zIndex);
  console.log(blueSquareCanvas.style.zIndex);
}*/


              
            
!
999px

Console