cleaned up some cropping logic in Image, and added a cropping unit test

This commit is contained in:
Eric Rowell 2012-11-30 20:01:10 -08:00
parent 047e26382e
commit 8a195618cf
4 changed files with 37 additions and 9 deletions

View File

@ -174,13 +174,11 @@
context.save();
var a = Array.prototype.slice.call(arguments);
if(a.length === 6 || a.length === 10) {
if(a.length === 6) {
context.drawImage(a[1], a[2], a[3], a[4], a[5]);
}
else {
context.drawImage(a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9]);
}
if(a.length === 6) {
context.drawImage(a[1], a[2], a[3], a[4], a[5]);
}
else if(a.length === 10) {
context.drawImage(a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9]);
}
context.restore();

View File

@ -38,8 +38,8 @@ Kinetic.Image.prototype = {
if(this.attrs.image) {
// if cropping
if(this.attrs.crop && this.attrs.crop.width && this.attrs.crop.height) {
var cropX = this.attrs.crop.x ? this.attrs.crop.x : 0;
var cropY = this.attrs.crop.y ? this.attrs.crop.y : 0;
var cropX = this.attrs.crop.x || 0;
var cropY = this.attrs.crop.y || 0;
var cropWidth = this.attrs.crop.width;
var cropHeight = this.attrs.crop.height;
params = [context, this.attrs.image, cropX, cropY, cropWidth, cropHeight, 0, 0, width, height];

File diff suppressed because one or more lines are too long

View File

@ -121,6 +121,35 @@ Test.Modules.IMAGE = {
};
imageObj.src = '../assets/darth-vader.jpg';
},
'crop add and scale image': function(containerId) {
var imageObj = new Image();
imageObj.onload = function() {
var stage = new Kinetic.Stage({
container: containerId,
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
darth = new Kinetic.Image({
x: 200,
y: 75,
image: imageObj,
width: 107,
height: 75,
crop: [186, 211, 292 - 186, 285 - 211],
draggable: true,
scale: [0.5, 0.5]
});
layer.add(darth);
stage.add(layer);
//console.log(layer.toDataURL());
warn(layer.toDataURL() === dataUrls['crop and scale image'], 'problem rendering cropped and scaled image');
};
imageObj.src = '../assets/darth-vader.jpg';
},
'create image hit region': function(containerId) {
var imageObj = new Image();