<commit_before>/*
 * Say Cheese!
 * <NAME>, 2012
 * http://leemach.in, http://new-bamboo.co.uk
 *
 * Minimal javascript library for integrating a webcam and snapshots into your app.
 *
 * Handles starting up the webcam and rendering the element, and also capturing shots
 * in a separate canvas element.
 *
 * Depends on video and canvas, and of course, getUserMedia. It's unlikely to work
 * on anything but the newest browsers.
 */

var SayCheese = (function($) {

  var SayCheese;

  /* Check for the existence of the userMedia feature. */
  function userMediaFeatureExists() {
    return 'getUserMedia' in navigator ||  'webkitGetUserMedia' in navigator;
  };

  function eventCoords(evt) {
     return { x: evt.offsetX || evt.layerX, y: evt.offsetY || evt.layerY };
  };

  SayCheese = function SayCheese(element) {
    if (userMediaFeatureExists()) {

      this.viewfinder = {},
      this.snapshots = [],
      this.canvas = null,
      this.context = null,
      this.video = null,
      this.events = {};

      this.element = document.querySelectorAll(element)[0];
      this.element.style.position = 'relative';
    } else {
      // should make this more graceful in future
      throw new Error("getUserMedia() is not supported in this browser");
    }

    return this;
  };


  /**
   * Reimplementing events? No. This is intended to make it easier to
   * replace jQuery with a different solution in future, should that
   * become necessary, and also minimises the impact of the framework
   * on the rest of the code.
   */
  SayCheese.prototype.on = function on(evt, handler) {
    return $(this).on(evt, handler);
  };

  SayCheese.prototype.off = function off(evt, handler) {
    return $(this).off(evt, handler);
  };

  SayCheese.prototype.trigger = function trigger(evt, data) {
    // bubbling up the DOM makes things go a bit crazy. This assumes
    // preventDefault
    return $(this).triggerHandler(evt, data);
  };

  /*
   * the getUserMedia function is different on Webkit browsers, so we have to
   * do a bit of faffing about to make sure we call the right one.
   */
  SayCheese.prototype.getUserMedia = function getUserMedia(success, error) {
    return (function() {
      // have to re-bind navigator because the context will be lost, and
      // we'll get illegal invocation errors.
      return (navigator.getUserMedia || navigator.webkitGetUserMedia).bind(navigator);
    })().call(this, { video: true }, success, error);
  };

  /*
   * Webkit behaves differently as regards getting a proper video URL. So we have
   * to have another check for it.
   */
  SayCheese.prototype.getStreamUrl = function getStreamUrl(stream) {
    url = (function() {
      return (window.URL || window.webkitURL);
    })();

    return (url && url.createObjectURL) ? url.createObjectURL(stream) : stream;
  };

  SayCheese.prototype.createVideo = function createVideo() {
    this.video = document.createElement('video');
    this.video.autoplay = true;
  };

  SayCheese.prototype.setupCanvas = function setupCanvas() {
    this.canvas = document.createElement('canvas');
    this.canvas.width = this.video.offsetWidth;
    this.canvas.height = this.video.offsetHeight;

    this.canvas.style.position = 'absolute';
    this.canvas.style.top = this.video.offsetTop;
    this.canvas.style.left = this.video.offsetLeft;

    this.context = this.canvas.getContext('2d');

    this.element.appendChild(this.canvas);
    this.initDefaultViewfinder();
    this.initDynamicViewfinder();

    // we're now all set up, so dispatch the ready event
    return this.trigger('start');
  };

  SayCheese.prototype.watermark = function watermark() {
    this.context.fillStyle = '#ee5f00';
    this.context.font = 'bold 32px Helvetica';

    x = this.video.offsetWidth - 100;
    y = this.video.offsetHeight - 15;

    this.context.fillText('demo', x, y);
  };


  /*
   * The default viewfinder is just the exact size of the video
   */
  SayCheese.prototype.initDefaultViewfinder = function initDefaultViewfinder() {
    return this.viewfinder = {
      startX: 0,
      startY: 0,
      width: this.video.offsetWidth,
      height: this.video.offsetHeight
    };
  };

  /*
   *  This viewfinder can be resized to capture select portions of the video stream
   */
  SayCheese.prototype.initDynamicViewfinder = function initDynamicViewfinder() {
    // track the dragging status when events are fired
    var isDragging = false,
        box = {};

    var start = function start(evt) {
      evt.preventDefault();
      var coords = eventCoords(evt);
      box.startX = coords.x;
      box.startY = coords.y;
      isDragging = true;
    }.bind(this);

    var stop = function stop(evt) {
      evt.preventDefault();
      isDragging = false;
      this.viewfinder = box;
      this.trigger('change');
    }.bind(this);

    var draw = function draw(evt) {
      evt.preventDefault();
      var coords = eventCoords(evt);
      if (isDragging) {
        width = coords.x - box.startX,
        height = coords.y - box.startY;

        box.width = Math.min(coords.x, box.startX) - Math.max(coords.x, box.startX),
        box.height = Math.min(coords.y, box.startY) - Max.max(coords.y, box.startY);
        
        // draw the shade
        this.context.globalCompositeOperation = 'xor';
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
        this.context.fillStyle = 'rgba(0, 0, 0, .8)';
        this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);

        // draw the window
        this.context.strokeStyle = 'rgba(255, 255, 255, .5)';<commit_msg>Fix typo. Math, not max.<commit_after>/*
 * Say Cheese!
 * <NAME>, 2012
 * http://leemach.in, http://new-bamboo.co.uk
 *
 * Minimal javascript library for integrating a webcam and snapshots into your app.
 *
 * Handles starting up the webcam and rendering the element, and also capturing shots
 * in a separate canvas element.
 *
 * Depends on video and canvas, and of course, getUserMedia. It's unlikely to work
 * on anything but the newest browsers.
 */

var SayCheese = (function($) {

  var SayCheese;

  /* Check for the existence of the userMedia feature. */
  function userMediaFeatureExists() {
    return 'getUserMedia' in navigator ||  'webkitGetUserMedia' in navigator;
  };

  function eventCoords(evt) {
     return { x: evt.offsetX || evt.layerX, y: evt.offsetY || evt.layerY };
  };

  SayCheese = function SayCheese(element) {
    if (userMediaFeatureExists()) {

      this.viewfinder = {},
      this.snapshots = [],
      this.canvas = null,
      this.context = null,
      this.video = null,
      this.events = {};

      this.element = document.querySelectorAll(element)[0];
      this.element.style.position = 'relative';
    } else {
      // should make this more graceful in future
      throw new Error("getUserMedia() is not supported in this browser");
    }

    return this;
  };


  /**
   * Reimplementing events? No. This is intended to make it easier to
   * replace jQuery with a different solution in future, should that
   * become necessary, and also minimises the impact of the framework
   * on the rest of the code.
   */
  SayCheese.prototype.on = function on(evt, handler) {
    return $(this).on(evt, handler);
  };

  SayCheese.prototype.off = function off(evt, handler) {
    return $(this).off(evt, handler);
  };

  SayCheese.prototype.trigger = function trigger(evt, data) {
    // bubbling up the DOM makes things go a bit crazy. This assumes
    // preventDefault
    return $(this).triggerHandler(evt, data);
  };

  /*
   * the getUserMedia function is different on Webkit browsers, so we have to
   * do a bit of faffing about to make sure we call the right one.
   */
  SayCheese.prototype.getUserMedia = function getUserMedia(success, error) {
    return (function() {
      // have to re-bind navigator because the context will be lost, and
      // we'll get illegal invocation errors.
      return (navigator.getUserMedia || navigator.webkitGetUserMedia).bind(navigator);
    })().call(this, { video: true }, success, error);
  };

  /*
   * Webkit behaves differently as regards getting a proper video URL. So we have
   * to have another check for it.
   */
  SayCheese.prototype.getStreamUrl = function getStreamUrl(stream) {
    url = (function() {
      return (window.URL || window.webkitURL);
    })();

    return (url && url.createObjectURL) ? url.createObjectURL(stream) : stream;
  };

  SayCheese.prototype.createVideo = function createVideo() {
    this.video = document.createElement('video');
    this.video.autoplay = true;
  };

  SayCheese.prototype.setupCanvas = function setupCanvas() {
    this.canvas = document.createElement('canvas');
    this.canvas.width = this.video.offsetWidth;
    this.canvas.height = this.video.offsetHeight;

    this.canvas.style.position = 'absolute';
    this.canvas.style.top = this.video.offsetTop;
    this.canvas.style.left = this.video.offsetLeft;

    this.context = this.canvas.getContext('2d');

    this.element.appendChild(this.canvas);
    this.initDefaultViewfinder();
    this.initDynamicViewfinder();

    // we're now all set up, so dispatch the ready event
    return this.trigger('start');
  };

  SayCheese.prototype.watermark = function watermark() {
    this.context.fillStyle = '#ee5f00';
    this.context.font = 'bold 32px Helvetica';

    x = this.video.offsetWidth - 100;
    y = this.video.offsetHeight - 15;

    this.context.fillText('demo', x, y);
  };


  /*
   * The default viewfinder is just the exact size of the video
   */
  SayCheese.prototype.initDefaultViewfinder = function initDefaultViewfinder() {
    return this.viewfinder = {
      startX: 0,
      startY: 0,
      width: this.video.offsetWidth,
      height: this.video.offsetHeight
    };
  };

  /*
   *  This viewfinder can be resized to capture select portions of the video stream
   */
  SayCheese.prototype.initDynamicViewfinder = function initDynamicViewfinder() {
    // track the dragging status when events are fired
    var isDragging = false,
        box = {};

    var start = function start(evt) {
      evt.preventDefault();
      var coords = eventCoords(evt);
      box.startX = coords.x;
      box.startY = coords.y;
      isDragging = true;
    }.bind(this);

    var stop = function stop(evt) {
      evt.preventDefault();
      isDragging = false;
      this.viewfinder = box;
      this.trigger('change');
    }.bind(this);

    var draw = function draw(evt) {
      evt.preventDefault();
      var coords = eventCoords(evt);
      if (isDragging) {
        width = coords.x - box.startX,
        height = coords.y - box.startY;

        box.width = Math.min(coords.x, box.startX) - Math.max(coords.x, box.startX),
        box.height = Math.min(coords.y, box.startY) - Math.max(coords.y, box.startY);
        
        // draw the shade
        this.context.globalCompositeOperation = 'xor';
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
        this.context.fillStyle = 'rgba(0, 0, 0, .8)';
        this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);

        // draw the window
        this.context.strokeStyle = 'rgba(255, 255, 255, .5)';
{'fraction_non_alphanumeric': 0.1196329948334224, 'fraction_numerical': 0.006413682522715126, 'mean_word_length': 2.8647160068846818, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4841432', 'n_tokens_mistral': 3484, 'n_tokens_neox': 3379, 'n_words': 943}