Skip to content

Commit

Permalink
You can now use the HTML5 attributes data-camanwidth and/or data-cama…
Browse files Browse the repository at this point in the history
…nheight to resize the canvas
  • Loading branch information
meltingice committed Mar 8, 2011
1 parent 654604c commit ced52e9
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 60 deletions.
37 changes: 20 additions & 17 deletions dist/caman.full.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,26 +159,28 @@ var finishInit = function (image, canvas, callback) {
this.layerStack = [];

var old_height = image.height, old_width = image.width;
if (image.style.width || image.style.height) {
if (image.style.width) {
image.width = parseInt(image.style.width, 10);

if (image.style.height) {
image.height = parseInt(image.style.height, 10);
var new_width = image.dataset.camanwidth || canvas.dataset.camanwidth;
var new_height = image.dataset.camanheight || canvas.dataset.camanheight;
if (new_width || new_height) {
if (new_width) {
image.width = parseInt(new_width, 10);

if (new_height) {
image.height = parseInt(new_height, 10);
} else {
image.height = image.width * old_height / old_width;
}
} else if (image.style.height) {
image.height = parseInt(image.style.height, 10);
} else if (new_height) {
image.height = parseInt(new_height, 10);
image.width = image.height * old_width / old_height;
}
}

canvas.width = image.width;
canvas.height = image.height;

this.canvas = canvas;
this.context = canvas.getContext("2d");
this.canvas = canvas;
this.context = this.canvas.getContext("2d");
this.context.drawImage(image, 0, 0, image.width, image.height);

this.image_data = this.context.getImageData(0, 0, image.width, image.height);
Expand Down Expand Up @@ -212,6 +214,14 @@ Caman.manip = Caman.prototype = {
}

canvas.id = image.id;

if (image.dataset.camanwidth) {
canvas.dataset.camanwidth = image.dataset.camanwidth;
}
if (image.dataset.camanheight) {
canvas.dataset.camanheight = image.dataset.camanheight;
}

image.parentNode.replaceChild(canvas, image);

// Store the canvas ID
Expand Down Expand Up @@ -292,13 +302,6 @@ Caman.manip = Caman.prototype = {
throw "Given element ID isn't a canvas: " + canvas_id;
}

if (canvas.width) {
image.width = canvas.width;
}
if (canvas.height) {
image.height = canvas.height;
}

image.onload = function () {
finishInit.call(self, image, canvas, callback);
};
Expand Down
10 changes: 5 additions & 5 deletions dist/caman.full.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 20 additions & 17 deletions dist/caman.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,26 +159,28 @@ var finishInit = function (image, canvas, callback) {
this.layerStack = [];

var old_height = image.height, old_width = image.width;
if (image.style.width || image.style.height) {
if (image.style.width) {
image.width = parseInt(image.style.width, 10);

if (image.style.height) {
image.height = parseInt(image.style.height, 10);
var new_width = image.dataset.camanwidth || canvas.dataset.camanwidth;
var new_height = image.dataset.camanheight || canvas.dataset.camanheight;
if (new_width || new_height) {
if (new_width) {
image.width = parseInt(new_width, 10);

if (new_height) {
image.height = parseInt(new_height, 10);
} else {
image.height = image.width * old_height / old_width;
}
} else if (image.style.height) {
image.height = parseInt(image.style.height, 10);
} else if (new_height) {
image.height = parseInt(new_height, 10);
image.width = image.height * old_width / old_height;
}
}

canvas.width = image.width;
canvas.height = image.height;

this.canvas = canvas;
this.context = canvas.getContext("2d");
this.canvas = canvas;
this.context = this.canvas.getContext("2d");
this.context.drawImage(image, 0, 0, image.width, image.height);

this.image_data = this.context.getImageData(0, 0, image.width, image.height);
Expand Down Expand Up @@ -212,6 +214,14 @@ Caman.manip = Caman.prototype = {
}

canvas.id = image.id;

if (image.dataset.camanwidth) {
canvas.dataset.camanwidth = image.dataset.camanwidth;
}
if (image.dataset.camanheight) {
canvas.dataset.camanheight = image.dataset.camanheight;
}

image.parentNode.replaceChild(canvas, image);

// Store the canvas ID
Expand Down Expand Up @@ -292,13 +302,6 @@ Caman.manip = Caman.prototype = {
throw "Given element ID isn't a canvas: " + canvas_id;
}

if (canvas.width) {
image.width = canvas.width;
}
if (canvas.height) {
image.height = canvas.height;
}

image.onload = function () {
finishInit.call(self, image, canvas, callback);
};
Expand Down
10 changes: 5 additions & 5 deletions dist/caman.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 19 additions & 16 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,26 +159,28 @@ var finishInit = function (image, canvas, callback) {
this.layerStack = [];

var old_height = image.height, old_width = image.width;
if (image.style.width || image.style.height) {
if (image.style.width) {
image.width = parseInt(image.style.width, 10);
var new_width = image.dataset.camanwidth || canvas.dataset.camanwidth;
var new_height = image.dataset.camanheight || canvas.dataset.camanheight;
if (new_width || new_height) {
if (new_width) {
image.width = parseInt(new_width, 10);

if (image.style.height) {
image.height = parseInt(image.style.height, 10);
if (new_height) {
image.height = parseInt(new_height, 10);
} else {
image.height = image.width * old_height / old_width;
}
} else if (image.style.height) {
image.height = parseInt(image.style.height, 10);
} else if (new_height) {
image.height = parseInt(new_height, 10);
image.width = image.height * old_width / old_height;
}
}

canvas.width = image.width;
canvas.height = image.height;

this.canvas = canvas;
this.context = canvas.getContext("2d");
this.canvas = canvas;
this.context = this.canvas.getContext("2d");
this.context.drawImage(image, 0, 0, image.width, image.height);

this.image_data = this.context.getImageData(0, 0, image.width, image.height);
Expand Down Expand Up @@ -212,6 +214,14 @@ Caman.manip = Caman.prototype = {
}

canvas.id = image.id;

if (image.dataset.camanwidth) {
canvas.dataset.camanwidth = image.dataset.camanwidth;
}
if (image.dataset.camanheight) {
canvas.dataset.camanheight = image.dataset.camanheight;
}

image.parentNode.replaceChild(canvas, image);

// Store the canvas ID
Expand Down Expand Up @@ -292,13 +302,6 @@ Caman.manip = Caman.prototype = {
throw "Given element ID isn't a canvas: " + canvas_id;
}

if (canvas.width) {
image.width = canvas.width;
}
if (canvas.height) {
image.height = canvas.height;
}

image.onload = function () {
finishInit.call(self, image, canvas, callback);
};
Expand Down

0 comments on commit ced52e9

Please sign in to comment.