1"use strict"; 2(function (factory) { 3 if (typeof define === 'function' && define.amd) { 4 define(['jquery'], factory); 5 } else { 6 factory(jQuery); 7 } 8}(function ($) { 9 var cropbox = function(options, el){ 10 var el = el || $(options.imageBox), 11 obj = 12 { 13 state : {}, 14 ratio : 1, 15 options : options, 16 imageBox : el, 17 thumbBox : el.find(options.thumbBox), 18 spinner : el.find(options.spinner), 19 image : new Image(), 20 getDataURL: function () 21 { 22 var width = this.thumbBox.width(), 23 height = this.thumbBox.height(), 24 canvas = document.createElement("canvas"), 25 dim = el.css('background-position').split(' '), 26 size = el.css('background-size').split(' '), 27 dx = parseInt(dim[0]) - el.width()/2 + width/2, 28 dy = parseInt(dim[1]) - el.height()/2 + height/2, 29 dw = parseInt(size[0]), 30 dh = parseInt(size[1]), 31 sh = parseInt(this.image.height), 32 sw = parseInt(this.image.width); 33 34 canvas.width = width; 35 canvas.height = height; 36 var context = canvas.getContext("2d"); 37 context.drawImage(this.image, 0, 0, sw, sh, dx, dy, dw, dh); 38 var imageData = canvas.toDataURL('image/png'); 39 return imageData; 40 }, 41 getBlob: function() 42 { 43 var imageData = this.getDataURL(); 44 var b64 = imageData.replace('data:image/png;base64,',''); 45 var binary = atob(b64); 46 var array = []; 47 for (var i = 0; i < binary.length; i++) { 48 array.push(binary.charCodeAt(i)); 49 } 50 return new Blob([new Uint8Array(array)], {type: 'image/png'}); 51 }, 52 zoomIn: function () 53 { 54 this.ratio*=1.1; 55 setBackground(); 56 }, 57 zoomOut: function () 58 { 59 this.ratio*=0.9; 60 setBackground(); 61 } 62 }, 63 setBackground = function() 64 { 65 var w = parseInt(obj.image.width)*obj.ratio; 66 var h = parseInt(obj.image.height)*obj.ratio; 67 68 var pw = (el.width() - w) / 2; 69 var ph = (el.height() - h) / 2; 70 71 el.css({ 72 'background-image': 'url(' + obj.image.src + ')', 73 'background-size': w +'px ' + h + 'px', 74 'background-position': pw + 'px ' + ph + 'px', 75 'background-repeat': 'no-repeat'}); 76 }, 77 imgMouseDown = function(e) 78 { 79 e.stopImmediatePropagation(); 80 81 obj.state.dragable = true; 82 obj.state.mouseX = e.clientX; 83 obj.state.mouseY = e.clientY; 84 }, 85 imgMouseMove = function(e) 86 { 87 e.stopImmediatePropagation(); 88 89 if (obj.state.dragable) 90 { 91 var x = e.clientX - obj.state.mouseX; 92 var y = e.clientY - obj.state.mouseY; 93 94 var bg = el.css('background-position').split(' '); 95 96 var bgX = x + parseInt(bg[0]); 97 var bgY = y + parseInt(bg[1]); 98 99 el.css('background-position', bgX +'px ' + bgY + 'px'); 100 101 obj.state.mouseX = e.clientX; 102 obj.state.mouseY = e.clientY; 103 } 104 }, 105 imgMouseUp = function(e) 106 { 107 e.stopImmediatePropagation(); 108 obj.state.dragable = false; 109 }, 110 zoomImage = function(e) 111 { 112 e.originalEvent.wheelDelta > 0 || e.originalEvent.detail < 0 ? obj.ratio*=1.1 : obj.ratio*=0.9;
113 setBackground(); 114 } 115 116 obj.spinner.show(); 117 obj.image.onload = function() { 118 obj.spinner.hide(); 119 setBackground(); 120 121 el.bind('mousedown', imgMouseDown); 122 el.bind('mousemove', imgMouseMove); 123 $(window).bind('mouseup', imgMouseUp); 124 el.bind('mousewheel DOMMouseScroll', zoomImage); 125 }; 126 obj.image.src = options.imgSrc; 127 el.on('remove', function(){$(window).unbind('mouseup', imgMouseUp)}); 128 129 return obj; 130 }; 131 132 jQuery.fn.cropbox = function(options){ 133 return new cropbox(options, this); 134 }; 135})); 136
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.