Ajax Loader
×
HTML
<!DOCTYPE html>
1
<!DOCTYPE html>
2
<html>
3
<head lang="en">
4
    <meta charset="UTF-8">
5
    <title>Crop Box</title>
6
    <link rel="stylesheet" href="style.css" type="text/css" />
7
    <style>
8
        .container
9
        {
10
            position: absolute;
11
            top: 10%; left: 10%; right: 0; bottom: 0;
12
        }
13
        .action
14
        {
15
            width: 400px;
16
            height: 30px;
17
            margin: 10px 0;
18
        }
19
        .cropped>img
20
        {
21
            margin-right: 10px;
22
        }
23
    </style>
24
</head>
25
<body>
26
 
27
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
28
<script src="../cropbox.js"></script>
29
<div class="container">
30
    <div class="imageBox">
31
        <div class="thumbBox"></div>
32
        <div class="spinner" style="display: none">Loading...</div>
33
    </div>
34
    <div class="action">
35
        <input type="file" id="file" style="float:left; width: 250px">
36
        <input type="button" id="btnCrop" value="Crop" style="float: right">
37
        <input type="button" id="btnZoomIn" value="+" style="float: right">
38
        <input type="button" id="btnZoomOut" value="-" style="float: right">
39
    </div>
40
    <div class="cropped">
41
 
42
    </div>
43
</div>
44
<script type="text/javascript">
45
    $(window).load(function() {
46
        var options =
47
        {
48
            thumbBox: '.thumbBox',
49
            spinner: '.spinner',
50
            imgSrc: 'avatar.png'
51
        }
52
        var cropper;
53
        $('#file').on('change', function(){
54
            var reader = new FileReader();
55
            reader.onload = function(e) {
56
                options.imgSrc = e.target.result;
57
                cropper = $('.imageBox').cropbox(options);
58
            }
59
            reader.readAsDataURL(this.files[0]);
60
            this.files = [];
61
        })
62
        $('#btnCrop').on('click', function(){
63
            var img = cropper.getDataURL()
64
            $('.cropped').append('<img src="'+img+'">');
65
        })
66
        $('#btnZoomIn').on('click', function(){
67
            cropper.zoomIn();
68
        })
69
        $('#btnZoomOut').on('click', function(){
70
            cropper.zoomOut();
71
        })
72
    });
73
</script>
74
 
75
</body>
76
</html>
 
CSS
.imageBox
1
.imageBox
2
{
3
    position: relative;
4
    height: 400px;
5
    width: 400px;
6
    border:1px solid #aaa;
7
    background: #fff;
8
    overflow: hidden;
9
    background-repeat: no-repeat;
10
    cursor:move;
11
}
12
 
13
.imageBox .thumbBox
14
{
15
    position: absolute;
16
    top: 50%;
17
    left: 50%;
18
    width: 200px;
19
    height: 200px;
20
    margin-top: -100px;
21
    margin-left: -100px;
22
    border: 1px solid rgb(102, 102, 102);
23
    box-shadow: 0 0 0 1000px rgba(0, 0, 0, 0.5);
24
    background: none repeat scroll 0% 0% transparent;
25
}
26
 
27
.imageBox .spinner
28
{
29
    position: absolute;
30
    top: 0;
31
    left: 0;
32
    bottom: 0;
33
    right: 0;
34
    text-align: center;
35
    line-height: 400px;
36
    background: rgba(0,0,0,0.7);
37
}
 
JavaScript
/**
1
/**
2
 * Created by ezgoing on 14/9/2014.
3
 */
4
 
