Skip to content

Instantly share code, notes, and snippets.

@hagino3000
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save hagino3000/4560e4be81e55749314c to your computer and use it in GitHub Desktop.

Select an option

Save hagino3000/4560e4be81e55749314c to your computer and use it in GitHub Desktop.
画像の真ん中をいい感じに切り取るスクリプト
define(['utils', 'template'], function (Utils, JST) {
'use strict';
function render() {
// テンプレート使ってレンダリングする例
$('#target').html(JST['app/templates/fuga.ejs'], {images: [],....});
// 画像を60px * 60pxの枠に納める場合
var adjustFn = Utils.createImageAdjustFn(60, 60);
$('#target img').on('laod', function() {
adjustFn($(this)).removeClass('need_adjust');
});
}
render();
});
define([], function () {
'use strict';
function createImageAdjustFn(boxWidth, boxHeight) {
return function($img) {
var width = $img.width();
var height = $img.height();
if (width > height) {
$img.css({
'height': boxHeight,
'left': -1 * Math.floor(boxWidth/2 * (width/height - 1))
});
} else {
$img.css({
'width': boxWidth,
'top': -1 * Math.floor(boxHeight/2 * (height/width - 1))
});
}
return $img;
}
}
return {
createImageAdjustFn: createImageAdjustFn
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment