ayand04

plugin

Dec 26th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*!
  2. * Scribbly.js
  3. * @author  Ayan_Dey
  4. * @version 0.0.1
  5. */
  6.  
  7. (function (root, factory) {
  8.     if ( typeof define === 'function' && define.amd ) {
  9.         define([], factory(root));
  10.     } else if ( typeof exports === 'object' ) {
  11.         module.exports = factory(root);
  12.     } else {
  13.         root.Scribbly = factory(root);
  14.     }
  15. })(typeof global !== 'undefined' ? global : this.window || this.global, function (root) {
  16.  
  17.     'use strict';
  18.  
  19.     /**
  20.      * Variables
  21.      */
  22.     var board,
  23.         ctx,
  24.         opts,
  25.         isDrawing = false,
  26.         mousePos = [];
  27.  
  28.  
  29.     // Constructor
  30.     function Scribbly(options) {
  31.  
  32.         // Default settings
  33.         var defaults = {
  34.             canvas: "",
  35.             lineThickness: 2,
  36.             lineColor: "#000000"
  37.         };
  38.  
  39.         // extend config
  40.         opts = extend(defaults, options || {} );
  41.  
  42.         // initialize plugin
  43.         this.init();
  44.     }
  45.  
  46.  
  47.     /**
  48.      *  Public Methods
  49.      */
  50.  
  51.     Scribbly.prototype.init = function () {
  52.         // Get the canvas ready to draw
  53.         board = document.getElementById(opts.canvas);
  54.         ctx = board.getContext("2d");
  55.  
  56.         // Add mouse event listeners to canvas element
  57.         board.addEventListener("mousedown", press, false);
  58.         board.addEventListener("mousemove", drag, false);
  59.         board.addEventListener("mouseup", release);
  60.         board.addEventListener("mouseout", cancel, false);
  61.  
  62.         // Add touch event listeners to canvas element
  63.         board.addEventListener("touchstart", press, false);
  64.         board.addEventListener("touchmove", drag, false);
  65.         board.addEventListener("touchend", release, false);
  66.         board.addEventListener("touchcancel", cancel, false);
  67.  
  68.     };
  69.  
  70.     // Clear the canvas
  71.     Scribbly.prototype.clear = function() {
  72.         ctx.clearRect(0, 0, board.width, board.height);
  73.     };
  74.  
  75.  
  76.     /**
  77.      *  Private Methods
  78.      */
  79.  
  80.     /**
  81.      * Merge two or more objects. Returns a new object.
  82.      */
  83.     var extend = function () {
  84.  
  85.         // Variables
  86.         var extended = {};
  87.         var deep = false;
  88.         var i = 0;
  89.         var length = arguments.length;
  90.  
  91.         // Check if a deep merge
  92.         if ( Object.prototype.toString.call( arguments[0] ) === '[object Boolean]' ) {
  93.             deep = arguments[0];
  94.             i++;
  95.         }
  96.  
  97.         // Merge the object into the extended object
  98.         var merge = function (obj) {
  99.             for ( var prop in obj ) {
  100.                 if ( Object.prototype.hasOwnProperty.call( obj, prop ) ) {
  101.                     // If deep merge and property is an object, merge properties
  102.                     if ( deep && Object.prototype.toString.call(obj[prop]) === '[object Object]' ) {
  103.                         extended[prop] = extend( true, extended[prop], obj[prop] );
  104.                     } else {
  105.                         extended[prop] = obj[prop];
  106.                     }
  107.                 }
  108.             }
  109.         };
  110.  
  111.         // Loop through each object and conduct a merge
  112.         for ( ; i < length; i++ ) {
  113.             var obj = arguments[i];
  114.             merge(obj);
  115.         }
  116.  
  117.         return extended;
  118.  
  119.     };
  120.  
  121.     // Get the coordinates of the mouse click/ tap
  122.     var getMousePos = function(evt) {
  123.         var rect = board.getBoundingClientRect();
  124.         return {
  125.             x: evt.clientX - rect.left,
  126.             y: evt.clientY - rect.top
  127.         };
  128.     };
  129.  
  130.     // Mouse press/ touchstart event
  131.     var press = function(e) {
  132.         console.log('User clicked/tapped');
  133.         isDrawing = true;
  134.         mousePos.push([getMousePos(e).x, getMousePos(e).y]);
  135.         draw();
  136.     };
  137.  
  138.     // Mouse/ touch drag event
  139.     var drag = function(e) {
  140.         if(isDrawing) {
  141.             console.log('User dragging');
  142.            // mousePos.push([getMousePos(e).x, getMousePos(e).y]);
  143.         }
  144.     };
  145.  
  146.     // Mouse release/ touchend event
  147.     var release = function(e) {
  148.         console.log('User released click/touch');
  149.         isDrawing = false;
  150.     };
  151.  
  152.     // When mouse or touch goes out of the canvas
  153.     var cancel = function(e) {
  154.         console.log('Drawing cancelled');
  155.         isDrawing = false;
  156.     };
  157.  
  158.     var draw = function() {
  159.         ctx.lineJoin = "round";
  160.         ctx.lineWidth = opts.lineThickness;
  161.         ctx.strokeStyle = opts.lineColor;
  162.  
  163.         for(var i=0; i<mousePos.length; i++) {
  164.             ctx.beginPath();
  165.             ctx.moveTo(mousePos[i][0], mousePos[i][1]);
  166.             ctx.lineTo(mousePos[i][0], mousePos[i][1]);
  167.             ctx.closePath();
  168.             ctx.stroke();
  169.  
  170.             console.log(mousePos[i][0], mousePos[i][1]);
  171.         }
  172.     };
  173.  
  174.  
  175.     /**
  176.      * Public APIs
  177.      */
  178.  
  179.     return Scribbly;
  180.  
  181. });
Add Comment
Please, Sign In to add comment