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://yui.yahooapis.com/3.17.2/build/yui/yui-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
    YUI().use('node', 'crop-box', function(Y){
46
        var options =
47
        {
48
            imageBox: '.imageBox',
49
            thumbBox: '.thumbBox',
50
            spinner: '.spinner',
51
            imgSrc: ''
52
        }
53
        var cropper;
54
        Y.one('#file').on('change', function(){
55
            var reader = new FileReader();
56
            reader.onload = function(e) {
57
                options.imgSrc = e.target.result;
58
                cropper = new Y.CropBox(options);
59
            }
60
            reader.readAsDataURL(this.get('files')._nodes[0]);
61
            this.get('files')._nodes = [];
62
        })
63
        Y.one('#btnCrop').on('click', function(){
64
            var img = cropper.getAvatar()
65
            Y.one('.cropped').append('<img src="'+img+'">');
66
        })
67
        Y.one('#btnZoomIn').on('click', function(){
68
            cropper.zoomIn();
69
        })
70
        Y.one('#btnZoomOut').on('click', function(){
71
            cropper.zoomOut();
72
        })
73
    })
74
</script>
75
 
76
</body>
77
</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
    box-sizing: border-box;
23
    border: 1px solid rgb(102, 102, 102);
24
    box-shadow: 0 0 0 1000px rgba(0, 0, 0, 0.5);
25
    background: none repeat scroll 0% 0% transparent;
26
}
27
 
28
.imageBox .spinner
29
{
30
    position: absolute;
31
    top: 0;
32
    left: 0;
33
    bottom: 0;
34
    right: 0;
35
    text-align: center;
36
    line-height: 300px;
37
    background: rgba(0,0,0,0.7);
38
}
 
JavaScript
/**
1
/**
2
 * Created by ezgoing on 14/9/2014.
3
 */
4
 
5
YUI.add('crop-box', function (Y) {
6
    Y.CropBox = Y.Base.create('crop-box', Y.Base, [],
7
        {
8
            initializer: function (options)
9
            {
10
                this.options = options;
11
                this.state = {};
12
                this.render();
13
            },
14
            render: function ()
15
            {
16
                var self = this;
17
                this.imageBox = Y.one(this.options.imageBox);
18
                this.thumbBox = this.imageBox.one(this.options.thumbBox);
19
                this.spinner = this.imageBox.one(this.options.spinner);
20
 
21
                this.initObject();
22
                return this;
23
            },
24
            initObject: function()
25
            {
26
                var self = this;
27
                this.spinner.show();
28
 
29
                this.image = new Image();
30
                this.image.onload = function() {
31
                    self.spinner.hide();
32
                    self.setBackground();
33
 
34
                    //event handler
35
                    self.imageBox.on('mousedown', self.imgMouseDown, self);
36
                    self.imageBox.on('mousemove', self.imgMouseMove, self);
37
                    self.mouseup = Y.one('body').on('mouseup', self.imgMouseUp, self);
38
 
39
                    Y.UA.gecko > 0?
40
                        self.imageBox.on('DOMMouseScroll', self.zoomImage, self):
41
                        self.imageBox.on('mousewheel', self.zoomImage, self);
42
                };
43
                this.image.src = this.options.imgSrc;
44
            },
45
            setBackground: function()
46
            {
47
                if(!this.ratio) this.ratio = 1;
48
 
49
                var w =  parseInt(this.image.width)*this.ratio;
50
                var h =  parseInt(this.image.height)*this.ratio;
51
 
52
                var pw = (this.imageBox.get('clientWidth') - w) / 2;
53
                var ph = (this.imageBox.get('clientHeight') - h) / 2;
54
 
55
                this.imageBox.setAttribute('style',
56
                    'background-image: url(' + this.image.src + '); ' +
57
                    'background-size: ' + w +'px ' + h + 'px; ' +
58
                    'background-position: ' + pw + 'px ' + ph + 'px; ' +
59
                    'background-repeat: no-repeat');
60
            },
61
            imgMouseDown: function(e)
62
            {
63
                e.stopImmediatePropagation();
64
                this.state.dragable = true;
65
                this.state.mouseX = e.clientX;
66
                this.state.mouseY = e.clientY;
67
            },
68
            imgMouseMove: function(e)
69
            {
70
                e.stopImmediatePropagation();
71
                if (this.state.dragable)
72
                {
73
                    var x = e.clientX - this.state.mouseX;
74
                    var y = e.clientY - this.state.mouseY;
75
 
76
                    var bg = this.imageBox.getStyle('backgroundPosition').split(' ');
77
 
78
                    var bgX = x + parseInt(bg[0]);
79
                    var bgY = y + parseInt(bg[1]);
80
 
81
                    this.imageBox.setStyle('backgroundPosition', bgX +'px ' + bgY + 'px');
82
 
83
                    this.state.mouseX = e.clientX;
84
                    this.state.mouseY = e.clientY;
85
                }
86
            },
87
            imgMouseUp: function(e)
88
            {
89
                e.stopImmediatePropagation();
90
                this.state.dragable = false;
91
            },
92
            zoomImage: function(e)
93
            {
94
                e.wheelDelta > 0? this.ratio*=1.1 : this.ratio*=0.9;
95
                this.setBackground();
96
            },
97
            getAvatar: function ()
98
            {
99
                var self = this,
100
                    width = this.thumbBox.get('clientWidth'),
101
                    height = this.thumbBox.get('clientHeight'),
102
                    canvas = document.createElement("canvas"),
103
                    dim = this.imageBox.getStyle('backgroundPosition').split(' '),
104
                    size = this.imageBox.getStyle('backgroundSize').split(' '),
105
                    dx = parseInt(dim[0]) - this.imageBox.get('clientWidth')/2 + width/2,
106
                    dy = parseInt(dim[1]) - this.imageBox.get('clientHeight')/2 + height/2,
107
                    dw = parseInt(size[0]);
108
                    dh = parseInt(size[1]);
109
                    sh = parseInt(this.image.height);
110
                    sw = parseInt(this.image.width);
111
 
112
                canvas.width = width;
113
                canvas.height = height;
114
                var context = canvas.getContext("2d");
115
                context.drawImage(this.image, 0, 0, sw, sh, dx, dy, dw, dh);
116
                var imageData = canvas.toDataURL('image/jpeg');
117
 
118
                return imageData;
119
            },
120
            zoomIn: function ()
121
            {
122
                this.ratio*=1.1;
123
                this.setBackground();
124
            },
125
            zoomOut: function ()
126
            {
127
                this.ratio*=0.9;
128
                this.setBackground();
129
            },
130
            destructor: function ()
131
            {
132
                if (this.mouseup) this.mouseup.detach()
133
            }
134
        });
135
}, '1.0',
136
{
137
    requires: [ 'node', 'base' ]
138
});
139
 
 

Untitled

CSSDeck G+