Skip to content

Commit 2d16dce

Browse files
committed
Merge pull request #3 from upfrontIO/set-image
Add setImage() method
2 parents 576fb9f + 63c0b0a commit 2d16dce

File tree

9 files changed

+99
-20
lines changed

9 files changed

+99
-20
lines changed

Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11

2+
# v0.3.0
3+
4+
- Add setImage() method [#2](https://github.com/upfrontIO/srcissors/pull/3)
5+
6+
27
# v0.2.0
38

49
- Add actions options [#1](https://github.com/upfrontIO/srcissors/pull/1)

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,19 @@ cropper.getCrop();
2929
// Set a ratio
3030
cropper.setRatio(4/3);
3131

32-
// Listen for events
32+
// Set a new url
33+
cropper.setImage('/images/storytelling-painting.jpg');
34+
35+
// Listen for the ready event (only fired once)
3336
cropper.on('ready', function() {
3437
// your code
3538
});
3639

40+
// Listen for image load events
41+
cropper.on('load', function() {
42+
// your code
43+
});
44+
3745
cropper.on('change', function(crop) {
3846
var x, y, width, height;
3947
x = crop.x;

examples/css/page.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,18 @@
66
body {
77
background-color: #222;
88
}
9+
10+
.swap-image {
11+
position: absolute;
12+
top: 0;
13+
right: 0;
14+
15+
margin: 10px;
16+
padding: 3px 5px;
17+
display: inline-block;
18+
19+
border-radius: 5px;
20+
text-decoration: none;
21+
color: #000;
22+
background: rgba(255, 255, 255, 0.5);
23+
}

examples/fit-to-area.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
</div>
3535
</div>
3636

37+
<a class="swap-image" href="">swap image</a>
38+
3739
<!-- jquery -->
3840
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
3941

@@ -66,6 +68,11 @@
6668
event.stopPropagation();
6769
});
6870

71+
$('.swap-image').on('click', function(event) {
72+
crop.setImage('/images/storytelling-painting.jpg');
73+
event.preventDefault();
74+
});
75+
6976
});
7077

7178
</script>

examples/index.html

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<link rel="stylesheet" href="/css/srcissors.css">
1313
</head>
1414
<body>
15-
1615
<div class="clip-content">
1716
<div class="crop-arena no-selection">
1817
<div class="crop-view crop-view--is-loading">
@@ -37,6 +36,8 @@
3736
</div>
3837
</div>
3938

39+
<a class="swap-image" href="">swap image</a>
40+
4041
<!-- jquery -->
4142
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
4243

@@ -76,6 +77,19 @@
7677
event.stopPropagation();
7778
});
7879

80+
$('.swap-image').on('click', function(event) {
81+
crop.setImage('/images/storytelling-painting.jpg');
82+
event.preventDefault();
83+
});
84+
85+
crop.on('ready', function() {
86+
console.log('ready event fired');
87+
});
88+
89+
crop.on('load', function() {
90+
console.log('load event fired');
91+
});
92+
7993
});
8094

8195
</script>

src/crop.coffee

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,9 @@ module.exports = class Crop
2020

2121
# Events
2222
@readyEvent = $.Callbacks('memory once')
23+
@loadEvent = $.Callbacks()
2324
@changeEvent = $.Callbacks()
2425

25-
# Ready
26-
@isReady = false
27-
@view.addClass(@loadingCssClass)
28-
this.on 'ready', =>
29-
@isReady = true
30-
@view.removeClass(@loadingCssClass)
31-
3226
# Confguration
3327
@zoomInStep = zoomStep
3428
@zoomOutStep = 1 / @zoomInStep
@@ -46,14 +40,24 @@ module.exports = class Crop
4640
img: @img
4741
outline: @outline
4842

49-
@preview.setImage({ url })
43+
@setImage(url)
44+
45+
46+
setImage: (url) ->
47+
return unless url != @preview.url
48+
49+
@preview.reset() if @isInitialized
50+
@isReady = false
51+
@view.addClass(@loadingCssClass)
52+
@preview.setImage({ url })
5053

5154

5255
onPreviewReady: ({ width, height }) =>
53-
@events = new Events
54-
parent: this
55-
view: @view
56-
actions: @actions
56+
if not @isInitialized
57+
@events = new Events
58+
parent: this
59+
view: @view
60+
actions: @actions
5761

5862
@imageWidth = width
5963
@imageHeight = height
@@ -68,14 +72,19 @@ module.exports = class Crop
6872
height: @imageHeight
6973
keepDimension: keepDimension
7074

71-
@readyEvent.fire()
75+
# ready state
76+
@isReady = true
77+
@view.removeClass(@loadingCssClass)
7278

73-
if @initialCrop?
79+
if not @isInitialized && @initialCrop?
7480
@setCrop(@initialCrop)
7581
else
7682
@zoomAllOut()
7783
@center()
7884

85+
@isInitialized = true
86+
@readyEvent.fire()
87+
@loadEvent.fire()
7988

8089

8190
setCrop: ({ x, y, width, height }) ->
@@ -488,5 +497,3 @@ module.exports = class Crop
488497
console.log(obj)
489498
return obj
490499

491-
492-

src/preview.coffee

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@ module.exports = class Preview
1414
@img.show()
1515

1616

17-
setImage: ({ url }) ->
18-
@img.attr('src', url)
17+
setImage: ({ @url }) ->
18+
@img.attr('src', @url)
19+
20+
21+
reset: ->
22+
@url = undefined
23+
@x = @y = 0
24+
@width = @height = 0
25+
@img.attr('src', '')
26+
@img.css(width: '', height: '', transform: '')
27+
@outline.css(transform: '') if @outline
1928

2029

2130
setWidth: (width) ->

test/images/berge.jpg

65.7 KB
Loading

test/specs/srcissors_spec.coffee

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ describe 'srcissors', ->
8585
done()
8686

8787

88+
describe 'setImage()', ->
89+
90+
beforeEach (done) ->
91+
@crop.on 'load', done
92+
93+
# Set a different 300x400 image
94+
@crop.setImage('base/test/images/berge.jpg')
95+
96+
97+
it 'sets the new image dimensions', ->
98+
expect(@crop.imageWidth).to.equal(300)
99+
expect(@crop.imageHeight).to.equal(400)
100+
101+
88102
describe 'with a 100x200 arena', ->
89103

90104
beforeEach (done) ->

0 commit comments

Comments
 (0)