├── readme ├── license ├── index.html └── image_editor.js /readme: -------------------------------------------------------------------------------- 1 | Yufei Liu 2 | liu.yufei@gmail.com 3 | @_y 4 | 5 | Basic image editor in JavaScript with panning, cropped, and zooming. 6 | 7 | All additional params are stored in hidden input tags and can be processed on the server. 8 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Yufei Liu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Testing image editor 4 | 5 | 6 | 13 | 14 | 44 | 45 | 46 |
47 | 48 |
49 |
50 | Resize image 51 |
52 | 53 | 54 | 55 | 56 |
57 | -------------------------------------------------------------------------------- /image_editor.js: -------------------------------------------------------------------------------- 1 | var ImageEditor = function (options) { 2 | var width = options.width; 3 | var height = options.height; 4 | this.$el = options.$el; 5 | var disabled = true; 6 | var $fileInput = this.$('input[name="image"]'); 7 | var $hiddenImage = this.$('.image-hidden-preview'); 8 | var $imageSize = this.$('.image-size'); 9 | var $offsetX = this.$('input[name="offset_x"]'); 10 | var $offsetY = this.$('input[name="offset_y"]'); 11 | 12 | var lastVal = Number($imageSize.val()); 13 | 14 | // Stuff for generating preview and dragging 15 | var $bg = $('.image-preview'), 16 | elbounds = { 17 | w: parseInt($bg.width()), 18 | h: parseInt($bg.height()) 19 | }, 20 | bounds = {w: 2350 - elbounds.w, h: 1750 - elbounds.h}, 21 | origin = {x: 0, y: 0}, 22 | start = {x: 0, y: 0}, 23 | movecontinue = false; 24 | 25 | function move (e){ 26 | var inbounds = {x: false, y: false}; 27 | var offset = { 28 | x: start.x - (origin.x - e.clientX), 29 | y: start.y - (origin.y - e.clientY) 30 | }; 31 | 32 | inbounds.x = offset.x < 0 && (offset.x * -1) < bounds.w; 33 | inbounds.y = offset.y < 0 && (offset.y * -1) < bounds.h; 34 | 35 | if (movecontinue && inbounds.x && inbounds.y) { 36 | start.x = offset.x; 37 | start.y = offset.y; 38 | 39 | $(this).css('background-position', start.x + 'px ' + start.y + 'px'); 40 | $offsetX.val(Math.round(start.x)); 41 | $offsetY.val(Math.round(start.y)); 42 | } 43 | 44 | origin.x = e.clientX; 45 | origin.y = e.clientY; 46 | 47 | e.stopPropagation(); 48 | return false; 49 | } 50 | 51 | function handle (e) { 52 | if (disabled) { 53 | return; 54 | } 55 | movecontinue = false; 56 | $bg.unbind('mousemove', move); 57 | 58 | if (e.type == 'mousedown') { 59 | origin.x = e.clientX; 60 | origin.y = e.clientY; 61 | movecontinue = true; 62 | $bg.bind('mousemove', move); 63 | } else { 64 | $(document.body).focus(); 65 | } 66 | 67 | e.stopPropagation(); 68 | return false; 69 | } 70 | 71 | function reset () { 72 | start = {x: 0, y: 0}; 73 | $(this).css('backgroundPosition', '0 0'); 74 | } 75 | 76 | $bg.bind('mousedown mouseup mouseleave', handle); 77 | $bg.bind('dblclick', reset); 78 | 79 | //read image locally 80 | var imageHeight, imageWidth; 81 | $fileInput.on('change', function () { 82 | var oFReader = new FileReader(); 83 | var file = $fileInput.get(0).files[0]; 84 | oFReader.readAsDataURL(file); 85 | oFReader.onload = function (oFREvent) { 86 | $bg.css('background-image', 'url(' + oFREvent.target.result + ')'); 87 | $hiddenImage.attr('src', oFREvent.target.result); 88 | 89 | var bgHeight = $bg.height(); 90 | var bgWidth = $bg.width(); 91 | imageHeight = $hiddenImage.height(); 92 | imageWidth = $hiddenImage.width(); 93 | 94 | $imageSize.val(100); 95 | lastVal = 100; 96 | 97 | var widthOffset = -(imageWidth - bgWidth) / 2; 98 | var heightOffset = -(imageHeight - bgHeight) / 2; 99 | 100 | $bg.css('background-size', 'auto'); 101 | $bg.css('background-position', widthOffset + 'px ' + heightOffset + 'px'); 102 | $offsetX.val(widthOffset); 103 | $offsetY.val(heightOffset); 104 | start = { x: widthOffset, y: heightOffset }; 105 | 106 | $bg.addClass('with-cursor'); 107 | 108 | disabled = false; 109 | }; 110 | }); 111 | 112 | $imageSize.on('change', function () { 113 | var val = Number($imageSize.val()); 114 | if (imageHeight && imageWidth) { 115 | var updatedWidth = Math.round(imageWidth / 100 * val); 116 | var updatedHeight = Math.round(imageHeight / 100 * val); 117 | 118 | $bg.css({ 119 | 'background-size': updatedWidth + 'px ' + updatedHeight + 'px' 120 | }); 121 | 122 | var viewportSize = $bg.width() / 2; 123 | var x, y; 124 | x = parseInt($bg.css('background-position-x'), 10); 125 | y = parseInt($bg.css('background-position-y'), 10); 126 | 127 | var oldRatio = lastVal / 100; 128 | var newRatio = val / 100; 129 | 130 | var newX = (x / oldRatio * newRatio + width / 2) - width / 2 / oldRatio * newRatio; 131 | var newY = (y / oldRatio * newRatio + height / 2) - height / 2 / oldRatio * newRatio; 132 | 133 | start.x = newX; 134 | start.y = newY; 135 | 136 | $bg.css('background-position', newX + 'px ' + newY + 'px'); 137 | $offsetX.val(Math.round(newX)); 138 | $offsetY.val(Math.round(newY)); 139 | 140 | lastVal = val; 141 | } 142 | }); 143 | }; 144 | 145 | ImageEditor.prototype.$ = function (selector) { 146 | return this.$el.find(selector); 147 | }; --------------------------------------------------------------------------------