Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Jcrop resize api #237

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
162 changes: 162 additions & 0 deletions demos/tutorial6.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Jcrop resizable | Jcrop Demo</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />

<script src="../js/jquery.min.js"></script>
<script src="../js/jquery.Jcrop.js"></script>
<script type="text/javascript">
$(function() {
var cropApi = null;
$('#target').Jcrop({ aspectRatio: 16 / 9 }, function() {
cropApi = this;
});

var $holder = cropApi.ui.holder;
var $imgs = $holder.find('img');


$('.J-get-crop-info').on('click', function() {
console.log(cropApi.tellSelect());
console.log(cropApi.tellScaled());
});

var scale = 1;

$('.J-zoom-in').on('click', function() {
scale = Math.max(scale - 0.1, 0.1);
zoom(scale);
});

$('.J-zoom-out').on('click', function() {
scale = Math.min(scale + 0.1, 2);
zoom(scale);
});

var originSize = null;
var currentSize = { width: 0, height: 0 }

function getImgOriginSize() {
if (!originSize) {
originSize = {
width: $imgs.width(),
height: $imgs.height()
};

currentSize.width = originSize.width;
currentSize.height = originSize.height;
}

return originSize;
}

function zoom(value) {
var size = getImgOriginSize();
value = value == undefined ? 1 : value;

cropApi.resize(size.width * value, size.height * value);
}

});
</script>
<link rel="stylesheet" href="demo_files/main.css" type="text/css" />
<link rel="stylesheet" href="demo_files/demos.css" type="text/css" />
<link rel="stylesheet" href="../css/jquery.Jcrop.css" type="text/css" />
<style type="text/css">

#crop-wrapper {
width: 500px;
height: 375px;
margin: 0 auto;

border: 1px solid #ccc;
box-shadow: 0 2px 5px 2px rgba(0, 0, 0, .1);
overflow: visible;
position: relative;
z-index: 1;
}

#crop-wrapper .jcrop-holder > .jcrop-tracker:after {
content: '';
position: absolute;
top: 2px;
left: 2px;
width: 500px;
height: 375px;

box-shadow: 0 0 0 100000px rgba(0, 0, 0, .8);
}

#button-bar-wrapper {
padding-top: 10px;
text-align: center;
position: relative;
z-index: 2;
}
#button-bar-wrapper button {
padding: 5px 10px;
border: 1px solid #ccc;
background-color: #f2f2f2;
border-radius: 2px;
}

</style>

</head>
<body>

<div class="container">
<div class="row">
<div class="span12">
<div class="jc-demo-box">

<div class="page-header">
<ul class="breadcrumb first">
<li><a href="../index.html">Jcrop</a> <span class="divider">/</span></li>
<li><a href="../index.html">Demos</a> <span class="divider">/</span></li>
<li class="active">Jcrop resizable</li>
</ul>
<h1>Jcrop resizable</h1>
</div>
<div style="overflow: hidden; padding: 50px 0 150px 0; margin-bottom: 50px">
<div id="crop-wrapper">
<img src="./demo_files/sago.jpg" id="target" alt="[Jcrop Example]" />
</div>
</div>
<div id="button-bar-wrapper">
<button class="J-zoom-in">Zoom In</button>
<button class="J-zoom-out">Zoom Out</button>
<button class="J-get-crop-info">Crop Info</button>
</div>

<div class="description">
<p>
Jcrop resizable
</p>
<pre>
var cropApi = null;
$('#target').Jcrop({ }, function() {
cropApi = this;
});
cropApi.resize(500, 300);
</pre>
</div>

<div class="tapmodo-footer">
<a href="http://tapmodo.com" class="tapmodo-logo segment">tapmodo.com</a>
<div class="segment"><b>&copy; 2008-2013 Tapmodo Interactive LLC</b><br />
Jcrop is free software released under <a href="../MIT-LICENSE.txt">MIT License</a>
</div>
</div>

<div class="clearfix"></div>

</div>
</div>
</div>
</div>

</body>
</html>

2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ <h3>Static Demos</h3>
&mdash; animation/fading demo</li>
<li><a href="demos/tutorial5.html">API Interface</a>
&mdash; real-time API example</li>
<li><a href="demos/tutorial6.html">Jcrop resizable</a>
&mdash; resize API example</li>
<li><a href="demos/styling.html">Styling Example</a>
&mdash; style Jcrop dynamically with CSS
<small>New in 0.9.10</small>
Expand Down
52 changes: 44 additions & 8 deletions js/jquery.Jcrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@
function newSelection(e) //{{{
{
if (options.disabled) {
return;
return false;
}
if (!options.allowSelect) {
return;
return false;
}
btndown = true;
docOffset = getPos($img);
Expand Down Expand Up @@ -266,7 +266,42 @@
return trk;
}
//}}}

function resize(width, height) {
$([$img[0], $img2[0], $div[0], $trk[0]]).css({
width: width,
height: height
})
boundx = width;
boundy = height;
var select = tellSelect();
var x = select.x;
var y = select.y;
var x2 = select.x2;
var y2 = select.y2;

if (select.w <= 0 && select.h <= 0) {
return;
}

if (x2 > boundx) {
x -= x2 - boundx;
x2 = x + select.w;
}

if (y2 > boundy) {
y -= y2 - boundy;
y2 = y + select.h;
}

setSelect([x, y, x2, y2]);
// if (select.x2 > boundx) {
// //var
// select.x1 -=
// //select.x2 -=
// }
}
///}}}

// }}}
// Initialization {{{
// Sanitize some options {{{
Expand Down Expand Up @@ -493,7 +528,7 @@
if (0 > y1 + oy) {
oy -= oy + y1;
}

if (boundy < y2 + oy) {
oy += boundy - (y2 + oy);
}
Expand Down Expand Up @@ -654,7 +689,6 @@
var xsize = x2 - x1,
ysize = y2 - y1,
delta;

if (xlimit && (Math.abs(xsize) > xlimit)) {
x2 = (xsize > 0) ? (x1 + xlimit) : (x1 - xlimit);
}
Expand All @@ -668,7 +702,7 @@
if (xmin / xscale && (Math.abs(xsize) < xmin / xscale)) {
x2 = (xsize > 0) ? (x1 + xmin / xscale) : (x1 - xmin / xscale);
}

if (x1 < 0) {
x2 -= x1;
x1 -= x1;
Expand Down Expand Up @@ -1547,7 +1581,7 @@
tellSelect: tellSelect,
tellScaled: tellScaled,
setClass: setClass,

disable: disableCrop,
enable: enableCrop,
cancel: cancelCrop,
Expand All @@ -1569,7 +1603,9 @@
// careful: internal values are returned
return options;
},


resize: resize,

ui: {
holder: $div,
selection: $sel
Expand Down