<commit_before>/**
 * Created by ltischuk on 2/10/15.
 */
angular.module('ngcrop')
    .factory('CropCanvas',
    function CropCanvasFactory
        (CropSelection,
         ResultCanvas)
    {

      /**
       * Class to control the crop canvas which controls the main canvas and
       * @param canvasElement
       * @param maxLength
       * @param selectorWidth
       * @param selectorColor
       * @param outputImageFormat
       * @constructor
       */
      function CropCanvas(canvasElement, maxLength, selectorWidth, selectorColor, outputImageFormat){

        this._canvas = canvasElement;
        this._context = this._canvas[0].getContext('2d');
        this._resultCanvas = angular.isDefined(outputImageFormat) ? new ResultCanvas(outputImageFormat): new ResultCanvas('image/png');
        this._maxLength = maxLength;
        this._cropSelector = new CropSelection(maxLength);
        this._selectorLineWidth = selectorWidth;
        this._selectorColor = selectorColor;
        this._imgScale = 1;
        this.currentImg = undefined;
        this.onDrawResult = undefined;
        this._isSelecting = false;
        this._moveCorner = false;
        this._lastMouseX = 0;
        this._lastMouseY = 0;
        this._mouseX = 0;
        this._mouseY = 0;
        this._corner = 0;
        this.addEventHandlers();

      }

      /**
       * Handles the event for mouse down on the main canvas
       * @param e
       */
      CropCanvas.prototype.handleMouseDown = function(e){

        this._mouseX = e.offsetX;
        this._mouseY = e.offsetY;
        this._lastMouseX = this._mouseX;
        this._lastMouseY = this._mouseY;
        this._isSelecting = true;
        this._cropSelector.setCurrentCorner(this._mouseX, this._mouseY);
        if(this._cropSelector.isInMoveZone(this._mouseX, this._mouseY)){

          this._canvas[0].style.cursor = 'move';
          this._moveCorner = false;

        }
        else{
          this._canvas[0].style.cursor = 'crosshair';
          this._corner = this._cropSelector.nearestCorner(this._mouseX, this._mouseY);
          this._moveCorner = true;

        }

      }

      /**
       * Handles the even for mouse move on the main canvas
       * @param e
       */
      CropCanvas.prototype.handleMouseMove = function(e){

        this._mouseX = e.offsetX;
        this._mouseY = e.offsetY;

        if(!this._isSelecting){

          if(this._cropSelector.isInMoveZone(this._mouseX, this._mouseY)){
            this._canvas[0].style.cursor = 'move';
          }
          else{

            this._canvas[0].style.cursor = 'crosshair';
            this._moveCorner = true;

          }

        }else{
          this.drawImageOnCanvas();

          var xdiff = this._mouseX - this._lastMouseX;
          var ydiff = this._mouseY - this._lastMouseY;

          this._cropSelector.move(xdiff, ydiff, this._moveCorner, this._corner);
          this._lastMouseX = this._mouseX;
          this._lastMouseY = this._mouseY;
          this.drawImageOnCanvas();
        }

      }

      /**
       * Handles event handlers to the canvas variable
       * @param e
       */
      CropCanvas.prototype.handleMouseUp = function(e){
        this._isSelecting = false;
        this._moveCorner = false;
        this.drawImageOnCanvas();
        this._canvas[0].style.cursor = 'default';
        this.getCroppedImageData();
      }

      CropCanvas.prototype.addEventHandlers = function(){

        var self = this;

        this._canvas.on('mousedown', function(e) {

          self.handleMouseDown(e);

        });

        this._canvas.on('mouseup', function(e) {

          self.handleMouseUp(e);

        });

        this._canvas.on('mousemove', function(e) {

          self.handleMouseMove(e);

        });

        this._canvas.on('mouseout', function(e){

          self.handleMouseUp(e);

        });

      }

      /**
       * Process a new image
       * @param img
       * @param onDrawResult
       */
      CropCanvas.prototype.processNewImage = function(img, onDrawResult){

        this._imgScale = Math.min ((this._maxLength / img.width),(this._maxLength/ img.height), 1);
        this._canvas[0].width = img.width * this._imgScale;
        this._canvas[0].height = img.width * this._imgScale;
        this._cropSelector.initSelectorDimensions(this._canvas[0].width, this._canvas[0].height);
        this.currentImg = img;
        this.onDrawResult = onDrawResult;
        this.drawImageOnCanvas();
        this.getCroppedImageData();

      }

      /**<commit_msg>Fixed bug in cropCanvas misrepresented canvas height<commit_after>/**
 * Created by ltischuk on 2/10/15.
 */
angular.module('ngcrop')
    .factory('CropCanvas',
    function CropCanvasFactory
        (CropSelection,
         ResultCanvas)
    {

      /**
       * Class to control the crop canvas which controls the main canvas and
       * @param canvasElement
       * @param maxLength
       * @param selectorWidth
       * @param selectorColor
       * @param outputImageFormat
       * @constructor
       */
      function CropCanvas(canvasElement, maxLength, selectorWidth, selectorColor, outputImageFormat){

        this._canvas = canvasElement;
        this._context = this._canvas[0].getContext('2d');
        this._resultCanvas = angular.isDefined(outputImageFormat) ? new ResultCanvas(outputImageFormat): new ResultCanvas('image/png');
        this._maxLength = maxLength;
        this._cropSelector = new CropSelection(maxLength);
        this._selectorLineWidth = selectorWidth;
        this._selectorColor = selectorColor;
        this._imgScale = 1;
        this.currentImg = undefined;
        this.onDrawResult = undefined;
        this._isSelecting = false;
        this._moveCorner = false;
        this._lastMouseX = 0;
        this._lastMouseY = 0;
        this._mouseX = 0;
        this._mouseY = 0;
        this._corner = 0;
        this.addEventHandlers();

      }

      /**
       * Handles the event for mouse down on the main canvas
       * @param e
       */
      CropCanvas.prototype.handleMouseDown = function(e){

        this._mouseX = e.offsetX;
        this._mouseY = e.offsetY;
        this._lastMouseX = this._mouseX;
        this._lastMouseY = this._mouseY;
        this._isSelecting = true;
        this._cropSelector.setCurrentCorner(this._mouseX, this._mouseY);
        if(this._cropSelector.isInMoveZone(this._mouseX, this._mouseY)){

          this._canvas[0].style.cursor = 'move';
          this._moveCorner = false;

        }
        else{
          this._canvas[0].style.cursor = 'crosshair';
          this._corner = this._cropSelector.nearestCorner(this._mouseX, this._mouseY);
          this._moveCorner = true;

        }

      }

      /**
       * Handles the even for mouse move on the main canvas
       * @param e
       */
      CropCanvas.prototype.handleMouseMove = function(e){

        this._mouseX = e.offsetX;
        this._mouseY = e.offsetY;

        if(!this._isSelecting){

          if(this._cropSelector.isInMoveZone(this._mouseX, this._mouseY)){
            this._canvas[0].style.cursor = 'move';
          }
          else{

            this._canvas[0].style.cursor = 'crosshair';
            this._moveCorner = true;

          }

        }else{
          this.drawImageOnCanvas();

          var xdiff = this._mouseX - this._lastMouseX;
          var ydiff = this._mouseY - this._lastMouseY;

          this._cropSelector.move(xdiff, ydiff, this._moveCorner, this._corner);
          this._lastMouseX = this._mouseX;
          this._lastMouseY = this._mouseY;
          this.drawImageOnCanvas();
        }

      }

      /**
       * Handles event handlers to the canvas variable
       * @param e
       */
      CropCanvas.prototype.handleMouseUp = function(e){
        this._isSelecting = false;
        this._moveCorner = false;
        this.drawImageOnCanvas();
        this._canvas[0].style.cursor = 'default';
        this.getCroppedImageData();
      }

      CropCanvas.prototype.addEventHandlers = function(){

        var self = this;

        this._canvas.on('mousedown', function(e) {

          self.handleMouseDown(e);

        });

        this._canvas.on('mouseup', function(e) {

          self.handleMouseUp(e);

        });

        this._canvas.on('mousemove', function(e) {

          self.handleMouseMove(e);

        });

        this._canvas.on('mouseout', function(e){

          self.handleMouseUp(e);

        });

      }

      /**
       * Process a new image
       * @param img
       * @param onDrawResult
       */
      CropCanvas.prototype.processNewImage = function(img, onDrawResult){

        this._imgScale = Math.min ((this._maxLength / img.width),(this._maxLength/ img.height), 1);
        this._canvas[0].width = img.width * this._imgScale;
        this._canvas[0].height = img.height * this._imgScale;
        this._cropSelector.initSelectorDimensions(this._canvas[0].width, this._canvas[0].height);
        this.currentImg = img;
        this.onDrawResult = onDrawResult;
        this.drawImageOnCanvas();
        this.getCroppedImageData();

      }

      /**
{'fraction_non_alphanumeric': 0.11271613044429854, 'fraction_numerical': 0.005033924272269643, 'mean_word_length': 2.047349116372124, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '889416', 'n_tokens_mistral': 2729, 'n_tokens_neox': 2608, 'n_words': 476}