{"id":4536,"date":"2024-02-11T06:56:16","date_gmt":"2024-02-11T06:56:16","guid":{"rendered":"https:\/\/truelogic.org\/wordpress\/?p=4536"},"modified":"2024-02-11T07:01:28","modified_gmt":"2024-02-11T07:01:28","slug":"svg-ruler-in-javascript","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2024\/02\/11\/svg-ruler-in-javascript\/","title":{"rendered":"SVG Ruler in Javascript"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>This is a small, compact ruler display tool in an svg container using plain javascript. Its less than 200 lines of code and can be used in any browser or platform. The purpose is to display a horizontal and\/or vertical ruler along the edges of a svg container to show measurements of various objects which are within the svg container.<\/p>\n\n\n\n<p>The code is available on github here: <a href=\"https:\/\/github.com\/amitonline\/true-ruler\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/amitonline\/true-ruler<\/a><\/p>\n\n\n\n<p>You are free to use\/modify it as required. <\/p>\n\n\n\n<p><strong>SCREENSHOTS<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"379\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler1.png\" alt=\"\" class=\"wp-image-4540\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler1.png 706w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler1-620x333.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler1-300x161.png 300w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"735\" height=\"552\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler2.png\" alt=\"\" class=\"wp-image-4541\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler2.png 735w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler2-620x466.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler2-300x225.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler2-678x509.png 678w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler2-326x245.png 326w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler2-80x60.png 80w\" sizes=\"auto, (max-width: 735px) 100vw, 735px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"735\" height=\"552\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler3.png\" alt=\"\" class=\"wp-image-4542\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler3.png 735w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler3-620x466.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler3-300x225.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler3-678x509.png 678w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler3-326x245.png 326w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2024\/02\/ruler3-80x60.png 80w\" sizes=\"auto, (max-width: 735px) 100vw, 735px\" \/><\/figure>\n\n\n\n<p><strong>USAGE<\/strong><\/p>\n\n\n\n<p>In HTML, you need an svg container with two group items &#8211; grpLeft for the left vertical ruler and grpTop for the top, horizontal ruler. You need to have these two groups in place even if you intend to use only one of the rulers. The transform attributes are also required to be present. <br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;svg id=\"svgElem\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 320 120\"&gt;\n\t&lt;g id=\"grpTop\" transform=\"translate(60,0)\"&gt;&lt;\/g&gt;\n\t&lt;g id=\"grpLeft\" transform=\"translate(60,0)\"&gt;&lt;\/g&gt;\n&lt;\/svg&gt;<\/code>\n\n\n<\/code><\/pre>\n\n\n\n<p>In Javascript, a single constructor will render the ruler with default arguments, But most of the times, you will want to modify various aspects. The constructor takes in the following arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>svgElem &#8211; the svg container id<\/li>\n\n\n\n<li>inches &#8211; true to display ruler in inches, false to display in pixels. Default=false<\/li>\n\n\n\n<li>vertical &#8211; if false then the ruler will be horizontal across the x-axis at the top . If true, then  vertical across the y-axis in the left. Default=false<\/li>\n\n\n\n<li>pw &#8211; pixel width of the svg container in pixels. Use the same width as defined in the viewBox. Default=100<\/li>\n\n\n\n<li>ph &#8211; pixel height of the svg container in pixels. Use the same height as defined in the viewBox. Default=100<\/li>\n\n\n\n<li>vw &#8211; virtual width of the svg container in pixels. In case you want to render the ruler for a different width than the actual width. Default=100<\/li>\n\n\n\n<li>vh &#8211; virtual height of the svg container in pixels. In case you want to render the ruler for a different height than the actual height. Default=100<\/li>\n\n\n\n<li>hdiv &#8211; number of divisions to divide the ruler into. Default=10<\/li>\n\n\n\n<li>hdivsmall &#8211; number of sub-divisions between two divisions. Default=10<\/li>\n<\/ul>\n\n\n\n<p>So the minimal function call can be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var ruler = new TrueRuler(\"svgBox\");\n<\/code><\/pre>\n\n\n\n<p>This will render the ruler horizontally in svgBox with default values.. But a more realistic call could be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var ruler = new TrueRuler(\"svgElem\", false, false, 260,160,260,160);\n<\/code><\/pre>\n\n\n\n<p>It is important that the pw and ph arguments match the width and height of the viewBox.<\/p>\n\n\n\n<p>The code below shows an actual implementation of a horizontal ruler. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ruleHoriz = new TrueRuler(\"svgElem\", false, false, 260,160,260,160);\n\t\truleHoriz.setDivisions(10);\n\t\truleHoriz.setDivisionsSmall(4);\n\t\truleHoriz.setDivisionHeightWidth(10,0.2);\n\t\truleHoriz.setDivisionSmallHeightWidthMid(3,0.1,6);\n\t\truleHoriz.setTextYOffset(10);\n\t\truleHoriz.init();\n\t\truleHoriz.render();\n<\/code><\/pre>\n\n\n\n<p>Any time you change any parameter, you should call init() and render() so that the changes can be updated in the ruler.<\/p>\n\n\n\n<p><strong>STYLING<\/strong><\/p>\n\n\n\n<p>Use of CSS has been kept to a minimum. There is a single class called <em>.svg_label_text<\/em> which is used to style the text labels. This is generally defined in the parent html or web page.<\/p>\n\n\n\n<p><strong>IMPORTANCE OF KEEPING A TOP AND LEFT MARGIN<\/strong><\/p>\n\n\n\n<p>In the svg tag, you will see that the two group elements left a space of 60 pixels from the left. This is needed, otherwise the rulers would overlap at the top left origin (0,0) and any other svg element added to the svg container would also overlap the rulers. As you can see in the above screenshot with the image, the image starts just after the rulers, but yet starts at 0,0. This makes the measurement realistic.<\/p>\n\n\n\n<p>You will find the example code for this in the github repo. <a href=\"https:\/\/github.com\/amitonline\/true-ruler\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/amitonline\/true-ruler<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW This is a small, compact ruler display tool in an svg container using plain javascript. Its less than 200 lines of code and can <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2024\/02\/11\/svg-ruler-in-javascript\/\" title=\"SVG Ruler in Javascript\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":4537,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,364],"tags":[],"class_list":["post-4536","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","category-tools-and-components"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4536"}],"version-history":[{"count":16,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4536\/revisions"}],"predecessor-version":[{"id":4558,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4536\/revisions\/4558"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/4537"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}