Skip to content

Commit e9afe03

Browse files
committed
Implement background-size cover/contain
1 parent 57d20a9 commit e9afe03

File tree

3 files changed

+109
-111
lines changed

3 files changed

+109
-111
lines changed

build/html2canvas.js

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,8 @@ function parseBackgroundSizePosition(value, element, attribute, index) {
235235
value = (value || '').split(',');
236236
value = value[index || 0] || value[0] || 'auto';
237237
value = _html2canvas.Util.trimText(value).split(' ');
238-
239-
if(attribute === 'backgroundSize' && (!value[0] || value[0].match(/cover|contain|auto/))) {
240-
//these values will be handled in the parent function
238+
if(attribute === 'backgroundSize' && (value[0] && value[0].match(/^(cover|contain|auto)$/))) {
239+
return value;
241240
} else {
242241
value[0] = (value[0].indexOf( "%" ) === -1) ? toPX(element, attribute + "X", value[0]) : value[0];
243242
if(value[1] === undefined) {
@@ -296,71 +295,71 @@ _html2canvas.Util.resizeBounds = function( current_width, current_height, target
296295
};
297296
};
298297

299-
function backgroundBoundsFactory( prop, el, bounds, image, imageIndex, backgroundSize ) {
300-
var bgposition = _html2canvas.Util.getCSS( el, prop, imageIndex ) ,
301-
topPos,
302-
left,
303-
percentage,
304-
val;
298+
_html2canvas.Util.BackgroundPosition = function(element, bounds, image, imageIndex, backgroundSize ) {
299+
var backgroundPosition = _html2canvas.Util.getCSS(element, 'backgroundPosition', imageIndex),
300+
leftPosition,
301+
topPosition;
302+
if (backgroundPosition.length === 1){
303+
backgroundPosition = [backgroundPosition[0], backgroundPosition[0]];
304+
}
305305

306-
if (bgposition.length === 1){
307-
val = bgposition[0];
306+
if (backgroundPosition[0].toString().indexOf("%") !== -1){
307+
leftPosition = (bounds.width - (backgroundSize || image).width) * (parseFloat(backgroundPosition[0]) / 100);
308+
} else {
309+
leftPosition = parseInt(backgroundPosition[0], 10);
310+
}
308311

309-
bgposition = [];
312+
if (backgroundPosition[1] === 'auto') {
313+
topPosition = leftPosition / image.width * image.height;
314+
} else if (backgroundPosition[1].toString().indexOf("%") !== -1){
315+
topPosition = (bounds.height - (backgroundSize || image).height) * parseFloat(backgroundPosition[1]) / 100;
316+
} else {
317+
topPosition = parseInt(backgroundPosition[1], 10);
318+
}
310319

311-
bgposition[0] = val;
312-
bgposition[1] = val;
320+
if (backgroundPosition[0] === 'auto') {
321+
leftPosition = topPosition / image.height * image.width;
313322
}
314323

315-
if (bgposition[0].toString().indexOf("%") !== -1){
316-
percentage = (parseFloat(bgposition[0])/100);
317-
left = bounds.width * percentage;
318-
if(prop !== 'backgroundSize') {
319-
left -= (backgroundSize || image).width*percentage;
320-
}
324+
return {left: leftPosition, top: topPosition};
325+
};
326+
327+
_html2canvas.Util.BackgroundSize = function(element, bounds, image, imageIndex) {
328+
var backgroundSize = _html2canvas.Util.getCSS(element, 'backgroundSize', imageIndex),
329+
width,
330+
height;
331+
332+
if (backgroundSize.length === 1){
333+
backgroundSize = [backgroundSize[0], backgroundSize[0]];
334+
}
335+
336+
if (backgroundSize[0].toString().indexOf("%") !== -1){
337+
width = bounds.width * parseFloat(backgroundSize[0]) / 100;
338+
} else if(backgroundSize[0] === 'auto') {
339+
width = image.width;
321340
} else {
322-
if(prop === 'backgroundSize') {
323-
if(bgposition[0] === 'auto') {
324-
left = image.width;
341+
if (/contain|cover/.test(backgroundSize[0])) {
342+
var resized = _html2canvas.Util.resizeBounds(image.width, image.height, bounds.width, bounds.height, backgroundSize[0]);
343+
return {width: resized.width, height: resized.height};
325344
} else {
326-
if (/contain|cover/.test(bgposition[0])) {
327-
var resized = _html2canvas.Util.resizeBounds(image.width, image.height, bounds.width, bounds.height, bgposition[0]);
328-
left = resized.width;
329-
topPos = resized.height;
330-
} else {
331-
left = parseInt(bgposition[0], 10);
332-
}
345+
width = parseInt(backgroundSize[0], 10);
333346
}
334-
} else {
335-
left = parseInt( bgposition[0], 10);
336-
}
337347
}
338348

339-
340-
if(bgposition[1] === 'auto') {
341-
topPos = left / image.width * image.height;
342-
} else if (bgposition[1].toString().indexOf("%") !== -1){
343-
percentage = (parseFloat(bgposition[1])/100);
344-
topPos = bounds.height * percentage;
345-
if(prop !== 'backgroundSize') {
346-
topPos -= (backgroundSize || image).height * percentage;
347-
}
348-
349+
if(backgroundSize[1] === 'auto') {
350+
height = width / image.width * image.height;
351+
} else if (backgroundSize[1].toString().indexOf("%") !== -1){
352+
height = bounds.height * parseFloat(backgroundSize[1]) / 100;
349353
} else {
350-
topPos = parseInt(bgposition[1],10);
354+
height = parseInt(backgroundSize[1],10);
351355
}
352356

353-
return [left, topPos];
354-
}
355357

356-
_html2canvas.Util.BackgroundPosition = function( el, bounds, image, imageIndex, backgroundSize ) {
357-
var result = backgroundBoundsFactory( 'backgroundPosition', el, bounds, image, imageIndex, backgroundSize );
358-
return { left: result[0], top: result[1] };
359-
};
358+
if (backgroundSize[0] === 'auto') {
359+
width = height / image.height * image.width;
360+
}
360361

361-
_html2canvas.Util.BackgroundSize = function( el, bounds, image, imageIndex ) {
362-
var result = backgroundBoundsFactory( 'backgroundSize', el, bounds, image, imageIndex );
363-
return { width: result[0], height: result[1] };
362+
return {width: width, height: height};
364363
};
365364

366365
_html2canvas.Util.Extend = function (options, defaults) {
@@ -415,7 +414,7 @@ _html2canvas.Util.Children = function( elem ) {
415414
};
416415

417416
_html2canvas.Util.isTransparent = function(backgroundColor) {
418-
return (backgroundColor === "transparent" || backgroundColor === "rgba(0, 0, 0, 0)");
417+
return (!backgroundColor || backgroundColor === "transparent" || backgroundColor === "rgba(0, 0, 0, 0)");
419418
};
420419
_html2canvas.Util.Font = (function () {
421420

@@ -2945,7 +2944,7 @@ _html2canvas.Renderer.Canvas = function(options) {
29452944
canvas.height = canvas.style.height = options.height || zStack.ctx.height;
29462945

29472946
fstyle = ctx.fillStyle;
2948-
ctx.fillStyle = (Util.isTransparent(zStack.backgroundColor) && options.background !== undefined) ? options.background : parsedData.backgroundColor;
2947+
ctx.fillStyle = (Util.isTransparent(parsedData.backgroundColor) && options.background !== undefined) ? options.background : parsedData.backgroundColor;
29492948
ctx.fillRect(0, 0, canvas.width, canvas.height);
29502949
ctx.fillStyle = fstyle;
29512950

0 commit comments

Comments
 (0)