Phylo/Treedrawer/Canvas.pm untested

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