5
"use strict";
6
(function (factory) {
7
    if (typeof define === 'function' && define.amd) {
8
        define(['jquery'], factory);
9
    } else {
10
        factory(jQuery);
11
    }
12
}(function ($) {
13
    var cropbox = function(options, el){
14
        var el = el || $(options.imageBox),
15
            obj =
16
            {
17
                state : {},
18
                ratio : 1,
19
                options : options,
20
                imageBox : el,
21
                thumbBox : el.find(options.thumbBox),
22
                spinner : el.find(options.spinner),
23
                image : new Image(),
24
                getDataURL: function ()
25
                {
26
                    var width = this.thumbBox.width(),
27
                        height = this.thumbBox.height(),
28
                        canvas = document.createElement("canvas"),
29
                        dim = el.css('background-position').split(' '),
30
                        size = el.css('background-size').split(' '),
31
                        dx = parseInt(dim[0]) - el.width()/2 + width/2,
32
                        dy = parseInt(dim[1]) - el.height()/2 + height/2,
33
                        dw = parseInt(size[0]),
34
                        dh = parseInt(size[1]),
35
                        sh = parseInt(this.image.height),
36
                        sw = parseInt(this.image.width);
37
 
38
                    canvas.width = width;
39
                    canvas.height = height;
40
                    var context = canvas.getContext("2d");
41
                    context.drawImage(this.image, 0, 0, sw, sh, dx, dy, dw, dh);
42
                    var imageData = canvas.toDataURL('image/png');
43
                    return imageData;
44
                },
45
                getBlob: function()
46
                {
47
                    var imageData = this.getDataURL();
48
                    var b64 = imageData.replace('data:image/png;base64,','');
49
                    var binary = atob(b64);
50
                    var array = [];
51
                    for (var i = 0; i < binary.length; i++) {
52
                        array.push(binary.charCodeAt(i));
53
                    }
54
                    return  new Blob([new Uint8Array(array)], {type: 'image/png'});
55
                },
56
                zoomIn: function ()
57
                {
58
                    this.ratio*=1.1;
59
                    setBackground();
60
                },
61
                zoomOut: function ()
62
                {
63
                    this.ratio*=0.9;
64
                    setBackground();
65
                }
66
            },
67
            setBackground = function()
68
            {
69
                var w =  parseInt(obj.image.width)*obj.ratio;
70
                var h =  parseInt(obj.image.height)*obj.ratio;
71
 
72
                var pw = (el.width() - w) / 2;
73
                var ph = (el.height() - h) / 2;
74
 
75
                el.css({
76
                    'background-image': 'url(' + obj.image.src + ')',
77
                    'background-size': w +'px ' + h + 'px',
78
                    'background-position': pw + 'px ' + ph + 'px',
79
                    'background-repeat': 'no-repeat'});
80
            },
81
            imgMouseDown = function(e)
82
            {
83
                e.stopImmediatePropagation();
84
 
85
                obj.state.dragable = true;
86
                obj.state.mouseX = e.clientX;
87
                obj.state.mouseY = e.clientY;
88
            },
89
            imgMouseMove = function(e)
90
            {
91
                e.stopImmediatePropagation();
92
 
93
                if (obj.state.dragable)
94
                {
95
                    var x = e.clientX - obj.state.mouseX;
96
                    var y = e.clientY - obj.state.mouseY;
97
 
98
                    var bg = el.css('background-position').split(' ');
99
 
100
                    var bgX = x + parseInt(bg[0]);
101
                    var bgY = y + parseInt(bg[1]);
102
 
103
                    el.css('background-position', bgX +'px ' + bgY + 'px');
104
 
105
                    obj.state.mouseX = e.clientX;
106
                    obj.state.mouseY = e.clientY;
107
                }
108
            },
109
            imgMouseUp = function(e)
110
            {
111
                e.stopImmediatePropagation();
112
                obj.state.dragable = false;
113
            },
114
            zoomImage = function(e)
115
            {
116
                e.originalEvent.wheelDelta > 0 || e.originalEvent.detail < 0 ? obj.ratio*=1.1 : obj.ratio*=0.9;
117
                setBackground();
118
            }
119
 
120
        obj.spinner.show();
121
        obj.image.onload = function() {
122
            obj.spinner.hide();
123
            setBackground();
124
 
125
            el.bind('mousedown', imgMouseDown);
126
            el.bind('mousemove', imgMouseMove);
127
            $(window).bind('mouseup', imgMouseUp);
128
            el.bind('mousewheel DOMMouseScroll', zoomImage);
129
        };
130
        obj.image.src = options.imgSrc;
131
        el.on('remove', function(){$(window).unbind('mouseup', imgMouseUp)});
132
 
133
        return obj;
134
    };
135
 
136
    jQuery.fn.cropbox = function(options){
137
        return new cropbox(options, this);
138
    };
139
}));
140
 
141
 
 

Untitled

CSSDeck G+