Programmatically Clip/Cut image using Javascript

后端 未结 8 1808
[愿得一人]
[愿得一人] 2020-12-12 17:57

Are there any documents/tutorials on how to clip or cut a large image so that the user only sees a small portion of this image? Let\'s say the source image is 10 frames of

相关标签:
8条回答
  • 2020-12-12 18:08

    try use haxcv library haxcv js by simple functions

    go to https://docs.haxcv.org/Methods/cutImage to read more about his library

    var Pixels = _("img").cutImage (x , y , width , height  );
    

    _("img").src (Pixels.src);

    // return cut image but try to include library first

    0 讨论(0)
  • 2020-12-12 18:11

    I suppose you want to take a thumbnail for your image. You can use ImageThumbnail.js that created from prototype library in this way:

    <script type="text/javascript" src="prototype.js"></script>
    <script type="text/javascript" src="ImageThumbnail.js"></script>
    <input type="file" id="photo">
    <img src="empty.gif" id="thumbnail" width="80" height="0">
    <script type="text/javascript">
    <!--
        new Image.Thumbnail('thumbnail', 'photo');
    //-->
    </script>
    

    for more information

    0 讨论(0)
  • 2020-12-12 18:12

    What spriting does is essentially position a absolutely-positioned DIV inside another DIV that has overflow:hidden. You can do the same, all you need to do is resize the outer DIV depending on the size of each frame of the larger image. You can do that in code easily.

    You can just set the inner DIV's style:

    left: (your x-position = 0 or a negative integer * frame width)px
    

    Most JavaScript Frameworks make this quite easy.

    0 讨论(0)
  • 2020-12-12 18:13

    This can be done by enclosing your image in a "viewport" div. Set a width and height on the div (according to your needs), then set position: relative and overflow: hidden on it. Absolutely position your image inside of it and change the position to change which portions are displayed.

    To display a 30x40 section of an image starting at (10,20):

    <style type="text/css">
        div.viewport {
            overflow: hidden;
            position: relative;
        }
    
        img.clipped {
            display: block;
            position: absolute;
        }
    </style>
    
    <script type="text/javascript">
        function setViewport(img, x, y, width, height) {
            img.style.left = "-" + x + "px";
            img.style.top  = "-" + y + "px";
    
            if (width !== undefined) {
                img.parentNode.style.width  = width  + "px";
                img.parentNode.style.height = height + "px";
            }
        }
    
        setViewport(document.getElementsByTagName("img")[0], 10, 20, 30, 40);
    </script>
    
    <div class="viewport">
        <img class="clipped" src="/images/clipped.png" alt="Clipped image"/>
    </div>
    

    The common CSS properties are associated with classes so that you can have multiple viewports / clipped images on your page. The setViewport(…) function can be called at any time to change what part of the image is displayed.

    0 讨论(0)
  • 2020-12-12 18:22

    Alas, JavaScript simply isn't capable of extracting the properties of the image you'd require to do something like this. However, there may be salvation in the form of the HTML <canvas> element combined with a bit of server-side scripting.

    PHP code to go about extracting the width and height of the really large image:

    <?php
    $large_image = 'path/to/large_image';
    $full_w = imagesx($large_image);
    $full_h = imagesy($large_image);
    ?>
    

    From here, you'd then load the image into a <canvas> element, an example of which is documented here. Now, my theory was that you may be able to extract pixel data from a <canvas> element; assuming that you can, you would simply make sure to have some form of definite divider between the frames of the large image and then search for it within the canvas. Let's say you found the divider 110 pixels from the left of the image; you would then know that each "frame" was 110 pixels wide, and you've already got the full width stored in a PHP variable, so deciphering how much image you're working with would be a breeze.

    The only speculative aspect to this method is whether or not JavaScript is capable of extracting color data from a specified location within an image loaded into a <canvas> element; if this is possible, then what you're trying to accomplish is entirely feasible.

    0 讨论(0)
  • 2020-12-12 18:25

    In answer to :

    Alas, JavaScript simply isn't capable of extracting the properties of the image you'd require to do something like this. However, there may be salvation in the form of the HTML element combined with a bit of server-side scripting. ...

    < ? (open php) 
    $large_image = 'path/to/large_image';
    $full_w = imagesx($large_image);
    $full_h = imagesy($large_image);
    (close php) ? >
    

    This can be done in Javascript, just google a bit :

    var newimage = new Image();
    newimage.src = document.getElementById('background').src;
    var height = newimage.height;
    var width  = newimage.width;
    

    This generates a new image from an existing one and captures this way in java script the original height and width properties of the original image (not the one id'ed as background.

    In answer to :

    The width/height properties of the document's image object are read only. If you could change them, however, you would only squish the frames, not cut the frames up like you desire. The kind of image manipulation you want can not be done with client-side javascript. I suggest cutting the images up on the server, or overlay a div on the image to hide the parts you do not wish to display.

    ...

    var newimage = new Image();
    newimage.src = document.getElementById('background').src;
    var height = newimage.height;
    var width  = newimage.width;
    newimage.style.height = '200px';
    newimage.style.width = '200px';
    newimage.height = '200px';
    newimage.width = '200px';
    

    and if wanted :

    newimage.setAttribute('height','200px');
    

    The doubled newimage.style.height and newimage.height is needed in certain circumstances in order to make sure that a IE will understand in time that the image is resized (you are going to render the thing immediately after, and the internal IE processing is too slow for that.)

    Thanks for the above script I altered and implemented on http://morethanvoice.net/m1/reader13.php (right click menu... mouseover zoom lent) correct even in IE , but as you will notice the on mousemove image processing is too fast for the old styled IE, renders the position but only once the image. In any case any good idea is welcome.

    Thanks to all for your attention, hope that the above codes can help someone...

    Claudio Klemp http://morethanvoice.net/m1/reader13.php

    0 讨论(0)
提交回复
热议问题