Browse Source

Support scaling images larger and smaller.

pull/96/head
Evan Theurer 11 years ago
parent
commit
2a9ddd18fa
  1. 31
      src/styles/components/_buddypictureupload.scss
  2. 80
      static/js/directives/buddypictureupload.js
  3. 14
      static/partials/buddypictureupload.html

31
src/styles/components/_buddypictureupload.scss

@ -37,9 +37,11 @@ @@ -37,9 +37,11 @@
top: 0px;
left: 0px;
}
.moveImage {
position: relative;
visibility:hidden;
.imageUtilites {
line-height: 30px;
position: absolute;
visibility: hidden;
width: 200px;
z-index: 1;
.fa {
cursor: pointer;
@ -47,26 +49,23 @@ @@ -47,26 +49,23 @@
text-shadow: 0 0 5px black;
opacity: 0.8;
font-size: 40px;
width: 50px;
height: 50px;
}
}
.showUploadPicture:hover .moveImage {
.showUploadPicture:hover .imageUtilites {
visibility:visible;
}
.moveHorizontal {
width: 200px;
position: absolute;
top: -75px;
left: 0;
.fa {
width: 50px;
}
top: -4px;
position: relative;
}
.moveVertical {
position: absolute;
top: -30px;
left: 174px;
.item {
height: 50px;
}
left: 158px;
}
.resize {
position: relative;
top: 108px;
}
}

80
static/js/directives/buddypictureupload.js

@ -122,42 +122,53 @@ define(['jquery', 'underscore', 'text!partials/buddypictureupload.html'], functi @@ -122,42 +122,53 @@ define(['jquery', 'underscore', 'text!partials/buddypictureupload.html'], functi
};
// Auto fit by smallest dimension
// (image, canvas) -> object
var getAutoFitDimensions = function(from, to) {
if(!from.width && !from.height && !to.width && !to.height) {
return null;
}
var width = null;
var height = null;
var x = null;
var y = null;
var fromAspectRatio = 1;
var scaleFactorX = 1;
var scaleFactorY = 1;
if (from.width < from.height) {
fromAspectRatio = from.height/from.width;
scaleFactorX = to.width/from.width;
scaleFactorY = to.height/from.height;
height = to.width * (from.height/from.width);
width = to.width;
height = to.width * fromAspectRatio;
x = scaleFactorX * getNumFromPx(from.style.left);
y = (scaleFactorY * getNumFromPx(from.style.top)) * fromAspectRatio;
} else {
fromAspectRatio = from.width/from.height;
scaleFactorX = to.width/from.width;
scaleFactorY = to.height/from.height;
width = to.height * fromAspectRatio;
height = to.height;
x = (scaleFactorX * getNumFromPx(from.style.left)) * fromAspectRatio;
y = scaleFactorY * getNumFromPx(from.style.top);
width = to.height * (from.width/from.height);
}
return{width: width, height: height};
};
// (image, canvas) -> object
var getScaledDimensions = function(from, to) {
if(!from.style.width && !from.style.height && !to.width && !to.height) {
return null;
}
var current = {
width: getNumFromPx(from.style.width),
height: getNumFromPx(from.style.height),
top: getNumFromPx(from.style.top),
left: getNumFromPx(from.style.left)
};
var scaleFactorX = previewWidth / to.width;
var scaleFactorY = previewHeight / to.height;
var width = current.width / scaleFactorX;
var height = current.height / scaleFactorY;
var x = current.left / scaleFactorX;
var y = current.top / scaleFactorY;
if(current.width < previewWidth) {
x = ((previewWidth - current.width) / scaleFactorX ) / 2;
}
if(current.height < previewHeight) {
y = ((previewHeight - current.height) / scaleFactorY ) / 2;
}
return{width: width, height: height, x: x, y: y};
};
var writeUploadToCanvas = function(canvas, img) {
var dim = getAutoFitDimensions(img, canvas);
var dim = getScaledDimensions(img, canvas);
canvas.getContext("2d").drawImage(img, dim.x, dim.y, dim.width, dim.height);
console.log('writeUploadToCanvas', dim);
};
@ -200,7 +211,15 @@ define(['jquery', 'underscore', 'text!partials/buddypictureupload.html'], functi @@ -200,7 +211,15 @@ define(['jquery', 'underscore', 'text!partials/buddypictureupload.html'], functi
var moveImageRight = function() {
$scope.prevImage.style.left = incrementPx($scope.prevImage.style.left);
};
var moveImage = function(evt) {
var makeImageLarger = function() {
$scope.prevImage.style.height = incrementPx($scope.prevImage.style.height);
$scope.prevImage.style.width = incrementPx($scope.prevImage.style.width);
};
var makeImageSmaller = function() {
$scope.prevImage.style.height = decrementPx($scope.prevImage.style.height);
$scope.prevImage.style.width = decrementPx($scope.prevImage.style.width);
};
var changeImage = function(evt) {
if(evt.data.intervalNum.num || !evt.data.action) {
clearInterval(evt.data.intervalNum.num);
evt.data.intervalNum.num = null;
@ -211,15 +230,20 @@ define(['jquery', 'underscore', 'text!partials/buddypictureupload.html'], functi @@ -211,15 +230,20 @@ define(['jquery', 'underscore', 'text!partials/buddypictureupload.html'], functi
}
};
$element.find("#arrow-up").on('mousedown', null, {intervalNum: intervalNum, action: moveImageUp}, moveImage);
$element.find("#arrow-down").on('mousedown', null, {intervalNum: intervalNum, action: moveImageDown}, moveImage);
$element.find("#arrow-up").on('mouseup', null, {intervalNum: intervalNum}, moveImage);
$element.find("#arrow-down").on('mouseup', null, {intervalNum: intervalNum}, moveImage);
$element.find("#arrow-up").on('mousedown', null, {intervalNum: intervalNum, action: moveImageUp}, changeImage);
$element.find("#arrow-down").on('mousedown', null, {intervalNum: intervalNum, action: moveImageDown}, changeImage);
$element.find("#arrow-up").on('mouseup', null, {intervalNum: intervalNum}, changeImage);
$element.find("#arrow-down").on('mouseup', null, {intervalNum: intervalNum}, changeImage);
$element.find("#arrow-left").on('mousedown', null, {intervalNum: intervalNum, action: moveImageLeft}, changeImage);
$element.find("#arrow-right").on('mousedown', null, {intervalNum: intervalNum, action: moveImageRight}, changeImage);
$element.find("#arrow-left").on('mouseup', null, {intervalNum: intervalNum}, changeImage);
$element.find("#arrow-right").on('mouseup', null, {intervalNum: intervalNum}, changeImage);
$element.find("#arrow-left").on('mousedown', null, {intervalNum: intervalNum, action: moveImageLeft}, moveImage);
$element.find("#arrow-right").on('mousedown', null, {intervalNum: intervalNum, action: moveImageRight}, moveImage);
$element.find("#arrow-left").on('mouseup', null, {intervalNum: intervalNum}, moveImage);
$element.find("#arrow-right").on('mouseup', null, {intervalNum: intervalNum}, moveImage);
$element.find("#larger").on('mousedown', null, {intervalNum: intervalNum, action: makeImageLarger}, changeImage);
$element.find("#smaller").on('mousedown', null, {intervalNum: intervalNum, action: makeImageSmaller}, changeImage);
$element.find("#larger").on('mouseup', null, {intervalNum: intervalNum}, changeImage);
$element.find("#smaller").on('mouseup', null, {intervalNum: intervalNum}, changeImage);
};
return {

14
static/partials/buddypictureupload.html

@ -1,18 +1,18 @@ @@ -1,18 +1,18 @@
<div class="buddyPictureUpload collapse" collapse="!showUploadPicture">
<div class="showUploadPicture">
<canvas class="uploadPrev hidden"></canvas>
<div class="moveImage">
<div class="imageUtilites">
<div class="moveHorizontal">
<i id="arrow-left" class="fa fa-long-arrow-left"></i>
<i id="arrow-right" class="fa fa-long-arrow-right"></i>
</div>
<div class="moveVertical">
<div class="item">
<i id="arrow-up" class="fa fa-long-arrow-up"></i>
</div>
<div class="item">
<i id="arrow-down" class="fa fa-long-arrow-down"></i>
</div>
<i id="arrow-up" class="fa fa-long-arrow-up"></i>
<i id="arrow-down" class="fa fa-long-arrow-down"></i>
</div>
<div class="resize">
<i id="larger" class="fa fa-plus"></i>
<i id="smaller" class="fa fa-minus"></i>
</div>
</div>
<img class="preview" ng-class="{previewUpload: previewUpload}" ng-src="{{imgData}}" />

Loading…
Cancel
Save