4 WORK FILE

In this example below you will see how to do a 4 WORK FILE with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by efferon, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright efferon ©
  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>4 WORK FILE </title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

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

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/efferon/4-work-file-jqZPOP */
#canvas1, #canvas2 {
  border-style: solid;
  border-width: 1px;
}

/*Downloaded from https://www.codeseek.co/efferon/4-work-file-jqZPOP */
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)
  }
}

Comments