Skip to content

Commit 730ebcf

Browse files
committed
border-radius parsing
1 parent cce6e35 commit 730ebcf

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

src/Core.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ _html2canvas.Util.getCSS = function (el, attribute) {
120120
val[ 0 ] = ( val[0].indexOf( "%" ) === -1 ) ? toPX( attribute + "X", val[ 0 ] ) : val[ 0 ];
121121
val[ 1 ] = ( val[1] === undefined ) ? val[0] : val[1]; // IE 9 doesn't return double digit always
122122
val[ 1 ] = ( val[1].indexOf( "%" ) === -1 ) ? toPX( attribute + "Y", val[ 1 ] ) : val[ 1 ];
123+
} else if ( /border(Top|Bottom)(Left|Right)Radius/.test( attribute) ) {
124+
var arr = val.split(" ");
125+
if ( arr.length <= 1 ) {
126+
arr[ 1 ] = arr[ 0 ];
127+
}
128+
arr[ 0 ] = parseInt( arr[ 0 ], 10 );
129+
arr[ 1 ] = parseInt( arr[ 1 ], 10 );
130+
val = arr;
123131
}
124132

125133
} else if ( el.currentStyle ) {

src/Parse.js

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ _html2canvas.Parse = function ( images, options ) {
619619
by,
620620
bw,
621621
bh,
622+
i,
622623
borderArgs,
623624
borderBounds,
624625
borders = (function(el){
@@ -636,9 +637,10 @@ _html2canvas.Parse = function ( images, options ) {
636637
return borders;
637638

638639
}(el)),
640+
// http://www.w3.org/TR/css3-background/#the-border-radius
639641
borderRadius = (function( el ) {
640642
var borders = [],
641-
sides = ["TopLeft","TopRight","BottomLeft","BottomRight"],
643+
sides = ["TopLeft","TopRight","BottomRight","BottomLeft"],
642644
s;
643645

644646
for (s = 0; s < 4; s+=1){
@@ -664,42 +666,47 @@ _html2canvas.Parse = function ( images, options ) {
664666
// top border
665667
bh = borders[0].width;
666668

667-
borderArgs[ 0 ] = [ bx, by ]; // top left
668-
borderArgs[ 1 ] = [ bx + bw, by ]; // top right
669-
borderArgs[ 2 ] = [ bx + bw - borders[ 1 ].width, by + bh ]; // bottom right
670-
borderArgs[ 3 ] = [ bx + borders[ 3 ].width, by + bh ]; // bottom left
669+
i = 0;
670+
borderArgs[ i++ ] = [ "line", bx, by ]; // top left
671+
borderArgs[ i++ ] = [ "line", bx + bw, by ]; // top right
672+
borderArgs[ i++ ] = [ "line", bx + bw - borders[ 1 ].width, by + bh ]; // bottom right
673+
borderArgs[ i++ ] = [ "line", bx + borders[ 3 ].width, by + bh ]; // bottom left
671674

672675
break;
673676
case 1:
674677
// right border
675678
bx = x + w - (borders[1].width);
676679
bw = borders[1].width;
677680

678-
borderArgs[ 0 ] = [ bx, by + borders[ 0 ].width]; // top left
679-
borderArgs[ 1 ] = [ bx + bw, by ]; // top right
680-
borderArgs[ 2 ] = [ bx + bw, by + bh + borders[ 2 ].width ]; // bottom right
681-
borderArgs[ 3 ] = [ bx, by + bh ]; // bottom left
681+
i = 0;
682+
borderArgs[ i++ ] = [ "line", bx, by + borders[ 0 ].width]; // top left
683+
borderArgs[ i++ ] = [ "line", bx + bw, by ]; // top right
684+
borderArgs[ i++ ] = [ "line", bx + bw, by + bh + borders[ 2 ].width ]; // bottom right
685+
borderArgs[ i++ ] = [ "line", bx, by + bh ]; // bottom left
682686

683687
break;
684688
case 2:
685689
// bottom border
686690
by = (by + h) - (borders[2].width);
687691
bh = borders[2].width;
688-
689-
borderArgs[ 0 ] = [ bx + borders[ 3 ].width, by ]; // top left
690-
borderArgs[ 1 ] = [ bx + bw - borders[ 2 ].width, by ]; // top right
691-
borderArgs[ 2 ] = [ bx + bw, by + bh ]; // bottom right
692-
borderArgs[ 3 ] = [ bx, by + bh ]; // bottom left
692+
693+
694+
i = 0;
695+
borderArgs[ i++ ] = [ "line", bx + borders[ 3 ].width, by ]; // top left
696+
borderArgs[ i++ ] = [ "line", bx + bw - borders[ 2 ].width, by ]; // top right
697+
borderArgs[ i++ ] = [ "line", bx + bw, by + bh ]; // bottom right
698+
borderArgs[ i++ ] = [ "line", bx, by + bh ]; // bottom left
693699

694700
break;
695701
case 3:
696702
// left border
697703
bw = borders[3].width;
698704

699-
borderArgs[ 0 ] = [ bx, by ]; // top left
700-
borderArgs[ 1 ] = [ bx + bw, by + borders[ 0 ].width ]; // top right
701-
borderArgs[ 2 ] = [ bx + bw, by + bh ]; // bottom right
702-
borderArgs[ 3 ] = [ bx, by + bh + borders[ 2 ].width ]; // bottom left
705+
i = 0;
706+
borderArgs[ i++ ] = [ "line", bx, by ]; // top left
707+
borderArgs[ i++ ] = [ "line", bx + bw, by + borders[ 0 ].width ]; // top right
708+
borderArgs[ i++ ] = [ "line", bx + bw, by + bh ]; // bottom right
709+
borderArgs[ i++ ] = [ "line", bx, by + bh + borders[ 2 ].width ]; // bottom left
703710

704711
break;
705712
}
@@ -721,12 +728,13 @@ _html2canvas.Parse = function ( images, options ) {
721728
if ( borderData.color !== "transparent" ){
722729
ctx.setVariable( "fillStyle", borderData.color );
723730

724-
var shape = ctx.drawShape();
725-
shape.moveTo.apply( null, borderArgs[ 0 ] ); // top left
726-
shape.lineTo.apply( null, borderArgs[ 1 ] ); // top right
727-
shape.lineTo.apply( null, borderArgs[ 2 ] ); // bottom right
728-
shape.lineTo.apply( null, borderArgs[ 3 ] ); // bottom left
729-
// ctx.fillRect (x, y, w, h);
731+
var shape = ctx.drawShape(),
732+
numBorderArgs = borderArgs.length;
733+
734+
for ( i = 0; i < numBorderArgs; i++ ) {
735+
shape[( i === 0) ? "moveTo" : borderArgs[ i ][ 0 ] + "To" ].apply( null, borderArgs[ i ].slice(1) );
736+
}
737+
730738
numDraws+=1;
731739
}
732740

tests/borders.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737
height: 201px;
3838
top: 20px;
3939
left: 170px;
40-
border: 20px dotted #990000;
40+
border: 20px solid #990000;
4141
background-color: #ffdddd;
4242
text-align: center;
43-
44-
border-top-color: blue;
43+
border-radius: 50px/160px;
44+
border-left-color: violet;
4545
border-top-width:0px;
4646
}
4747

0 commit comments

Comments
 (0)