Skip to content

Commit b6f019c

Browse files
committed
Added Image Rotation support - closes #338
1 parent 2b53fb4 commit b6f019c

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

examples/basic.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,16 @@ <h2><a href="#">Draw example: Images</a></h2>
467467
doc.addImage('monkey', 70, 10, 100, 120); // use the cached 'monkey' image, JPEG is optional regardless
468468
// As you can guess, using the cached image reduces the generated PDF size by 50%!
469469

470+
// Rotate Image - new feature as of 2014-09-20
471+
doc.addImage({
472+
imageData : imgData,
473+
angle : -20,
474+
x : 10,
475+
y : 78,
476+
w : 45,
477+
h : 58
478+
});
479+
470480
// Output as Data URI
471481
doc.output('datauri');
472482
}

jspdf.plugin.addimage.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,12 @@
169169
, isDOMElement = function(object) {
170170
return typeof object === 'object' && object.nodeType === 1;
171171
}
172-
, createDataURIFromElement = function(element, format) {
172+
, createDataURIFromElement = function(element, format, angle) {
173173

174174
//if element is an image which uses data url defintion, just return the dataurl
175175
if (element.nodeName === 'IMG' && element.hasAttribute('src')) {
176176
var src = ''+element.getAttribute('src');
177-
if (src.indexOf('data:image/') === 0) return src;
177+
if (!angle && src.indexOf('data:image/') === 0) return src;
178178

179179
// only if the user doesn't care about a format
180180
if (!format && /\.png(?:[?#].*)?$/i.test(src)) format = 'png';
@@ -190,8 +190,40 @@
190190
var ctx = canvas.getContext('2d');
191191
if (!ctx) {
192192
throw ('addImage requires canvas to be supported by browser.');
193+
}document.body.appendChild(canvas);
194+
if (angle) {
195+
var x, y, b, c, s, w, h, to_radians = Math.PI/180, angleInRadians;
196+
197+
if (typeof angle === 'object') {
198+
x = angle.x;
199+
y = angle.y;
200+
b = angle.bg;
201+
angle = angle.angle;
202+
}
203+
angleInRadians = angle*to_radians;
204+
c = Math.abs(Math.cos(angleInRadians));
205+
s = Math.abs(Math.sin(angleInRadians));
206+
w = canvas.width;
207+
h = canvas.height;
208+
canvas.width = h * s + w * c;
209+
canvas.height = h * c + w * s;
210+
211+
if (isNaN(x)) x = canvas.width / 2;
212+
if (isNaN(y)) y = canvas.height / 2;
213+
214+
ctx.clearRect(0,0,canvas.width, canvas.height);
215+
ctx.fillStyle = b || 'white';
216+
ctx.fillRect(0, 0, canvas.width, canvas.height);
217+
ctx.save();
218+
ctx.translate(x, y);
219+
ctx.rotate(angleInRadians);
220+
ctx.drawImage(element, -(w/2), -(h/2));
221+
ctx.rotate(-angleInRadians);
222+
ctx.translate(-x, -y);
223+
ctx.restore();
224+
} else {
225+
ctx.drawImage(element, 0, 0, canvas.width, canvas.height);
193226
}
194-
ctx.drawImage(element, 0, 0, canvas.width, canvas.height);
195227
}
196228
return canvas.toDataURL((''+format).toLowerCase() == 'png' ? 'image/png' : 'image/jpeg');
197229
}
@@ -479,7 +511,7 @@
479511
return info;
480512
};
481513

482-
jsPDFAPI.addImage = function(imageData, format, x, y, w, h, alias, compression) {
514+
jsPDFAPI.addImage = function(imageData, format, x, y, w, h, alias, compression, rotation) {
483515
'use strict'
484516

485517
if(typeof format !== 'string') {
@@ -491,6 +523,20 @@
491523
format = tmp;
492524
}
493525

526+
if (typeof imageData === 'object' && !isDOMElement(imageData) && "imageData" in imageData) {
527+
var options = imageData;
528+
529+
imageData = options.imageData;
530+
format = options.format || format;
531+
x = options.x || x || 0;
532+
y = options.y || y || 0;
533+
w = options.w || w;
534+
h = options.h || h;
535+
alias = options.alias || alias;
536+
compression = options.compression || compression;
537+
rotation = options.rotation || options.angle || rotation;
538+
}
539+
494540
if (isNaN(x) || isNaN(y))
495541
{
496542
console.error('jsPDF.addImage: Invalid coordinates', arguments);
@@ -503,7 +549,7 @@
503549
var dataAsBinaryString;
504550

505551
if(isDOMElement(imageData))
506-
imageData = createDataURIFromElement(imageData, format);
552+
imageData = createDataURIFromElement(imageData, format, rotation);
507553

508554
if(notDefined(alias))
509555
alias = generateAliasFromData(imageData);

0 commit comments

Comments
 (0)