| 1 | package Bio::Phylo::Treedrawer::Canvas; | ||
| 2 | use strict; | ||
| 3 | use base 'Bio::Phylo::Treedrawer::Abstract'; | ||
| 4 | use Bio::Phylo::Util::Exceptions 'throw'; | ||
| 5 | use Bio::Phylo::Util::Logger ':levels'; | ||
| 6 | use Bio::Phylo::Util::CONSTANT '_PI_'; | ||
| 7 | |||
| 8 | my $logger = Bio::Phylo::Util::Logger->new; | ||
| 9 | my $PI = _PI_; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Bio::Phylo::Treedrawer::Canvas - Graphics format writer used by treedrawer, no | ||
| 14 | serviceable parts inside | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | This module creates an HTML5 canvas graphic from a Bio::Phylo::Forest::DrawTree | ||
| 19 | object. It is called by the L<Bio::Phylo::Treedrawer> object, so look there to | ||
| 20 | learn how to create tree drawings. | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | sub _new { | ||
| 25 | my $class = shift; | ||
| 26 | my %args = @_; | ||
| 27 | my $tmpl = do { local $/; <DATA> }; | ||
| 28 | my $canvas = sprintf( $tmpl, | ||
| 29 | 'myCanvas', | ||
| 30 | $args{'-drawer'}->get_width, | ||
| 31 | $args{'-drawer'}->get_height, 'myCanvas' ); | ||
| 32 | my $self = $class->SUPER::_new( %args, '-api' => \$canvas ); | ||
| 33 | return bless $self, $class; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub _finish { | ||
| 37 | my $self = shift; | ||
| 38 | my $api = $self->_api; | ||
| 39 | my $shape = $self->_drawer->get_shape; | ||
| 40 | if ( $shape =~ /^curvy/i ) { | ||
| 41 | $$api .= "drawCurvedTree(branches);\n"; | ||
| 42 | } | ||
| 43 | elsif ( $shape =~ /^rect/i ) { | ||
| 44 | $$api .= "drawRectangularTree(branches);\n"; | ||
| 45 | } | ||
| 46 | elsif ( $shape =~ /^diag/i ) { | ||
| 47 | $$api .= "drawDiagonalTree(branches);\n"; | ||
| 48 | } | ||
| 49 | elsif( $shape =~ /^radial/i ) { | ||
| 50 | $$api .= "drawRadialTree(branches);\n"; | ||
| 51 | } | ||
| 52 | $$api .= '</script>'; | ||
| 53 | return $$api; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub _draw_text { | ||
| 57 | my $self = shift; | ||
| 58 | my %args = @_; | ||
| 59 | my ($x,$y,$text,$url,$rotation) = @args{qw(-x -y -text -url -rotation)}; | ||
| 60 | my $api = $self->_api; | ||
| 61 | $$api .= "drawText(ctx,$x,$y,'$text',0);\n"; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _draw_circle { | ||
| 65 | my $self = shift; | ||
| 66 | my %args = @_; | ||
| 67 | my ( $x, $y, $radius, $width, $stroke, $fill, $api, $url ) = | ||
| 68 | @args{qw(-x -y -radius -width -stroke -fill -api -url)}; | ||
| 69 | if ($radius) { | ||
| 70 | my $api = $self->_api; | ||
| 71 | $$api .= "drawCircle(ctx,$x,$y,$radius);\n"; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _draw_line { | ||
| 76 | my $self = shift; | ||
| 77 | my %args = @_; | ||
| 78 | my @keys = qw(-x1 -y1 -x2 -y2 -width -color ); | ||
| 79 | my ( $x1, $y1, $x2, $y2, $width, $color ) = @args{@keys}; | ||
| 80 | my $api = $self->_api; | ||
| 81 | $$api .= "drawLine(ctx,$x1,$y1,$x2,$y2);\n"; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _draw_curve { | ||
| 85 | my $self = shift; | ||
| 86 | my %args = @_; | ||
| 87 | my @keys = qw(-x1 -y1 -x2 -y2 -width -color); | ||
| 88 | my ( $x1, $y1, $x2, $y2, $width, $color ) = @args{@keys}; | ||
| 89 | my $api = $self->_api; | ||
| 90 | $$api .= "drawCurve(ctx,$x1,$y1,$x2,$y2);\n"; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _draw_multi { | ||
| 94 | my $self = shift; | ||
| 95 | my %args = @_; | ||
| 96 | my @keys = qw(-x1 -y1 -x2 -y2 -width -color); | ||
| 97 | my ( $x1, $y1, $x2, $y2, $width, $color ) = @args{@keys}; | ||
| 98 | my $api = $self->_api; | ||
| 99 | $$api .= "drawMulti(ctx,$x1,$y1,$x2,$y2);\n"; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _draw_triangle { | ||
| 103 | my $self = shift; | ||
| 104 | my %args = @_; | ||
| 105 | my @coord = qw(-x1 -y1 -x2 -y2 -x3 -y3); | ||
| 106 | my ( $x1, $y1, $x2, $y2, $x3, $y3 ) = @args{@coord}; | ||
| 107 | my @optional = qw(-fill -stroke -width -url -api); | ||
| 108 | my $fill = $args{'-fill'} || 'white'; | ||
| 109 | my $stroke = $args{'-stroke'} || 'black'; | ||
| 110 | my $width = $args{'-width'} || 1; | ||
| 111 | my $api = $self->_api; | ||
| 112 | $$api .= "drawTriangle(ctx,$x1,$y1,$x2,$y2,$x3,$y3);\n"; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub _draw_branch { | ||
| 116 | my ( $self, $node ) = @_; | ||
| 117 | $logger->info( "Drawing branch for " . $node->get_internal_name ); | ||
| 118 | if ( my $parent = $node->get_parent ) { | ||
| 119 | my ( $x1, $x2 ) = ( int $parent->get_x, int $node->get_x ); | ||
| 120 | my ( $y1, $y2 ) = ( int $parent->get_y, int $node->get_y ); | ||
| 121 | my $width = $self->_drawer->get_branch_width($node); | ||
| 122 | my $shape = $self->_drawer->get_shape; | ||
| 123 | my $drawer = '_draw_curve'; | ||
| 124 | if ( $shape =~ m/CURVY/i ) { | ||
| 125 | $drawer = '_draw_curve'; | ||
| 126 | } | ||
| 127 | elsif ( $shape =~ m/RECT/i ) { | ||
| 128 | $drawer = '_draw_multi'; | ||
| 129 | } | ||
| 130 | elsif ( $shape =~ m/DIAG/i ) { | ||
| 131 | $drawer = '_draw_line'; | ||
| 132 | } | ||
| 133 | elsif ( $shape =~ m/RADIAL/i ) { | ||
| 134 | $drawer = '_draw_arc'; | ||
| 135 | } | ||
| 136 | my $api = $self->_api; | ||
| 137 | $$api .= "branches.push({x1:$x1,y1:$y1,x2:$x2,y2:$y2});\n"; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | =head1 SEE ALSO | ||
| 142 | |||
| 143 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||
| 144 | for any user or developer questions and discussions. | ||
| 145 | |||
| 146 | =over | ||
| 147 | |||
| 148 | =item L<Bio::Phylo::Treedrawer> | ||
| 149 | |||
| 150 | The canvas treedrawer is called by the L<Bio::Phylo::Treedrawer> object. Look | ||
| 151 | there to learn how to create tree drawings. | ||
| 152 | |||
| 153 | =item L<Bio::Phylo::Manual> | ||
| 154 | |||
| 155 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>. | ||
| 156 | |||
| 157 | =back | ||
| 158 | |||
| 159 | =head1 CITATION | ||
| 160 | |||
| 161 | If you use Bio::Phylo in published research, please cite it: | ||
| 162 | |||
| 163 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 164 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 165 | I<BMC Bioinformatics> B<12>:63. | ||
| 166 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | 1; | ||
| 171 | __DATA__ | ||
| 172 | <canvas id="%s" width="%s" height="%s"> | ||
| 173 | <p>Your browser doesn't support canvas.</p> | ||
| 174 | </canvas> | ||
| 175 | <script type="text/javascript"> | ||
| 176 | function drawCurve( ctx, x1, y1, x2, y2 ) { | ||
| 177 | ctx.beginPath(); | ||
| 178 | ctx.moveTo( x1, y1 ); | ||
| 179 | ctx.bezierCurveTo( x1, (y1+y2)/2, (x1+x2)/2, y2, x2, y2 ); | ||
| 180 | ctx.stroke(); | ||
| 181 | } | ||
| 182 | |||
| 183 | function drawLine( ctx, x1, y1, x2, y2 ) { | ||
| 184 | ctx.beginPath(); | ||
| 185 | ctx.moveTo( x1, y1 ); | ||
| 186 | ctx.lineTo( x2, y2 ); | ||
| 187 | ctx.stroke(); | ||
| 188 | } | ||
| 189 | |||
| 190 | function drawMulti( ctx, x1, y1, x2, y2 ) { | ||
| 191 | ctx.beginPath(); | ||
| 192 | ctx.moveTo( x1, y1 ); | ||
| 193 | ctx.lineTo( x1, y2 ); | ||
| 194 | ctx.lineTo( x2, y2 ); | ||
| 195 | ctx.stroke(); | ||
| 196 | } | ||
| 197 | |||
| 198 | function drawTriangle( ctx, x1, y1, x2, y2, x3, y3 ) { | ||
| 199 | ctx.beginPath(); | ||
| 200 | ctx.moveTo( x1, y1 ); | ||
| 201 | ctx.lineTo( x2, y2 ); | ||
| 202 | ctx.lineTo( x3, y3 ); | ||
| 203 | ctx.lineTo( x1, y1 ); | ||
| 204 | ctx.fill(); | ||
| 205 | } | ||
| 206 | |||
| 207 | function drawCircle( ctx, x, y, radius ) { | ||
| 208 | ctx.beginPath(); | ||
| 209 | ctx.arc( x, y, radius, 0, Math.PI * 2, false ); | ||
| 210 | ctx.fill(); | ||
| 211 | } | ||
| 212 | |||
| 213 | function polarToCartesian( radius, angleInRadians ) { | ||
| 214 | var x = radius * Math.cos(angleInRadians); | ||
| 215 | var y = radius * Math.sin(angleInRadians); | ||
| 216 | return {x:x,y:y}; | ||
| 217 | } | ||
| 218 | |||
| 219 | function cartesianToPolar( ctx, x, y ) { | ||
| 220 | var width = ctx.canvas.width; | ||
| 221 | var height = ctx.canvas.height; | ||
| 222 | var cx = width / 2; | ||
| 223 | var cy = height / 2; | ||
| 224 | var x1 = x - cx; | ||
| 225 | var y1 = y - cy; | ||
| 226 | var radius = Math.sqrt( y1 * y1 + x1 * x1 ); | ||
| 227 | var angleInDegrees = Math.atan2( y1, x1 ); | ||
| 228 | if ( angleInDegrees < 0 ) { | ||
| 229 | angleInDegrees = angleInDegrees + 2 * Math.PI; | ||
| 230 | } | ||
| 231 | return {angle:angleInDegrees,radius:radius}; | ||
| 232 | } | ||
| 233 | |||
| 234 | function drawArc( ctx, x1, y1, x2, y2 ) { | ||
| 235 | ctx.beginPath(); | ||
| 236 | var width = ctx.canvas.width; | ||
| 237 | var height = ctx.canvas.height; | ||
| 238 | var cx = width / 2; | ||
| 239 | var cy = height / 2; | ||
| 240 | var r1 = cartesianToPolar( ctx, x1, y1 ); | ||
| 241 | var r2 = cartesianToPolar( ctx, x2, y2 ); | ||
| 242 | ctx.arc( cx, cy, r1.radius, r1.angle, r2.angle, r1.angle > r2.angle ); | ||
| 243 | ctx.stroke(); | ||
| 244 | } | ||
| 245 | |||
| 246 | function drawText( ctx, x, y, text, rotation ) { | ||
| 247 | ctx.rotate(rotation); | ||
| 248 | ctx.fillText( text, x, y ); | ||
| 249 | ctx.rotate(0); | ||
| 250 | } | ||
| 251 | |||
| 252 | function drawCurvedTree (allBranches) { | ||
| 253 | for ( var i = 0; i < allBranches.length; i++ ) { | ||
| 254 | var branch = allBranches[i]; | ||
| 255 | drawCurve(ctx,branch.x1,branch.y1,branch.x2,branch.y2); | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | function drawDiagonalTree (allBranches) { | ||
| 260 | for ( var i = 0; i < allBranches.length; i++ ) { | ||
| 261 | var branch = allBranches[i]; | ||
| 262 | drawLine(ctx,branch.x1,branch.y1,branch.x2,branch.y2); | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | function drawRectangularTree (allBranches) { | ||
| 267 | for ( var i = 0; i < allBranches.length; i++ ) { | ||
| 268 | var branch = allBranches[i]; | ||
| 269 | drawMulti(ctx,branch.x1,branch.y1,branch.x2,branch.y2); | ||
| 270 | } | ||
| 271 | } | ||
| 272 | |||
| 273 | function drawRadialTree (allBranches) { | ||
| 274 | for ( var i = 0; i < allBranches.length; i++ ) { | ||
| 275 | var branch = allBranches[i]; | ||
| 276 | drawArc(ctx,branch.x1,branch.y1,branch.x2,branch.y2); | ||
| 277 | var r1 = cartesianToPolar(ctx,branch.x1,branch.y1); | ||
| 278 | var r2 = cartesianToPolar(ctx,branch.x2,branch.y2); | ||
| 279 | var r3 = polarToCartesian(r1.radius,r2.angle); | ||
| 280 | var width = ctx.canvas.width; | ||
| 281 | var height = ctx.canvas.height; | ||
| 282 | var cx = width / 2; | ||
| 283 | var cy = height / 2; | ||
| 284 | drawLine(ctx,r3.x + cx,r3.y + cy,branch.x2,branch.y2); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 | var canvas = document.getElementById('%s'); | ||
| 289 | var ctx = canvas.getContext('2d'); | ||
| 290 | var branches = new Array(); |