|
169 | 169 | , isDOMElement = function(object) {
|
170 | 170 | return typeof object === 'object' && object.nodeType === 1;
|
171 | 171 | }
|
172 |
| - , createDataURIFromElement = function(element, format) { |
| 172 | + , createDataURIFromElement = function(element, format, angle) { |
173 | 173 |
|
174 | 174 | //if element is an image which uses data url defintion, just return the dataurl
|
175 | 175 | if (element.nodeName === 'IMG' && element.hasAttribute('src')) {
|
176 | 176 | var src = ''+element.getAttribute('src');
|
177 |
| - if (src.indexOf('data:image/') === 0) return src; |
| 177 | + if (!angle && src.indexOf('data:image/') === 0) return src; |
178 | 178 |
|
179 | 179 | // only if the user doesn't care about a format
|
180 | 180 | if (!format && /\.png(?:[?#].*)?$/i.test(src)) format = 'png';
|
|
190 | 190 | var ctx = canvas.getContext('2d');
|
191 | 191 | if (!ctx) {
|
192 | 192 | 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); |
193 | 226 | }
|
194 |
| - ctx.drawImage(element, 0, 0, canvas.width, canvas.height); |
195 | 227 | }
|
196 | 228 | return canvas.toDataURL((''+format).toLowerCase() == 'png' ? 'image/png' : 'image/jpeg');
|
197 | 229 | }
|
|
479 | 511 | return info;
|
480 | 512 | };
|
481 | 513 |
|
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) { |
483 | 515 | 'use strict'
|
484 | 516 |
|
485 | 517 | if(typeof format !== 'string') {
|
|
491 | 523 | format = tmp;
|
492 | 524 | }
|
493 | 525 |
|
| 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 | + |
494 | 540 | if (isNaN(x) || isNaN(y))
|
495 | 541 | {
|
496 | 542 | console.error('jsPDF.addImage: Invalid coordinates', arguments);
|
|
503 | 549 | var dataAsBinaryString;
|
504 | 550 |
|
505 | 551 | if(isDOMElement(imageData))
|
506 |
| - imageData = createDataURIFromElement(imageData, format); |
| 552 | + imageData = createDataURIFromElement(imageData, format, rotation); |
507 | 553 |
|
508 | 554 | if(notDefined(alias))
|
509 | 555 | alias = generateAliasFromData(imageData);
|
|
0 commit comments