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

Adding API calls for helping with selection. #163

Open
wants to merge 4 commits 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
84 changes: 84 additions & 0 deletions demos/tutorial6.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello World | 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">
jQuery(function($){

var jCropApi;
$('#target').Jcrop({
aspectRatio: 2 / 3,
boxHeight: 300,
boxWidth: 300
}, function () {
jCropApi = this;

$('#SelectMax').click(function() {jCropApi.selectMax();});
$('#SelectCenter').click(jCropApi.selectCenter);
$('#SelectTopLeft').click(jCropApi.selectTopLeft);
$('#SelectTopRight').click(jCropApi.selectTopRight);
$('#SelectBottomLeft').click(jCropApi.selectBottomLeft);
$('#SelectBottomRight').click(jCropApi.selectBottomRight);
});

});

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

</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">API Selection Helpers</li>
</ul>
<h1>API Selection Helpers</h1>
</div>

<img src="../demos/demo_files/sago.jpg" id="target" alt="[Jcrop Example]" />
<button id="SelectMax">Maximize</button>
<button id="SelectTopLeft">Top-Left</button>
<button id="SelectBottomLeft">Bottom-Left</button>
<button id="SelectCenter">Center</button>
<button id="SelectTopRight">Top-Right</button>
<button id="SelectBottomRight">Bottom-Right</button>

<div class="description">
<p>
<b>This example demonstrates the default behavior of Jcrop.</b><br />
Since no event handlers have been attached it only performs
the cropping behavior.
</p>
</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>

128 changes: 128 additions & 0 deletions js/jquery.Jcrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,127 @@
Selection.refresh();
}
//}}}
function selectMax() //{{{
{
var
originalHeight = $origimg.height(),
originalWidth = $origimg.width(),
aspectRatio = options.aspectRatio;

var
targetWidth,
targetHeight;

if (aspectRatio == 0) {
targetHeight = originalHeight;
targetWidth = originalWidth;
} else if (originalWidth > originalHeight && aspectRatio <= 1) {
targetHeight = originalHeight;
targetWidth = originalHeight * aspectRatio;
} else {
targetWidth = originalWidth;
targetHeight = originalWidth / aspectRatio;
}

setSelect([0, 0, targetWidth, targetHeight]);
selectCenter();
}
//}}}
function selectCenter() { //{{{
var
originalHeight = $origimg.height(),
originalWidth = $origimg.width(),
aspectRatio = options.aspectRatio;
var
selected = tellSelect(),
targetWidth = selected.x2 - selected.x,
targetHeight = selected.y2 - selected.y;

var
x1 = originalWidth / 2 - targetWidth / 2,
x2 = x1 + targetWidth,
y1 = originalHeight / 2 - targetHeight / 2,
y2 = y1 + targetHeight;

animateTo([x1, y1, x2, y2]);
}
//}}}
function selectTopLeft() { //{{{
var
originalHeight = $origimg.height(),
originalWidth = $origimg.width(),
aspectRatio = options.aspectRatio;
var
selected = tellSelect(),
targetWidth = selected.x2 - selected.x,
targetHeight = selected.y2 - selected.y;

var
x1 = 0,
x2 = targetWidth,
y1 = 0,
y2 = targetHeight;

animateTo([x1, y1, x2, y2]);
}
//}}}
function selectTopRight() { //{{{
var
originalHeight = $origimg.height(),
originalWidth = $origimg.width(),
aspectRatio = options.aspectRatio;
var
selected = tellSelect(),
targetWidth = selected.x2 - selected.x,
targetHeight = selected.y2 - selected.y;

var
x1 = originalWidth - targetWidth,
x2 = originalWidth,
y1 = 0,
y2 = originalHeight - targetHeight;

animateTo([x1, y1, x2, y2]);
}
//}}}
function selectBottomLeft() { //{{{
var
originalHeight = $origimg.height(),
originalWidth = $origimg.width(),
aspectRatio = options.aspectRatio;
var
selected = tellSelect(),
targetWidth = selected.x2 - selected.x,
targetHeight = selected.y2 - selected.y;

var
x1 = 0,
x2 = targetWidth,
y1 = originalHeight - targetHeight,
y2 = originalHeight;

animateTo([x1, y1, x2, y2]);
}
//}}}
function selectBottomRight() { //{{{
var
originalHeight = $origimg.height(),
originalWidth = $origimg.width(),
aspectRatio = options.aspectRatio;
var
selected = tellSelect(),
targetWidth = selected.x2 - selected.x,
targetHeight = selected.y2 - selected.y;

var
x1 = originalWidth - targetWidth,
x2 = originalWidth,
y1 = originalHeight - targetHeight,
y2 = originalHeight;

animateTo([x1, y1, x2, y2]);
}
//}}}
//}}}

if (Touch.support) $trk.bind('touchstart.jcrop', Touch.newSelection);
Expand All @@ -1548,6 +1669,13 @@
tellScaled: tellScaled,
setClass: setClass,

selectMax: selectMax,
selectCenter: selectCenter,
selectTopLeft: selectTopLeft,
selectTopRight: selectTopRight,
selectBottomLeft: selectBottomLeft,
selectBottomRight: selectBottomRight,

disable: disableCrop,
enable: enableCrop,
cancel: cancelCrop,
Expand Down