Guest User

gf

a guest
Feb 24th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 8.00 KB | None | 0 0
  1. <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  2. image 1
  3. <input id="fileupa1"  type="file" >
  4.  
  5. <div class="container">
  6. </div>
  7.  
  8. <style>
  9.  
  10. .container {
  11.     border: 1px solid #DDDDDD;
  12.     display: flex;
  13.     background: red;
  14.     width: 612px;
  15.     height: 612px;
  16. }
  17.  
  18. .container canvas {
  19.     display: block;
  20. }
  21.  
  22. .masked-img {
  23.     overflow: hidden;
  24.     margin-top: 50px;
  25.     position: relative;
  26. }
  27.  
  28. </style>
  29.  
  30.  
  31. <script>
  32.  
  33. var mask1;
  34.  
  35. $(document).ready(function() {
  36.  
  37.     var maskedImageUrla = "";
  38.  
  39.     $.getJSON('test.json', function(json) {
  40.         for (let layer of json.layers) {
  41.             if (layer.layers && layer.layers.length > 0) {
  42.                for (let temp of layer.layers) {
  43.                    if (temp.src) maskedImageUrla = temp.src;
  44.                     else if (temp.layers) {
  45.                         for (let tl of temp.layers)
  46.                             if (tl.src) maskedImageUrla = tl.src;
  47.                     }
  48.                 }
  49.             }
  50.         }
  51.  
  52.         var mask1 = $(".container").mask({
  53.             maskImageUrl: maskedImageUrla,
  54.             onMaskImageCreate: function(img) {
  55.  
  56.                 img.css({
  57.                     "position": "fixed",
  58.                     "left": 50, // get x value from json
  59.                     "top": 69   // get y value from json
  60.                 });
  61.             }
  62.         });
  63.  
  64.         fileupa1.onchange = function() {
  65.             mask1.loadImage(URL.createObjectURL(fileupa1.files[0]));
  66.         };
  67.     });
  68.  
  69. }); // end of document ready
  70.  
  71. // jq plugin for mask
  72. (function($) {
  73.     var JQmasks = [];
  74.     $.fn.mask = function(options) {
  75.         // This is the easiest way to have default options.
  76.         var settings = $.extend({
  77.             // These are the defaults.
  78.             maskImageUrl: undefined,
  79.             imageUrl: undefined,
  80.             scale: 1,
  81.             id: new Date().getUTCMilliseconds().toString(),
  82.             x: 0, // image start position
  83.             y: 0, // image start position
  84.             onMaskImageCreate: function(div) {},
  85.         }, options);
  86.  
  87.  
  88.         var container = $(this);
  89.  
  90.         let prevX = 0,
  91.             prevY = 0,
  92.             draggable = false,
  93.             img,
  94.             canvas,
  95.             context,
  96.             image,
  97.             timeout,
  98.             initImage = false,
  99.             startX = settings.x,
  100.             startY = settings.y,
  101.             div;
  102.  
  103.         container.mousePosition = function(event) {
  104.             return {
  105.                 x: event.pageX || event.offsetX,
  106.                 y: event.pageY || event.offsetY
  107.             };
  108.         }
  109.  
  110.         container.selected = function(ev) {
  111.             var pos = container.mousePosition(ev);
  112.             var item = $(".masked-img canvas").filter(function() {
  113.                 var offset = $(this).offset()
  114.                 var x = pos.x - offset.left;
  115.                 var y = pos.y - offset.top;
  116.                 var d = this.getContext('2d').getImageData(x, y, 1, 1).data;
  117.                 return d[0] > 0
  118.             });
  119.  
  120.             JQmasks.forEach(function(el) {
  121.                 var id = item.length > 0 ? $(item).attr("id") : "";
  122.                 if (el.id == id)
  123.                     el.item.enable();
  124.                 else el.item.disable();
  125.             });
  126.         };
  127.  
  128.         container.enable = function() {
  129.             draggable = true;
  130.             $(canvas).attr("active", "true");
  131.             div.css({
  132.                 "z-index": 2
  133.             });
  134.         }
  135.  
  136.         container.disable = function() {
  137.             draggable = false;
  138.             $(canvas).attr("active", "false");
  139.             div.css({
  140.                 "z-index": 1
  141.             });
  142.         }
  143.  
  144.         container.onDragStart = function(evt) {
  145.             container.selected(evt);
  146.             prevX = evt.clientX;
  147.             prevY = evt.clientY;
  148.         };
  149.  
  150.         container.getImagePosition = function() {
  151.             return {
  152.                 x: settings.x,
  153.                 y: settings.y,
  154.                 scale: settings.scale
  155.             };
  156.         };
  157.  
  158.         container.onDragOver = function(evt) {
  159.             if (draggable && $(canvas).attr("active") === "true") {
  160.                var x = settings.x + evt.clientX - prevX;
  161.                 var y = settings.y + evt.clientY - prevY;
  162.                 if (x == settings.x && y == settings.y)
  163.                    return; // position has not changed
  164.                 settings.x += evt.clientX - prevX;
  165.                 settings.y += evt.clientY - prevY;
  166.                 prevX = evt.clientX;
  167.                 prevY = evt.clientY;
  168.                 container.updateStyle();
  169.             }
  170.         };
  171.  
  172.         container.updateStyle = function() {
  173.             clearTimeout(timeout);
  174.             timeout = setTimeout(function() {
  175.                 context.clearRect(0, 0, canvas.width, canvas.height);
  176.                 context.beginPath();
  177.                 context.globalCompositeOperation = "source-over";
  178.                 image = new Image();
  179.                 image.setAttribute('crossOrigin', 'anonymous');
  180.                 image.src = settings.maskImageUrl;
  181.                 image.onload = function() {
  182.                     canvas.width = image.width;
  183.                     canvas.height = image.height;
  184.                     context.drawImage(image, 0, 0, image.width, image.height);
  185.                     div.css({
  186.                         "width": image.width,
  187.                         "height": image.height
  188.                     });
  189.                 };
  190.  
  191.                 img = new Image();
  192.                 img.src = settings.imageUrl;
  193.                 img.setAttribute('crossOrigin', 'anonymous');
  194.                 img.onload = function() {
  195.                     settings.x = settings.x == 0 && initImage ? (canvas.width - (img.width * settings.scale)) / 2 : settings.x;
  196.                     settings.y = settings.y == 0 && initImage ? (canvas.height - (img.height * settings.scale)) / 2 : settings.y;
  197.                     context.globalCompositeOperation = 'source-atop';
  198.                     context.drawImage(img, settings.x, settings.y, img.width * settings.scale, img.height * settings.scale);
  199.                     initImage = false;
  200.                 };
  201.             }, 0);
  202.         };
  203.  
  204.         // change the draggable image
  205.         container.loadImage = function(imageUrl) {
  206.             if (img)
  207.                 img.remove();
  208.             // reset the code.
  209.             settings.y = startY;
  210.             settings.x = startX;
  211.             prevX = prevY = 0;
  212.             settings.imageUrl = imageUrl;
  213.             initImage = true;
  214.             container.updateStyle();
  215.         };
  216.  
  217.         // change the masked Image
  218.         container.loadMaskImage = function(imageUrl, from) {
  219.             if (div)
  220.                 div.remove();
  221.             canvas = document.createElement("canvas");
  222.             context = canvas.getContext('2d');
  223.             canvas.setAttribute("draggable", "true");
  224.             canvas.setAttribute("id", settings.id);
  225.             settings.maskImageUrl = imageUrl;
  226.             div = $("<div/>", {
  227.                 "class": "masked-img"
  228.             }).append(canvas);
  229.  
  230.             div.find("canvas").on('touchstart mousedown', function(event) {
  231.                 if (event.handled === false) return;
  232.                 event.handled = true;
  233.                 container.onDragStart(event);
  234.             });
  235.  
  236.             div.find("canvas").on('touchend mouseup', function(event) {
  237.                 if (event.handled === false) return;
  238.                 event.handled = true;
  239.                 container.selected(event);
  240.             });
  241.  
  242.             div.find("canvas").bind("dragover", container.onDragOver);
  243.             container.append(div);
  244.             if (settings.onMaskImageCreate)
  245.                 settings.onMaskImageCreate(div);
  246.             container.loadImage(settings.imageUrl);
  247.         };
  248.         container.loadMaskImage(settings.maskImageUrl);
  249.         JQmasks.push({
  250.             item: container,
  251.             id: settings.id
  252.         })
  253.         return container;
  254.     };
  255. }(jQuery));          
  256.  
  257. </script>
Add Comment
Please, Sign In to add comment