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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <div><canvas id="canvas1" width=300 height=300></canvas><canvas id="canvas2" width=300 height=300></canvas></div>
<div id="debug"></div>
              
            
!

CSS

              
                #canvas1, #canvas2 {
  border-style: solid;
  border-width: 1px;
}
              
            
!

JS

              
                function Rectangle(x, y, width, height) {
  this.x = x;
  this.y = y;
  this.width = width;
  this.height = height;
  this.imageData = {}; // place to store imageData if we decide to do so
  this.isTemporary = false;
}

function grayScale(data) {
  for (var i = 0; i < data.length; i += 4) {

    var avg = (data[i] + data[i + 1] + data[i + 2]) / 3;

    data[i] = avg; //red
    data[i + 1] = avg; //green
    data[i + 2] = avg; //blue
  }
  return data;
}

function Position(x, y) {
  this.x = x;
  this.y = y;
}

var debugMsg;

function displayDebug() {
  //since all variables are global no need to bas valued
  debugMsg = "Debug: <br>";
  debugMsg += "  sX: " + startPos.x + ", sY: " + startPos.y + "<br>";
  debugMsg += "  isMouseDown: " + isMouseDown + " <br>";
  debugMsg += "  eX: " + endPos.x + ", eY: " + endPos.y + "<br>";
  debugMsg += "  dX: " + deltaPos.x + ", dY: " + deltaPos.y + "<br>";
  document.getElementById("debug").innerHTML = debugMsg;
}

var canvas1 = document.getElementById("canvas1");
var canvas2 = document.getElementById("canvas2");
var ctx1 = canvas1.getContext("2d");
var ctx2 = canvas2.getContext("2d");

var img = new Image();
//altnernative images
//img.src  = "http://wonderfl.net/images/icon/e/ec/ec3c/ec3c37ba9594a7b47f1126b2561efd35df2251bfm";
//img.src  = "http://images.christmastimeclipart.com/images/2/1271716593176_1788/img_1271716593176_17881.jpg";
img.src = "http://www.elitespice.com/assets/1/14/SlideShowDimensionMain/Spice_Tiles1.png"
  // ID should be assigned by Database when client first loads image
  // additionally need to consider if project has multiple images.
img.id = "ABC21102015";

var rectangles = [] // for saving selected regions on canvas

var isMovingCanvas = false;
var isDrawingRegions = !isMovingCanvas;
var isMouseDown = false;
var isMouseDragging = false;
var startPos = new Position(0, 0) //for saving the start of a drag event which occurs onMouseDown
var endPos = new Position(0, 0) //for saving the end of a drag event which occurs onMouseUp
var deltaPos = new Position(0, 0) //for saving the distance the mouse moved with occurs onMouseUp

//Color two different canvases so its easy to see the data retreived from the first canvase
//  and put into the second canvas as a grey image.
ctx1.fillStyle = "yellow";
ctx2.fillStyle = "green";
ctx1.fillRect(0, 0, canvas1.width, canvas1.height);
ctx2.fillRect(0, 0, canvas2.width, canvas2.height);

canvas1.addEventListener("mousedown", handleMouseDown, false);
canvas1.addEventListener("mouseup", handleMouseUp, false);
canvas1.addEventListener("mousemove", handleMouseMove, false);

function handleMouseDown(e) {
  isMouseDown = true;
  startPos.x = e.offsetX;
  startPos.y = e.offsetY;
  
  displayDebug();
  // debugMsg = "DEBUG: <br>";
  // debugMsg += "  sX: " + startPos.x + ", sY: " + startPos.y + "<br>";
  // debugMsg += "  isMouseDown: " + isMouseDown + " <br>";
  // document.getElementById("debug").innerHTML = debugMsg;
}

function handleMouseMove(e) {

  endPos = getPos(e); //get mouse position
  if (isDrawingRegions) {
    //draw current box
    //draw all boxes
  }
  displayDebug();
  
}






function getPos(e) {
  return new Position(e.offsetX, e.offsetY);
}

function getSelectedRectangle(startPos, endPos) {
  //ensures we always start drawing rect from TopLeft corner 
  //    even if user drew the rectange from one of the other 
  //    directions BottomLeft BottomRight TopRight 
  var minX = (startPos.x > endPos.x) ? endPos.x : startPos.x;
  var minY = (startPos.y > endPos.y) ? endPos.y : startPos.y;
  var maxX = (startPos.x < endPos.x) ? endPos.x : startPos.x;
  var maxY = (startPos.y < endPos.y) ? endPos.y : startPos.y;

  startPos.x = minX;
  startPos.y = minY;
  endPos.x = maxX;
  endPos.y = maxY;

  deltaPos.x = endPos.x - startPos.x;
  deltaPos.y = endPos.y - startPos.y;

  return new Rectangle(startPos.x, startPos.y, deltaPos.x, deltaPos.y)
}

function handleMouseUp(e) {
  isMouseDown = false;
  endPos = getPos(e);
  //getSelectedRectangle returns a Rectangle object based on start and ending mouse Position
  rectangles.push(getSelectedRectangle(startPos, endPos));
  draw();

  displayDebug();
}

function handleMouseOut(e) {
  //Need to determine functionality whe mouse out
  // is it a mouse up event or do we continue dragging
  // is it different for drawing versus moving image
}

function draw() {
  for (i in rectangles) {
    data = ctx1.getImageData(rectangles[i].x, rectangles[i].y, rectangles[i].width, rectangles[i].height);
    grayScale(data.data);
    ctx2.strokeStyle = "red";
    ctx2.strokeRect(rectangles[i].x, rectangles[i].y, rectangles[i].width, rectangles[i].height);
    ctx2.putImageData(data, rectangles[i].x, rectangles[i].y)
  }
}
              
            
!
999px

Console