Google Classroom
GeoGebraGeoGebra Classroom

Controlling Your Drawing's Pixel Dimensions

Introduction

If you regularly use image manipulation programs like Adobe Illustrator or Photoshop, you may be accustomed to having precise control over the contents of your drawings, as well as the ability to resize your drawings to whatever pixel dimensions you require. GeoGebra does not, unfortunately, have native GUI controls to accomplish this. Instead, you are going to have to create this facility on your own using a combination of JavaScript-enabled buttons, sliders and a pair of special points.

Steps

Let me start by describing these last two points. The points Export_1 and Export_2 define the area of your drawing that will be made visible when you export it as an SVG or PNG image file. Contents outside the box defined by these two points will be cropped or otherwise be made invisible. Contents inside this box will appear in the exported image. The points should be located at opposite corners of the box, or else the export area will be a one-dimensional straight line. In the worksheet above I have made Export_1 and Export_2 visible, and have added two more points and several segments to show you the extents of this box. Before saving your document you will probably want to hide these objects, or else parts of them will appear in the exported image. The next thing you need to do is create a slider to set the scale of your drawing. I named mine "PixelsPerUnit", and configured the slider to fit within the range of 0 to 500. Lastly, you need to create a button to trigger a script that will resize the drawing. Attached to the button click in the "Scripting" tab should be the following GeoGebra code: ZoomIn(CopyFreeObject(x(Export_1)),CopyFreeObject(y(Export_1)-y(Corner(5)+(2,2))/PixelsPerUnit),CopyFreeObject(x(Export_1)+x(Corner(5)+(2,2))/PixelsPerUnit),CopyFreeObject(y(Export_1))) Now, when you press the button, the drawing will be shifted so that the Export_1 point is in the top-left corner of the drawing, and the dimensions of the drawing will be the size in GeoGebra units multiplied by the value of "PixelsPerUnit" slider. In the example drawing above, the size of the export area is -1.25 to +1.25 units along the x and y axes, and the slider is set to 150 pixels per unit. Thus the dimensions of the exported image should equal 375 by 375 pixels.

Extra remarks

Firstly, if you are using GeoGebra 5 Classic, when you export the PNG or SVG image, you will need to select the "Size in pixels" option from the drop-down menu of the export dialog. If you are using GeoGebra 6 Classic, I think this is done automatically for you. Secondly, sometimes the dimensions of the exported image are off the mark by a pixel or two. For instance, the dimensions of the above drawing may end up being 376 by 375 pixels instead of 375 by 375 pixels. If you export as SVG you can fix these errors using a text editor. Using InkScape you can then convert the SVG to PNG.