Clipping in Javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Doug van Vianen

    #1

    Clipping in Javascript

    Hi,

    I am trying to show a clipped part of a picture on a webpage and
    move it on the page and/or change the clipping to a different part
    of the picture. I show the picture in the body of the page by
    using

    <div id=div1 style="position :absolute; top:200; left:400; width:200;
    height:200; z-index:1; background-image:url(ole01 .jpg);
    clip:rect(0,150 ,50,100)"></div>

    This shows the appropriate clipped part of the picture at the correct
    location. I try to use the following Javascript code in the head to
    change the position of the picture clip:

    <script language="JavaS cript">
    <!--
    function Dochange(ident) {
    var ref=document.ge tElementById(id ent);
    ref.style.left= 300;
    }
    -->
    </script>}

    and the following button in a form:

    <input type="submit" name="Change" value="Change" size=20
    onClick="Dochan ge('div1')">

    The code is accessed when the button is clicked (I used an alert to
    check this), but nothing happens to the position of the clipped
    picture and no error message appears. Can someone help me?

    When I get this working I would like to add any statements necessary
    in the Javascript to change the clipping area. Would it be something
    like

    ref.style.clip: rect(0,50,50,0) ;

    or what?

    Thank you.

    Doug van Vianen
    courses@shaw.ca


  • Martin Honnen

    #2
    Re: Clipping in Javascript



    Doug van Vianen wrote:

    [color=blue]
    > <div id=div1 style="position :absolute; top:200; left:400; width:200;
    > height:200; z-index:1; background-image:url(ole01 .jpg);
    > clip:rect(0,150 ,50,100)"></div>[/color]

    Try with
    top: 200px;
    left: 400px;
    and so on to have proper CSS and then try
    [color=blue]
    > ref.style.left= 300;[/color]

    ref.style.left = '300px';
    here and

    [color=blue]
    > <input type="submit" name="Change" value="Change" size=20
    > onClick="Dochan ge('div1')">[/color]

    <input type="button" onClick="Dochan ge('div1')"
    here

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • RobB

      #3
      Re: Clipping in Javascript

      Doug van Vianen wrote:[color=blue]
      > Hi,
      >
      > I am trying to show a clipped part of a picture on a webpage and
      > move it on the page and/or change the clipping to a different part
      > of the picture. I show the picture in the body of the page by
      > using
      >
      > <div id=div1 style="position :absolute; top:200; left:400; width:200;
      > height:200; z-index:1; background-image:url(ole01 .jpg);
      > clip:rect(0,150 ,50,100)"></div>
      >
      > This shows the appropriate clipped part of the picture at the correct
      > location. I try to use the following Javascript code in the head to
      > change the position of the picture clip:
      >
      > <script language="JavaS cript">
      > <!--
      > function Dochange(ident) {
      > var ref=document.ge tElementById(id ent);
      > ref.style.left= 300;
      > }
      > -->
      > </script>}
      >
      > and the following button in a form:
      >
      > <input type="submit" name="Change" value="Change" size=20
      > onClick="Dochan ge('div1')">
      >
      > The code is accessed when the button is clicked (I used an alert to
      > check this), but nothing happens to the position of the clipped
      > picture and no error message appears. Can someone help me?
      >
      > When I get this working I would like to add any statements necessary
      > in the Javascript to change the clipping area. Would it be something
      > like
      >
      > ref.style.clip: rect(0,50,50,0) ;
      >
      > or what?
      >
      > Thank you.
      >
      > Doug van Vianen
      > courses@shaw.ca[/color]

      It's a CSS property, like any other, although the manipulation of it
      can get a bit sticky as the four clip values are combined.

      1) Get the element reference
      2) Specify the style.clip property
      3) Assign it a new string value in the combined format

      var el;
      if (el = document.getEle mentById('div1' ))
      el.style.clip = 'rect(40px,auto ,auto,40px)';

      Use 'auto' to return that specific clip value to fully revealed
      (unclipped).

      One of the few lovable things about Netscape 4 is it's parsing out of
      the separate clip values into read/write properties. IE exposes
      separate .currentStyle properties for each clip, but they're read-only.
      <script type="text/javascript"> ["language" attribute deprecated]

      Comment

      • Doug van Vianen

        #4
        Re: Clipping in Javascript

        Hi,

        Thanks. It now works great!

        Doug van Vianen
        courses@shaw.ca

        "Doug van Vianen" <courses@shaw.c a> wrote in message
        news:Z4dHd.1129 37$Xk.88837@pd7 tw3no...[color=blue]
        > Hi,
        >
        > I am trying to show a clipped part of a picture on a webpage and
        > move it on the page and/or change the clipping to a different part
        > of the picture. I show the picture in the body of the page by
        > using
        >
        > <div id=div1 style="position :absolute; top:200; left:400; width:200;
        > height:200; z-index:1; background-image:url(ole01 .jpg);
        > clip:rect(0,150 ,50,100)"></div>
        >
        > This shows the appropriate clipped part of the picture at the correct
        > location. I try to use the following Javascript code in the head to
        > change the position of the picture clip:
        >
        > <script language="JavaS cript">
        > <!--
        > function Dochange(ident) {
        > var ref=document.ge tElementById(id ent);
        > ref.style.left= 300;
        > }
        > -->
        > </script>}
        >
        > and the following button in a form:
        >
        > <input type="submit" name="Change" value="Change" size=20
        > onClick="Dochan ge('div1')">
        >
        > The code is accessed when the button is clicked (I used an alert to
        > check this), but nothing happens to the position of the clipped
        > picture and no error message appears. Can someone help me?
        >
        > When I get this working I would like to add any statements necessary
        > in the Javascript to change the clipping area. Would it be something
        > like
        >
        > ref.style.clip: rect(0,50,50,0) ;
        >
        > or what?
        >
        > Thank you.
        >
        > Doug van Vianen
        > courses@shaw.ca
        >
        >[/color]


        Comment

        • RobG

          #5
          Re: Clipping in Javascript

          RobB wrote:
          [...][color=blue]
          > if (el = document.getEle mentById('div1' ))
          > el.style.clip = 'rect(40px,auto ,auto,40px)';[/color]

          Probably should test style too:

          if (el = document.getEle mentById('div1' ) && el.style)
          ...


          --
          Rob

          Comment

          • RobB

            #6
            Re: Clipping in Javascript

            RobG wrote:[color=blue]
            > RobB wrote:
            > [...][color=green]
            > > if (el = document.getEle mentById('div1' ))
            > > el.style.clip = 'rect(40px,auto ,auto,40px)';[/color]
            >
            > Probably should test style too:
            >
            > if (el = document.getEle mentById('div1' ) && el.style)
            > ...
            >
            >
            > --
            > Rob[/color]

            Why? Are there browsers that support getElementById and not Style
            objects?

            Comment

            • Richard Cornford

              #7
              Re: Clipping in Javascript

              RobB wrote:[color=blue]
              > RobG wrote:[color=green]
              >> RobB wrote:
              >> [...][color=darkred]
              >> > if (el = document.getEle mentById('div1' ))
              >> > el.style.clip = 'rect(40px,auto ,auto,40px)';[/color]
              >>
              >> Probably should test style too:
              >>
              >> if (el = document.getEle mentById('div1' ) && el.style)
              >> ...[/color][/color]
              <snip>[color=blue]
              > Why? Are there browsers that support getElementById and
              > not Style objects?[/color]

              If - document.getEle mentById - has been emulated in its absence for
              increased back compatibility then there is every chance that it will
              return element references that do not implement a - style - object. This
              would be particularly significant for authors of code that may be
              deployed alongside code written by others as the fact that you know that
              you did not emulate getElementById in its absence does not guarantee
              that those other authors haven't.

              Generally it is best to verify everything practical in javascript, and
              therefor worth considering how that testing may effectively be done
              without itself becoming a burden to the executing code.

              Richard.


              Comment

              • RobG

                #8
                Re: Clipping in Javascript

                RobB wrote:

                [...][color=blue]
                > Why? Are there browsers that support getElementById and not Style
                > objects?
                >[/color]

                I have no idea, maybe not - but support for one feature should not be
                used to infer support for another. Unless you can guarantee that every
                browser that supports getElementById *does* support the style object,
                it seems prudent to test it.

                --
                Rob

                Comment

                • RobB

                  #9
                  Re: Clipping in Javascript

                  Richard Cornford wrote:[color=blue]
                  > RobB wrote:[color=green]
                  > > RobG wrote:[color=darkred]
                  > >> RobB wrote:
                  > >> [...]
                  > >> > if (el = document.getEle mentById('div1' ))
                  > >> > el.style.clip = 'rect(40px,auto ,auto,40px)';
                  > >>
                  > >> Probably should test style too:
                  > >>
                  > >> if (el = document.getEle mentById('div1' ) && el.style)
                  > >> ...[/color][/color]
                  > <snip>[color=green]
                  > > Why? Are there browsers that support getElementById and
                  > > not Style objects?[/color][/color]

                  (snip)
                  [color=blue]
                  > Generally it is best to verify everything practical in javascript,[/color]
                  and[color=blue]
                  > therefor worth considering how that testing may effectively be done
                  > without itself becoming a burden to the executing code.
                  >
                  > Richard.[/color]

                  That would seem to be the operant question: when does
                  pants-and-suspenders (and cummerbund & drawstring) object testing of
                  this sort become a 'burden', occupying a significant volume of the
                  codebase? I've never seen a script which intentionally enhanced
                  functionality by patching in getElementById in user agents which did
                  not implement Style objects, although I suppose anything is possible.
                  Should everything be tested for before assuming its implementation?
                  When may asumptions be made?

                  Comment

                  • RobG

                    #10
                    Re: Clipping in Javascript

                    RobB wrote:
                    [...][color=blue]
                    > That would seem to be the operant question: when does
                    > pants-and-suspenders (and cummerbund & drawstring) object testing of[/color]

                    Belt and braces? ;-)
                    [color=blue]
                    > this sort become a 'burden', occupying a significant volume of the
                    > codebase? I've never seen a script which intentionally enhanced
                    > functionality by patching in getElementById in user agents which did
                    > not implement Style objects, although I suppose anything is possible.
                    > Should everything be tested for before assuming its implementation?
                    > When may asumptions be made?[/color]

                    A good question and worth pondering. However, before navel gazing,
                    lets look at the practicalities. I put each of the following into a
                    separate function, then called them 1000 times from separate functions,
                    hopefully avoiding any conflicts between the two:

                    if (document.getEl ementById) {var x = document.getEle mentById('rob') }

                    versus:

                    var x = document.getEle mentById('rob') ;

                    for 1,000 iterations took approximately 30 ms and 15 ms respectively -
                    though in Firefox the tested script sometimes ran in 15 ms also. IE
                    gave similar but less consistent results. The tested script sometimes
                    ran in 47ms, and the untested one often took 30ms.

                    So even testing a feature every time it's used will likely cause an
                    insignificant delay in program execution (I would think that anyone
                    calling gEBI 1000 times has more issues with program design in general
                    than with optimising their feature testing).

                    Therefore given the minimal overhead that thorough testing costs, why
                    not go for thoroughness and elegance within reason and just do it?

                    PS.
                    I also took the time to test using an array length versus setting a
                    variable in the for loop, e.g.:

                    for (var i=0; i< z.length, i++)

                    versus

                    for (var i=0, len=z.length; i<len; i++)

                    There was no measurable difference between the two.


                    Code:

                    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
                    <html><head><ti tle>demo</title>
                    <script type="text/javascript">

                    function testYes() {
                    if (document.getEl ementById) {
                    var x = document.getEle mentById('rob1' );
                    }
                    }

                    function testNo() {
                    var x = document.getEle mentById('rob2' );
                    }

                    function runTest1() {
                    var now0 = new Date();
                    for ( var i=0; i<1000; i++ ){
                    testYes();
                    }
                    var now1 = new Date();
                    var elapsed = now1.getTime() - now0.getTime();
                    alert('Feature tested: ' + elapsed);
                    }

                    function runTest2() {
                    var now0 = new Date();
                    for ( var i=0; i<1000; i++ ){
                    testNo();
                    }
                    var now1 = new Date();
                    var elapsed = now1.getTime() - now0.getTime();
                    alert('Not feature tested: ' + elapsed);
                    }
                    </script>
                    </head>
                    <body>
                    <div id="rob1" onclick="runTes t1();">tested</div>
                    <div id="rob2" onclick="runTes t2();">untested </div>
                    </body>
                    </html>



                    --
                    Rob

                    Comment

                    • Grant Wagner

                      #11
                      Re: Clipping in Javascript

                      "RobG" <rgqld@iinet.ne t.auau> wrote in message
                      news:r1mHd.213$ Ud4.18025@news. optus.net.au...[color=blue]
                      > RobB wrote:
                      > [...][color=green]
                      >> That would seem to be the operant question: when does
                      >> pants-and-suspenders (and cummerbund & drawstring) object testing of[/color]
                      >
                      > Belt and braces? ;-)
                      >[color=green]
                      >> this sort become a 'burden', occupying a significant volume of the
                      >> codebase? I've never seen a script which intentionally enhanced
                      >> functionality by patching in getElementById in user agents which did
                      >> not implement Style objects, although I suppose anything is possible.
                      >> Should everything be tested for before assuming its implementation?
                      >> When may asumptions be made?[/color]
                      >
                      > A good question and worth pondering. However, before navel gazing,
                      > lets look at the practicalities. I put each of the following into a
                      > separate function, then called them 1000 times from separate
                      > functions,
                      > hopefully avoiding any conflicts between the two:[/color]

                      It is neither a performance issue, nor a burden, if handled properly:

                      Once at the beginning of your code you could do:

                      document.getEle mentStyleById = (function()
                      {
                      if (document.getEl ementById)
                      {
                      return function(id)
                      {
                      var node;
                      if ((node = document.getEle mentById(id)) && node.style)
                      {
                      return node.style;
                      }
                      return null;
                      }
                      }
                      else if (document.all)
                      {
                      return function(id)
                      {
                      var node;
                      if ((node = document.all[id]) && node.style)
                      {
                      return node.style;
                      }
                      return null;
                      }
                      }
                      })();

                      By the way, I'm surprised no one noticed, but:

                      if (node = document.getEle mentById(id) && node.style)

                      won't work. It will be interpreted as:

                      if (node = (document.getEl ementById(id) && node.style))

                      and generate an error that "node has no properties" (or worse, test
                      the -style- property of some other object assigned to -node-). To make
                      it work properly, you need extra brackets:

                      if ((node = (document.getEl ementById(id)) && node.style)

                      --
                      Grant Wagner <gwagner@agrico reunited.com>
                      comp.lang.javas cript FAQ - http://jibbering.com/faq


                      Comment

                      • Dr John Stockton

                        #12
                        Re: Clipping in Javascript

                        JRS: In article <b%THd.112$3d3. 509@news2.mts.n et>, dated Thu, 20 Jan
                        2005 19:53:43, seen in news:comp.lang. javascript, Grant Wagner
                        <gwagner@agrico reunited.com> posted :[color=blue]
                        >
                        >document.getEl ementStyleById = (function()
                        >{
                        > if (document.getEl ementById)
                        > {
                        > return function(id)
                        > {
                        > var node;
                        > if ((node = document.getEle mentById(id)) && node.style)
                        > {
                        > return node.style;
                        > }
                        > return null;
                        > }
                        > }
                        > else if (document.all)
                        > {
                        > return function(id)
                        > {
                        > var node;
                        > if ((node = document.all[id]) && node.style)
                        > {
                        > return node.style;
                        > }
                        > return null;
                        > }
                        > }
                        >})();[/color]

                        Would the following not work, assuming I've correctly edited gESBI ?

                        if (document.all && !document.getEl ementById) {
                        document.getEle mentById = function(id) {
                        return document.all[id] } }

                        document.getEle mentStyleById = (function()
                        {
                        return function(id)
                        {
                        var node;
                        if ((node = document.getEle mentById(id)) && node.style)
                        {
                        return node.style;
                        }
                        return null;
                        }
                        })();

                        Having executed the first part is likely to be useful elsewhere.

                        --
                        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                        <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                        Comment

                        • Grant Wagner

                          #13
                          Re: Clipping in Javascript

                          "Dr John Stockton" <spam@merlyn.de mon.co.uk> wrote in message
                          news:tZfnqdAnaQ 8BFwad@merlyn.d emon.co.uk...[color=blue]
                          > JRS: In article <b%THd.112$3d3. 509@news2.mts.n et>, dated Thu, 20 Jan
                          > 2005 19:53:43, seen in news:comp.lang. javascript, Grant Wagner
                          > Would the following not work, assuming I've correctly edited gESBI ?
                          >
                          > if (document.all && !document.getEl ementById) {
                          > document.getEle mentById = function(id) {
                          > return document.all[id] } }
                          >
                          > document.getEle mentStyleById = (function()
                          > {
                          > return function(id)
                          > {
                          > var node;
                          > if ((node = document.getEle mentById(id)) && node.style)
                          > {
                          > return node.style;
                          > }
                          > return null;
                          > }
                          > })();
                          >
                          > Having executed the first part is likely to be useful elsewhere.[/color]

                          Yes, when I wrote my example I was thinking that it was pretty obvious
                          that I had a lot of duplicate code there, but I didn't give much thought
                          about how to best factor out the commonality. Although the second part
                          doesn't need to return a function now and could be re-written as:

                          document.getEle mentStyleById = function(id)
                          {
                          var node;
                          if ((node = document.getEle mentById(id)) && node.style)
                          {
                          return node.style;
                          }
                          return null;
                          }
                          }

                          One point, and both you and I are guilty of doing it. We are treating
                          document.all[id] as equivilent to document.getEle mentById(id), and they
                          are not. It is possible for document.all[id] to return a NodeList,
                          document.getEle mentById(id) never will (or should not).

                          Extra code (or care) is required to ensure that document.all[] returns a
                          single Node. A simple solution to prevent any errors might be:

                          if (document.all && !document.getEl ementById) {
                          document.getEle mentById = function(id)
                          {
                          var node = document.all[id];
                          if (node && 'undefined' != typeof node.length)
                          {
                          return node[0];
                          }
                          else
                          {
                          return node;
                          }
                          }
                          }

                          Whether returning the first Node of a NodeList of matches is "correct"
                          would have to be determined by the situation.

                          --
                          Grant Wagner <gwagner@agrico reunited.com>
                          comp.lang.javas cript FAQ - http://jibbering.com/faq


                          Comment

                          • Dr John Stockton

                            #14
                            Re: Clipping in Javascript

                            JRS: In article <2QcId.115$3d3. 633@news2.mts.n et>, dated Fri, 21 Jan
                            2005 19:35:26, seen in news:comp.lang. javascript, Grant Wagner
                            <gwagner@agrico reunited.com> posted :

                            [color=blue]
                            >One point, and both you and I are guilty of doing it. We are treating
                            >document.all[id] as equivilent to document.getEle mentById(id), and they
                            >are not. It is possible for document.all[id] to return a NodeList,
                            >document.getEl ementById(id) never will (or should not).
                            >
                            >Extra code (or care) is required to ensure that document.all[] returns a
                            >single Node. A simple solution to prevent any errors might be:
                            >
                            >if (document.all && !document.getEl ementById) {
                            > document.getEle mentById = function(id)
                            > {
                            > var node = document.all[id];
                            > if (node && 'undefined' != typeof node.length)
                            > {
                            > return node[0];
                            > }
                            > else
                            > {
                            > return node;
                            > }
                            > }
                            >}
                            >
                            >Whether returning the first Node of a NodeList of matches is "correct"
                            >would have to be determined by the situation.[/color]


                            (a) I suppose getElementById will return the first of a list.

                            (c) The substitute getElementById needs to be documented to say
                            specifically when it will, or may, return a nodelist. So does FAQ
                            DynWrite.

                            (c) The identifier document.getEle mentById is rather long; maybe it
                            is better to define a function findID to use instead.


                            So I'd try

                            if (document.all && !document.getEl ementById) {

                            findID = function(id) {
                            var node = document.all[id];
                            return (node && 'undefined' != typeof node.length) ? node[0] : node
                            }

                            }


                            If it was felt useful to have document.all[id] return a nodelist,
                            then ISTM that it should be useful to have code so that IE6 cam also get
                            a list :-

                            if (!document.all && document.getEle mentById) { ... // ??


                            --
                            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                            <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                            Comment

                            • Randy Webb

                              #15
                              Re: Clipping in Javascript

                              John Stockton wrote:

                              <--snip-->
                              [color=blue]
                              > If it was felt useful to have document.all[id] return a nodelist,
                              > then ISTM that it should be useful to have code so that IE6 cam also get
                              > a list :-
                              >
                              > if (!document.all && document.getEle mentById) { ... // ??[/color]

                              IE6 fails that test since it has document.all.
                              Gecko based browsers pass that test.

                              if (document.all && document.getEle mentById){
                              //It might be IE5+, might not be.
                              }

                              If you want a document.all nodelist, just use document.all

                              --
                              Randy
                              comp.lang.javas cript FAQ - http://jibbering.com/faq

                              Comment

                              Working...