3

I have an image in html img tag. My requirement is that when a user click on the image it will mark a point and draw a line while dragging the mouse. Then, While user finishes the drag and clicks on the image it should display the line also show the dimension of the line in millimeter/centimeter. ie , User has to draw a line on the image and show the distance/length (in millimeter/centimeter)of line he drawn.

How can implement this feature in a web application? Can anyone please help me to implement this feature ?

1
  • a fiddle with what you have so far? Commented Sep 18, 2012 at 12:50

1 Answer 1

10

Use the html5 canvas element, set the image as a css background to the canvas element (makes drawing the lines easier because you don't have to redraw the image) and draw the line on the canvas:

1) On mousedown, record the mouse position and register a mousemove handler closed around those starting positions, and register a mouseup handler to remove the mousemove handler.

2) In the mousemove handler, find the offset between the current mouse position and the mouse's starting position, add this offset to the starting line position and then redraw the canvas using this new position.

You can't label the line with a physical distance because this will depend on the screen that displays your work. The best you can do is decide on a scale/print resolution for your image (in dpi, e.g. 300 pixels per inch) and calculate the length of the line based on that. The exact implementation depends on how you want to use the results.

UPDATE: EXAMPLE

DEMO JSFIDDLE

HTML

<canvas id="canvas" width="200" height="200">
    Your browser doesn't support canvas
</canvas>

CSS

canvas{
    background-image: url("image.jpg");
    background-position: center;
    background-size: 100% 100%;
}

JS

$(document).ready(function(){

    var imageDpi = 300;

    var can = document.getElementById('canvas');
    var ctx = can.getContext('2d');
    var startX, startY;

    $("canvas").mousedown(function(event){
        startX = event.pageX;
        startY= event.pageY;

        $(this).bind('mousemove', function(e){
            drawLine(startX, startY, e.pageX, e.pageY);
        });
    }).mouseup(function(){
        $(this).unbind('mousemove');
    });

    function drawLine(x, y, stopX, stopY){
        ctx.clearRect (0, 0, can.width, can.height);
        ctx.beginPath();
        ctx.moveTo(x, y);
        ctx.lineTo(stopX, stopY);
        ctx.closePath();
        ctx.stroke();

        // calculate length   
        var pixelLength = Math.sqrt(Math.pow((stopX - x),2) + Math.pow((stopY-y),2));
        var physicalLength = pixelLength / imageDpi;
        console.log("line length = " + physicalLength + 
                    " inches (image at " + imageDpi + " dpi)");
    }
});

UPDATE 2: LINE LENGTH

I updated my example. It defines the image's physical resolution and calculates the line's length based on that assumption.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you please help me calculate the physical length of the line which was drawn on the image?
I'd recommend to use (offsetX, offsetY) instead of (pageX, pageY), just in case the canvas element is not located on the (0,0) position of the page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.