Results 1 to 8 of 8

Thread: Drawing Rectangle using Javascript

  1. #1
    Join Date
    Aug 2007
    Location
    MO USA
    Posts
    106
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Drawing Rectangle using Javascript

    Hi,

    I need a javascript to draw rectangle on the webpage. I have an image inside a div - when i select a zoom tool (icon) i click and drag on the image - the image zooms in - for this i need an effect of drawing a rectangle with the mouse. Please see the url below - i need the exact same effect using javascript.

    http://onlinedemos.thinkgeo.com/renderusa/
    Click on Zoom In and draw a rectangle on the map. I need the same effect.

    Thanks a lot.

  2. #2
    Join Date
    Aug 2007
    Location
    MO USA
    Posts
    106
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default

    I found the below code. When i use it in IE7, after drawing the rectangle within the DIV, when you click outside the DIV, a small line appears. Is there a way to disable that line and limit the drawing to only within the DIV - Please Help.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <style type="text/css">
    <!--
    .square {
    border: 1px solid #FF0000;
    position: absolute;
    }
    -->
    </style>
    <script type="text/JavaScript">
    var d;var posx;var posy;var initx=false;var inity=false
    function getMouse(obj,e){
    posx=0;posy=0;
    var ev=(!e)?window.event:e;//Moz:IE
    if (ev.pageX){//Moz
    posx=ev.pageX+window.pageXOffset;
    posy=ev.pageY+window.pageYOffset;
    }
    else if(ev.clientX){//IE
    posx=ev.clientX+document.body.scrollLeft;
    posy=ev.clientY+document.body.scrollTop;
    }
    else{return false}//old browsers
    var target = (e && e.target) || (event && event.srcElement);
    if(target.id=='Canvas'){
    obj.onmousedown=function(){
    initx=posx; inity=posy;
    d = document.createElement('div');
    d.className='square'
    d.style.left=initx+'px';d.style.top=inity+'px';
    document.getElementsByTagName('body')[0].appendChild(d)
    }
    obj.onmouseup=function(){initx=false;inity=false;}
    if(initx){
    d.style.width=Math.abs(posx-initx)+'px';d.style.height=Math.abs(posy-inity)+'px';
    d.style.left=posx-initx<0?posx+'px':initx+'px';
    d.style.top=posy-inity<0?posy+'px':inity+'px';
    }
    }
    else{return false}
    }
    document.onmousemove=function(event){
    getMouse(document,event);
    }
    </script>
    </head>
    <body>
    <div id="Canvas" style="width:200px; height:200px; border:solid black;"></div>
    </body>
    </html>
    Thanks.

  3. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

  4. #4
    Join Date
    Jan 2008
    Location
    High Wycombe, UK
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Image zooming

    I think you need quite a specialist JavaScript to do that. You might need to hack a script in order to get the effect you want.

    One of these two might help:

    http://www.magictoolbox.com/magiczoom/

    or

    http://luckyteam.co.uk/products/LuckyView/

  5. #5
    Join Date
    Aug 2007
    Location
    MO USA
    Posts
    106
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the links, thesource.

    I am almost done with the code that i've pasted above. Just one small thing, i am not able to reduce the size while i'm drawing (in the same mouse movement). Can someone please help me to get that done?

    Thanks.

  6. #6
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    what's the code? the code in the second post?
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@ /\/\@|{
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  7. #7
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Rectangle issue

    Hi im doing same work(rectangle creation) with the same problem what u have ,
    when i created the rectangle with your code,

    its working perfectly and after created that i just have to drag and resize the created the BOX's, i have a code for its but its not working in IE7 i think u faced the same problem.

    can u help out of this??

    Thanks in advance.

    Danie.

  8. #8
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,791
    Thanks
    2
    Thanked 418 Times in 412 Posts

    Default

    plase post a link to your page
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •