Skip to main content

Doing some SVG drawings on a web page (Raphael.js)

So for this tree based problem that i am currently trying to solve, one of the first perquisites was to get better at Recursion, the next thing to work out was, how do I draw shapes and lines on an HTML page?

So prior to this my only experience with drawing on an HTML page was this simple badly presented snake game that i had created using the canvas. Anyway for this little project i decided to look around for some libraries that would allow me to draw some shapes and I found a couple of libraries such as Raphel.js and D3.js that would let me draw SVG shapes on a web page. After very briefly investigating them, it seemed that Raphaeljs would be a better fit for my problem and hence i began my SVG drawing journey with Raphael.

Now when i think about this problem, i can think of it as a sequential list of little sub-problems that i need to individually solve before i can build my great grand tree drawing solution. This is a slightly larger problem and hence it will be spread out over a few posts. Anyway below is a break of all the sub-problems
  1. Find a library that lets you draw shapes
  2. Draw some basic shapes and lines with the library. 
  3. Add some user interactivity by making the shapes drag-able across the drawing area.
  4. Connect the shapes by drawing lines between them and ensure that the connections are maintained even when the shapes are dragged.
  5. Draw a relatively large tree and ensure the nodes do not overlap.
  6. Polish the product by adding, the ability to edit the text in the shapes, or perhaps let the users add shapes to the canvas.
So No 1. is already solved in this post and in that i have found Raphael.js, which will let me draw some shapes on the web page.

Problem

Draw some basic shapes on a web page.

Solution

So the first thing to do was to get a the container on the web page where i would do all my drawings. A container is a div like this.   <div id="canvas_container"> </div>. 

    var paper = Raphael(document.getElementById('canvas_container'), 1200, 900);
    var r1 = paper.rect(200, 200, 120, 50).attr("fill", "#ff0");
    var c1 = paper.circle(50,50, 40).attr("fill", "#ff0");

The above code first gets the drawing area through the Raphael object and then uses the methods provided by Raphaeljs to draw a rectangle and a circle. The rectangle drawing starts at x, and y of 200 with a width of 120 and height of 50  respectively. The circle starts at x, and y position of 50 and has a radius of 40. Both the circle and the rectangle will be coloured yellow by setting their fill attribute i.e. .attr("fill", "#ff0"). Now both the rectangle and circle looks a little empty and boring, so i decided to add some rounded corners and some text in the center of it.

    var r1 = paper.rect(200, 200, 120, 50).attr("fill", "#ff0");
    var c1 = paper.circle(50,50, 40).attr("fill", "#ff0");
    /* get the bounding box of the object, to get the start and the end co-ordinates of the shape */
     var rBox = r1.getBBox();
     var cBox = c1.getBBox();
     /*calculate the midpoint(x, y) co-ordinates of the shape where the text has to be placed */
     var rMidpointX = (rBox.x + rBox.x2)/2;
     var rMidpointY = (rBox.y + rBox.y2)/2;
     var cMidpointX = (cBox.x + cBox.x2)/2;
     var cMidpointY = (cBox.y + cBox.y2)/2;
     /* draw the text at the center of the objects  */
     var t1 = paper.text(rMidpointX, rMidpointY, "Box 1");
     var t2 = paper.text(cMidpointX, cMidpointY, "Circle 1");
     /*style the text that was added */          
     t1.attr({fill: "#3D5C9D", "font-weight": "bold", "font-size":"12"});
     t2.attr({fill: "#3D5C9D", "font-weight": "bold", "font-size":"12"});

The above example with the comments is fairly self-explanatory and  the comments placed in there should provide a clear understanding of what each line does.

Ok, the next thing i thought of was hmm, i may need to draw custom shapes, so how do i go about doing it? thankfully this can be done using Paths in Raphael.

var path = paper.path("M300,300 L340,300 L340,340 L300,340z")

M300,300,  the point at which the line drawing starts i.e. synonymous to the point of the paper where the pencil is placed before anything is drawn. L340,300, draw a line to point 340,300 and move the draw cursor to that point, L340,340 draw a line down from the previous point followed by L300,340. So syntactically, a letter (M, L) indicates what is to be done and is generally followed by the direction to do it in i.e. x and y co-ordinates. this is a special case as it simply means close the drawing i.e. draw a line back to the point where we started from i.e. M.  When debugging drawings, i drew some small circles to visualise where each point was , something like this

                paper.circle(300,300,2);
                paper.circle(340,300,2);
                paper.circle(300,340,2);
                paper.circle(340,340,2);

Ok so this should be all for basic shape drawing for now, and in the next post i will talk about how to make the shapes draggable and draw paths to link these shapes.
Some useful references for Raphael.js

Introduction to Raphael.js
Raphaeljs tutorial
Raphael.js documentation

Comments

Popular posts from this blog

Upload to AWS S3 from Java API

In this post, you will see code samples for how to upload a file to AWS S3 bucket from a Java Spring Boot app. The code you will see here is from one of my open-source repositories on Github, called document-sharing. Problem Let’s say you are building a document sharing app where you allow your users to upload the file to a public cloud solution. Now, let’s say you are building the API for your app with Spring Boot and you are using AWS S3 as your public cloud solution. How would you do that? This blog post contains the code that can help you achieve that. Read more below,  Upload to AWS S3 bucket from Java Spring Boot app - My Day To-Do (mydaytodo.com)

Addressing app review rejections for auto-renewing subscription in-app purchase (iOS)

The ability to know what the weather is like while planning your day is a feature of  My Day To-Do  Pro and as of the last update it’s also a part of the  Lite version . Unlike the Pro version it’s an auto-renewing subscription based  in-app purchase (IAP)  in the Lite version. What means is that when a user purchases it, the user only pays for the subscription duration after which the user will be automatically charged for the next period. Adding an  auto-renewing  subscription based IAP proved to be somewhat challenging in terms of the app store review i.e. the app update was rejected by the App Review team thrice because of missing information about the IAP. Therefore in this post I will share my experiences and knowledge of adding auto-renewing IAP in hopes to save someone else the time that I had to spend on this problem. In-App purchase This year I started adding IAPs to My Day To-Do Lite which lead to learning about different types of IAP...

App update, discovering Protractor(testing) and an Angularjs State machine

So for the last couple of weeks, I have not be able to update my blog or finish a couple of the posts that I started writing. Well, I have...been a bit busy, with some the following things,  Testing my app I have been using the app( iOS ) on my iPhone for the past couple of months now, which is both good and bad. The good thing is I can test it and the bad thing is, I can use it every day. Part of the reason, I started building this app is to have something that I can and like to use everyday and since I am already using it, the incentive to release it is not as high as if it were something that I could not use. Anyway I did make some good progress over the last couple of weeks, like  Delete the app from my iPhone and do a clean install: this did help me find a few simple bugs that would pop-up when the app is first installed GET AN APP ICON, FINALLY! this was a bit of a hurdle and I thanks to Fiverr  and fivercrazyguy , I finally have my app icon. The day job ...