Dragging only in some part of image

前端 未结 1 1853
别跟我提以往
别跟我提以往 2021-01-19 06:50

We cant able to drag the uploaded image in right part of 1st mask image.... Because it is overlapped with the left part of second image....

Here is

相关标签:
1条回答
  • 2021-01-19 07:34

    I have recreated the plugin, now it only uses canvas to mask, t here is no css mask involved any more.

    $(document).ready(function() {
        var maskedImageUrla = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQMAAAEWCAMAAABVFALRAAAAaVBMVEVHcEz////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////dvhWBAAAAInRSTlMABWcZ/QmP9IX67TovokSZ5d0hD65eudRVbnvATScSx89zp18fZAAABsRJREFUGBnlwAeCo0gWBNAAEhLvjbyJ+x9yd6a7p8vIAEIq5c+Hd+FGkQOreT3JPWyWxCS5g8U2/NcZ1jr0/CWDreqSvw2wVBrzj8iBlXLNv1LYaKX4wRoW6viRymAfl59VsI7Lrw6wTMdvVrBLoviVKmCVOuZ3A2ziD7zEg0VaXtTBHhtelsEaOa8oYYta85o97OCceNUadgh53RZWSHiDOsACfsVbXFgg4E095Et4W9RAOr/iHR2kC3lPC+G8iPdoH7IFvG8F0byI97UQLeAI0QGCeRHHcCFYyFGOkOsQcZw9xAo5UgipGs2RSgdChRxtBZl8zdF6yLTheKqGRI7mBCEk2nGK2IdAGSdxIY8fcZIT5FlxohzitJyohTROxIlUDWF2nKyAMAUn0x5kiTldCFESzhA3kCTkHGdIMnCO0occHudxIceK85Q+xCg40xpiDJyp9CGErzjXGkIknK30IYPL+c6QIeB8cQMRtnxACBFiPkB7EKDhQwoIkPAh0R7m6/iYFuYL+KAcxuv5oBOMV/FRLkwX80EqbmC4iA8LYDaHD1NRDaN5XEALo+25hB1MlnAJlQ+D5VxECIOtuIhoD3N1XITawlxrLqSDsdZcSNnAVBsupYCpXC4mh6F2XEzlw0wplxPATAcuR6UwU8TlDA6MVHFBGxhpywVFKUyUcUmDAwOFXFSIt+OlO3cTFhmQFUEQhud11+UePnC5KJXgXRz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VaRrmgMU5eXCiQbZ4jmYXDDTEGs9z2IVbzbenPDyXk7rZoPjOeryCn6yzk+abWuF16vxcHDXfTezg1bx8XfRVxLcR4Kd4SbcptqXiPxR/UI2fVifdpmgHzZ/S4234+7wLs2PMfyi+zg5vx9/v1kE7aL5I6eBtebkb9CWfboN31yRusNV8HuXBDPUq7GM+RQuT7Lui4uJymKbZBQOXVMFIXpdpLuUMYyVBxSVEB5hsH5Z8WAbTJYXmYxKYz+mOfMAAGdIs4lwupDiEmrNoH3I0m5gzFBDF32hOlkKYQ6A4zRHypEdO0kEiV3O82IFI9ZGjbSBVyJG0D7F2mmOoEIKlMUfQDSSrK94XQjav4j3RAcJ5Je8IIF4dK94SebBAxFsK2KDjDaqGFQpel8EOfsWr9rBEwitUC2sEvCKFNZqYF7WwiMuLEljEKXlBD6u4vCCHVXzNb7awTMBvclhmzy/UCdbp+cUO1kn42REWCviB0jVslPEvDUu5mr9ta9iqWW81VVUkGOV/xCNgIHRY9GIAAAAASUVORK5CYII=";
        var maskedImageUrlb = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAaAAAAGECAMAAABH1QRLAAAAXVBMVEVHcEz///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////+EHRzTAAAAHnRSTlMAS6Ph+PEC/BEI6SQaLY0/ethWzsQ0uoSXZ6xws1/MHbQyAAALUElEQVQYGe3AB2KjSBQE0AKaDuQgUK77H3PXE+2xZSVCg/6DuMTmwX4bbuooiRV/UiqO2v4QbvdBbiFmovNmG9aKPxleEG92+9JCTCo771rDOyT9tskgpuDKouZDovCcQYwqPfaKz6i3HcQ4XFBEHEBSlBBD02UYczDRNoUYULqNOLDN2UEMwjU9x6B2KcTTul3M0fSBhniC29ccV3S0EA9y+4TjU0UG8QB9jjgNU1iIewU1p6MqB3GPcsNpxXsNcav8xOnVJcRN0pDzOKUQ12hbGM7FVA7iW65SnFNUQlzmjjHnZbhzEBekLT2QlBBf2iv6odAQn9gTvdGmEB/pMqFHVAPxni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+KcwgRhQaPqMuIcZl+Dh11BDjOhk+7JBBQ4xrw0dFAcT4Ij7GVA5iAooP6VOIKVg+IgkgppHzfqZwEBNpeLdNDjEVveWd4gZiQifDu+wsxJQS3qPNISaV8g5RoyGmdebN4r2GmFrIG6nKQUwv4U3U1kLMIOUtzC6DmINueIMwhZjJkVedcoi56IpXbDqIGe35rTYANMRsdMlvRA3EzJziJclZQ8wu5Nfio4PwQGr4BbV1EH6o+IkpLIQ3DvxHmEFDeEPv+N4hhfBME/G3voPwj27CyFC1RY6X9B+l783jEhxnoQAAAABJRU5ErkJggg==";
    
        // maskedImage one
        var mask1 = $(".container").mask({
            maskImageUrl: maskedImageUrla,
            onMaskImageCreate: function(img) {
                // add your style to the img example below
                img.css({
                    "left": 105,
                    "top": 105
                })
            }
        });
        // maskedImage two
        var mask2 = $(".container").mask({
            maskImageUrl: maskedImageUrlb,
        });
    
        fileupa1.onchange = function() {
            mask1.loadImage(URL.createObjectURL(fileupa1.files[0]));
        };
    
        fileupa2.onchange = function() {
            mask2.loadImage(URL.createObjectURL(fileupa2.files[0]));
        };
    
    }); // end of document ready
    
    // jq plugin for mask
    (function($) {
        var JQmasks = [];
        $.fn.mask = function(options) {
            // This is the easiest way to have default options.
            var settings = $.extend({
                // These are the defaults.
                maskImageUrl: undefined,
                imageUrl: undefined,
                scale: 1,
                id: new Date().getUTCMilliseconds().toString(),
                x: 0, // image start position
                y: 0, // image start position
                onMaskImageCreate: function(div) {},
            }, options);
    
    
            var container = $(this);
    
            let prevX = 0,
                prevY = 0,
                draggable = false,
                img,
                canvas,
                context,
                image,
                timeout,
                initImage = false,
                startX = settings.x,
                startY = settings.y,
                div;
    
            container.mousePosition = function(event) {
                return {
                    x: event.pageX || event.offsetX,
                    y: event.pageY || event.offsetY
                };
            };
    
            container.selected = function(ev) {
                var pos = container.mousePosition(ev);
                var item = $(".masked-img canvas").filter(function() {
                    var offset = $(this).offset()
                    var x = pos.x - offset.left;
                    var y = pos.y - offset.top;
                    var d = this.getContext('2d').getImageData(x, y, 1, 1).data;
                    return d[0] > 0;
                });
    
                JQmasks.forEach(function(el) {
                    var id = item.length > 0 ? $(item).attr("id") : "";
                    if (el.id === id)
                        el.item.enable();
                    else el.item.disable();
                });
            };
    
            container.enable = function() {
                draggable = true;
                $(canvas).attr("active", "true");
                div.css({
                    "z-index": 2
                });
            };
    
            container.disable = function() {
                draggable = false;
                $(canvas).attr("active", "false");
                div.css({
                    "z-index": 1
                });
            };
    
            container.onDragStart = function(evt) {
                container.selected(evt);
                prevX = evt.clientX;
                prevY = evt.clientY;
            };
    
            container.getImagePosition = function() {
                return {
                    x: settings.x,
                    y: settings.y,
                    scale: settings.scale
                };
            };
    
            container.onDragOver = function(evt) {
                if (draggable && $(canvas).attr("active") === "true") {
                    var x = settings.x + evt.clientX - prevX;
                    var y = settings.y + evt.clientY - prevY;
                    if (x === settings.x && y === settings.y)
                        return; // position has not changed
                    settings.x += evt.clientX - prevX;
                    settings.y += evt.clientY - prevY;
                    prevX = evt.clientX;
                    prevY = evt.clientY;
                    container.updateStyle();
                }
            };
    
            container.updateStyle = function() {
                clearTimeout(timeout);
                timeout = setTimeout(function() {
                    context.clearRect(0, 0, canvas.width, canvas.height);
                    context.beginPath();
                    context.globalCompositeOperation = "source-over";
                    image = new Image();
                    image.setAttribute('crossOrigin', 'anonymous');
                    image.src = settings.maskImageUrl;
                    image.onload = function() {
                        canvas.width = image.width;
                        canvas.height = image.height;
                        context.drawImage(image, 0, 0, image.width, image.height);
                        div.css({
                            "width": image.width,
                            "height": image.height
                        });
                    };
    
                    img = new Image();
                    img.src = settings.imageUrl;
                    img.setAttribute('crossOrigin', 'anonymous');
                    img.onload = function() {
                        settings.x = settings.x === 0 && initImage ? (canvas.width - (img.width * settings.scale)) / 2 : settings.x;
                        settings.y = settings.y === 0 && initImage ? (canvas.height - (img.height * settings.scale)) / 2 : settings.y;
                        context.globalCompositeOperation = 'source-atop';
                        context.drawImage(img, settings.x, settings.y, img.width * settings.scale, img.height * settings.scale);
                        initImage = false;
                    };
                }, 0);
            };
    
            // change the draggable image
            container.loadImage = function(imageUrl) {
                if (img)
                    img.remove();
                // reset the code.
                settings.y = startY;
                settings.x = startX;
                prevX = prevY = 0;
                settings.imageUrl = imageUrl;
                initImage = true;
                container.updateStyle();
            };
    
            // change the masked Image
            container.loadMaskImage = function(imageUrl, from) {
                if (div)
                    div.remove();
                canvas = document.createElement("canvas");
                context = canvas.getContext('2d');
                canvas.setAttribute("draggable", "true");
                canvas.setAttribute("id", settings.id);
                settings.maskImageUrl = imageUrl;
                div = $("<div/>", {
                    "class": "masked-img"
                }).append(canvas);
    
                div.find("canvas").on('touchstart mousedown', function(event) {
                    if (event.handled === false) return;
                    event.handled = true;
                    container.onDragStart(event);
                });
    
                div.find("canvas").on('touchend mouseup', function(event) {
                    if (event.handled === false) return;
                    event.handled = true;
                    container.selected(event);
                });
    
                div.find("canvas").bind("dragover", container.onDragOver);
                container.append(div);
                if (settings.onMaskImageCreate)
                    settings.onMaskImageCreate(div);
                container.loadImage(settings.imageUrl);
            };
            container.loadMaskImage(settings.maskImageUrl);
            JQmasks.push({
                item: container,
                id: settings.id
            });
            return container;
        };
    }(jQuery));
    .container {
        border: 1px solid #DDDDDD;
        display: flex;
        background: red;
    }
    
    .container canvas {
        display: block;
    }
    
    .masked-img {
        overflow: hidden;
        margin-left: 10px;
        position: relative;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    image 1 
    <input id="fileupa1"  type="file" >
    
    image 2 
    <input FILEDIA="1" id="fileupa2"  type="file" >
    <div class="container">
    
    </div>

    CodePen

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