Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

# v0.3.0

- Add setImage() method [#2](https://github.com/upfrontIO/srcissors/pull/3)


# v0.2.0

- Add actions options [#1](https://github.com/upfrontIO/srcissors/pull/1)
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,19 @@ cropper.getCrop();
// Set a ratio
cropper.setRatio(4/3);

// Listen for events
// Set a new url
cropper.setImage('/images/storytelling-painting.jpg');

// Listen for the ready event (only fired once)
cropper.on('ready', function() {
// your code
});

// Listen for image load events
cropper.on('load', function() {
// your code
});

cropper.on('change', function(crop) {
var x, y, width, height;
x = crop.x;
Expand Down
15 changes: 15 additions & 0 deletions examples/css/page.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,18 @@
body {
background-color: #222;
}

.swap-image {
position: absolute;
top: 0;
right: 0;

margin: 10px;
padding: 3px 5px;
display: inline-block;

border-radius: 5px;
text-decoration: none;
color: #000;
background: rgba(255, 255, 255, 0.5);
}
7 changes: 7 additions & 0 deletions examples/fit-to-area.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
</div>
</div>

<a class="swap-image" href="">swap image</a>

<!-- jquery -->
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>

Expand Down Expand Up @@ -66,6 +68,11 @@
event.stopPropagation();
});

$('.swap-image').on('click', function(event) {
crop.setImage('/images/storytelling-painting.jpg');
event.preventDefault();
});

});

</script>
Expand Down
16 changes: 15 additions & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<link rel="stylesheet" href="/css/srcissors.css">
</head>
<body>

<div class="clip-content">
<div class="crop-arena no-selection">
<div class="crop-view crop-view--is-loading">
Expand All @@ -37,6 +36,8 @@
</div>
</div>

<a class="swap-image" href="">swap image</a>

<!-- jquery -->
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>

Expand Down Expand Up @@ -76,6 +77,19 @@
event.stopPropagation();
});

$('.swap-image').on('click', function(event) {
crop.setImage('/images/storytelling-painting.jpg');
event.preventDefault();
});

crop.on('ready', function() {
console.log('ready event fired');
});

crop.on('load', function() {
console.log('load event fired');
});

});

</script>
Expand Down
39 changes: 23 additions & 16 deletions src/crop.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@ module.exports = class Crop

# Events
@readyEvent = $.Callbacks('memory once')
@loadEvent = $.Callbacks()
@changeEvent = $.Callbacks()

# Ready
@isReady = false
@view.addClass(@loadingCssClass)
this.on 'ready', =>
@isReady = true
@view.removeClass(@loadingCssClass)

# Confguration
@zoomInStep = zoomStep
@zoomOutStep = 1 / @zoomInStep
Expand All @@ -46,14 +40,24 @@ module.exports = class Crop
img: @img
outline: @outline

@preview.setImage({ url })
@setImage(url)


setImage: (url) ->
return unless url != @preview.url

@preview.reset() if @isInitialized
@isReady = false
@view.addClass(@loadingCssClass)
@preview.setImage({ url })


onPreviewReady: ({ width, height }) =>
@events = new Events
parent: this
view: @view
actions: @actions
if not @isInitialized
@events = new Events
parent: this
view: @view
actions: @actions

@imageWidth = width
@imageHeight = height
Expand All @@ -68,14 +72,19 @@ module.exports = class Crop
height: @imageHeight
keepDimension: keepDimension

@readyEvent.fire()
# ready state
@isReady = true
@view.removeClass(@loadingCssClass)

if @initialCrop?
if not @isInitialized && @initialCrop?
@setCrop(@initialCrop)
else
@zoomAllOut()
@center()

@isInitialized = true
@readyEvent.fire()
@loadEvent.fire()


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



13 changes: 11 additions & 2 deletions src/preview.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ module.exports = class Preview
@img.show()


setImage: ({ url }) ->
@img.attr('src', url)
setImage: ({ @url }) ->
@img.attr('src', @url)


reset: ->
@url = undefined
@x = @y = 0
@width = @height = 0
@img.attr('src', '')
@img.css(width: '', height: '', transform: '')
@outline.css(transform: '') if @outline


setWidth: (width) ->
Expand Down
Binary file added test/images/berge.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions test/specs/srcissors_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ describe 'srcissors', ->
done()


describe 'setImage()', ->

beforeEach (done) ->
@crop.on 'load', done

# Set a different 300x400 image
@crop.setImage('base/test/images/berge.jpg')


it 'sets the new image dimensions', ->
expect(@crop.imageWidth).to.equal(300)
expect(@crop.imageHeight).to.equal(400)


describe 'with a 100x200 arena', ->

beforeEach (done) ->
Expand Down