Blame | Letzte Änderung | Log anzeigen | RSS feed
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=iso-iso-8859-1"><LINK REL="Start" HREF="index.html"><LINK REL="Contents" HREF="toc.html"><LINK REL="Prev" HREF="113Addinglinesandrectanglestoacanvas.html"><LINK REL="Next" HREF="115SampleapplicationDrawingDBschema.html"><STYLE TYPE="text/css"><!--BODY { font-family: serif }H1 { font-family: sans-serif }H2 { font-family: sans-serif }H3 { font-family: sans-serif }H4 { font-family: sans-serif }H5 { font-family: sans-serif }H6 { font-family: sans-serif }SUB { font-size: smaller }SUP { font-size: smaller }PRE { font-family: monospace }A { text-decoration: none }--></STYLE></HEAD><BODY><A HREF="toc.html">Contents</A><A HREF="113Addinglinesandrectanglestoacanvas.html">Previous</A><A HREF="115SampleapplicationDrawingDBschema.html">Next</A><HR NOSHADE><H2><A NAME="11_4">11.4 Using a canvas scale</A></H2><P> The previous method using absolute coordinates works. But nothingmore. It doesn't give you any chance to easily scale the image (unlessyou manually recalculate all used coordinates) , it gets tedious towork with pixel level resolution. Especially if you just like to draw afew basic shapes.</P><P> To help with this you can use a scale for the canvas. This lets youdefine a "work-space" of your choice. You can for example set thecoordinates to be between X:0-10, Y:0-10. This makes it easier toposition objects on the canvas. This also has two additionaladvantages:</P><UL><LI> If you increase the size of the canvas all objects will beautomatically scale to keep their proportions without any changes.</LI><LI> You can shrink/enlarge your drawing (not the image) by just usinganother scale. For example if you originally draw the image using a(0:10, 0:10) scale and then change the scale to (0:20, 0:20) then theeffect will be that you drawings will "shrink" to half their size.</LI></UL><P></P><P> To use this type of scaling you must make sure you include the file"jpgraph_canvtools.php" . In addition to the scaling class their arealso a couple of other utility classes that may come in handy,especially the <A href="../ref/Shape.html#_C_SHAPE"> Shape</A> class.</P><P> Using the scale is quite simple. You first instantiate a scaleobject passing the graph as a parameter and then specify the scale youwant to use. This means you need to add the lines<DIV class="phpscript"><CODE><FONT color="#000000"> <FONT color="#0000BB"> $scale </FONT><FONT color="#007700">= new </FONT><FONT color="#0000BB">CanvasScale</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$scale</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">Set</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">0</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">$xmax</FONT><FONTcolor="#007700">,</FONT><FONT color="#0000BB">0</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">$ymax</FONT><FONT color="#007700">);</FONT><FONTcolor="#0000BB"></FONT></FONT></CODE></DIV></P><P></P><P> to your code. You can then use one of the translation methods (forexample <A href="../ref/CanvasScale.html#_CANVASSCALE_TRANSLATE">CanvasScale::Translate()</A>) in the canvas scale class to translatebetween your world coordinates and the absolute screen coordinates.This means you could take the code in the example above and just addthe lines, for example,<DIV class="phpscript"><CODE><FONT color="#000000"><FONT color="#0000BB"> </FONT><FONT color="#007700">list(</FONT><FONT color="#0000BB">$x1</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">$y1</FONT><FONTcolor="#007700">) = </FONT><FONT color="#0000BB">$this</FONT><FONT color="#007700">-></FONT><FONT color="#0000BB">scale</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">Translate</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">$x1</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">$y1</FONT><FONTcolor="#007700">);<BR>list(</FONT><FONT color="#0000BB">$x2</FONT><FONT color="#007700">,</FONT><FONTcolor="#0000BB">$y2</FONT><FONT color="#007700">) = </FONT><FONT color="#0000BB">$this</FONT><FONT color="#007700">-></FONT><FONT color="#0000BB">scale</FONT><FONTcolor="#007700">-></FONT><FONT color="#0000BB">Translate</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">$x2</FONT><FONT color="#007700">,</FONT><FONTcolor="#0000BB">$y2</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">img</FONT><FONT color="#007700">-></FONT><FONT color="#0000BB">Line</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">$x1</FONT><FONTcolor="#007700">,</FONT><FONT color="#0000BB">$y1</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">$x2</FONT><FONT color="#007700">,</FONT><FONTcolor="#0000BB">$y2</FONT><FONT color="#007700">);</FONT><FONT color="#0000BB"></FONT></FONT></CODE></DIV></P><P> Since this pattern has to be repeated for every object that has tobe drawn it makes good sense to encapsulate this in a separate class.This is exactly why the canvas tools file also have a utility classcalled <A href="../ref/Shape.html#_C_SHAPE"> Shape</A> This class ismainly a wrapper around the most commonly used methods in the basicImage class (with one important exception) and does all these thetranslation for you. Please see the class reference for a complete listof the available methods To set up the Shape class you instantiate itwith the graphic context and the scale you want to use as argument asin</P><P><DIV class="phpscript"><CODE><FONT color="#000000"> <FONT color="#0000BB"> $shape </FONT><FONT color="#007700">= new </FONT><FONT color="#0000BB">Shape</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">$g</FONT><FONTcolor="#007700">,</FONT><FONT color="#0000BB">$scale</FONT><FONT color="#007700">);</FONT><FONT color="#0000BB"></FONT></FONT></CODE></DIV></P><P></P><P> You are then ready to use all the methods in the shape class. Usinga scale and imitating the previous example we would get the sourceshown below.<DIV class="phpscript">(File: canvasex03.php)<BR><CODE><FONT color="#000000"> <FONT color="#0000BB"><?php<BR></FONT><FONT color="#FF8000">// $Id: canvasex03.php,v 1.1 2002/08/27 20:08:57 aditus Exp $<BR></FONT><FONT color="#007700">include </FONT><FONT color="#DD0000">"../jpgraph.php"</FONT><FONT color="#007700">;<BR>include </FONT><FONT color="#DD0000">"../jpgraph_canvas.php"</FONT><FONTcolor="#007700">;<BR>include </FONT><FONT color="#DD0000">"../jpgraph_canvtools.php"</FONT><FONTcolor="#007700">;<BR><BR></FONT><FONT color="#FF8000">// Define work space<BR></FONT><FONT color="#0000BB">$xmax</FONT><FONT color="#007700">=</FONT><FONTcolor="#0000BB">20</FONT><FONT color="#007700">;<BR></FONT><FONT color="#0000BB">$ymax</FONT><FONT color="#007700">=</FONT><FONTcolor="#0000BB">20</FONT><FONT color="#007700">;<BR><BR></FONT><FONT color="#FF8000">// Setup a basic canvas we can work <BR></FONT><FONT color="#0000BB">$g </FONT><FONT color="#007700">= new </FONT><FONTcolor="#0000BB">CanvasGraph</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">400</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">200</FONT><FONTcolor="#007700">,</FONT><FONT color="#DD0000">'auto'</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">SetMargin</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">5</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">11</FONT><FONTcolor="#007700">,</FONT><FONT color="#0000BB">6</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">11</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">SetShadow</FONT><FONT color="#007700">();<BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">SetMarginColor</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">"teal"</FONT><FONT color="#007700">);<BR><BR></FONT><FONT color="#FF8000">// We need to stroke the plotarea and margin before we add the<BR>// text since we otherwise would overwrite the text.<BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">InitFrame</FONT><FONT color="#007700">();<BR><BR></FONT><FONT color="#FF8000">// Create a new scale<BR></FONT><FONT color="#0000BB">$scale </FONT><FONT color="#007700">= new </FONT><FONT color="#0000BB">CanvasScale</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$scale</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">Set</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">0</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">$xmax</FONT><FONTcolor="#007700">,</FONT><FONT color="#0000BB">0</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">$ymax</FONT><FONT color="#007700">);<BR><BR></FONT><FONT color="#FF8000">// The shape class is wrapper around the Imgae class which translates<BR>// the coordinates for us<BR></FONT><FONT color="#0000BB">$shape </FONT><FONT color="#007700">= new </FONT><FONT color="#0000BB">Shape</FONT><FONT color="#007700">(</FONT><FONTcolor="#0000BB">$g</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">$scale</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$shape</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">SetColor</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">'black'</FONT><FONT color="#007700">);<BR><BR><BR></FONT><FONT color="#FF8000">// Add a black line<BR></FONT><FONT color="#0000BB">$shape</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">SetColor</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">'black'</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$shape</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">Line</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">0</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">0</FONT><FONTcolor="#007700">,</FONT><FONT color="#0000BB">20</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">20</FONT><FONT color="#007700">);<BR><BR></FONT><FONT color="#FF8000">// .. and a circle (x,y,diameter)<BR></FONT><FONT color="#0000BB">$shape</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">Circle</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">5</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">14</FONT><FONTcolor="#007700">,</FONT><FONT color="#0000BB">2</FONT><FONT color="#007700">);<BR><BR></FONT><FONT color="#FF8000">// .. and a filled circle (x,y,diameter)<BR></FONT><FONT color="#0000BB">$shape</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">SetColor</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">'red'</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$shape</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">FilledCircle</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">11</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">8</FONT><FONTcolor="#007700">,</FONT><FONT color="#0000BB">3</FONT><FONT color="#007700">);<BR><BR></FONT><FONT color="#FF8000">// .. add a rectangle<BR></FONT><FONT color="#0000BB">$shape</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">SetColor</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">'green'</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$shape</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">FilledRectangle</FONT><FONT color="#007700">(</FONT><FONTcolor="#0000BB">15</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">8</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">19</FONT><FONTcolor="#007700">,</FONT><FONT color="#0000BB">14</FONT><FONT color="#007700">);<BR><BR></FONT><FONT color="#FF8000">// .. add a filled rounded rectangle<BR></FONT><FONT color="#0000BB">$shape</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">SetColor</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">'green'</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$shape</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">FilledRoundedRectangle</FONT><FONT color="#007700">(</FONT><FONTcolor="#0000BB">2</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">3</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">8</FONT><FONTcolor="#007700">,</FONT><FONT color="#0000BB">6</FONT><FONT color="#007700">);<BR></FONT><FONT color="#FF8000">// .. with a darker border<BR></FONT><FONT color="#0000BB">$shape</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">SetColor</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">'darkgreen'</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$shape</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">RoundedRectangle</FONT><FONT color="#007700">(</FONT><FONTcolor="#0000BB">2</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">3</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">8</FONT><FONTcolor="#007700">,</FONT><FONT color="#0000BB">6</FONT><FONT color="#007700">);<BR><BR><BR></FONT><FONT color="#FF8000">// Stroke the graph<BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">Stroke</FONT><FONT color="#007700">();<BR><BR></FONT><FONT color="#0000BB">?><BR></FONT><BR></FONT></CODE></DIV> The source above gives the following result<DIV class="example"><BR> <A href="exframes/frame_canvasex03.html" target="blank"><IMG border="0"HEIGHT="200" src="img/canvasex03.png" WIDTH="400"></A><BR><B>Figure 186:</B> Drawing shapes on a canvas using a scale. <A href="exframes/frame_canvasex03.html"target="blank">[src]</A> <P></P></DIV></P><P> If we like to make a smaller image we could just change the imagesize and everything will be rescaled without any further code changes.SO for example making the image half the size would give the result<DIV class="example"><BR> <A href="exframes/frame_canvasex04.html" target="blank"><IMG border="0"HEIGHT="100" src="img/canvasex04.png" WIDTH="200"></A><BR><B>Figure 187:</B> Shrinking the image to half the size is easysince the scaling will maintain the relative position of the objects <A href="exframes/frame_canvasex04.html"target="blank">[src]</A> <P></P></DIV></P><P> If we instead wanted to keep the image size but shrink the shapes wecould just make the scale twice as large which would result in<DIV class="example"><BR> <A href="exframes/frame_canvasex05.html" target="blank"><IMG border="0"HEIGHT="200" src="img/canvasex05.png" WIDTH="400"></A><BR><B>Figure 188:</B> Shrinking hte graphic object by making the scaletwice as large <A href="exframes/frame_canvasex05.html" target="blank">[src]</A> <P></P></DIV></P><P></P><P> We previously mentioned that the Shape class was a wrapper aroundthe image class with one exception. So what is the exception? Well,glad you asked. The exception is that it contain an additional methodwhich draws an "indented rectangle". An indented rectangle is arectangle where one of it's four corners have been moved into therectangle. You create an indented rectangle by calling either <A href="../ref/Shape.html#_SHAPE_INDENTEDRECTANGLE">Shape::IndentedRectangle()</A> or A few examples illustrates what thisshape looks like.<DIV class="example"><BR> <A href="exframes/frame_canvasex06.html" target="blank"><IMG border="0"HEIGHT="200" src="img/canvasex06.png" WIDTH="400"></A><BR><B>Figure 189:</B> Examples of filled indented rectangles <A href="exframes/frame_canvasex06.html"target="blank">[src]</A> <P></P></DIV></P><P></P><P> As a final note we mention the class <A href="../ref/CanvasRectangleText.html#_C_CANVASRECTANGLETEXT">CanvasRectangleText</A> Which can be used to add a text with a roundedrectangle (possibly filled) onto the canvas. The previous example whereall the available fonts were drawn were using this class. We don'tdescribe it further but refer the interested reader to the classreference and the 'listfontsex1.php' example file.</P><HR NOSHADE><A HREF="toc.html">Contents</A><A HREF="113Addinglinesandrectanglestoacanvas.html">Previous</A><A HREF="115SampleapplicationDrawingDBschema.html">Next</A></BODY></HTML>